2 * Copyright 1999 (c) Adrian Sun (asun@u.washington.edu)
3 * All Rights Reserved. See COPYRIGHT.
5 * format of the password file:
6 * name:****************:****************:********
7 * password last login date failed usage count
9 * ***'s are illegal. they're just place holders for hex values. hex
10 * values that represent actual numbers are in network byte order.
12 * last login date is currently a 4-byte number representing seconds
13 * since 1970 (UTC). there's enough space to extend it to 8 bytes.
15 * the last two fields aren't currently used by the randnum uams.
17 * root syntax: afppasswd [-c] [-a] [-p path] [-f] [username]
18 * user syntax: afppasswd
26 #include <sys/types.h>
28 #include <sys/param.h>
32 #include <netatalk/endian.h>
41 #define OPT_ISROOT (1 << 0)
42 #define OPT_CREATE (1 << 1)
43 #define OPT_FORCE (1 << 2)
44 #define OPT_ADDUSER (1 << 3)
46 #define PASSWD_ILLEGAL '*'
48 #define FORMAT ":****************:****************:********\n"
50 #define OPTIONS "cafu:p:"
53 #define HEXPASSWDLEN 16
56 static char buf[MAXPATHLEN + 1];
58 /* if newpwd is null, convert buf from hex to binary. if newpwd isn't
59 * null, convert newpwd to hex and save it in buf. */
60 #define unhex(x) (isdigit(x) ? (x) - '0' : toupper(x) + 10 - 'A')
61 static void convert_passwd(char *buf, char *newpwd, const int keyfd)
63 u_int8_t key[HEXPASSWDLEN];
64 Key_schedule schedule;
68 /* convert to binary */
69 for (i = j = 0; i < sizeof(key); i += 2, j++)
70 buf[j] = (unhex(buf[i]) << 4) | unhex(buf[i + 1]);
72 memset(buf + j, 0, sizeof(key) - j);
76 lseek(keyfd, 0, SEEK_SET);
77 read(keyfd, key, sizeof(key));
78 /* convert to binary */
79 for (i = j = 0; i < sizeof(key); i += 2, j++)
80 key[j] = (unhex(key[i]) << 4) | unhex(key[i + 1]);
82 memset(key + j, 0, sizeof(key) - j);
83 key_sched((C_Block *) key, schedule);
84 memset(key, 0, sizeof(key));
86 ecb_encrypt((C_Block *) newpwd, (C_Block *) newpwd, schedule,
89 /* decrypt the password */
90 ecb_encrypt((C_Block *) buf, (C_Block *) buf, schedule, DES_DECRYPT);
92 memset(schedule, 0, sizeof(schedule));
96 const unsigned char hextable[] = "0123456789ABCDEF";
99 for (i = j = 0; i < DES_KEY_SZ; i++, j += 2) {
100 buf[j] = hextable[(newpwd[i] & 0xF0) >> 4];
101 buf[j + 1] = hextable[newpwd[i] & 0x0F];
106 /* this matches the code in uam_randnum.c */
107 static int update_passwd(const char *path, const char *name, int flags)
109 char password[PASSWDLEN + 1], *p, *passwd;
112 int keyfd = -1, err = 0;
114 if ((fp = fopen(path, "r+")) == NULL) {
115 fprintf(stderr, "afppasswd: can't open %s\n", path);
119 /* open the key file if it exists */
121 if (strlen(path) < sizeof(buf) - 5) {
123 keyfd = open(buf, O_RDONLY);
127 memset(buf, 0, sizeof(buf));
128 while (fgets(buf, sizeof(buf), fp)) {
129 if ((p = strchr(buf, ':'))) {
130 /* check for a match */
131 if (strncmp(buf, name, p - buf) == 0) {
133 if (!(flags & OPT_ISROOT) && (*p == PASSWD_ILLEGAL)) {
134 fprintf(stderr, "Your password is disabled. Please see your administrator.\n");
141 memset(buf, 0, sizeof(buf));
144 if (flags & OPT_ADDUSER) {
147 p = strchr(buf, ':') + 1;
148 fwrite(buf, strlen(buf), 1, fp);
150 fprintf(stderr, "afppasswd: can't find %s in %s\n", name, path);
156 /* need to verify against old password */
157 if ((flags & OPT_ISROOT) == 0) {
158 passwd = getpass("Enter OLD AFP password: ");
159 convert_passwd(p, NULL, keyfd);
160 if (strncmp(passwd, p, PASSWDLEN)) {
161 fprintf(stderr, "afppasswd: invalid password.\n");
168 passwd = getpass("Enter NEW AFP password: ");
169 memcpy(password, passwd, sizeof(password));
170 password[PASSWDLEN] = '\0';
172 if ((passwd = FascistCheck(password, _PATH_CRACKLIB))) {
173 fprintf(stderr, "Error: %s\n", passwd);
179 passwd = getpass("Enter NEW AFP password again: ");
180 if (strcmp(passwd, password) == 0) {
184 convert_passwd(p, password, keyfd);
185 lock.l_type = F_WRLCK;
188 lock.l_whence = SEEK_SET;
189 fseek(fp, pos, SEEK_SET);
190 fcntl(fd, F_SETLKW, &lock);
191 fwrite(buf, p - buf + HEXPASSWDLEN, 1, fp);
192 lock.l_type = F_UNLCK;
193 fcntl(fd, F_SETLK, &lock);
194 printf("afppasswd: updated password.\n");
197 fprintf(stderr, "afppasswd: passwords don't match!\n");
209 /* creates a file with all the password entries */
210 static int create_file(const char *path, uid_t minuid)
213 int fd, len, err = 0;
216 if ((fd = open(path, O_CREAT | O_TRUNC | O_RDWR, 0600)) < 0) {
217 fprintf(stderr, "afppasswd: can't create %s\n", path);
222 while ((pwd = getpwent())) {
223 if (pwd->pw_uid < minuid)
225 /* a little paranoia */
226 if (strlen(pwd->pw_name) + FORMAT_LEN > sizeof(buf) - 1)
228 strcpy(buf, pwd->pw_name);
231 if (write(fd, buf, len) != len) {
232 fprintf(stderr, "afppasswd: problem writing to %s: %m\n", path);
244 int main(int argc, char **argv)
248 uid_t uid_min = UID_START, uid;
249 char *path = _PATH_AFPDPWFILE;
253 extern int optind, opterr;
255 flags = ((uid = getuid()) == 0) ? OPT_ISROOT : 0;
257 if (((flags & OPT_ISROOT) == 0) && (argc > 1)) {
258 fprintf(stderr, "Usage: afppasswd\n");
262 while ((i = getopt(argc, argv, OPTIONS)) != EOF) {
264 case 'c': /* create and initialize password file or specific user */
267 case 'a': /* add a new user */
268 flags |= OPT_ADDUSER;
269 case 'f': /* force an action */
272 case 'u': /* minimum uid to use. default is 100 */
273 uid_min = atoi(optarg);
275 case 'p': /* path to afppasswd file */
284 if (err || (optind + ((flags & OPT_CREATE) ? 0 :
285 (flags & OPT_ISROOT)) != argc)) {
286 fprintf(stderr, "Usage: afppasswd [-acf] [-u minuid] [-p path] [username]\n");
291 if (flags & OPT_CREATE) {
292 if ((flags & OPT_ISROOT) == 0) {
293 fprintf(stderr, "afppasswd: only root can create the password file.\n");
297 if (!i && ((flags & OPT_FORCE) == 0)) {
298 fprintf(stderr, "afppasswd: password file already exists.\n");
301 return create_file(path, uid_min);
304 struct passwd *pwd = NULL;
307 fprintf(stderr, "afppasswd: %s doesn't exist.\n", path);
311 /* if we're root, we need to specify the username */
312 pwd = (flags & OPT_ISROOT) ? getpwnam(argv[optind]) : getpwuid(uid);
314 return update_passwd(path, pwd->pw_name, flags);
316 fprintf(stderr, "afppasswd: can't get password entry.\n");
322 main(int argc, char **argv)
324 fprintf(stderr, "afppasswd is only useful if you're using centralized passwords\n");
325 fprintf(stderr, "for the Random Number authentication methods.\n");