]> git.saurik.com Git - apple/network_cmds.git/blob - rpc_yppasswdd.tproj/yppasswdd_mkpw.c
network_cmds-245.19.tar.gz
[apple/network_cmds.git] / rpc_yppasswdd.tproj / yppasswdd_mkpw.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.0 (the 'License'). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
12 * this file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
20 * under the License."
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24 /* $OpenBSD: yppasswdd_mkpw.c,v 1.16 1997/11/17 23:56:20 gene Exp $ */
25
26 /*
27 * Copyright (c) 1994 Mats O Jansson <moj@stacken.kth.se>
28 * All rights reserved.
29 *
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
32 * are met:
33 * 1. Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * 2. Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in the
37 * documentation and/or other materials provided with the distribution.
38 * 3. All advertising materials mentioning features or use of this software
39 * must display the following acknowledgement:
40 * This product includes software developed by Mats O Jansson
41 * 4. The name of the author may not be used to endorse or promote products
42 * derived from this software without specific prior written permission.
43 *
44 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
45 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
46 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
48 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 * SUCH DAMAGE.
55 */
56
57 #ifndef LINT
58 static char rcsid[] = "$OpenBSD: yppasswdd_mkpw.c,v 1.16 1997/11/17 23:56:20 gene Exp $";
59 #endif
60
61 #include <sys/param.h>
62 #include <sys/types.h>
63 #include <sys/stat.h>
64 #include <stdio.h>
65 #include <fcntl.h>
66 #include <rpc/rpc.h>
67 #include <rpcsvc/yppasswd.h>
68 #include <db.h>
69 #include <pwd.h>
70 #include <stdlib.h>
71 #include <unistd.h>
72 #include <util.h>
73 #include <ctype.h>
74 #include <string.h>
75 #include <syslog.h>
76 #include <err.h>
77
78 extern int noshell;
79 extern int nogecos;
80 extern int nopw;
81 extern int make;
82 extern char make_arg[];
83
84 static void _pw_copy(int, int, struct passwd *);
85
86 /* This is imported from OpenBSD's libutil because it's argument
87 * incompatible with NetBSD's. However, the NetBSD libutil is
88 * at least what the prototypes suggest is in System.framework,
89 * even though I can't find the code. I assume it will be there
90 * eventually. We need to use NetBSD's because it works with the
91 * pwd_mkdb binary that's shipped with Rhapsody. This is an area
92 * where OpenBSD diverges; however, we wanted to keep the OpenBSD
93 * rpc.yppasswdd because the rest of our YP code is from OpenBSD.
94 * What a mess.
95 */
96 static void
97 _pw_copy(ffd, tfd, pw)
98 int ffd, tfd;
99 struct passwd *pw;
100 {
101 FILE *from, *to;
102 int done;
103 char *p, buf[8192];
104
105 if (!(from = fdopen(ffd, "r")))
106 pw_error(_PATH_MASTERPASSWD, 1, 1);
107 if (!(to = fdopen(tfd, "w")))
108 pw_error(_PATH_MASTERPASSWD_LOCK, 1, 1);
109
110 for (done = 0; fgets(buf, sizeof(buf), from);) {
111 if (!strchr(buf, '\n')) {
112 warnx("%s: line too long", _PATH_MASTERPASSWD);
113 pw_error(NULL, 0, 1);
114 }
115 if (done) {
116 (void)fprintf(to, "%s", buf);
117 if (ferror(to))
118 goto err;
119 continue;
120 }
121 if (!(p = strchr(buf, ':'))) {
122 warnx("%s: corrupted entry", _PATH_MASTERPASSWD);
123 pw_error(NULL, 0, 1);
124 }
125 *p = '\0';
126 if (strcmp(buf, pw->pw_name)) {
127 *p = ':';
128 (void)fprintf(to, "%s", buf);
129 if (ferror(to))
130 goto err;
131 continue;
132 }
133 (void)fprintf(to, "%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s\n",
134 pw->pw_name, pw->pw_passwd, pw->pw_uid, pw->pw_gid,
135 pw->pw_class, pw->pw_change, pw->pw_expire, pw->pw_gecos,
136 pw->pw_dir, pw->pw_shell);
137 done = 1;
138 if (ferror(to))
139 goto err;
140 }
141 if (!done)
142 (void)fprintf(to, "%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s\n",
143 pw->pw_name, pw->pw_passwd, pw->pw_uid, pw->pw_gid,
144 pw->pw_class, pw->pw_change, pw->pw_expire, pw->pw_gecos,
145 pw->pw_dir, pw->pw_shell);
146
147 if (ferror(to))
148 err:
149 pw_error(NULL, 0, 1);
150 (void)fclose(to);
151 }
152
153
154 int
155 badchars(base)
156 char *base;
157 {
158 int ampr = 0;
159 char *s;
160
161 for (s = base; *s; s++) {
162 if (*s == '&')
163 ampr++;
164 if (!isprint(*s))
165 return 1;
166 if (strchr(":\n\t\r", *s))
167 return 1;
168 }
169 if (ampr > 10)
170 return 1;
171 return 0;
172 }
173
174 int
175 subst(s, from, to)
176 char *s;
177 char from, to;
178 {
179 int n = 0;
180
181 while (*s) {
182 if (*s == from) {
183 *s = to;
184 n++;
185 }
186 s++;
187 }
188 return (n);
189 }
190
191 int
192 make_passwd(argp)
193 yppasswd *argp;
194 {
195 struct passwd pw;
196 int pfd, tfd;
197 char buf[10], *bp = NULL, *p, *t;
198 int n;
199 ssize_t cnt;
200 size_t resid;
201 struct stat st;
202
203 pw_init();
204 pfd = open(_PATH_MASTERPASSWD, O_RDONLY);
205 if (pfd < 0)
206 goto fail;
207 if (fstat(pfd, &st))
208 goto fail;
209 p = bp = malloc((resid = st.st_size) + 1);
210 do {
211 cnt = read(pfd, p, resid);
212 if (cnt < 0)
213 goto fail;
214 p += cnt;
215 resid -= cnt;
216 } while (resid > 0);
217 close(pfd);
218 pfd = -1;
219 *p = '\0'; /* Buf oflow prevention */
220
221 p = bp;
222 subst(p, '\n', '\0');
223 for (n = 1; p < bp + st.st_size; n++, p = t) {
224 t = strchr(p, '\0') + 1;
225 /* Rhapsody allows the passwd file to have comments in it. */
226 if (p[0] == '#') {
227 continue;
228 }
229 cnt = subst(p, ':', '\0');
230 if (cnt != 9) {
231 syslog(LOG_WARNING, "bad entry at line %d of %s", n,
232 _PATH_MASTERPASSWD);
233 continue;
234 }
235
236 if (strcmp(p, argp->newpw.pw_name) == 0)
237 break;
238 }
239 if (p >= bp + st.st_size)
240 goto fail;
241
242 #define EXPAND(e) e = p; while (*p++);
243 EXPAND(pw.pw_name);
244 EXPAND(pw.pw_passwd);
245 pw.pw_uid = atoi(p); EXPAND(t);
246 pw.pw_gid = atoi(p); EXPAND(t);
247 EXPAND(pw.pw_class);
248 pw.pw_change = (time_t)atol(p); EXPAND(t);
249 pw.pw_expire = (time_t)atol(p); EXPAND(t);
250 EXPAND(pw.pw_gecos);
251 EXPAND(pw.pw_dir);
252 EXPAND(pw.pw_shell);
253
254 /* crypt() is broken under Rhapsody. It doesn't deal with
255 * empty keys or salts like other Unices.
256 */
257 if (pw.pw_passwd[0] != '\0' && argp->oldpass != NULL && argp->oldpass[0] != '\0') {
258 if (strcmp(crypt(argp->oldpass, pw.pw_passwd), pw.pw_passwd) != 0)
259 goto fail;
260 }
261
262 if (!nopw && badchars(argp->newpw.pw_passwd))
263 goto fail;
264 if (!nogecos && badchars(argp->newpw.pw_gecos))
265 goto fail;
266 if (!nogecos && badchars(argp->newpw.pw_shell))
267 goto fail;
268
269 /*
270 * Get the new password. Reset passwd change time to zero; when
271 * classes are implemented, go and get the "offset" value for this
272 * class and reset the timer.
273 */
274 if (!nopw) {
275 pw.pw_passwd = argp->newpw.pw_passwd;
276 pw.pw_change = 0;
277 }
278 if (!nogecos)
279 pw.pw_gecos = argp->newpw.pw_gecos;
280 if (!noshell)
281 pw.pw_shell = argp->newpw.pw_shell;
282
283 for (n = 0, p = pw.pw_gecos; *p; p++)
284 if (*p == '&')
285 n = n + strlen(pw.pw_name) - 1;
286 if (strlen(pw.pw_name) + 1 + strlen(pw.pw_passwd) + 1 +
287 strlen((sprintf(buf, "%d", pw.pw_uid), buf)) + 1 +
288 strlen((sprintf(buf, "%d", pw.pw_gid), buf)) + 1 +
289 strlen(pw.pw_gecos) + n + 1 + strlen(pw.pw_dir) + 1 +
290 strlen(pw.pw_shell) >= 1023)
291 goto fail;
292
293 pfd = open(_PATH_MASTERPASSWD, O_RDONLY, 0);
294 if (pfd < 0) {
295 syslog(LOG_ERR, "cannot open %s", _PATH_MASTERPASSWD);
296 goto fail;
297 }
298
299 tfd = pw_lock(0);
300 if (tfd < 0)
301 goto fail;
302
303 _pw_copy(pfd, tfd, &pw);
304 pw_mkdb();
305 free(bp);
306
307 if (fork() == 0) {
308 chdir("/var/yp");
309 (void)umask(022);
310 system(make_arg);
311 exit(0);
312 }
313 return (0);
314
315 fail:
316 if (bp)
317 free(bp);
318 if (pfd >= 0)
319 close(pfd);
320 return (1);
321 }