1 /* $NetBSD: lockd.c,v 1.13 2019/08/15 08:34:19 kamil Exp $ */ 2 3 /* 4 * Copyright (c) 1995 5 * A.R. Gordon (andrew.gordon@net-tel.co.uk). All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed for the FreeBSD project 18 * 4. Neither the name of the author nor the names of any co-contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY ANDREW GORDON AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 */ 35 36 #include <sys/cdefs.h> 37 #ifndef lint 38 __RCSID("$NetBSD: lockd.c,v 1.13 2019/08/15 08:34:19 kamil Exp $"); 39 #endif 40 41 /* 42 * main() function for NFS lock daemon. Most of the code in this 43 * file was generated by running rpcgen /usr/include/rpcsvc/nlm_prot.x. 44 * 45 * The actual program logic is in the file lock_proc.c 46 */ 47 48 #include <sys/types.h> 49 #include <sys/socket.h> 50 51 #include <err.h> 52 #include <stdio.h> 53 #include <stdlib.h> 54 #include <stdbool.h> 55 #include <errno.h> 56 #include <syslog.h> 57 #include <signal.h> 58 #include <string.h> 59 #include <unistd.h> 60 #include <util.h> 61 #include <netconfig.h> 62 63 #include <rpc/rpc.h> 64 #include <rpcsvc/sm_inter.h> 65 66 #include "lockd.h" 67 #include <rpcsvc/nlm_prot.h> 68 69 int debug_level = 0; /* 0 = no debugging syslog() calls */ 70 extern int _rpcsvcdirty; 71 72 int grace_expired; 73 74 void nlm_prog_0(struct svc_req *, SVCXPRT *); 75 void nlm_prog_1(struct svc_req *, SVCXPRT *); 76 void nlm_prog_3(struct svc_req *, SVCXPRT *); 77 void nlm_prog_4(struct svc_req *, SVCXPRT *); 78 static void usage(void) __dead; 79 80 static void sigalarm_handler(int); 81 82 static const char *transports[] = { "udp", "tcp", "udp6", "tcp6" }; 83 84 int 85 main(int argc, char **argv) 86 { 87 SVCXPRT *transp; 88 int ch, i, minindex, maxindex, s; 89 bool use_ipv4, use_ipv6; 90 struct sigaction sigchild, sigalarm; 91 int grace_period = 30; 92 struct netconfig *nconf; 93 int maxrec = RPC_MAXDATASIZE; 94 95 (void)setprogname(*argv); 96 use_ipv4 = use_ipv6 = false; 97 while ((ch = getopt(argc, argv, "d:g:46")) != (-1)) { 98 switch (ch) { 99 case 'd': 100 debug_level = atoi(optarg); 101 if (!debug_level) { 102 usage(); 103 /* NOTREACHED */ 104 } 105 break; 106 case 'g': 107 grace_period = atoi(optarg); 108 if (!grace_period) { 109 usage(); 110 /* NOTREACHED */ 111 } 112 break; 113 case '4': 114 use_ipv4 = true; 115 break; 116 case '6': 117 use_ipv6 = true; 118 break; 119 default: 120 case '?': 121 usage(); 122 /* NOTREACHED */ 123 } 124 } 125 126 (void)rpcb_unset(NLM_PROG, NLM_SM, NULL); 127 (void)rpcb_unset(NLM_PROG, NLM_VERS, NULL); 128 (void)rpcb_unset(NLM_PROG, NLM_VERSX, NULL); 129 (void)rpcb_unset(NLM_PROG, NLM_VERS4, NULL); 130 131 if (!use_ipv4 && !use_ipv6) { 132 s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP); 133 if (s < 0) 134 use_ipv4 = true; 135 else { 136 (void)close(s); 137 use_ipv4 = use_ipv6 = true; 138 } 139 } 140 if (use_ipv4) 141 minindex = 0; 142 else 143 minindex = 2; 144 145 if (use_ipv6) 146 maxindex = 4; 147 else 148 maxindex = 2; 149 150 (void)rpc_control(RPC_SVC_CONNMAXREC_SET, &maxrec); 151 152 for (i = minindex; i < maxindex; i++) { 153 nconf = getnetconfigent(transports[i]); 154 if (nconf == NULL) 155 errx(1, "cannot get %s netconf.", transports[i]); 156 157 transp = svc_tli_create(RPC_ANYFD, nconf, NULL, RPC_MAXDATASIZE, 158 RPC_MAXDATASIZE); 159 if (transp == NULL) { 160 errx(1, "cannot create %s service.", transports[i]); 161 /* NOTREACHED */ 162 } 163 if (!svc_reg(transp, NLM_PROG, NLM_SM, nlm_prog_0, nconf)) { 164 errx(1, "unable to register (NLM_PROG, NLM_SM, %s)", 165 transports[i]); 166 /* NOTREACHED */ 167 } 168 if (!svc_reg(transp, NLM_PROG, NLM_VERS, nlm_prog_1, nconf)) { 169 errx(1, "unable to register (NLM_PROG, NLM_VERS, %s)", 170 transports[i]); 171 /* NOTREACHED */ 172 } 173 if (!svc_reg(transp, NLM_PROG, NLM_VERSX, nlm_prog_3, nconf)) { 174 errx(1, "unable to register (NLM_PROG, NLM_VERSX, %s)", 175 transports[i]); 176 /* NOTREACHED */ 177 } 178 if (!svc_reg(transp, NLM_PROG, NLM_VERS4, nlm_prog_4, nconf)) { 179 errx(1, "unable to register (NLM_PROG, NLM_VERS4, %s)", 180 transports[i]); 181 /* NOTREACHED */ 182 } 183 freenetconfigent(nconf); 184 } 185 186 /* 187 * Note that it is NOT sensible to run this program from inetd - the 188 * protocol assumes that it will run immediately at boot time. 189 */ 190 if (daemon(0, 0) == -1) { 191 err(1, "cannot fork"); 192 /* NOTREACHED */ 193 } 194 (void)pidfile(NULL); 195 196 openlog("rpc.lockd", 0, LOG_DAEMON); 197 if (debug_level) 198 syslog(LOG_INFO, "Starting, debug level %d", debug_level); 199 else 200 syslog(LOG_INFO, "Starting"); 201 202 sigchild.sa_handler = sigchild_handler; 203 (void)sigemptyset(&sigchild.sa_mask); 204 sigchild.sa_flags = SA_RESTART; 205 if (sigaction(SIGCHLD, &sigchild, NULL) != 0) { 206 syslog(LOG_WARNING, "sigaction(SIGCHLD) failed (%m)"); 207 exit(1); 208 } 209 sigalarm.sa_handler = sigalarm_handler; 210 (void)sigemptyset(&sigalarm.sa_mask); 211 sigalarm.sa_flags = SA_RESETHAND; /* should only happen once */ 212 sigalarm.sa_flags |= SA_RESTART; 213 if (sigaction(SIGALRM, &sigalarm, NULL) != 0) { 214 syslog(LOG_WARNING, "sigaction(SIGALRM) failed (%m)"); 215 exit(1); 216 } 217 grace_expired = 0; 218 if (alarm(10) == (unsigned int)-1) { 219 syslog(LOG_WARNING, "alarm failed (%m)"); 220 exit(1); 221 } 222 223 svc_run(); /* Should never return */ 224 return 1; 225 } 226 227 static void 228 /*ARGSUSED*/ 229 sigalarm_handler(int s) 230 { 231 grace_expired = 1; 232 } 233 234 static void 235 usage(void) 236 { 237 (void)fprintf(stderr, "Usage: %s [-46] [-d debug_level] [-g grace_period]\n", 238 getprogname()); 239 exit(1); 240 } 241