1 /* 2 * Copyright (c) 1995, 1996 3 * Bill Paul <wpaul@ctr.columbia.edu>. 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 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by Bill Paul. 16 * 4. Neither the name of the author nor the names of any co-contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 #ifndef lint 34 static const char rcsid[] = 35 "$FreeBSD$"; 36 #endif /* not lint */ 37 38 #include <sys/param.h> 39 #include <sys/ioctl.h> 40 #include <sys/stat.h> 41 #include <sys/socket.h> 42 #include <sys/time.h> 43 #include <sys/resource.h> 44 #include <netinet/in.h> 45 46 #include <err.h> 47 #include <errno.h> 48 #include <fcntl.h> 49 #include <memory.h> 50 #include <signal.h> 51 #include <stdio.h> 52 #include <stdlib.h> /* getenv, exit */ 53 #include <string.h> 54 #include <string.h> /* strcmp */ 55 #include <syslog.h> 56 #include <unistd.h> 57 58 #include <rpc/rpc.h> 59 #include <rpc/pmap_clnt.h> /* for pmap_unset */ 60 #include <rpcsvc/yp.h> 61 struct dom_binding {}; 62 #include <rpcsvc/ypclnt.h> 63 64 #include "yppasswd.h" 65 #include "yppasswdd_extern.h" 66 #include "yppasswd_private.h" 67 #include "ypxfr_extern.h" 68 #include "yp_extern.h" 69 70 #ifndef SIG_PF 71 #define SIG_PF void(*)(int) 72 #endif 73 74 #ifdef DEBUG 75 #define RPC_SVC_FG 76 #endif 77 78 #define _RPCSVC_CLOSEDOWN 120 79 int _rpcpmstart = 0; /* Started by a port monitor ? */ 80 static int _rpcfdtype; 81 /* Whether Stream or Datagram ? */ 82 /* States a server can be in wrt request */ 83 84 #define _IDLE 0 85 #define _SERVED 1 86 #define _SERVING 2 87 88 static char _localhost[] = "localhost"; 89 static char _passwd_byname[] = "passwd.byname"; 90 extern int _rpcsvcstate; /* Set when a request is serviced */ 91 static char _progname[] = "rpc.yppasswdd"; 92 char *progname = _progname; 93 static char _yp_dir[] = _PATH_YP; 94 char *yp_dir = _yp_dir; 95 static char _passfile_default[] = _PATH_YP "master.passwd"; 96 char *passfile_default = _passfile_default; 97 char *passfile; 98 char *yppasswd_domain = NULL; 99 int no_chsh = 0; 100 int no_chfn = 0; 101 int allow_additions = 0; 102 int multidomain = 0; 103 int verbose = 0; 104 int resvport = 1; 105 int inplace = 0; 106 char sockname[] = YP_SOCKNAME; 107 108 static void 109 terminate(int sig __unused) 110 { 111 rpcb_unset(YPPASSWDPROG, YPPASSWDVERS, NULL); 112 rpcb_unset(MASTER_YPPASSWDPROG, MASTER_YPPASSWDVERS, NULL); 113 unlink(sockname); 114 exit(0); 115 } 116 117 static void 118 reload(int sig __unused) 119 { 120 load_securenets(); 121 } 122 123 static void 124 closedown(int sig __unused) 125 { 126 if (_rpcsvcstate == _IDLE) { 127 extern fd_set svc_fdset; 128 static int size; 129 int i, openfd; 130 131 if (_rpcfdtype == SOCK_DGRAM) { 132 unlink(sockname); 133 exit(0); 134 } 135 if (size == 0) { 136 size = getdtablesize(); 137 } 138 for (i = 0, openfd = 0; i < size && openfd < 2; i++) 139 if (FD_ISSET(i, &svc_fdset)) 140 openfd++; 141 if (openfd <= 1) { 142 unlink(sockname); 143 exit(0); 144 } 145 } 146 if (_rpcsvcstate == _SERVED) 147 _rpcsvcstate = _IDLE; 148 149 (void) signal(SIGALRM, (SIG_PF) closedown); 150 (void) alarm(_RPCSVC_CLOSEDOWN/2); 151 } 152 153 static void 154 usage(void) 155 { 156 fprintf(stderr, "%s\n%s\n", 157 "usage: rpc.yppasswdd [-t master.passwd file] [-d domain] [-p path] [-s]", 158 " [-f] [-m] [-i] [-a] [-v] [-u] [-h]"); 159 exit(1); 160 } 161 162 int 163 main(int argc, char *argv[]) 164 { 165 struct rlimit rlim; 166 SVCXPRT *transp = NULL; 167 struct sockaddr_in saddr; 168 int asize = sizeof (saddr); 169 struct netconfig *nconf; 170 void *localhandle; 171 int ch; 172 char *mastername; 173 char myname[MAXHOSTNAMELEN + 2]; 174 175 extern int debug; 176 177 debug = 1; 178 179 while ((ch = getopt(argc, argv, "t:d:p:sfamuivh")) != -1) { 180 switch (ch) { 181 case 't': 182 passfile_default = optarg; 183 break; 184 case 'd': 185 yppasswd_domain = optarg; 186 break; 187 case 's': 188 no_chsh++; 189 break; 190 case 'f': 191 no_chfn++; 192 break; 193 case 'p': 194 yp_dir = optarg; 195 break; 196 case 'a': 197 allow_additions++; 198 break; 199 case 'm': 200 multidomain++; 201 break; 202 case 'i': 203 inplace++; 204 break; 205 case 'v': 206 verbose++; 207 break; 208 case 'u': 209 resvport = 0; 210 break; 211 default: 212 case 'h': 213 usage(); 214 break; 215 } 216 } 217 218 if (yppasswd_domain == NULL) { 219 if (yp_get_default_domain(&yppasswd_domain)) { 220 yp_error("no domain specified and system domain \ 221 name isn't set -- aborting"); 222 usage(); 223 } 224 } 225 226 load_securenets(); 227 228 if (getrpcport(_localhost, YPPROG, YPVERS, IPPROTO_UDP) <= 0) { 229 yp_error("no ypserv processes registered with local portmap"); 230 yp_error("this host is not an NIS server -- aborting"); 231 exit(1); 232 } 233 234 if ((mastername = ypxfr_get_master(yppasswd_domain, 235 _passwd_byname, _localhost, 0)) == NULL) { 236 yp_error("can't get name of NIS master server for domain %s", 237 yppasswd_domain); 238 exit(1); 239 } 240 241 if (gethostname((char *)&myname, sizeof(myname)) == -1) { 242 yp_error("can't get local hostname: %s", strerror(errno)); 243 exit(1); 244 } 245 246 if (strncasecmp(mastername, (char *)&myname, sizeof(myname))) { 247 yp_error("master of %s is %s, but we are %s", 248 "passwd.byname", mastername, myname); 249 yp_error("this host is not the NIS master server for \ 250 the %s domain -- aborting", yppasswd_domain); 251 exit(1); 252 } 253 254 debug = 0; 255 256 if (getsockname(0, (struct sockaddr *)&saddr, &asize) == 0) { 257 int ssize = sizeof (int); 258 if (saddr.sin_family != AF_INET) 259 exit(1); 260 if (getsockopt(0, SOL_SOCKET, SO_TYPE, 261 (char *)&_rpcfdtype, &ssize) == -1) 262 exit(1); 263 _rpcpmstart = 1; 264 } 265 266 if (!debug && _rpcpmstart == 0) { 267 if (daemon(0,0)) { 268 err(1,"cannot fork"); 269 } 270 } 271 openlog("rpc.yppasswdd", LOG_PID, LOG_DAEMON); 272 273 rpcb_unset(YPPASSWDPROG, YPPASSWDVERS, NULL); 274 rpcb_unset(MASTER_YPPASSWDPROG, MASTER_YPPASSWDVERS, NULL); 275 276 if (svc_create(yppasswdprog_1, YPPASSWDPROG, YPPASSWDVERS, "netpath") == 0) { 277 yp_error("cannot create yppasswd service."); 278 exit(1); 279 } 280 if (svc_create(master_yppasswdprog_1, MASTER_YPPASSWDPROG, 281 MASTER_YPPASSWDVERS, "netpath") == 0) { 282 yp_error("cannot create master_yppasswd service."); 283 exit(1); 284 } 285 286 nconf = NULL; 287 localhandle = setnetconfig(); 288 while ((nconf = getnetconfig(localhandle)) != NULL) { 289 if (nconf->nc_protofmly != NULL && 290 strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0) 291 break; 292 } 293 if (nconf == NULL) { 294 yp_error("getnetconfigent unix: %s", nc_sperror()); 295 exit(1); 296 } 297 unlink(sockname); 298 transp = svcunix_create(RPC_ANYSOCK, 0, 0, sockname); 299 if (transp == NULL) { 300 yp_error("cannot create AF_LOCAL service."); 301 exit(1); 302 } 303 if (!svc_reg(transp, MASTER_YPPASSWDPROG, MASTER_YPPASSWDVERS, 304 master_yppasswdprog_1, nconf)) { 305 yp_error("unable to register (MASTER_YPPASSWDPROG, \ 306 MASTER_YPPASSWDVERS, unix)."); 307 exit(1); 308 } 309 endnetconfig(localhandle); 310 311 /* Only root may connect() to the AF_UNIX link. */ 312 if (chmod(sockname, 0)) 313 err(1, "chmod of %s failed", sockname); 314 315 if (transp == (SVCXPRT *)NULL) { 316 yp_error("could not create a handle"); 317 exit(1); 318 } 319 if (_rpcpmstart) { 320 (void) signal(SIGALRM, (SIG_PF) closedown); 321 (void) alarm(_RPCSVC_CLOSEDOWN/2); 322 } 323 324 /* Unlimited resource limits. */ 325 rlim.rlim_cur = rlim.rlim_max = RLIM_INFINITY; 326 (void)setrlimit(RLIMIT_CPU, &rlim); 327 (void)setrlimit(RLIMIT_FSIZE, &rlim); 328 (void)setrlimit(RLIMIT_STACK, &rlim); 329 (void)setrlimit(RLIMIT_DATA, &rlim); 330 (void)setrlimit(RLIMIT_RSS, &rlim); 331 332 /* Don't drop core (not really necessary, but GP's). */ 333 rlim.rlim_cur = rlim.rlim_max = 0; 334 (void)setrlimit(RLIMIT_CORE, &rlim); 335 336 /* Turn off signals. */ 337 (void)signal(SIGALRM, SIG_IGN); 338 (void)signal(SIGHUP, (SIG_PF) reload); 339 (void)signal(SIGINT, SIG_IGN); 340 (void)signal(SIGPIPE, SIG_IGN); 341 (void)signal(SIGQUIT, SIG_IGN); 342 (void)signal(SIGTERM, (SIG_PF) terminate); 343 344 svc_run(); 345 yp_error("svc_run returned"); 346 exit(1); 347 /* NOTREACHED */ 348 } 349