1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21 /*
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
24 */
25
26 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
27 /* All Rights Reserved */
28
29 /*
30 * Portions of this source code were derived from Berkeley 4.3 BSD
31 * under license from the Regents of the University of California.
32 */
33
34 /*
35 * This module provides the user level support for the NFSv4
36 * callback program. It is modeled after nfsd. When a nfsv4
37 * mount occurs, the mount command forks and the child runs
38 * start_nfs4_callback. If this is the first mount, then the
39 * process will hang around listening for incoming connection
40 * requests from the nfsv4 server.
41 *
42 * For connection-less protocols, the krpc is started immediately.
43 * For connection oriented protocols, the kernel module is informed
44 * of netid and universal address that it can give this
45 * information to the server during setclientid.
46 */
47
48 #include <sys/param.h>
49 #include <sys/types.h>
50 #include <syslog.h>
51 #include <tiuser.h>
52 #include <rpc/rpc.h>
53 #include <errno.h>
54 #include <thread.h>
55 #include <sys/resource.h>
56 #include <sys/file.h>
57 #include <nfs/nfs.h>
58 #include <nfs/nfssys.h>
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <netconfig.h>
62 #include <netdir.h>
63 #include <string.h>
64 #include <unistd.h>
65 #include <stropts.h>
66 #include <sys/tihdr.h>
67 #include <netinet/tcp.h>
68 #include "nfs_tbind.h"
69 #include "thrpool.h"
70 #include <rpcsvc/nfs4_prot.h>
71 #include <netdb.h>
72 #include <signal.h>
73 #include <strings.h>
74 #include <priv_utils.h>
75 #include <rpcsvc/daemon_utils.h>
76
77 static int nfs4svc(int, struct netbuf *, struct netconfig *, int,
78 struct netbuf *);
79 extern int _nfssys(int, void *);
80
81 static char *MyName;
82
83 /*
84 * The following are all globals used by routines in nfs_tbind.c.
85 */
86 size_t end_listen_fds; /* used by conn_close_oldest() */
87 size_t num_fds = 0; /* used by multiple routines */
88 int listen_backlog = 32; /* used by bind_to_{provider,proto}() */
89 int num_servers; /* used by cots_listen_event() */
90 int (*Mysvc)(int, struct netbuf, struct netconfig *) = NULL;
91 /* used by cots_listen_event() */
92 int max_conns_allowed = -1; /* used by cots_listen_event() */
93
94 int
95 main(int argc, char *argv[])
96 {
97 int pid;
98 int i;
99 struct protob *protobp;
100 struct flock f;
101 pid_t pi;
102 struct svcpool_args cb_svcpool;
103
104 MyName = "nfs4cbd";
105 Mysvc4 = nfs4svc;
106
107 #ifndef DEBUG
108 /*
109 * Close existing file descriptors, open "/dev/null" as
110 * standard input, output, and error, and detach from
111 * controlling terminal.
112 */
113 closefrom(0);
114 (void) open("/dev/null", O_RDONLY);
115 (void) open("/dev/null", O_WRONLY);
116 (void) dup(1);
117 (void) setsid();
118 #endif
119
120 /*
121 * create a child to continue our work
122 * Parent's exit will tell mount command we're ready to go
123 */
124 if ((pi = fork()) > 0) {
125 exit(0);
126 }
127
128 if (pi == -1) {
129 (void) syslog(LOG_ERR,
130 "Could not start NFS4_CALLBACK service");
131 exit(1);
132 }
133
134 (void) _create_daemon_lock(NFS4CBD, DAEMON_UID, DAEMON_GID);
135
136 svcsetprio();
137
138 if (__init_daemon_priv(PU_RESETGROUPS|PU_CLEARLIMITSET,
139 DAEMON_UID, DAEMON_GID, PRIV_SYS_NFS, (char *)NULL) == -1) {
140 (void) fprintf(stderr, "%s must be run with sufficient"
141 " privileges\n", argv[0]);
142 exit(1);
143 }
144 /* Basic privileges we don't need, remove from E/P. */
145 __fini_daemon_priv(PRIV_PROC_EXEC, PRIV_PROC_FORK, PRIV_FILE_LINK_ANY,
146 PRIV_PROC_SESSION, PRIV_PROC_INFO, (char *)NULL);
147
148 /*
149 * establish our lock on the lock file and write our pid to it.
150 * exit if some other process holds the lock, or if there's any
151 * error in writing/locking the file.
152 */
153 pid = _enter_daemon_lock(NFS4CBD);
154 switch (pid) {
155 case 0:
156 break;
157 case -1:
158 syslog(LOG_ERR, "error locking for %s: %s", NFS4CBD,
159 strerror(errno));
160 exit(2);
161 default:
162 /* daemon was already running */
163 exit(0);
164 }
165
166 openlog(MyName, LOG_PID | LOG_NDELAY, LOG_DAEMON);
167
168 cb_svcpool.id = NFS_CB_SVCPOOL_ID;
169 cb_svcpool.maxthreads = 0;
170 cb_svcpool.redline = 0;
171 cb_svcpool.qsize = 0;
172 cb_svcpool.timeout = 0;
173 cb_svcpool.stksize = 0;
174 cb_svcpool.max_same_xprt = 0;
175
176 /* create a SVC_POOL for the nfsv4 callback deamon */
177 if (_nfssys(SVCPOOL_CREATE, &cb_svcpool)) {
178 (void) syslog(LOG_ERR, "can't setup NFS_CB SVCPOOL: Exiting");
179 exit(1);
180 }
181
182 /*
183 * Set up blocked thread to do LWP creation on behalf of the kernel.
184 */
185 if (svcwait(NFS_CB_SVCPOOL_ID)) {
186 (void) syslog(LOG_ERR,
187 "Can't set up NFS_CB LWP creator: Exiting");
188 exit(1);
189 }
190
191
192 /*
193 * Build a protocol block list for registration.
194 */
195 protobp = (struct protob *)malloc(sizeof (struct protob));
196 protobp->serv = "NFS4_CALLBACK";
197 protobp->versmin = NFS_CB;
198 protobp->versmax = NFS_CB;
199 protobp->program = NFS4_CALLBACK;
200 protobp->next = NULL;
201
202 if (do_all(protobp, NULL, 0) == -1) {
203 exit(1);
204 }
205
206 free(protobp);
207
208 if (num_fds == 0) {
209 (void) syslog(LOG_ERR,
210 "Could not start NFS4_CALLBACK service for any protocol");
211 exit(1);
212 }
213
214 end_listen_fds = num_fds;
215 /*
216 * Poll for non-data control events on the transport descriptors.
217 */
218 poll_for_action();
219
220 /*
221 * If we get here, something failed in poll_for_action().
222 */
223 return (1);
224 }
225
226 char *
227 get_uaddr(int fd, struct netconfig *nconf, struct netbuf *nb)
228 {
229 struct nfs_svc_args nsa;
230 char *ua, *ua2, *mua = NULL;
231 char me[MAXHOSTNAMELEN];
232 struct nd_addrlist *nas;
233 struct nd_hostserv hs;
234 struct nd_mergearg ma;
235
236 ua = taddr2uaddr(nconf, nb);
237
238 if (ua == NULL) {
239 #ifdef DEBUG
240 fprintf(stderr, "taddr2uaddr failed for netid %s\n",
241 nconf->nc_netid);
242 #endif
243 return (NULL);
244 }
245
246 gethostname(me, MAXHOSTNAMELEN);
247
248 hs.h_host = me;
249 hs.h_serv = "nfs";
250 if (netdir_getbyname(nconf, &hs, &nas)) {
251 #ifdef DEBUG
252 netdir_perror("netdir_getbyname");
253 #endif
254 return (NULL);
255 }
256
257 ua2 = taddr2uaddr(nconf, nas->n_addrs);
258
259 if (ua2 == NULL) {
260 #ifdef DEBUG
261 fprintf(stderr, "taddr2uaddr failed for netid %s.\n",
262 nconf->nc_netid);
263 #endif
264 return (NULL);
265 }
266
267 ma.s_uaddr = ua;
268 ma.c_uaddr = ua2;
269 ma.m_uaddr = NULL;
270
271 if (netdir_options(nconf, ND_MERGEADDR, 0, (char *)&ma)) {
272 #ifdef DEBUG
273 netdir_perror("netdir_options");
274 #endif
275 return (NULL);
276 }
277
278 mua = ma.m_uaddr;
279 return (mua);
280 }
281
282 /*
283 * Establish NFS4 callback service thread.
284 */
285 static int
286 nfs4svc(int fd, struct netbuf *addrmask, struct netconfig *nconf,
287 int cmd, struct netbuf *addr)
288 {
289 struct nfs4_svc_args nsa;
290 char *ua;
291 int error;
292
293 ua = get_uaddr(fd, nconf, addr);
294
295 if (ua == NULL) {
296 syslog(LOG_NOTICE, "nfsv4 cannot determine local hostname "
297 "binding for transport %s - delegations will not be "
298 "available on this transport\n", nconf->nc_netid);
299 return (0);
300 }
301
302 #ifdef DEBUG
303 if (cmd & NFS4_KRPC_START)
304 fprintf(stderr, "nfs4cbd: starting callback rpc on %s %s\n",
305 nconf->nc_netid, ua);
306 else
307 fprintf(stderr, "nfs4cbd: listening on %s %s\n",
308 nconf->nc_netid, ua);
309 #endif
310
311 nsa.fd = fd;
312 nsa.cmd = cmd;
313 nsa.netid = nconf->nc_netid;
314 if (addrmask)
315 nsa.addrmask = *addrmask;
316 else
317 bzero(&nsa.addrmask, sizeof (struct netbuf));
318 nsa.addr = ua;
319 nsa.protofmly = nconf->nc_protofmly;
320 nsa.proto = nconf->nc_proto;
321 if ((error = _nfssys(NFS4_SVC, &nsa)) != 0)
322 syslog(LOG_ERR, "nfssys NFS4_SVC failed\n");
323
324 return (error);
325 }