yppasswdd_mkpw.c revision 1.16 1 /* $NetBSD: yppasswdd_mkpw.c,v 1.16 2008/02/24 21:10:02 dholland 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.16 2008/02/24 21:10:02 dholland 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 <errno.h>
51 #include <fcntl.h>
52 #include <stdio.h>
53 #include <pwd.h>
54 #include <signal.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <unistd.h>
58 #include <util.h>
59 #include <limits.h>
60
61 #include <rpc/rpc.h>
62 #include <rpc/xdr.h>
63 #include <rpcsvc/yppasswd.h>
64
65 #include "extern.h"
66
67 int handling_request; /* simple mutex */
68
69 void
70 make_passwd(yppasswd *argp, struct svc_req *rqstp, SVCXPRT *transp)
71 {
72 struct passwd pw;
73 int pfd, tfd;
74 char mpwd[MAXPATHLEN];
75 char buf[8192]; /* from libutil */
76 char *p;
77 int lineno;
78 FILE *fpw;
79
80 #define REPLY(val) do { \
81 int res = (val); \
82 if (!svc_sendreply(transp, xdr_int, (caddr_t)&res)) \
83 svcerr_systemerr(transp); \
84 } while (0)
85
86 #define RETURN(val) do { \
87 REPLY((val)); \
88 handling_request = 0; \
89 return; \
90 } while (0)
91
92 if (handling_request) {
93 warnx("already handling request; try again later");
94 REPLY(1);
95 return;
96 }
97 handling_request = 1;
98
99 (void)strlcpy(mpwd, pw_getprefix(), sizeof(mpwd));
100 (void)strlcat(mpwd, _PATH_MASTERPASSWD, sizeof(mpwd));
101 fpw = fopen(mpwd, "r");
102 if (fpw == NULL) {
103 warnx("%s", mpwd);
104 RETURN(1);
105 }
106 for(lineno = 1; ; lineno++) {
107 if (fgets(buf, sizeof(buf), fpw) == NULL) {
108 if (feof(fpw))
109 warnx("%s: %s not found", mpwd,
110 argp->newpw.pw_name);
111 else
112 warnx("%s: %s", mpwd, strerror(errno));
113 (void)fclose(fpw);
114 RETURN(1);
115 }
116 if ((p = strchr(buf, '\n')) == NULL) {
117 warnx("line %d too long", lineno);
118 (void)fclose(fpw);
119 RETURN(1);
120 }
121 /* get rid of trailing \n */
122 *p = '\0';
123 if (pw_scan(buf, &pw, NULL) == 0)
124 continue;
125 if (strncmp(argp->newpw.pw_name, pw.pw_name, MAXLOGNAME) == 0)
126 break;
127 }
128 fclose(fpw);
129 if (*pw.pw_passwd &&
130 strcmp(crypt(argp->oldpass, pw.pw_passwd), pw.pw_passwd) != 0)
131 RETURN(1);
132
133 pw_init();
134 tfd = pw_lock(0);
135 if (tfd < 0) {
136 warnx("the passwd file is busy.");
137 RETURN(1);
138 }
139 pfd = open(mpwd, O_RDONLY, 0);
140 if (pfd < 0) {
141 pw_abort();
142 warnx("%s", mpwd);
143 RETURN(1);
144 }
145
146 /*
147 * Get the new password. Reset passwd change time to zero; when
148 * classes are implemented, go and get the "offset" value for this
149 * class and reset the timer.
150 */
151 if (!nopw) {
152 pw.pw_passwd = argp->newpw.pw_passwd;
153 pw.pw_change = 0;
154 }
155 if (!nogecos)
156 pw.pw_gecos = argp->newpw.pw_gecos;
157 if (!noshell)
158 pw.pw_shell = argp->newpw.pw_shell;
159
160 pw_copy(pfd, tfd, &pw, NULL);
161
162 if (pw_mkdb(pw.pw_name, 0) < 0) {
163 warnx("pw_mkdb failed");
164 pw_abort();
165 RETURN(1);
166 }
167
168 /* XXX RESTORE SIGNAL STATE? XXX */
169
170 /* Notify caller we succeeded. */
171 REPLY(0);
172
173 /* Update the YP maps. */
174 if (chdir("/var/yp"))
175 err(EXIT_FAILURE, "/var/yp");
176 (void) umask(022);
177 (void) system(make_arg);
178
179 handling_request = 0;
180 }
181