]> git.saurik.com Git - apple/system_cmds.git/blob - chkpasswd.tproj/file_passwd.c
system_cmds-880.100.5.tar.gz
[apple/system_cmds.git] / chkpasswd.tproj / file_passwd.c
1 /*
2 * Copyright (c) 1999-2016 Apple 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 #include <stdio.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <pwd.h>
28 #include <errno.h>
29 #include <string.h>
30 #include "stringops.h"
31
32 #define TEMP_FILE "/tmp/.pwtmp"
33
34 #define _PASSWD_FILE "/etc/master.passwd"
35 #define _COMPAT_FILE "/etc/passwd"
36 #define _PASSWD_FIELDS 10
37 #define BUFSIZE 8192
38
39 #include "passwd.h"
40
41 static char *
42 _getline(FILE *fp)
43 {
44 static char s[BUFSIZE];
45 size_t len;
46
47 s[0] = '\0';
48
49 fgets(s, BUFSIZE, fp);
50 if (s[0] == '\0') return NULL;
51
52 if (s[0] == '#') return s;
53
54 len = strlen(s) - 1;
55 s[len] = '\0';
56
57 return s;
58 }
59
60 static struct passwd *
61 parse_user(char *line)
62 {
63 static struct passwd pw = {0};
64 char **tokens;
65 int i, len;
66
67 if (pw.pw_name != NULL) free(pw.pw_name);
68 pw.pw_name = NULL;
69 if (pw.pw_passwd != NULL) free(pw.pw_passwd);
70 pw.pw_passwd = NULL;
71 if (pw.pw_gecos != NULL) free(pw.pw_gecos);
72 pw.pw_gecos = NULL;
73 if (pw.pw_dir != NULL) free(pw.pw_dir);
74 pw.pw_dir = NULL;
75 if (pw.pw_shell != NULL) free(pw.pw_shell);
76 pw.pw_shell = NULL;
77
78 if (pw.pw_class != NULL) free(pw.pw_class);
79 pw.pw_class = NULL;
80
81 if (line == NULL) return (struct passwd *)NULL;
82 tokens = explode(line, ':');
83 len = listLength(tokens);
84
85 if (len != _PASSWD_FIELDS)
86 {
87 freeList(tokens);
88 return (struct passwd *)NULL;
89 }
90
91 i = 0;
92 pw.pw_name = tokens[i++];
93 pw.pw_passwd = tokens[i++];
94 pw.pw_uid = atoi(tokens[i]);
95 free(tokens[i++]);
96 pw.pw_gid = atoi(tokens[i]);
97 free(tokens[i++]);
98 pw.pw_class = tokens[i++];
99 pw.pw_change = atoi(tokens[i]);
100 free(tokens[i++]);
101 pw.pw_expire = atoi(tokens[i]);
102 free(tokens[i++]);
103 pw.pw_gecos = tokens[i++];
104 pw.pw_dir = tokens[i++];
105 pw.pw_shell = tokens[i++];
106
107 return &pw;
108 }
109
110 static struct passwd *
111 find_user(char *uname, FILE *fp)
112 {
113 char *line;
114 struct passwd *pw;
115
116 rewind(fp);
117
118 while (NULL != (line = _getline(fp)))
119 {
120 if (line[0] == '#') continue;
121 pw = parse_user(line);
122 if (pw == (struct passwd *)NULL) continue;
123 if (!strcmp(uname, pw->pw_name)) return pw;
124 }
125
126 pw = parse_user(NULL);
127 return (struct passwd *)NULL;
128 }
129
130 #if 0
131 static void
132 rewrite_file(char *pwname, FILE *fp, struct passwd *newpw)
133 {
134 char *line;
135 struct passwd *pw;
136 FILE *tfp, *cfp;
137 char fname[256];
138
139 sprintf(fname, "%s.%d", TEMP_FILE, getpid());
140
141 tfp = fopen(fname, "w+");
142 if (tfp == NULL)
143 {
144 fprintf(stderr, "can't write temporary file \"%s\": ", fname);
145 perror("");
146 exit(1);
147 }
148
149 cfp = NULL;
150 if (!strcmp(pwname, _PASSWD_FILE))
151 {
152 cfp = fopen(_COMPAT_FILE, "w");
153 if (cfp == NULL)
154 {
155 fprintf(stderr, "warning: can't write compatability file \"%s\": ",
156 _COMPAT_FILE);
157 perror("");
158 }
159 }
160
161 if (cfp != NULL)
162 {
163 fprintf(cfp, "#\n");
164 fprintf(cfp, "# 4.3BSD-compatable User Database\n");
165 fprintf(cfp, "#\n");
166 fprintf(cfp, "# Note that this file is not consulted for login.\n");
167 fprintf(cfp, "# It only exisits for compatability with 4.3BSD utilities.\n");
168 fprintf(cfp, "#\n");
169 fprintf(cfp, "# This file is automatically re-written by various system utilities.\n");
170 fprintf(cfp, "# Do not edit this file. Changes will be lost.\n");
171 fprintf(cfp, "#\n");
172 }
173
174 rewind(fp);
175
176 while (NULL != (line = _getline(fp)))
177 {
178 if (line[0] == '#')
179 {
180 fprintf(tfp, "%s", line);
181 continue;
182 }
183
184 pw = parse_user(line);
185 if (pw == (struct passwd *)NULL)
186 {
187 fprintf(stderr, "warning: bad format for entry: \"%s\"\n", line);
188 fprintf(tfp, "%s\n", line);
189 if (cfp != NULL) fprintf(cfp, "%s\n", line);
190 continue;
191 }
192
193 if (strcmp(newpw->pw_name, pw->pw_name))
194 {
195 fprintf(tfp, "%s\n", line);
196 if (cfp != NULL) fprintf(cfp, "%s\n", line);
197 continue;
198 }
199
200 fprintf(tfp, "%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s\n",
201 newpw->pw_name, newpw->pw_passwd, newpw->pw_uid, newpw->pw_gid,
202 newpw->pw_class, newpw->pw_change, newpw->pw_expire,
203 newpw->pw_gecos, newpw->pw_dir, newpw->pw_shell);
204 if (cfp != NULL)
205 {
206 fprintf(cfp, "%s:",newpw->pw_name);
207 if ((newpw->pw_passwd == NULL) || (newpw->pw_passwd[0] == '\0'))
208 fprintf(cfp, ":");
209 else
210 fprintf(cfp, "*:");
211 fprintf(cfp, "%d:%d:%s:%s:%s\n",
212 newpw->pw_uid, newpw->pw_gid, newpw->pw_gecos,
213 newpw->pw_dir, newpw->pw_shell);
214 }
215 }
216
217 if (cfp != NULL) fclose(cfp);
218 fclose(fp);
219 if (unlink(pwname) < 0)
220 {
221 fprintf(stderr, "can't update \"%s\": ", pwname);
222 perror("");
223 }
224
225 rewind(tfp);
226
227 fp = fopen(pwname, "w");
228 if (fp == NULL)
229 {
230 fprintf(stderr, "ERROR: lost file \"%s\"\n", pwname);
231 fprintf(stderr, "new passwd file is \"%s\"\n", fname);
232 perror("open");
233 exit(1);
234 }
235
236 while (NULL != (line = _getline(tfp)))
237 {
238 fprintf(fp, "%s", line);
239 if (line[0] != '#') fprintf(fp, "\n");
240 }
241 fclose(fp);
242 fclose(tfp);
243 unlink(fname);
244 }
245 #endif /* 0 */
246
247 int
248 file_check_passwd(char *uname, char *locn)
249 {
250 FILE *fp;
251 char *fname;
252 struct passwd *pw;
253
254 fname = _PASSWD_FILE;
255 if (locn != NULL) fname = locn;
256
257 if (access(fname,R_OK) || (fp = fopen(fname, "r")) == NULL)
258 {
259 fprintf(stderr, "can't read file \"%s\": ", fname);
260 perror("");
261 exit(1);
262 }
263
264
265 pw = find_user(uname, fp);
266 if (pw == (struct passwd *)NULL)
267 {
268 fprintf(stderr, "user %s not found in file %s\n", uname, fname);
269 exit(1);
270 }
271
272 checkpasswd(uname, pw->pw_passwd);
273 fclose(fp);
274
275 return 0;
276 }