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