]>
git.saurik.com Git - apple/system_cmds.git/blob - passwd.tproj/file_passwd.c
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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
22 * @APPLE_LICENSE_HEADER_END@
30 #include "stringops.h"
32 #define TEMP_FILE "/tmp/.pwtmp"
34 #define _PASSWD_FILE "/etc/master.passwd"
35 #define _COMPAT_FILE "/etc/passwd"
36 #define _PASSWD_FIELDS 10
39 extern void getpasswd(char *, int, int, int, int, char *, char **, char**, char **);
41 static int do_compat
= 1;
46 static char s
[BUFSIZE
];
51 fgets(s
, BUFSIZE
, fp
);
52 if (s
== NULL
|| s
[0] == '\0') return NULL
;
54 if (s
[0] == '#') return s
;
63 parse_user(char *line
)
65 static struct passwd pw
= {0};
69 if (pw
.pw_name
!= NULL
) free(pw
.pw_name
);
71 if (pw
.pw_passwd
!= NULL
) free(pw
.pw_passwd
);
73 if (pw
.pw_gecos
!= NULL
) free(pw
.pw_gecos
);
75 if (pw
.pw_dir
!= NULL
) free(pw
.pw_dir
);
77 if (pw
.pw_shell
!= NULL
) free(pw
.pw_shell
);
80 if (pw
.pw_class
!= NULL
) free(pw
.pw_class
);
83 if (line
== NULL
) return (struct passwd
*)NULL
;
84 tokens
= explode(line
, ':');
85 len
= listLength(tokens
);
87 if (len
!= _PASSWD_FIELDS
)
90 return (struct passwd
*)NULL
;
94 pw
.pw_name
= tokens
[i
++];
95 pw
.pw_passwd
= tokens
[i
++];
96 pw
.pw_uid
= atoi(tokens
[i
]);
98 pw
.pw_gid
= atoi(tokens
[i
]);
100 pw
.pw_class
= tokens
[i
++];
101 pw
.pw_change
= atoi(tokens
[i
]);
103 pw
.pw_expire
= atoi(tokens
[i
]);
105 pw
.pw_gecos
= tokens
[i
++];
106 pw
.pw_dir
= tokens
[i
++];
107 pw
.pw_shell
= tokens
[i
++];
113 find_user(char *uname
, FILE *fp
)
120 while (NULL
!= (line
= getline(fp
)))
122 if (line
[0] == '#') continue;
123 pw
= parse_user(line
);
124 if (pw
== (struct passwd
*)NULL
) continue;
125 if (!strcmp(uname
, pw
->pw_name
)) return pw
;
128 pw
= parse_user(NULL
);
129 return (struct passwd
*)NULL
;
133 rewrite_file(char *pwname
, FILE *fp
, struct passwd
*newpw
)
140 sprintf(fname
, "%s.%d", TEMP_FILE
, getpid());
142 tfp
= fopen(fname
, "w+");
145 fprintf(stderr
, "can't write temporary file \"%s\": ", fname
);
151 if (!strcmp(pwname
, _PASSWD_FILE
))
153 cfp
= fopen(_COMPAT_FILE
, "w");
156 fprintf(stderr
, "warning: can't write compatability file \"%s\": ",
165 fprintf(cfp
, "# 4.3BSD-compatable User Database\n");
167 fprintf(cfp
, "# Note that this file is not consulted for login.\n");
168 fprintf(cfp
, "# It only exisits for compatability with 4.3BSD utilities.\n");
170 fprintf(cfp
, "# This file is automatically re-written by various system utilities.\n");
171 fprintf(cfp
, "# Do not edit this file. Changes will be lost.\n");
177 while (NULL
!= (line
= getline(fp
)))
181 fprintf(tfp
, "%s", line
);
185 pw
= parse_user(line
);
186 if (pw
== (struct passwd
*)NULL
)
188 fprintf(stderr
, "warning: bad format for entry: \"%s\"\n", line
);
189 fprintf(tfp
, "%s\n", line
);
190 if (cfp
!= NULL
) fprintf(cfp
, "%s\n", line
);
194 if (strcmp(newpw
->pw_name
, pw
->pw_name
))
196 fprintf(tfp
, "%s\n", line
);
197 if (cfp
!= NULL
) fprintf(cfp
, "%s\n", line
);
201 fprintf(tfp
, "%s:%s:%d:%d:%s:%d:%d:%s:%s:%s\n",
202 newpw
->pw_name
, newpw
->pw_passwd
, newpw
->pw_uid
, newpw
->pw_gid
,
203 newpw
->pw_class
, newpw
->pw_change
, newpw
->pw_expire
,
204 newpw
->pw_gecos
, newpw
->pw_dir
, newpw
->pw_shell
);
207 fprintf(cfp
, "%s:",newpw
->pw_name
);
208 if ((newpw
->pw_passwd
== NULL
) || (newpw
->pw_passwd
[0] == '\0'))
212 fprintf(cfp
, "%d:%d:%s:%s:%s\n",
213 newpw
->pw_uid
, newpw
->pw_gid
, newpw
->pw_gecos
,
214 newpw
->pw_dir
, newpw
->pw_shell
);
218 if (cfp
!= NULL
) fclose(cfp
);
220 if (unlink(pwname
) < 0)
222 fprintf(stderr
, "can't update \"%s\": ", pwname
);
228 fp
= fopen(pwname
, "w");
231 fprintf(stderr
, "ERROR: lost file \"%s\"\n", pwname
);
232 fprintf(stderr
, "new passwd file is \"%s\"\n", fname
);
237 while (NULL
!= (line
= getline(tfp
)))
239 fprintf(fp
, "%s", line
);
240 if (line
[0] != '#') fprintf(fp
, "\n");
248 file_passwd(char *uname
, char *locn
)
257 fname
= _PASSWD_FILE
;
258 if (locn
!= NULL
) fname
= locn
;
260 fp
= fopen(fname
, "a+");
263 fprintf(stderr
, "can't write to file \"%s\": ", fname
);
269 pw
= find_user(uname
, fp
);
270 if (pw
== (struct passwd
*)NULL
)
272 fprintf(stderr
, "user %s not found in file %s\n", uname
, fname
);
277 if ((uid
!= 0) && (uid
!= pw
->pw_uid
))
279 fprintf(stderr
, "Permission denied\n");
284 * Get the new password
286 getpasswd(uname
, (uid
== 0), 5, 0, 0, pw
->pw_passwd
, &ne
, &oc
, &nc
);
288 newpw
.pw_name
= copyString(pw
->pw_name
);
289 newpw
.pw_passwd
= copyString(ne
);
290 newpw
.pw_uid
= pw
->pw_uid
;
291 newpw
.pw_gid
= pw
->pw_gid
;
292 newpw
.pw_class
= copyString(pw
->pw_class
);
293 newpw
.pw_change
= pw
->pw_change
;
294 newpw
.pw_expire
= pw
->pw_expire
;
295 newpw
.pw_gecos
= copyString(pw
->pw_gecos
);
296 newpw
.pw_dir
= copyString(pw
->pw_dir
);
297 newpw
.pw_shell
= copyString(pw
->pw_shell
);
302 rewrite_file(fname
, fp
, &newpw
);
307 pw
= parse_user(NULL
);
309 free(newpw
.pw_passwd
);
310 free(newpw
.pw_gecos
);
312 free(newpw
.pw_shell
);
313 free(newpw
.pw_class
);