yppasswdd_mkpw.c revision 1.13 1 /* $NetBSD: yppasswdd_mkpw.c,v 1.13 2006/05/25 01:51:21 christos Exp $ */
2
3 /*
4 * Copyright (c) 1996 Jason R. Thorpe <thorpej (at) NetBSD.org>
5 * All rights reserved.
6 *
7 * Copyright (c) 1994 Mats O Jansson <moj (at) stacken.kth.se>
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by Mats O Jansson
21 * 4. The name of the author may not be used to endorse or promote products
22 * derived from this software without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
25 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
28 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37 #include <sys/cdefs.h>
38 #ifndef lint
39 __RCSID("$NetBSD: yppasswdd_mkpw.c,v 1.13 2006/05/25 01:51:21 christos Exp $");
40 #endif /* not lint */
41
42 #include <sys/types.h>
43 #include <sys/param.h>
44 #include <sys/stat.h>
45 #include <sys/time.h>
46 #include <sys/resource.h>
47 #include <sys/wait.h>
48
49 #include <err.h>
50 #include <fcntl.h>
51 #include <stdio.h>
52 #include <pwd.h>
53 #include <signal.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <unistd.h>
57 #include <util.h>
58 #include <limits.h>
59
60 #include <rpc/rpc.h>
61 #include <rpc/xdr.h>
62 #include <rpcsvc/yppasswd.h>
63
64 #include "extern.h"
65
66 int handling_request; /* simple mutex */
67
68 void
69 make_passwd(yppasswd *argp, struct svc_req *rqstp, SVCXPRT *transp)
70 {
71 struct passwd pw;
72 int pfd, tfd;
73 char mpwd[MAXPATHLEN];
74 char buf[8192]; /* from libutil */
75 char *p;
76 int lineno;
77 FILE *fpw;
78
79 #define REPLY(val) do { \
80 int res = (val); \
81 if (!svc_sendreply(transp, xdr_int, (caddr_t)&res)) \
82 svcerr_systemerr(transp); \
83 } while (0)
84
85 #define RETURN(val) do { \
86 REPLY((val)); \
87 handling_request = 0; \
88 return; \
89 } while (0)
90
91 if (handling_request) {
92 warnx("already handling request; try again later");
93 REPLY(1);
94 return;
95 }
96 handling_request = 1;
97
98 (void)strlcpy(mpwd, pw_getprefix(), sizeof(mpwd));
99 (void)strlcat(mpwd, _PATH_MASTERPASSWD, sizeof(mpwd));
100 fpw = fopen(mpwd, "r");
101 if (fpw == NULL) {
102 warnx("%s", mpwd);
103 RETURN(1);
104 }
105 for(lineno = 1; ; lineno++) {
106 if (fgets(buf, sizeof(buf), fpw) == NULL) {
107 if (feof(fpw))
108 warnx("%s: %s not found", mpwd,
109 argp->newpw.pw_name);
110 else
111 warnx("%s: %s", mpwd, strerror(errno));
112 (void)fclose(fp);
113 RETURN(1);
114 }
115 if ((p = strchr(buf, '\n')) == NULL) {
116 warnx("line %d too long", lineno);
117 (void)fclose(fp);
118 RETURN(1);
119 }
120 /* get rid of trailing \n */
121 *p = '\0';
122 if (pw_scan(buf, &pw, NULL) == 0)
123 continue;
124 if (strncmp(argp->newpw.pw_name, pw.pw_name, MAXLOGNAME) == 0)
125 break;
126 }
127 fclose(fpw);
128 if (*pw.pw_passwd &&
129 strcmp(crypt(argp->oldpass, pw.pw_passwd), pw.pw_passwd) != 0)
130 RETURN(1);
131
132 pw_init();
133 tfd = pw_lock(0);
134 if (tfd < 0) {
135 warnx("the passwd file is busy.");
136 RETURN(1);
137 }
138 pfd = open(mpwd, O_RDONLY, 0);
139 if (pfd < 0) {
140 pw_abort();
141 warnx("%s", mpwd);
142 RETURN(1);
143 }
144
145 /*
146 * Get the new password. Reset passwd change time to zero; when
147 * classes are implemented, go and get the "offset" value for this
148 * class and reset the timer.
149 */
150 if (!nopw) {
151 pw.pw_passwd = argp->newpw.pw_passwd;
152 pw.pw_change = 0;
153 }
154 if (!nogecos)
155 pw.pw_gecos = argp->newpw.pw_gecos;
156 if (!noshell)
157 pw.pw_shell = argp->newpw.pw_shell;
158
159 pw_copy(pfd, tfd, &pw, NULL);
160
161 if (pw_mkdb(pw.pw_name, 0) < 0) {
162 warnx("pw_mkdb failed");
163 pw_abort();
164 RETURN(1);
165 }
166
167 /* XXX RESTORE SIGNAL STATE? XXX */
168
169 /* Notify caller we succeeded. */
170 REPLY(0);
171
172 /* Update the YP maps. */
173 if (chdir("/var/yp"))
174 err(EXIT_FAILURE, "/var/yp");
175 (void) umask(022);
176 (void) system(make_arg);
177
178 handling_request = 0;
179 }
180