rpc.yppasswdd.c revision 1.10.2.1 1 /* $NetBSD: rpc.yppasswdd.c,v 1.10.2.1 2003/01/06 04:53:19 jmc Exp $ */
2
3 /*
4 * Copyright (c) 1994 Mats O Jansson <moj (at) 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/cdefs.h>
35 #ifndef lint
36 __RCSID("$NetBSD: rpc.yppasswdd.c,v 1.10.2.1 2003/01/06 04:53:19 jmc Exp $");
37 #endif /* not lint */
38
39 #include <sys/types.h>
40 #include <sys/wait.h>
41
42 #include <err.h>
43 #include <errno.h>
44 #include <limits.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <signal.h>
49 #include <unistd.h>
50 #include <util.h>
51
52 #include <rpc/rpc.h>
53 #include <rpc/pmap_clnt.h>
54 #include <rpcsvc/yppasswd.h>
55
56 #include "extern.h"
57
58 int noshell, nogecos, nopw;
59 char make_arg[_POSIX2_LINE_MAX] = "make";
60
61 int main(int, char *[]);
62 void yppasswddprog_1(struct svc_req *, SVCXPRT *);
63 void usage(void);
64
65 int
66 main(int argc, char *argv[])
67 {
68 SVCXPRT *transp;
69 int i;
70 char *arg;
71 int maxrec = RPC_MAXDATASIZE;
72
73 for (i = 1; i < argc; i++) {
74 arg = argv[i];
75 if (*arg++ != '-')
76 usage();
77 if (strcmp("d", arg) == 0)
78 if (++i == argc)
79 usage();
80 else {
81 if (pw_setprefix(argv[i]) < 0)
82 err(EXIT_FAILURE,NULL);
83 }
84 else if (strcmp("noshell", arg) == 0)
85 noshell = 1;
86 else if (strcmp("nogecos", arg) == 0)
87 nogecos = 1;
88 else if (strcmp("nopw", arg) == 0)
89 nopw = 1;
90 else if (strcmp("m", arg) == 0) {
91 int len;
92
93 len = strlen(make_arg);
94 if (++i == argc)
95 usage();
96 for (; i < argc; i++) {
97 int arglen;
98
99 arglen = strlen(argv[i]);
100 if ((len + arglen) > (sizeof(make_arg) - 2))
101 errx(EXIT_FAILURE, strerror(E2BIG));
102 make_arg[len++] = ' ';
103 (void)strcpy(&make_arg[len], argv[i]);
104 len += arglen;
105 }
106 } else
107 usage();
108 }
109
110 if (daemon(0, 0))
111 err(EXIT_FAILURE, "can't detach");
112 pidfile(NULL);
113
114 rpc_control(RPC_SVC_CONNMAXREC_SET, &maxrec);
115
116 (void)pmap_unset(YPPASSWDPROG, YPPASSWDVERS);
117
118 transp = svcudp_create(RPC_ANYSOCK);
119 if (transp == NULL)
120 errx(EXIT_FAILURE, "cannot create UDP service");
121
122 if (!svc_register(transp, YPPASSWDPROG, YPPASSWDVERS, yppasswddprog_1,
123 IPPROTO_UDP))
124 errx(EXIT_FAILURE,
125 "unable to register YPPASSWDPROG/YPPASSWDVERS/UDP");
126
127 transp = svctcp_create(RPC_ANYSOCK, RPC_MAXDATASIZE, RPC_MAXDATASIZE);
128 if (transp == NULL)
129 errx(EXIT_FAILURE, "cannot create TCP service");
130
131 if (!svc_register(transp, YPPASSWDPROG, YPPASSWDVERS, yppasswddprog_1,
132 IPPROTO_TCP))
133 errx(EXIT_FAILURE,
134 "unable to register YPPASSWDPROG/YPPASSWDVERS/TCP");
135
136 svc_run();
137 errx(EXIT_FAILURE, "svc_run returned");
138 /* NOTREACHED */
139 }
140
141 void
142 yppasswddprog_1(struct svc_req *rqstp, SVCXPRT *transp)
143 {
144 union {
145 yppasswd yppasswdproc_update_1_arg;
146 } argument;
147
148 switch (rqstp->rq_proc) {
149 case NULLPROC:
150 (void)svc_sendreply(transp, xdr_void, (char *) NULL);
151 return;
152
153 case YPPASSWDPROC_UPDATE:
154 /*
155 * We'd like this to look like a regular RPC
156 * stub, but we have to send the reply in the
157 * handler in order to avoid both signal race
158 * conditions locally and timeouts on the
159 * client.
160 */
161 (void)memset(&argument, 0, sizeof(argument));
162 if (!svc_getargs(transp, xdr_yppasswd, (caddr_t) & argument)) {
163 svcerr_decode(transp);
164 return;
165 }
166 make_passwd((yppasswd *)&argument, rqstp, transp);
167 if (!svc_freeargs(transp, xdr_yppasswd, (caddr_t) &argument))
168 errx(EXIT_FAILURE, "unable to free arguments");
169 return;
170 }
171
172 svcerr_noproc(transp);
173 }
174
175 void
176 usage(void)
177 {
178
179 fprintf(stderr, "usage: %s [-d directory] [-noshell] [-nogecos] "
180 "[-nopw] [-m arg1 [arg2 ...]]\n", getprogname());
181 exit(EXIT_FAILURE);
182 }
183