Commit | Line | Data |
---|---|---|
99ee2953 | 1 | /*- |
8964deec KB |
2 | * Copyright (c) 1990, 1993 |
3 | * The Regents of the University of California. All rights reserved. | |
99ee2953 KB |
4 | * |
5 | * %sccs.include.redist.c% | |
6 | */ | |
7 | ||
8 | #ifndef lint | |
5e42a97c | 9 | static char sccsid[] = "@(#)pw_copy.c 8.3 (Berkeley) %G%"; |
99ee2953 KB |
10 | #endif /* not lint */ |
11 | ||
12 | /* | |
13 | * This module is used to copy the master password file, replacing a single | |
14 | * record, by chpass(1) and passwd(1). | |
15 | */ | |
16 | ||
cc952d1c | 17 | #include <err.h> |
99ee2953 KB |
18 | #include <pwd.h> |
19 | #include <stdio.h> | |
20 | #include <string.h> | |
21 | ||
cc952d1c | 22 | #include <pw_util.h> |
5e42a97c | 23 | #include "pw_copy.h" |
99ee2953 | 24 | |
cc952d1c JSP |
25 | extern char *tempname; |
26 | ||
27 | void | |
456319a0 KB |
28 | pw_copy(ffd, tfd, pw) |
29 | int ffd, tfd; | |
99ee2953 KB |
30 | struct passwd *pw; |
31 | { | |
cc952d1c JSP |
32 | FILE *from, *to; |
33 | int done; | |
34 | char *p, buf[8192]; | |
99ee2953 KB |
35 | |
36 | if (!(from = fdopen(ffd, "r"))) | |
37 | pw_error(_PATH_MASTERPASSWD, 1, 1); | |
456319a0 | 38 | if (!(to = fdopen(tfd, "w"))) |
99ee2953 KB |
39 | pw_error(tempname, 1, 1); |
40 | ||
41 | for (done = 0; fgets(buf, sizeof(buf), from);) { | |
cc952d1c JSP |
42 | if (!strchr(buf, '\n')) { |
43 | warnx("%s: line too long", _PATH_MASTERPASSWD); | |
0d67ba24 | 44 | pw_error(NULL, 0, 1); |
99ee2953 KB |
45 | } |
46 | if (done) { | |
47 | (void)fprintf(to, "%s", buf); | |
0d67ba24 KB |
48 | if (ferror(to)) |
49 | goto err; | |
99ee2953 KB |
50 | continue; |
51 | } | |
cc952d1c JSP |
52 | if (!(p = strchr(buf, ':'))) { |
53 | warnx("%s: corrupted entry", _PATH_MASTERPASSWD); | |
0d67ba24 | 54 | pw_error(NULL, 0, 1); |
99ee2953 KB |
55 | } |
56 | *p = '\0'; | |
57 | if (strcmp(buf, pw->pw_name)) { | |
58 | *p = ':'; | |
59 | (void)fprintf(to, "%s", buf); | |
0d67ba24 KB |
60 | if (ferror(to)) |
61 | goto err; | |
99ee2953 KB |
62 | continue; |
63 | } | |
64 | (void)fprintf(to, "%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s\n", | |
65 | pw->pw_name, pw->pw_passwd, pw->pw_uid, pw->pw_gid, | |
66 | pw->pw_class, pw->pw_change, pw->pw_expire, pw->pw_gecos, | |
67 | pw->pw_dir, pw->pw_shell); | |
68 | done = 1; | |
0d67ba24 KB |
69 | if (ferror(to)) |
70 | goto err; | |
99ee2953 KB |
71 | } |
72 | if (!done) | |
73 | (void)fprintf(to, "%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s\n", | |
74 | pw->pw_name, pw->pw_passwd, pw->pw_uid, pw->pw_gid, | |
75 | pw->pw_class, pw->pw_change, pw->pw_expire, pw->pw_gecos, | |
76 | pw->pw_dir, pw->pw_shell); | |
0d67ba24 KB |
77 | |
78 | if (ferror(to)) | |
79 | err: pw_error(NULL, 1, 1); | |
99ee2953 KB |
80 | (void)fclose(to); |
81 | } |