]>
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@
29 #include "stringops.h"
31 #define TEMP_FILE "/tmp/.pwtmp"
33 #define _PASSWD_FILE "/etc/master.passwd"
34 #define _COMPAT_FILE "/etc/passwd"
35 #define _PASSWD_FIELDS 10
38 extern void checkpasswd(char *, char *);
40 static int do_compat
= 1;
45 static char s
[BUFSIZE
];
50 fgets(s
, BUFSIZE
, fp
);
51 if (s
== NULL
|| s
[0] == '\0') return NULL
;
53 if (s
[0] == '#') return s
;
62 parse_user(char *line
)
64 static struct passwd pw
= {0};
68 if (pw
.pw_name
!= NULL
) free(pw
.pw_name
);
70 if (pw
.pw_passwd
!= NULL
) free(pw
.pw_passwd
);
72 if (pw
.pw_gecos
!= NULL
) free(pw
.pw_gecos
);
74 if (pw
.pw_dir
!= NULL
) free(pw
.pw_dir
);
76 if (pw
.pw_shell
!= NULL
) free(pw
.pw_shell
);
79 if (pw
.pw_class
!= NULL
) free(pw
.pw_class
);
82 if (line
== NULL
) return (struct passwd
*)NULL
;
83 tokens
= explode(line
, ':');
84 len
= listLength(tokens
);
86 if (len
!= _PASSWD_FIELDS
)
89 return (struct passwd
*)NULL
;
93 pw
.pw_name
= tokens
[i
++];
94 pw
.pw_passwd
= tokens
[i
++];
95 pw
.pw_uid
= atoi(tokens
[i
]);
97 pw
.pw_gid
= atoi(tokens
[i
]);
99 pw
.pw_class
= tokens
[i
++];
100 pw
.pw_change
= atoi(tokens
[i
]);
102 pw
.pw_expire
= atoi(tokens
[i
]);
104 pw
.pw_gecos
= tokens
[i
++];
105 pw
.pw_dir
= tokens
[i
++];
106 pw
.pw_shell
= tokens
[i
++];
112 find_user(char *uname
, FILE *fp
)
119 while (NULL
!= (line
= getline(fp
)))
121 if (line
[0] == '#') continue;
122 pw
= parse_user(line
);
123 if (pw
== (struct passwd
*)NULL
) continue;
124 if (!strcmp(uname
, pw
->pw_name
)) return pw
;
127 pw
= parse_user(NULL
);
128 return (struct passwd
*)NULL
;
132 rewrite_file(char *pwname
, FILE *fp
, struct passwd
*newpw
)
139 sprintf(fname
, "%s.%d", TEMP_FILE
, getpid());
141 tfp
= fopen(fname
, "w+");
144 fprintf(stderr
, "can't write temporary file \"%s\": ", fname
);
150 if (!strcmp(pwname
, _PASSWD_FILE
))
152 cfp
= fopen(_COMPAT_FILE
, "w");
155 fprintf(stderr
, "warning: can't write compatability file \"%s\": ",
164 fprintf(cfp
, "# 4.3BSD-compatable User Database\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");
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");
176 while (NULL
!= (line
= getline(fp
)))
180 fprintf(tfp
, "%s", line
);
184 pw
= parse_user(line
);
185 if (pw
== (struct passwd
*)NULL
)
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
);
193 if (strcmp(newpw
->pw_name
, pw
->pw_name
))
195 fprintf(tfp
, "%s\n", line
);
196 if (cfp
!= NULL
) fprintf(cfp
, "%s\n", line
);
200 fprintf(tfp
, "%s:%s:%d:%d:%s:%d:%d:%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
);
206 fprintf(cfp
, "%s:",newpw
->pw_name
);
207 if ((newpw
->pw_passwd
== NULL
) || (newpw
->pw_passwd
[0] == '\0'))
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
);
217 if (cfp
!= NULL
) fclose(cfp
);
219 if (unlink(pwname
) < 0)
221 fprintf(stderr
, "can't update \"%s\": ", pwname
);
227 fp
= fopen(pwname
, "w");
230 fprintf(stderr
, "ERROR: lost file \"%s\"\n", pwname
);
231 fprintf(stderr
, "new passwd file is \"%s\"\n", fname
);
236 while (NULL
!= (line
= getline(tfp
)))
238 fprintf(fp
, "%s", line
);
239 if (line
[0] != '#') fprintf(fp
, "\n");
247 file_check_passwd(char *uname
, char *locn
)
253 fname
= _PASSWD_FILE
;
254 if (locn
!= NULL
) fname
= locn
;
256 fp
= fopen(fname
, "r");
259 fprintf(stderr
, "can't read file \"%s\": ", fname
);
265 pw
= find_user(uname
, fp
);
266 if (pw
== (struct passwd
*)NULL
)
268 fprintf(stderr
, "user %s not found in file %s\n", uname
, fname
);
272 checkpasswd(uname
, pw
->pw_passwd
);