xref: /netbsd-src/usr.sbin/rpc.yppasswdd/rpc.yppasswdd.c (revision d0fed6c87ddc40a8bffa6f99e7433ddfc864dd83)
1 /*	$NetBSD: rpc.yppasswdd.c,v 1.1.1.1 1996/08/09 10:19:49 thorpej 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  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by Mats O Jansson
18  * 4. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
22  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
25  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #include <sys/types.h>
35 #include <sys/wait.h>
36 #include <err.h>
37 #include <limits.h>
38 #include <stdio.h>
39 #include <signal.h>
40 #include <unistd.h>
41 #include <string.h>
42 
43 #include <rpc/rpc.h>
44 #include <rpc/pmap_clnt.h>
45 #include <rpcsvc/yppasswd.h>
46 
47 extern	char *__progname;		/* from crt0.s */
48 
49 int	noshell, nogecos, nopw, domake;
50 char	make_arg[_POSIX2_LINE_MAX] = "make";
51 
52 extern	void make_passwd __P((yppasswd *, struct svc_req *, SVCXPRT *));
53 
54 void	yppasswddprog_1 __P((struct svc_req *, SVCXPRT *));
55 void	usage __P((void));
56 
57 int
58 main(argc, argv)
59 	int     argc;
60 	char   *argv[];
61 {
62 	SVCXPRT *transp;
63 	int i;
64 
65 	for (i = 1; i < argc; i++) {
66 		if (argv[i][0] == '-') {
67 			if (strcmp("-noshell", argv[i]) == 0)
68 				noshell = 1;
69 			else if (strcmp("-nogecos", argv[i]) == 0)
70 				nogecos = 1;
71 			else if (strcmp("-nopw", argv[i]) == 0)
72 				nopw = 1;
73 			else if (strcmp("-m", argv[i]) == 0) {
74 				domake = 1;
75 				for (; i < argc; i++) {
76 					strcat(make_arg, " ");
77 					strcat(make_arg, argv[i]);
78 				}
79 			} else
80 				usage();
81 		} else
82 			usage();
83 	}
84 
85 	if (daemon(0, 0))
86 		err(1, "can't detach");
87 
88 	(void) pmap_unset(YPPASSWDPROG, YPPASSWDVERS);
89 
90 	transp = svcudp_create(RPC_ANYSOCK);
91 	if (transp == NULL)
92 		errx(1, "cannot create UDP service");
93 
94 	if (!svc_register(transp, YPPASSWDPROG, YPPASSWDVERS, yppasswddprog_1,
95 	    IPPROTO_UDP))
96 		errx(1, "unable to register YPPASSWDPROG/YPPASSWDVERS/UDP");
97 
98 	transp = svctcp_create(RPC_ANYSOCK, 0, 0);
99 	if (transp == NULL)
100 		errx(1, "cannot create TCP service");
101 
102 	if (!svc_register(transp, YPPASSWDPROG, YPPASSWDVERS, yppasswddprog_1,
103 	    IPPROTO_TCP))
104 		errx(1, "unable to register YPPASSWDPROG/YPPASSWDVERS/TCP");
105 
106 	svc_run();
107 	errx(1, "svc_run returned");
108 }
109 
110 void
111 yppasswddprog_1(rqstp, transp)
112 	struct svc_req *rqstp;
113 	SVCXPRT *transp;
114 {
115 	union {
116 		yppasswd yppasswdproc_update_1_arg;
117 	} argument;
118 
119 	switch (rqstp->rq_proc) {
120 	case NULLPROC:
121 		(void) svc_sendreply(transp, xdr_void, (char *) NULL);
122 		return;
123 
124 	case YPPASSWDPROC_UPDATE:
125 		/*
126 		 * We'd like this to look like a regular RPC
127 		 * stub, but we have to send the reply in the
128 		 * handler in order to avoid both signal race
129 		 * conditions locally and timeouts on the
130 		 * client.
131 		 */
132 		(void) memset(&argument, 0, sizeof(argument));
133 		if (!svc_getargs(transp, xdr_yppasswd, (caddr_t) & argument)) {
134 			svcerr_decode(transp);
135 			return;
136 		}
137 		make_passwd((yppasswd *)&argument, rqstp, transp);
138 		if (!svc_freeargs(transp, xdr_yppasswd, (caddr_t) &argument))
139 			errx(1, "unable to free arguments");
140 		return;
141 	}
142 
143 	svcerr_noproc(transp);
144 }
145 
146 void
147 usage()
148 {
149 	fprintf(stderr, "%s%s",
150 	    "usage: rpc.yppasswdd ",
151 	    "[-noshell] [-nogecos] [-nopw] [-m arg1 arg2 ... ]\n");
152 	exit(1);
153 }
154