yppasswdd_main.c revision 90297
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 34static const char rcsid[] = 35 "$FreeBSD: head/usr.sbin/rpc.yppasswdd/yppasswdd_main.c 90297 2002-02-06 13:30:31Z des $"; 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> 61struct 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 76int _rpcpmstart = 0; /* Started by a port monitor ? */ 77static 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 85extern int _rpcsvcstate; /* Set when a request is serviced */ 86char *progname = "rpc.yppasswdd"; 87char *yp_dir = _PATH_YP; 88char *passfile_default = _PATH_YP "master.passwd"; 89char *passfile; 90char *yppasswd_domain = NULL; 91int no_chsh = 0; 92int no_chfn = 0; 93int allow_additions = 0; 94int multidomain = 0; 95int verbose = 0; 96int resvport = 1; 97int inplace = 0; 98char *sockname = YP_SOCKNAME; 99 100static void terminate(sig) 101 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 109static void reload(sig) 110 int sig; 111{ 112 load_securenets(); 113} 114 115static void 116closedown(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 145static void usage() 146{ 147 fprintf(stderr, "%s\n%s\n", 148"usage: rpc.yppasswdd [-t master.passwd file] [-d domain] [-p path] [-s]", 149" [-f] [-m] [-i] [-a] [-v] [-u] [-h]"); 150 exit(1); 151} 152 153int 154main(argc, argv) 155 int argc; 156 char *argv[]; 157{ 158 register SVCXPRT *transp = NULL; 159 struct sockaddr_in saddr; 160 int asize = sizeof (saddr); 161 struct netconfig *nconf; 162 void *localhandle; 163 int ch; 164 char *mastername; 165 char myname[MAXHOSTNAMELEN + 2]; 166 167 extern int debug; 168 169 debug = 1; 170 171 while ((ch = getopt(argc, argv, "t:d:p:sfamuivh")) != -1) { 172 switch (ch) { 173 case 't': 174 passfile_default = optarg; 175 break; 176 case 'd': 177 yppasswd_domain = optarg; 178 break; 179 case 's': 180 no_chsh++; 181 break; 182 case 'f': 183 no_chfn++; 184 break; 185 case 'p': 186 yp_dir = optarg; 187 break; 188 case 'a': 189 allow_additions++; 190 break; 191 case 'm': 192 multidomain++; 193 break; 194 case 'i': 195 inplace++; 196 break; 197 case 'v': 198 verbose++; 199 break; 200 case 'u': 201 resvport = 0; 202 break; 203 default: 204 case 'h': 205 usage(); 206 break; 207 } 208 } 209 210 if (yppasswd_domain == NULL) { 211 if (yp_get_default_domain(&yppasswd_domain)) { 212 yp_error("no domain specified and system domain \ 213name isn't set -- aborting"); 214 usage(); 215 } 216 } 217 218 load_securenets(); 219 220 if (getrpcport("localhost", YPPROG, YPVERS, IPPROTO_UDP) <= 0) { 221 yp_error("no ypserv processes registered with local portmap"); 222 yp_error("this host is not an NIS server -- aborting"); 223 exit(1); 224 } 225 226 if ((mastername = ypxfr_get_master(yppasswd_domain, "passwd.byname", 227 "localhost",0)) == NULL) { 228 yp_error("can't get name of NIS master server for domain %s", 229 yppasswd_domain); 230 exit(1); 231 } 232 233 if (gethostname((char *)&myname, sizeof(myname)) == -1) { 234 yp_error("can't get local hostname: %s", strerror(errno)); 235 exit(1); 236 } 237 238 if (strncasecmp(mastername, (char *)&myname, sizeof(myname))) { 239 yp_error("master of %s is %s, but we are %s", 240 "passwd.byname", mastername, myname); 241 yp_error("this host is not the NIS master server for \ 242the %s domain -- aborting", yppasswd_domain); 243 exit(1); 244 } 245 246 debug = 0; 247 248 if (getsockname(0, (struct sockaddr *)&saddr, &asize) == 0) { 249 int ssize = sizeof (int); 250 if (saddr.sin_family != AF_INET) 251 exit(1); 252 if (getsockopt(0, SOL_SOCKET, SO_TYPE, 253 (char *)&_rpcfdtype, &ssize) == -1) 254 exit(1); 255 _rpcpmstart = 1; 256 } 257 258 if (!debug && _rpcpmstart == 0) { 259 if (daemon(0,0)) { 260 err(1,"cannot fork"); 261 } 262 } 263 openlog("rpc.yppasswdd", LOG_PID, LOG_DAEMON); 264 265 rpcb_unset(YPPASSWDPROG, YPPASSWDVERS, NULL); 266 rpcb_unset(MASTER_YPPASSWDPROG, MASTER_YPPASSWDVERS, NULL); 267 268 if (svc_create(yppasswdprog_1, YPPASSWDPROG, YPPASSWDVERS, "netpath") == 0) { 269 yp_error("cannot create yppasswd service."); 270 exit(1); 271 } 272 if (svc_create(master_yppasswdprog_1, MASTER_YPPASSWDPROG, 273 MASTER_YPPASSWDVERS, "netpath") == 0) { 274 yp_error("cannot create master_yppasswd service."); 275 exit(1); 276 } 277 278 nconf = NULL; 279 localhandle = setnetconfig(); 280 while ((nconf = getnetconfig(localhandle)) != NULL) { 281 if (nconf->nc_protofmly != NULL && 282 strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0) 283 break; 284 } 285 if (nconf == NULL) { 286 yp_error("getnetconfigent unix: %s", nc_sperror()); 287 exit(1); 288 } 289 unlink(sockname); 290 transp = svcunix_create(RPC_ANYSOCK, 0, 0, sockname); 291 if (transp == NULL) { 292 yp_error("cannot create AF_LOCAL service."); 293 exit(1); 294 } 295 if (!svc_reg(transp, MASTER_YPPASSWDPROG, MASTER_YPPASSWDVERS, 296 master_yppasswdprog_1, nconf)) { 297 yp_error("unable to register (MASTER_YPPASSWDPROG, 298 MASTER_YPPASSWDVERS, unix)."); 299 exit(1); 300 } 301 endnetconfig(localhandle); 302 303 /* Only root may connect() to the AF_UNIX link. */ 304 if (chmod(sockname, 0)) 305 err(1, "chmod of %s failed", sockname); 306 307 if (transp == (SVCXPRT *)NULL) { 308 yp_error("could not create a handle"); 309 exit(1); 310 } 311 if (_rpcpmstart) { 312 (void) signal(SIGALRM, (SIG_PF) closedown); 313 (void) alarm(_RPCSVC_CLOSEDOWN/2); 314 } 315 /* set up resource limits and block signals */ 316 pw_init(); 317 318 /* except SIGCHLD, which we need to catch */ 319 install_reaper(1); 320 signal(SIGTERM, (SIG_PF) terminate); 321 322 signal(SIGHUP, (SIG_PF) reload); 323 324 svc_run(); 325 yp_error("svc_run returned"); 326 exit(1); 327 /* NOTREACHED */ 328} 329