yppasswdd_main.c revision 59218
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 59218 2000-04-14 06:49:16Z imp $"; 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#include <sys/ttycom.h> /* TIOCNOTTY */ 52#ifdef __cplusplus 53#include <sysent.h> /* getdtablesize, open */ 54#endif /* __cplusplus */ 55#include <memory.h> 56#include <sys/socket.h> 57#include <netinet/in.h> 58#include <syslog.h> 59#include <err.h> 60#include <errno.h> 61#include <rpcsvc/yp.h> 62struct dom_binding {}; 63#include <rpcsvc/ypclnt.h> 64#include "yppasswdd_extern.h" 65#include "yppasswd_private.h" 66#include "ypxfr_extern.h" 67 68#ifndef SIG_PF 69#define SIG_PF void(*)(int) 70#endif 71 72#ifdef DEBUG 73#define RPC_SVC_FG 74#endif 75 76#define _RPCSVC_CLOSEDOWN 120 77int _rpcpmstart = 0; /* Started by a port monitor ? */ 78static int _rpcfdtype; 79 /* Whether Stream or Datagram ? */ 80 /* States a server can be in wrt request */ 81 82#define _IDLE 0 83#define _SERVED 1 84#define _SERVING 2 85 86extern int _rpcsvcstate; /* Set when a request is serviced */ 87char *progname = "rpc.yppasswdd"; 88char *yp_dir = _PATH_YP; 89char *passfile_default = _PATH_YP "master.passwd"; 90char *passfile; 91char *yppasswd_domain = NULL; 92int no_chsh = 0; 93int no_chfn = 0; 94int allow_additions = 0; 95int multidomain = 0; 96int verbose = 0; 97int resvport = 1; 98int inplace = 0; 99char *sockname = YP_SOCKNAME; 100 101static void terminate(sig) 102 int sig; 103{ 104 svc_unregister(YPPASSWDPROG, YPPASSWDVERS); 105 svc_unregister(MASTER_YPPASSWDPROG, MASTER_YPPASSWDVERS); 106 unlink(sockname); 107 exit(0); 108} 109 110static void reload(sig) 111 int sig; 112{ 113 load_securenets(); 114} 115 116static void 117closedown(int sig) 118{ 119 if (_rpcsvcstate == _IDLE) { 120 extern fd_set svc_fdset; 121 static int size; 122 int i, openfd; 123 124 if (_rpcfdtype == SOCK_DGRAM) { 125 unlink(sockname); 126 exit(0); 127 } 128 if (size == 0) { 129 size = getdtablesize(); 130 } 131 for (i = 0, openfd = 0; i < size && openfd < 2; i++) 132 if (FD_ISSET(i, &svc_fdset)) 133 openfd++; 134 if (openfd <= 1) { 135 unlink(sockname); 136 exit(0); 137 } 138 } 139 if (_rpcsvcstate == _SERVED) 140 _rpcsvcstate = _IDLE; 141 142 (void) signal(SIGALRM, (SIG_PF) closedown); 143 (void) alarm(_RPCSVC_CLOSEDOWN/2); 144} 145 146static void usage() 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 154int 155main(argc, argv) 156 int argc; 157 char *argv[]; 158{ 159 register SVCXPRT *transp = NULL; 160 int sock; 161 int proto = 0; 162 struct sockaddr_in saddr; 163 int asize = sizeof (saddr); 164 int ch; 165 char *mastername; 166 char myname[MAXHOSTNAMELEN + 2]; 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 251 if (saddr.sin_family != AF_INET) 252 exit(1); 253 if (getsockopt(0, SOL_SOCKET, SO_TYPE, 254 (char *)&_rpcfdtype, &ssize) == -1) 255 exit(1); 256 sock = 0; 257 _rpcpmstart = 1; 258 proto = 0; 259 openlog("rpc.yppasswdd", LOG_PID, LOG_DAEMON); 260 } else { 261 if (!debug) { 262 if (daemon(0,0)) { 263 err(1,"cannot fork"); 264 } 265 } 266 openlog("rpc.yppasswdd", LOG_PID, LOG_DAEMON); 267 sock = RPC_ANYSOCK; 268 (void) pmap_unset(YPPASSWDPROG, YPPASSWDVERS); 269 (void) pmap_unset(MASTER_YPPASSWDPROG, MASTER_YPPASSWDVERS); 270 unlink(sockname); 271 } 272 273 if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_DGRAM)) { 274 transp = svcudp_create(sock); 275 if (transp == NULL) { 276 yp_error("cannot create udp service."); 277 exit(1); 278 } 279 if (!_rpcpmstart) 280 proto = IPPROTO_UDP; 281 if (!svc_register(transp, YPPASSWDPROG, YPPASSWDVERS, yppasswdprog_1, proto)) { 282 yp_error("unable to register (YPPASSWDPROG, YPPASSWDVERS, udp)."); 283 exit(1); 284 } 285 } 286 287 if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_STREAM)) { 288 transp = svctcp_create(sock, 0, 0); 289 if (transp == NULL) { 290 yp_error("cannot create tcp service."); 291 exit(1); 292 } 293 if (!_rpcpmstart) 294 proto = IPPROTO_TCP; 295 if (!svc_register(transp, YPPASSWDPROG, YPPASSWDVERS, yppasswdprog_1, proto)) { 296 yp_error("unable to register (YPPASSWDPROG, YPPASSWDVERS, tcp)."); 297 exit(1); 298 } 299 } 300 301 unlink(sockname); 302 transp = svcunix_create(sock, 0, 0, sockname); 303 if (transp == NULL) { 304 yp_error("cannot create AF_LOCAL service."); 305 exit(1); 306 } 307 if (!svc_register(transp, MASTER_YPPASSWDPROG, MASTER_YPPASSWDVERS, master_yppasswdprog_1, 0)) { 308 yp_error("unable to register (MASTER_YPPASSWDPROG, MASTER_YPPASSWDVERS, unix)."); 309 exit(1); 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 /* set up resource limits and block signals */ 324 pw_init(); 325 326 /* except SIGCHLD, which we need to catch */ 327 install_reaper(1); 328 signal(SIGTERM, (SIG_PF) terminate); 329 330 signal(SIGHUP, (SIG_PF) reload); 331 332 svc_run(); 333 yp_error("svc_run returned"); 334 exit(1); 335 /* NOTREACHED */ 336} 337