PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/usr.sbin/nfscbd/nfscbd.c

https://bitbucket.org/freebsd/freebsd-base
C | 373 lines | 283 code | 30 blank | 60 comment | 59 complexity | 5807c7fe1ec1f5a9b8310771db61bf30 MD5 | raw file
  1. /*-
  2. * Copyright (c) 2009 Rick Macklem, University of Guelph
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  15. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  17. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  18. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  19. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  20. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  21. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  22. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  23. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  24. * SUCH DAMAGE.
  25. *
  26. */
  27. #include <sys/cdefs.h>
  28. __FBSDID("$FreeBSD$");
  29. #include <sys/param.h>
  30. #include <sys/ioctl.h>
  31. #include <sys/linker.h>
  32. #include <sys/module.h>
  33. #include <sys/mount.h>
  34. #include <sys/socket.h>
  35. #include <sys/socketvar.h>
  36. #include <sys/stat.h>
  37. #include <sys/ucred.h>
  38. #include <sys/uio.h>
  39. #include <sys/vnode.h>
  40. #include <sys/wait.h>
  41. #include <nfs/nfssvc.h>
  42. #include <rpc/rpc.h>
  43. #include <fs/nfs/rpcv2.h>
  44. #include <fs/nfs/nfsproto.h>
  45. #include <fs/nfs/nfskpiport.h>
  46. #include <fs/nfs/nfs.h>
  47. #include <err.h>
  48. #include <errno.h>
  49. #include <fcntl.h>
  50. #include <grp.h>
  51. #include <netdb.h>
  52. #include <pwd.h>
  53. #include <signal.h>
  54. #include <stdio.h>
  55. #include <stdlib.h>
  56. #include <string.h>
  57. #include <syslog.h>
  58. #include <unistd.h>
  59. /* Global defs */
  60. #ifdef DEBUG
  61. #define syslog(e, s) fprintf(stderr,(s))
  62. static int debug = 1;
  63. #else
  64. static int debug = 0;
  65. #endif
  66. static pid_t children;
  67. static void nonfs(int);
  68. static void reapchild(int);
  69. static void usage(void);
  70. static void cleanup(int);
  71. static void child_cleanup(int);
  72. static void nfscbd_exit(int);
  73. static void killchildren(void);
  74. /*
  75. * Nfs callback server daemon.
  76. *
  77. * 1 - do file descriptor and signal cleanup
  78. * 2 - fork the nfscbd(s)
  79. * 4 - create callback server socket(s)
  80. * 5 - set up server socket for rpc
  81. *
  82. * For connectionless protocols, just pass the socket into the kernel via.
  83. * nfssvc().
  84. * For connection based sockets, loop doing accepts. When you get a new
  85. * socket from accept, pass the msgsock into the kernel via. nfssvc().
  86. */
  87. int
  88. main(int argc, char *argv[])
  89. {
  90. struct nfscbd_args nfscbdargs;
  91. struct nfsd_nfscbd_args nfscbdargs2;
  92. struct sockaddr_in inetaddr, inetpeer;
  93. fd_set ready, sockbits;
  94. int ch, connect_type_cnt, len, maxsock, msgsock, error;
  95. int nfssvc_flag, on, sock, tcpsock, ret, mustfreeai = 0;
  96. char *cp, princname[128];
  97. char myname[MAXHOSTNAMELEN], *myfqdnname = NULL;
  98. struct addrinfo *aip, hints;
  99. pid_t pid;
  100. short myport = NFSV4_CBPORT;
  101. if (modfind("nfscl") < 0) {
  102. /* Not present in kernel, try loading it */
  103. if (kldload("nfscl") < 0 ||
  104. modfind("nfscl") < 0)
  105. errx(1, "nfscl is not available");
  106. }
  107. /*
  108. * First, get our fully qualified host name, if possible.
  109. */
  110. if (gethostname(myname, MAXHOSTNAMELEN) >= 0) {
  111. cp = strchr(myname, '.');
  112. if (cp != NULL && *(cp + 1) != '\0') {
  113. cp = myname;
  114. } else {
  115. /*
  116. * No domain on myname, so try looking it up.
  117. */
  118. cp = NULL;
  119. memset((void *)&hints, 0, sizeof (hints));
  120. hints.ai_flags = AI_CANONNAME;
  121. error = getaddrinfo(myname, NULL, &hints, &aip);
  122. if (error == 0) {
  123. if (aip->ai_canonname != NULL &&
  124. (cp = strchr(aip->ai_canonname, '.')) != NULL
  125. && *(cp + 1) != '\0') {
  126. cp = aip->ai_canonname;
  127. mustfreeai = 1;
  128. } else {
  129. freeaddrinfo(aip);
  130. }
  131. }
  132. }
  133. if (cp == NULL)
  134. warnx("Can't get fully qualified host name");
  135. myfqdnname = cp;
  136. }
  137. princname[0] = '\0';
  138. #define GETOPT "p:P:"
  139. #define USAGE "[ -p port_num ] [ -P client_principal ]"
  140. while ((ch = getopt(argc, argv, GETOPT)) != -1)
  141. switch (ch) {
  142. case 'p':
  143. myport = atoi(optarg);
  144. if (myport < 1) {
  145. warnx("port# non-positive, reset to %d",
  146. NFSV4_CBPORT);
  147. myport = NFSV4_CBPORT;
  148. }
  149. break;
  150. case 'P':
  151. cp = optarg;
  152. if (cp != NULL && strlen(cp) > 0 &&
  153. strlen(cp) < sizeof (princname)) {
  154. if (strchr(cp, '@') == NULL &&
  155. myfqdnname != NULL)
  156. snprintf(princname, sizeof (princname),
  157. "%s@%s", cp, myfqdnname);
  158. else
  159. strlcpy(princname, cp,
  160. sizeof (princname));
  161. } else {
  162. warnx("client princ invalid. ignored\n");
  163. }
  164. break;
  165. default:
  166. case '?':
  167. usage();
  168. }
  169. argv += optind;
  170. argc -= optind;
  171. if (argc > 0)
  172. usage();
  173. if (mustfreeai)
  174. freeaddrinfo(aip);
  175. nfscbdargs2.principal = (const char *)princname;
  176. if (debug == 0) {
  177. daemon(0, 0);
  178. (void)signal(SIGTERM, SIG_IGN);
  179. (void)signal(SIGHUP, SIG_IGN);
  180. (void)signal(SIGINT, SIG_IGN);
  181. (void)signal(SIGQUIT, SIG_IGN);
  182. }
  183. (void)signal(SIGSYS, nonfs);
  184. (void)signal(SIGCHLD, reapchild);
  185. openlog("nfscbd:", LOG_PID, LOG_DAEMON);
  186. pid = fork();
  187. if (pid < 0) {
  188. syslog(LOG_ERR, "fork: %m");
  189. nfscbd_exit(1);
  190. } else if (pid > 0) {
  191. children = pid;
  192. } else {
  193. (void)signal(SIGUSR1, child_cleanup);
  194. setproctitle("server");
  195. nfssvc_flag = NFSSVC_NFSCBD;
  196. if (nfssvc(nfssvc_flag, &nfscbdargs2) < 0) {
  197. syslog(LOG_ERR, "nfssvc: %m");
  198. nfscbd_exit(1);
  199. }
  200. exit(0);
  201. }
  202. (void)signal(SIGUSR1, cleanup);
  203. if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
  204. syslog(LOG_ERR, "can't create udp socket");
  205. nfscbd_exit(1);
  206. }
  207. memset(&inetaddr, 0, sizeof inetaddr);
  208. inetaddr.sin_family = AF_INET;
  209. inetaddr.sin_addr.s_addr = INADDR_ANY;
  210. inetaddr.sin_port = htons(myport);
  211. inetaddr.sin_len = sizeof(inetaddr);
  212. ret = bind(sock, (struct sockaddr *)&inetaddr, sizeof(inetaddr));
  213. /* If bind() fails, this is a restart, so just skip UDP. */
  214. if (ret == 0) {
  215. len = sizeof(inetaddr);
  216. if (getsockname(sock, (struct sockaddr *)&inetaddr, &len) < 0){
  217. syslog(LOG_ERR, "can't get bound addr");
  218. nfscbd_exit(1);
  219. }
  220. nfscbdargs.port = ntohs(inetaddr.sin_port);
  221. if (nfscbdargs.port != myport) {
  222. syslog(LOG_ERR, "BAD PORT#");
  223. nfscbd_exit(1);
  224. }
  225. nfscbdargs.sock = sock;
  226. nfscbdargs.name = NULL;
  227. nfscbdargs.namelen = 0;
  228. if (nfssvc(NFSSVC_CBADDSOCK, &nfscbdargs) < 0) {
  229. syslog(LOG_ERR, "can't Add UDP socket");
  230. nfscbd_exit(1);
  231. }
  232. }
  233. (void)close(sock);
  234. /* Now set up the master server socket waiting for tcp connections. */
  235. on = 1;
  236. FD_ZERO(&sockbits);
  237. connect_type_cnt = 0;
  238. if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
  239. syslog(LOG_ERR, "can't create tcp socket");
  240. nfscbd_exit(1);
  241. }
  242. if (setsockopt(tcpsock,
  243. SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
  244. syslog(LOG_ERR, "setsockopt SO_REUSEADDR: %m");
  245. /* sin_port is already set */
  246. inetaddr.sin_family = AF_INET;
  247. inetaddr.sin_addr.s_addr = INADDR_ANY;
  248. inetaddr.sin_port = htons(myport);
  249. inetaddr.sin_len = sizeof(inetaddr);
  250. if (bind(tcpsock,
  251. (struct sockaddr *)&inetaddr, sizeof (inetaddr)) < 0) {
  252. syslog(LOG_ERR, "can't bind tcp addr");
  253. nfscbd_exit(1);
  254. }
  255. if (listen(tcpsock, 5) < 0) {
  256. syslog(LOG_ERR, "listen failed");
  257. nfscbd_exit(1);
  258. }
  259. FD_SET(tcpsock, &sockbits);
  260. maxsock = tcpsock;
  261. connect_type_cnt++;
  262. setproctitle("master");
  263. /*
  264. * Loop forever accepting connections and passing the sockets
  265. * into the kernel for the mounts.
  266. */
  267. for (;;) {
  268. ready = sockbits;
  269. if (connect_type_cnt > 1) {
  270. if (select(maxsock + 1,
  271. &ready, NULL, NULL, NULL) < 1) {
  272. syslog(LOG_ERR, "select failed: %m");
  273. nfscbd_exit(1);
  274. }
  275. }
  276. if (FD_ISSET(tcpsock, &ready)) {
  277. len = sizeof(inetpeer);
  278. if ((msgsock = accept(tcpsock,
  279. (struct sockaddr *)&inetpeer, &len)) < 0) {
  280. syslog(LOG_ERR, "accept failed: %m");
  281. nfscbd_exit(1);
  282. }
  283. memset(inetpeer.sin_zero, 0,
  284. sizeof (inetpeer.sin_zero));
  285. if (setsockopt(msgsock, SOL_SOCKET,
  286. SO_KEEPALIVE, (char *)&on, sizeof(on)) < 0)
  287. syslog(LOG_ERR,
  288. "setsockopt SO_KEEPALIVE: %m");
  289. nfscbdargs.sock = msgsock;
  290. nfscbdargs.name = (caddr_t)&inetpeer;
  291. nfscbdargs.namelen = sizeof(inetpeer);
  292. nfssvc(NFSSVC_CBADDSOCK, &nfscbdargs);
  293. (void)close(msgsock);
  294. }
  295. }
  296. }
  297. static void
  298. usage(void)
  299. {
  300. errx(1, "usage: nfscbd %s", USAGE);
  301. }
  302. static void
  303. nonfs(int signo __unused)
  304. {
  305. syslog(LOG_ERR, "missing system call: NFS not available");
  306. }
  307. static void
  308. reapchild(int signo __unused)
  309. {
  310. pid_t pid;
  311. while ((pid = wait3(NULL, WNOHANG, NULL)) > 0) {
  312. if (pid == children)
  313. children = -1;
  314. }
  315. }
  316. static void
  317. killchildren(void)
  318. {
  319. if (children > 0)
  320. kill(children, SIGKILL);
  321. }
  322. /*
  323. * Cleanup master after SIGUSR1.
  324. */
  325. static void
  326. cleanup(int signo __unused)
  327. {
  328. nfscbd_exit(0);
  329. }
  330. /*
  331. * Cleanup child after SIGUSR1.
  332. */
  333. static void
  334. child_cleanup(int signo __unused)
  335. {
  336. exit(0);
  337. }
  338. static void
  339. nfscbd_exit(int status __unused)
  340. {
  341. killchildren();
  342. exit(status);
  343. }