1 /*	$NetBSD: rpc.yppasswdd.c,v 1.18 2017/05/04 16:26:09 sevan 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.18 2017/05/04 16:26:09 sevan 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 void	yppasswddprog_1(struct svc_req *, SVCXPRT *);
57 __dead static void	usage(void);
58 
59 int
main(int argc,char * argv[])60 main(int argc, char *argv[])
61 {
62 	SVCXPRT *transp;
63 	int i;
64 	char *arg;
65 	int maxrec = RPC_MAXDATASIZE;
66 
67 	for (i = 1; i < argc; i++) {
68 		arg = argv[i];
69 		if (*arg++ != '-')
70 			usage();
71 		if (strcmp("d", arg) == 0)
72 			if (++i == argc)
73 				usage();
74 			else {
75 				if (pw_setprefix(argv[i]) < 0)
76 					err(EXIT_FAILURE,NULL);
77 			}
78 		else if (strcmp("noshell", arg) == 0)
79 			noshell = 1;
80 		else if (strcmp("nogecos", arg) == 0)
81 			nogecos = 1;
82 		else if (strcmp("nopw", arg) == 0)
83 			nopw = 1;
84 		else if (strcmp("m", arg) == 0) {
85 			int len;
86 
87 			len = strlen(make_arg);
88 			if (++i == argc)
89 				usage();
90 			for (; i < argc; i++) {
91 				int arglen;
92 
93 				arglen = strlen(argv[i]);
94 				if ((len + arglen) > (int)(sizeof(make_arg) - 2))
95 					errx(EXIT_FAILURE, "%s", strerror(E2BIG));
96 				make_arg[len++] = ' ';
97 				(void)strcpy(&make_arg[len], argv[i]);
98 				len += arglen;
99 			}
100 		} else
101 			usage();
102 	}
103 
104 	if (daemon(0, 0))
105 		err(EXIT_FAILURE, "can't detach");
106 	pidfile(NULL);
107 
108 	rpc_control(RPC_SVC_CONNMAXREC_SET, &maxrec);
109 
110 	(void)pmap_unset(YPPASSWDPROG, YPPASSWDVERS);
111 
112 	transp = svcudp_create(RPC_ANYSOCK);
113 	if (transp == NULL)
114 		errx(EXIT_FAILURE, "cannot create UDP service");
115 
116 	if (!svc_register(transp, YPPASSWDPROG, YPPASSWDVERS, yppasswddprog_1,
117 	    IPPROTO_UDP))
118 		errx(EXIT_FAILURE,
119 		    "unable to register YPPASSWDPROG/YPPASSWDVERS/UDP");
120 
121 	transp = svctcp_create(RPC_ANYSOCK, RPC_MAXDATASIZE, RPC_MAXDATASIZE);
122 	if (transp == NULL)
123 		errx(EXIT_FAILURE, "cannot create TCP service");
124 
125 	if (!svc_register(transp, YPPASSWDPROG, YPPASSWDVERS, yppasswddprog_1,
126 	    IPPROTO_TCP))
127 		errx(EXIT_FAILURE,
128 		    "unable to register YPPASSWDPROG/YPPASSWDVERS/TCP");
129 
130 	svc_run();
131 	errx(EXIT_FAILURE, "svc_run returned");
132 	/* NOTREACHED */
133 }
134 
135 void
yppasswddprog_1(struct svc_req * rqstp,SVCXPRT * transp)136 yppasswddprog_1(struct svc_req *rqstp, SVCXPRT *transp)
137 {
138 	union {
139 		yppasswd yppasswdproc_update_1_arg;
140 	} argument;
141 
142 	switch (rqstp->rq_proc) {
143 	case NULLPROC:
144 		(void)svc_sendreply(transp, (xdrproc_t)xdr_void, NULL);
145 		return;
146 
147 	case YPPASSWDPROC_UPDATE:
148 		/*
149 		 * We'd like this to look like a regular RPC
150 		 * stub, but we have to send the reply in the
151 		 * handler in order to avoid both signal race
152 		 * conditions locally and timeouts on the
153 		 * client.
154 		 */
155 		(void)memset(&argument, 0, sizeof(argument));
156 		if (!svc_getargs(transp, xdr_yppasswd, (caddr_t) & argument)) {
157 			svcerr_decode(transp);
158 			return;
159 		}
160 		make_passwd((yppasswd *)&argument, rqstp, transp);
161 		if (!svc_freeargs(transp, xdr_yppasswd, (caddr_t) &argument))
162 			errx(EXIT_FAILURE, "unable to free arguments");
163 		return;
164 	}
165 
166 	svcerr_noproc(transp);
167 }
168 
169 void
usage(void)170 usage(void)
171 {
172 
173 	fprintf(stderr, "usage: %s [-d directory] [-noshell] [-nogecos] "
174 	    "[-nopw] [-m arg1 [arg2 ...]]\n", getprogname());
175 	exit(EXIT_FAILURE);
176 }
177