]>
git.saurik.com Git - apple/system_cmds.git/blob - chkpasswd.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 checkpasswd(char *, char *);
44 static char s
[BUFSIZE
];
49 fgets(s
, BUFSIZE
, fp
);
50 if (s
== NULL
|| s
[0] == '\0') return NULL
;
52 if (s
[0] == '#') return s
;
61 parse_user(char *line
)
63 static struct passwd pw
= {0};
67 if (pw
.pw_name
!= NULL
) free(pw
.pw_name
);
69 if (pw
.pw_passwd
!= NULL
) free(pw
.pw_passwd
);
71 if (pw
.pw_gecos
!= NULL
) free(pw
.pw_gecos
);
73 if (pw
.pw_dir
!= NULL
) free(pw
.pw_dir
);
75 if (pw
.pw_shell
!= NULL
) free(pw
.pw_shell
);
78 if (pw
.pw_class
!= NULL
) free(pw
.pw_class
);
81 if (line
== NULL
) return (struct passwd
*)NULL
;
82 tokens
= explode(line
, ':');
83 len
= listLength(tokens
);
85 if (len
!= _PASSWD_FIELDS
)
88 return (struct passwd
*)NULL
;
92 pw
.pw_name
= tokens
[i
++];
93 pw
.pw_passwd
= tokens
[i
++];
94 pw
.pw_uid
= atoi(tokens
[i
]);
96 pw
.pw_gid
= atoi(tokens
[i
]);
98 pw
.pw_class
= tokens
[i
++];
99 pw
.pw_change
= atoi(tokens
[i
]);
101 pw
.pw_expire
= atoi(tokens
[i
]);
103 pw
.pw_gecos
= tokens
[i
++];
104 pw
.pw_dir
= tokens
[i
++];
105 pw
.pw_shell
= tokens
[i
++];
111 find_user(char *uname
, FILE *fp
)
118 while (NULL
!= (line
= getline(fp
)))
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
;
126 pw
= parse_user(NULL
);
127 return (struct passwd
*)NULL
;
131 rewrite_file(char *pwname
, FILE *fp
, struct passwd
*newpw
)
138 sprintf(fname
, "%s.%d", TEMP_FILE
, getpid());
140 tfp
= fopen(fname
, "w+");
143 fprintf(stderr
, "can't write temporary file \"%s\": ", fname
);
149 if (!strcmp(pwname
, _PASSWD_FILE
))
151 cfp
= fopen(_COMPAT_FILE
, "w");
154 fprintf(stderr
, "warning: can't write compatability file \"%s\": ",
163 fprintf(cfp
, "# 4.3BSD-compatable User Database\n");
165 fprintf(cfp
, "# Note that this file is not consulted for login.\n");
166 fprintf(cfp
, "# It only exisits for compatability with 4.3BSD utilities.\n");
168 fprintf(cfp
, "# This file is automatically re-written by various system utilities.\n");
169 fprintf(cfp
, "# Do not edit this file. Changes will be lost.\n");
175 while (NULL
!= (line
= getline(fp
)))
179 fprintf(tfp
, "%s", line
);
183 pw
= parse_user(line
);
184 if (pw
== (struct passwd
*)NULL
)
186 fprintf(stderr
, "warning: bad format for entry: \"%s\"\n", line
);
187 fprintf(tfp
, "%s\n", line
);
188 if (cfp
!= NULL
) fprintf(cfp
, "%s\n", line
);
192 if (strcmp(newpw
->pw_name
, pw
->pw_name
))
194 fprintf(tfp
, "%s\n", line
);
195 if (cfp
!= NULL
) fprintf(cfp
, "%s\n", line
);
199 fprintf(tfp
, "%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s\n",
200 newpw
->pw_name
, newpw
->pw_passwd
, newpw
->pw_uid
, newpw
->pw_gid
,
201 newpw
->pw_class
, newpw
->pw_change
, newpw
->pw_expire
,
202 newpw
->pw_gecos
, newpw
->pw_dir
, newpw
->pw_shell
);
205 fprintf(cfp
, "%s:",newpw
->pw_name
);
206 if ((newpw
->pw_passwd
== NULL
) || (newpw
->pw_passwd
[0] == '\0'))
210 fprintf(cfp
, "%d:%d:%s:%s:%s\n",
211 newpw
->pw_uid
, newpw
->pw_gid
, newpw
->pw_gecos
,
212 newpw
->pw_dir
, newpw
->pw_shell
);
216 if (cfp
!= NULL
) fclose(cfp
);
218 if (unlink(pwname
) < 0)
220 fprintf(stderr
, "can't update \"%s\": ", pwname
);
226 fp
= fopen(pwname
, "w");
229 fprintf(stderr
, "ERROR: lost file \"%s\"\n", pwname
);
230 fprintf(stderr
, "new passwd file is \"%s\"\n", fname
);
235 while (NULL
!= (line
= getline(tfp
)))
237 fprintf(fp
, "%s", line
);
238 if (line
[0] != '#') fprintf(fp
, "\n");
246 file_check_passwd(char *uname
, char *locn
)
252 fname
= _PASSWD_FILE
;
253 if (locn
!= NULL
) fname
= locn
;
255 if (access(fname
,R_OK
) || (fp
= fopen(fname
, "r")) == NULL
)
257 fprintf(stderr
, "can't read file \"%s\": ", fname
);
263 pw
= find_user(uname
, fp
);
264 if (pw
== (struct passwd
*)NULL
)
266 fprintf(stderr
, "user %s not found in file %s\n", uname
, fname
);
270 checkpasswd(uname
, pw
->pw_passwd
);