]>
git.saurik.com Git - apple/system_cmds.git/blob - passwd.tproj/passwd.c
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
25 #define INFO_NETINFO 0
30 #define _PASSWD_FILE "/etc/master.passwd"
32 #define _PASSWD_FILE "/etc/passwd"
41 #include <netinfo/ni.h>
42 #include "stringops.h"
45 #define _PASSWORD_LEN 8
48 static char *saltchars
= "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./";
50 extern int file_passwd(char *, char *);
51 extern int netinfo_passwd(char *, char *);
52 extern int nis_passwd(char *, char *);
55 getpasswd(char *name
, int isroot
, int minlen
, int mixcase
, int nonalpha
,
56 char *old_pw
, char **new_pw
, char **old_clear
, char **new_clear
)
58 int i
, tries
, len
, pw_ok
, upper
, lower
, alpha
, notalpha
;
61 static char obuf
[_PASSWORD_LEN
+1];
62 static char nbuf
[_PASSWORD_LEN
+1];
65 printf("Changing password for %s.\n", name
);
69 if (old_pw
== NULL
) isNull
= 1;
70 if ((isNull
== 0) && (old_pw
[0] == '\0')) isNull
= 1;
71 if ((isroot
== 0) && (isNull
== 0))
73 p
= getpass("Old password:");
74 if (strcmp(crypt(p
, old_pw
), old_pw
))
77 fprintf(stderr
, "Sorry\n");
87 p
= getpass("New password:");
90 printf("Password unchanged.\n");
100 for (i
= 0; i
< len
; i
++)
102 if (isupper(p
[i
])) upper
++;
103 if (islower(p
[i
])) lower
++;
104 if (isalpha(p
[i
])) alpha
++;
110 if (len
< minlen
) pw_ok
= 0;
111 if ((mixcase
== 1) && ((upper
== 0) || (lower
== 0))) pw_ok
= 0;
112 if ((nonalpha
== 1) && (notalpha
== 0)) pw_ok
= 0;
115 * An insistent root may override security options.
117 if ((isroot
== 1) && (tries
> 2)) pw_ok
= 1;
120 * A very insistent user may override security options.
122 if (tries
> 4) pw_ok
= 1;
127 printf("Password must be at least %d characters long.\n", minlen
);
128 if ((mixcase
== 1) && ((upper
== 0) || (lower
== 0)))
129 printf("Password must contain both upper and lower case characters.\n");
130 if ((nonalpha
== 1) && (notalpha
== 0))
131 printf("Password must contain non-alphabetic characters.\n");
136 if (!strcmp(nbuf
, getpass("Retype new password:"))) break;
138 printf("Mismatch; try again, EOF to quit.\n");
142 * Create a random salt
144 srandom((int)time((time_t *)NULL
));
145 salt
[0] = saltchars
[random() % strlen(saltchars
)];
146 salt
[1] = saltchars
[random() % strlen(saltchars
)];
148 *new_pw
= crypt(nbuf
, salt
);
158 fprintf(stderr
, "usage: passwd [-i infosystem] [-l location] [name]\n");
159 fprintf(stderr
, "supported infosystems are:\n");
160 fprintf(stderr
, " netinfo\n");
161 fprintf(stderr
, " file\n");
162 fprintf(stderr
, " nis\n");
163 fprintf(stderr
, "for netinfo, location may be a domain name or server/tag\n");
164 fprintf(stderr
, "for file, location may be a file name (%s is the default)\n",
166 fprintf(stderr
, "for nis, location may be a NIS domainname\n");
171 main(int argc
, char *argv
[])
176 infosystem
= INFO_NETINFO
;
180 for (i
= 1; i
< argc
; i
++)
182 if (!strcmp(argv
[i
], "-i"))
186 fprintf(stderr
, "no argument for -i option\n");
190 if (!strcmp(argv
[i
], "NetInfo")) infosystem
= INFO_NETINFO
;
191 else if (!strcmp(argv
[i
], "netinfo")) infosystem
= INFO_NETINFO
;
192 else if (!strcmp(argv
[i
], "File")) infosystem
= INFO_FILE
;
193 else if (!strcmp(argv
[i
], "file")) infosystem
= INFO_FILE
;
194 else if (!strcmp(argv
[i
], "NIS")) infosystem
= INFO_NIS
;
195 else if (!strcmp(argv
[i
], "nis")) infosystem
= INFO_NIS
;
196 else if (!strcmp(argv
[i
], "YP")) infosystem
= INFO_NIS
;
197 else if (!strcmp(argv
[i
], "yp")) infosystem
= INFO_NIS
;
200 fprintf(stderr
, "unknown info system \"%s\"\n", argv
[i
]);
205 else if (!strcmp(argv
[i
], "-l"))
209 fprintf(stderr
, "no argument for -l option\n");
214 else if (user
== NULL
) user
= argv
[i
];
221 * Verify that the login name exists.
224 if ((user
= getlogin()) == NULL
)
226 fprintf(stderr
, "you don't have a login name\n");
234 netinfo_passwd(user
, locn
);
237 file_passwd(user
, locn
);
240 nis_passwd(user
, locn
);