1 /* $NetBSD: rpc.yppasswdd.c,v 1.17 2011/08/31 16:24:59 plunky Exp $ */ 2 3 /* 4 * Copyright (c) 1994 Mats O Jansson <moj@stacken.kth.se> 5 * 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 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 17 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 #ifndef lint 31 __RCSID("$NetBSD: rpc.yppasswdd.c,v 1.17 2011/08/31 16:24:59 plunky Exp $"); 32 #endif /* not lint */ 33 34 #include <sys/types.h> 35 #include <sys/wait.h> 36 37 #include <err.h> 38 #include <errno.h> 39 #include <limits.h> 40 #include <stdio.h> 41 #include <stdlib.h> 42 #include <string.h> 43 #include <signal.h> 44 #include <unistd.h> 45 #include <util.h> 46 47 #include <rpc/rpc.h> 48 #include <rpc/pmap_clnt.h> 49 #include <rpcsvc/yppasswd.h> 50 51 #include "extern.h" 52 53 int noshell, nogecos, nopw; 54 char make_arg[_POSIX2_LINE_MAX] = "make"; 55 56 int main(int, char *[]); 57 void yppasswddprog_1(struct svc_req *, SVCXPRT *); 58 __dead static void usage(void); 59 60 int 61 main(int argc, char *argv[]) 62 { 63 SVCXPRT *transp; 64 int i; 65 char *arg; 66 int maxrec = RPC_MAXDATASIZE; 67 68 for (i = 1; i < argc; i++) { 69 arg = argv[i]; 70 if (*arg++ != '-') 71 usage(); 72 if (strcmp("d", arg) == 0) 73 if (++i == argc) 74 usage(); 75 else { 76 if (pw_setprefix(argv[i]) < 0) 77 err(EXIT_FAILURE,NULL); 78 } 79 else if (strcmp("noshell", arg) == 0) 80 noshell = 1; 81 else if (strcmp("nogecos", arg) == 0) 82 nogecos = 1; 83 else if (strcmp("nopw", arg) == 0) 84 nopw = 1; 85 else if (strcmp("m", arg) == 0) { 86 int len; 87 88 len = strlen(make_arg); 89 if (++i == argc) 90 usage(); 91 for (; i < argc; i++) { 92 int arglen; 93 94 arglen = strlen(argv[i]); 95 if ((len + arglen) > (int)(sizeof(make_arg) - 2)) 96 errx(EXIT_FAILURE, "%s", strerror(E2BIG)); 97 make_arg[len++] = ' '; 98 (void)strcpy(&make_arg[len], argv[i]); 99 len += arglen; 100 } 101 } else 102 usage(); 103 } 104 105 if (daemon(0, 0)) 106 err(EXIT_FAILURE, "can't detach"); 107 pidfile(NULL); 108 109 rpc_control(RPC_SVC_CONNMAXREC_SET, &maxrec); 110 111 (void)pmap_unset(YPPASSWDPROG, YPPASSWDVERS); 112 113 transp = svcudp_create(RPC_ANYSOCK); 114 if (transp == NULL) 115 errx(EXIT_FAILURE, "cannot create UDP service"); 116 117 if (!svc_register(transp, YPPASSWDPROG, YPPASSWDVERS, yppasswddprog_1, 118 IPPROTO_UDP)) 119 errx(EXIT_FAILURE, 120 "unable to register YPPASSWDPROG/YPPASSWDVERS/UDP"); 121 122 transp = svctcp_create(RPC_ANYSOCK, RPC_MAXDATASIZE, RPC_MAXDATASIZE); 123 if (transp == NULL) 124 errx(EXIT_FAILURE, "cannot create TCP service"); 125 126 if (!svc_register(transp, YPPASSWDPROG, YPPASSWDVERS, yppasswddprog_1, 127 IPPROTO_TCP)) 128 errx(EXIT_FAILURE, 129 "unable to register YPPASSWDPROG/YPPASSWDVERS/TCP"); 130 131 svc_run(); 132 errx(EXIT_FAILURE, "svc_run returned"); 133 /* NOTREACHED */ 134 } 135 136 void 137 yppasswddprog_1(struct svc_req *rqstp, SVCXPRT *transp) 138 { 139 union { 140 yppasswd yppasswdproc_update_1_arg; 141 } argument; 142 143 switch (rqstp->rq_proc) { 144 case NULLPROC: 145 (void)svc_sendreply(transp, (xdrproc_t)xdr_void, NULL); 146 return; 147 148 case YPPASSWDPROC_UPDATE: 149 /* 150 * We'd like this to look like a regular RPC 151 * stub, but we have to send the reply in the 152 * handler in order to avoid both signal race 153 * conditions locally and timeouts on the 154 * client. 155 */ 156 (void)memset(&argument, 0, sizeof(argument)); 157 if (!svc_getargs(transp, xdr_yppasswd, (caddr_t) & argument)) { 158 svcerr_decode(transp); 159 return; 160 } 161 make_passwd((yppasswd *)&argument, rqstp, transp); 162 if (!svc_freeargs(transp, xdr_yppasswd, (caddr_t) &argument)) 163 errx(EXIT_FAILURE, "unable to free arguments"); 164 return; 165 } 166 167 svcerr_noproc(transp); 168 } 169 170 void 171 usage(void) 172 { 173 174 fprintf(stderr, "usage: %s [-d directory] [-noshell] [-nogecos] " 175 "[-nopw] [-m arg1 [arg2 ...]]\n", getprogname()); 176 exit(EXIT_FAILURE); 177 } 178