1 /* $NetBSD: lockd.c,v 1.7 2000/08/12 18:08:44 thorpej Exp $ */ 2 /* $FreeBSD$ */ 3 4 /* 5 * Copyright (c) 1995 6 * A.R. Gordon (andrew.gordon@net-tel.co.uk). All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed for the FreeBSD project 19 * 4. Neither the name of the author nor the names of any co-contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY ANDREW GORDON AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 * 35 */ 36 37 #include <sys/cdefs.h> 38 #ifndef lint 39 __RCSID("$NetBSD: lockd.c,v 1.7 2000/08/12 18:08:44 thorpej Exp $"); 40 #endif 41 42 /* 43 * main() function for NFS lock daemon. Most of the code in this 44 * file was generated by running rpcgen /usr/include/rpcsvc/nlm_prot.x. 45 * 46 * The actual program logic is in the file lock_proc.c 47 */ 48 49 #include <sys/types.h> 50 #include <sys/socket.h> 51 52 #include <err.h> 53 #include <stdio.h> 54 #include <stdlib.h> 55 #include <errno.h> 56 #include <syslog.h> 57 #include <signal.h> 58 #include <string.h> 59 #include <unistd.h> 60 #include <libutil.h> 61 #include <netconfig.h> 62 63 #include <rpc/rpc.h> 64 #include <rpc/rpc_com.h> 65 #include <rpcsvc/sm_inter.h> 66 67 #include "lockd.h" 68 #include <rpcsvc/nlm_prot.h> 69 70 int debug_level = 0; /* 0 = no debugging syslog() calls */ 71 int _rpcsvcdirty = 0; 72 73 int grace_expired; 74 int nsm_state; 75 pid_t client_pid; 76 struct mon mon_host; 77 78 void init_nsm(void); 79 void nlm_prog_0(struct svc_req *, SVCXPRT *); 80 void nlm_prog_1(struct svc_req *, SVCXPRT *); 81 void nlm_prog_3(struct svc_req *, SVCXPRT *); 82 void nlm_prog_4(struct svc_req *, SVCXPRT *); 83 void usage(void); 84 85 void sigalarm_handler(void); 86 87 const char *transports[] = { "udp", "tcp", "udp6", "tcp6" }; 88 89 int 90 main(argc, argv) 91 int argc; 92 char **argv; 93 { 94 SVCXPRT *transp; 95 int ch, i, maxindex, r, s, sock; 96 struct sockaddr_in sin; 97 struct sockaddr_in6 sin6; 98 char *endptr; 99 struct sigaction sigalarm; 100 int grace_period = 30; 101 struct netconfig *nconf; 102 int maxrec = RPC_MAXDATASIZE; 103 in_port_t svcport = 0; 104 105 while ((ch = getopt(argc, argv, "d:g:p:")) != (-1)) { 106 switch (ch) { 107 case 'd': 108 debug_level = atoi(optarg); 109 if (!debug_level) { 110 usage(); 111 /* NOTREACHED */ 112 } 113 break; 114 case 'g': 115 grace_period = atoi(optarg); 116 if (!grace_period) { 117 usage(); 118 /* NOTREACHED */ 119 } 120 break; 121 case 'p': 122 endptr = NULL; 123 svcport = (in_port_t)strtoul(optarg, &endptr, 10); 124 if (endptr == NULL || *endptr != '\0' || 125 svcport == 0 || svcport >= IPPORT_MAX) 126 usage(); 127 break; 128 default: 129 case '?': 130 usage(); 131 /* NOTREACHED */ 132 } 133 } 134 if (geteuid()) { /* This command allowed only to root */ 135 fprintf(stderr, "Sorry. You are not superuser\n"); 136 exit(1); 137 } 138 139 (void)rpcb_unset(NLM_PROG, NLM_SM, NULL); 140 (void)rpcb_unset(NLM_PROG, NLM_VERS, NULL); 141 (void)rpcb_unset(NLM_PROG, NLM_VERSX, NULL); 142 (void)rpcb_unset(NLM_PROG, NLM_VERS4, NULL); 143 144 /* 145 * Check if IPv6 support is present. 146 */ 147 s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP); 148 if (s < 0) 149 maxindex = 2; 150 else { 151 close(s); 152 maxindex = 4; 153 } 154 155 if (svcport != 0) { 156 bzero(&sin, sizeof(struct sockaddr_in)); 157 sin.sin_len = sizeof(struct sockaddr_in); 158 sin.sin_family = AF_INET; 159 sin.sin_port = htons(svcport); 160 161 bzero(&sin6, sizeof(struct sockaddr_in6)); 162 sin6.sin6_len = sizeof(struct sockaddr_in6); 163 sin6.sin6_family = AF_INET6; 164 sin6.sin6_port = htons(svcport); 165 } 166 167 rpc_control(RPC_SVC_CONNMAXREC_SET, &maxrec); 168 169 for (i = 0; i < maxindex; i++) { 170 nconf = getnetconfigent(transports[i]); 171 if (nconf == NULL) 172 errx(1, "cannot get %s netconf: %s.", transports[i], 173 nc_sperror()); 174 175 if (svcport != 0) { 176 if (strcmp(nconf->nc_netid, "udp6") == 0) { 177 sock = socket(AF_INET6, SOCK_DGRAM, 178 IPPROTO_UDP); 179 if (sock != -1) { 180 r = bindresvport_sa(sock, 181 (struct sockaddr *)&sin6); 182 if (r != 0) { 183 syslog(LOG_ERR, "bindresvport: %m"); 184 exit(1); 185 } 186 } 187 } 188 else if (strcmp(nconf->nc_netid, "udp") == 0) { 189 sock = socket(AF_INET, SOCK_DGRAM, 190 IPPROTO_UDP); 191 if (sock != -1) { 192 r = bindresvport(sock, &sin); 193 if (r != 0) { 194 syslog(LOG_ERR, "bindresvport: %m"); 195 exit(1); 196 } 197 } 198 } 199 else if (strcmp(nconf->nc_netid, "tcp6") == 0) { 200 sock = socket(AF_INET6, SOCK_STREAM, 201 IPPROTO_TCP); 202 if (sock != -1) { 203 r = bindresvport_sa(sock, 204 (struct sockaddr *)&sin6); 205 if (r != 0) { 206 syslog(LOG_ERR, "bindresvport: %m"); 207 exit(1); 208 } 209 } 210 } 211 else if (strcmp(nconf->nc_netid, "tcp") == 0) { 212 sock = socket(AF_INET, SOCK_STREAM, 213 IPPROTO_TCP); 214 if (sock != -1) { 215 r = bindresvport(sock, &sin); 216 if (r != 0) { 217 syslog(LOG_ERR, "bindresvport: %m"); 218 exit(1); 219 } 220 } 221 } 222 223 transp = svc_tli_create(sock, nconf, NULL, 224 RPC_MAXDATASIZE, RPC_MAXDATASIZE); 225 } else { 226 transp = svc_tli_create(RPC_ANYFD, nconf, NULL, 227 RPC_MAXDATASIZE, RPC_MAXDATASIZE); 228 } 229 230 if (transp == NULL) { 231 errx(1, "cannot create %s service.", transports[i]); 232 /* NOTREACHED */ 233 } 234 if (!svc_reg(transp, NLM_PROG, NLM_SM, nlm_prog_0, nconf)) { 235 errx(1, "unable to register (NLM_PROG, NLM_SM, %s)", 236 transports[i]); 237 /* NOTREACHED */ 238 } 239 if (!svc_reg(transp, NLM_PROG, NLM_VERS, nlm_prog_1, nconf)) { 240 errx(1, "unable to register (NLM_PROG, NLM_VERS, %s)", 241 transports[i]); 242 /* NOTREACHED */ 243 } 244 if (!svc_reg(transp, NLM_PROG, NLM_VERSX, nlm_prog_3, nconf)) { 245 errx(1, "unable to register (NLM_PROG, NLM_VERSX, %s)", 246 transports[i]); 247 /* NOTREACHED */ 248 } 249 if (!svc_reg(transp, NLM_PROG, NLM_VERS4, nlm_prog_4, nconf)) { 250 errx(1, "unable to register (NLM_PROG, NLM_VERS4, %s)", 251 transports[i]); 252 /* NOTREACHED */ 253 } 254 freenetconfigent(nconf); 255 } 256 257 /* 258 * Note that it is NOT sensible to run this program from inetd - the 259 * protocol assumes that it will run immediately at boot time. 260 */ 261 if (daemon(0, debug_level > 0)) { 262 err(1, "cannot fork"); 263 /* NOTREACHED */ 264 } 265 266 openlog("rpc.lockd", 0, LOG_DAEMON); 267 if (debug_level) 268 syslog(LOG_INFO, "Starting, debug level %d", debug_level); 269 else 270 syslog(LOG_INFO, "Starting"); 271 272 sigalarm.sa_handler = (sig_t) sigalarm_handler; 273 sigemptyset(&sigalarm.sa_mask); 274 sigalarm.sa_flags = SA_RESETHAND; /* should only happen once */ 275 sigalarm.sa_flags |= SA_RESTART; 276 if (sigaction(SIGALRM, &sigalarm, NULL) != 0) { 277 syslog(LOG_WARNING, "sigaction(SIGALRM) failed: %s", 278 strerror(errno)); 279 exit(1); 280 } 281 grace_expired = 0; 282 alarm(grace_period); 283 284 init_nsm(); 285 286 client_pid = client_request(); 287 288 svc_run(); /* Should never return */ 289 exit(1); 290 } 291 292 void 293 sigalarm_handler(void) 294 { 295 296 grace_expired = 1; 297 } 298 299 void 300 usage() 301 { 302 errx(1, "usage: rpc.lockd [-d <debuglevel>]" 303 " [-g <grace period>] [-p <port>]"); 304 } 305 306 /* 307 * init_nsm -- 308 * Reset the NSM state-of-the-world and acquire its state. 309 */ 310 void 311 init_nsm(void) 312 { 313 enum clnt_stat ret; 314 my_id id; 315 sm_stat stat; 316 char name[] = "NFS NLM"; 317 char localhost[] = "localhost"; 318 319 /* 320 * !!! 321 * The my_id structure isn't used by the SM_UNMON_ALL call, as far 322 * as I know. Leave it empty for now. 323 */ 324 memset(&id, 0, sizeof(id)); 325 id.my_name = name; 326 327 /* 328 * !!! 329 * The statd program must already be registered when lockd runs. 330 */ 331 do { 332 ret = callrpc("localhost", SM_PROG, SM_VERS, SM_UNMON_ALL, 333 (xdrproc_t)xdr_my_id, &id, (xdrproc_t)xdr_sm_stat, &stat); 334 if (ret == RPC_PROGUNAVAIL) { 335 syslog(LOG_WARNING, "%lu %s", SM_PROG, 336 clnt_sperrno(ret)); 337 sleep(2); 338 continue; 339 } 340 break; 341 } while (0); 342 343 if (ret != 0) { 344 syslog(LOG_ERR, "%lu %s", SM_PROG, clnt_sperrno(ret)); 345 exit(1); 346 } 347 348 nsm_state = stat.state; 349 350 /* setup constant data for SM_MON calls */ 351 mon_host.mon_id.my_id.my_name = localhost; 352 mon_host.mon_id.my_id.my_prog = NLM_PROG; 353 mon_host.mon_id.my_id.my_vers = NLM_SM; 354 mon_host.mon_id.my_id.my_proc = NLM_SM_NOTIFY; /* bsdi addition */ 355 } 356