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 "yppasswd.h" 39 #include <stdio.h> 40 #include <sys/types.h> 41 #include <stdlib.h> /* getenv, exit */ 42 #include <unistd.h> 43 #include <string.h> 44 #include <sys/param.h> 45 #include <rpc/pmap_clnt.h> /* for pmap_unset */ 46 #include <string.h> /* strcmp */ 47 #include <signal.h> 48 #include <fcntl.h> 49 #include <sys/ioctl.h> 50 #include <sys/stat.h> 51 #ifdef __cplusplus 52 #include <sysent.h> /* getdtablesize, open */ 53 #endif /* __cplusplus */ 54 #include <memory.h> 55 #include <sys/socket.h> 56 #include <netinet/in.h> 57 #include <syslog.h> 58 #include <err.h> 59 #include <errno.h> 60 #include <rpcsvc/yp.h> 61 struct dom_binding {}; 62 #include <rpcsvc/ypclnt.h> 63 #include "yppasswdd_extern.h" 64 #include "yppasswd_private.h" 65 #include "ypxfr_extern.h" 66 67 #ifndef SIG_PF 68 #define SIG_PF void(*)(int) 69 #endif 70 71 #ifdef DEBUG 72 #define RPC_SVC_FG 73 #endif 74 75 #define _RPCSVC_CLOSEDOWN 120 76 int _rpcpmstart = 0; /* Started by a port monitor ? */ 77 static int _rpcfdtype; 78 /* Whether Stream or Datagram ? */ 79 /* States a server can be in wrt request */ 80 81 #define _IDLE 0 82 #define _SERVED 1 83 #define _SERVING 2 84 85 extern int _rpcsvcstate; /* Set when a request is serviced */ 86 char *progname = "rpc.yppasswdd"; 87 char *yp_dir = _PATH_YP; 88 char *passfile_default = _PATH_YP "master.passwd"; 89 char *passfile; 90 char *yppasswd_domain = NULL; 91 int no_chsh = 0; 92 int no_chfn = 0; 93 int allow_additions = 0; 94 int multidomain = 0; 95 int verbose = 0; 96 int resvport = 1; 97 int inplace = 0; 98 char *sockname = YP_SOCKNAME; 99 100 static void 101 terminate(int sig) 102 { 103 rpcb_unset(YPPASSWDPROG, YPPASSWDVERS, NULL); 104 rpcb_unset(MASTER_YPPASSWDPROG, MASTER_YPPASSWDVERS, NULL); 105 unlink(sockname); 106 exit(0); 107 } 108 109 static void 110 reload(int sig) 111 { 112 load_securenets(); 113 } 114 115 static void 116 closedown(int sig) 117 { 118 if (_rpcsvcstate == _IDLE) { 119 extern fd_set svc_fdset; 120 static int size; 121 int i, openfd; 122 123 if (_rpcfdtype == SOCK_DGRAM) { 124 unlink(sockname); 125 exit(0); 126 } 127 if (size == 0) { 128 size = getdtablesize(); 129 } 130 for (i = 0, openfd = 0; i < size && openfd < 2; i++) 131 if (FD_ISSET(i, &svc_fdset)) 132 openfd++; 133 if (openfd <= 1) { 134 unlink(sockname); 135 exit(0); 136 } 137 } 138 if (_rpcsvcstate == _SERVED) 139 _rpcsvcstate = _IDLE; 140 141 (void) signal(SIGALRM, (SIG_PF) closedown); 142 (void) alarm(_RPCSVC_CLOSEDOWN/2); 143 } 144 145 static void 146 usage(void) 147 { 148 fprintf(stderr, "%s\n%s\n", 149 "usage: rpc.yppasswdd [-t master.passwd file] [-d domain] [-p path] [-s]", 150 " [-f] [-m] [-i] [-a] [-v] [-u] [-h]"); 151 exit(1); 152 } 153 154 int 155 main(int argc, char *argv[]) 156 { 157 register SVCXPRT *transp = NULL; 158 struct sockaddr_in saddr; 159 int asize = sizeof (saddr); 160 struct netconfig *nconf; 161 void *localhandle; 162 int ch; 163 char *mastername; 164 char myname[MAXHOSTNAMELEN + 2]; 165 166 extern int debug; 167 168 debug = 1; 169 170 while ((ch = getopt(argc, argv, "t:d:p:sfamuivh")) != -1) { 171 switch (ch) { 172 case 't': 173 passfile_default = optarg; 174 break; 175 case 'd': 176 yppasswd_domain = optarg; 177 break; 178 case 's': 179 no_chsh++; 180 break; 181 case 'f': 182 no_chfn++; 183 break; 184 case 'p': 185 yp_dir = optarg; 186 break; 187 case 'a': 188 allow_additions++; 189 break; 190 case 'm': 191 multidomain++; 192 break; 193 case 'i': 194 inplace++; 195 break; 196 case 'v': 197 verbose++; 198 break; 199 case 'u': 200 resvport = 0; 201 break; 202 default: 203 case 'h': 204 usage(); 205 break; 206 } 207 } 208 209 if (yppasswd_domain == NULL) { 210 if (yp_get_default_domain(&yppasswd_domain)) { 211 yp_error("no domain specified and system domain \ 212 name isn't set -- aborting"); 213 usage(); 214 } 215 } 216 217 load_securenets(); 218 219 if (getrpcport("localhost", YPPROG, YPVERS, IPPROTO_UDP) <= 0) { 220 yp_error("no ypserv processes registered with local portmap"); 221 yp_error("this host is not an NIS server -- aborting"); 222 exit(1); 223 } 224 225 if ((mastername = ypxfr_get_master(yppasswd_domain, "passwd.byname", 226 "localhost",0)) == NULL) { 227 yp_error("can't get name of NIS master server for domain %s", 228 yppasswd_domain); 229 exit(1); 230 } 231 232 if (gethostname((char *)&myname, sizeof(myname)) == -1) { 233 yp_error("can't get local hostname: %s", strerror(errno)); 234 exit(1); 235 } 236 237 if (strncasecmp(mastername, (char *)&myname, sizeof(myname))) { 238 yp_error("master of %s is %s, but we are %s", 239 "passwd.byname", mastername, myname); 240 yp_error("this host is not the NIS master server for \ 241 the %s domain -- aborting", yppasswd_domain); 242 exit(1); 243 } 244 245 debug = 0; 246 247 if (getsockname(0, (struct sockaddr *)&saddr, &asize) == 0) { 248 int ssize = sizeof (int); 249 if (saddr.sin_family != AF_INET) 250 exit(1); 251 if (getsockopt(0, SOL_SOCKET, SO_TYPE, 252 (char *)&_rpcfdtype, &ssize) == -1) 253 exit(1); 254 _rpcpmstart = 1; 255 } 256 257 if (!debug && _rpcpmstart == 0) { 258 if (daemon(0,0)) { 259 err(1,"cannot fork"); 260 } 261 } 262 openlog("rpc.yppasswdd", LOG_PID, LOG_DAEMON); 263 264 rpcb_unset(YPPASSWDPROG, YPPASSWDVERS, NULL); 265 rpcb_unset(MASTER_YPPASSWDPROG, MASTER_YPPASSWDVERS, NULL); 266 267 if (svc_create(yppasswdprog_1, YPPASSWDPROG, YPPASSWDVERS, "netpath") == 0) { 268 yp_error("cannot create yppasswd service."); 269 exit(1); 270 } 271 if (svc_create(master_yppasswdprog_1, MASTER_YPPASSWDPROG, 272 MASTER_YPPASSWDVERS, "netpath") == 0) { 273 yp_error("cannot create master_yppasswd service."); 274 exit(1); 275 } 276 277 nconf = NULL; 278 localhandle = setnetconfig(); 279 while ((nconf = getnetconfig(localhandle)) != NULL) { 280 if (nconf->nc_protofmly != NULL && 281 strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0) 282 break; 283 } 284 if (nconf == NULL) { 285 yp_error("getnetconfigent unix: %s", nc_sperror()); 286 exit(1); 287 } 288 unlink(sockname); 289 transp = svcunix_create(RPC_ANYSOCK, 0, 0, sockname); 290 if (transp == NULL) { 291 yp_error("cannot create AF_LOCAL service."); 292 exit(1); 293 } 294 if (!svc_reg(transp, MASTER_YPPASSWDPROG, MASTER_YPPASSWDVERS, 295 master_yppasswdprog_1, nconf)) { 296 yp_error("unable to register (MASTER_YPPASSWDPROG, 297 MASTER_YPPASSWDVERS, unix)."); 298 exit(1); 299 } 300 endnetconfig(localhandle); 301 302 /* Only root may connect() to the AF_UNIX link. */ 303 if (chmod(sockname, 0)) 304 err(1, "chmod of %s failed", sockname); 305 306 if (transp == (SVCXPRT *)NULL) { 307 yp_error("could not create a handle"); 308 exit(1); 309 } 310 if (_rpcpmstart) { 311 (void) signal(SIGALRM, (SIG_PF) closedown); 312 (void) alarm(_RPCSVC_CLOSEDOWN/2); 313 } 314 /* set up resource limits and block signals */ 315 pw_init(); 316 317 /* except SIGCHLD, which we need to catch */ 318 install_reaper(1); 319 signal(SIGTERM, (SIG_PF) terminate); 320 321 signal(SIGHUP, (SIG_PF) reload); 322 323 svc_run(); 324 yp_error("svc_run returned"); 325 exit(1); 326 /* NOTREACHED */ 327 } 328