]>
Commit | Line | Data |
---|---|---|
1815bff5 | 1 | /* |
34d340d7 | 2 | * Copyright (c) 1999-2006 Apple Computer, Inc. All rights reserved. |
1815bff5 A |
3 | * |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
34d340d7 A |
6 | * This file contains Original Code and/or Modifications of Original Code |
7 | * as defined in and that are subject to the Apple Public Source License | |
8 | * Version 2.0 (the 'License'). You may not use this file except in | |
9 | * compliance with the License. Please obtain a copy of the License at | |
10 | * http://www.opensource.apple.com/apsl/ and read it before using this | |
11 | * file. | |
1815bff5 A |
12 | * |
13 | * The Original Code and all software distributed under the License are | |
14 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
15 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, | |
16 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
34d340d7 A |
17 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. |
18 | * Please see the License for the specific language governing rights and | |
19 | * limitations under the License. | |
1815bff5 A |
20 | * |
21 | * @APPLE_LICENSE_HEADER_END@ | |
22 | */ | |
34d340d7 A |
23 | #include <TargetConditionals.h> |
24 | ||
1815bff5 | 25 | #define INFO_FILE 1 |
34d340d7 | 26 | #if !TARGET_OS_EMBEDDED |
916eb79e | 27 | #define INFO_NIS 2 |
34d340d7 A |
28 | #define INFO_OPEN_DIRECTORY 3 |
29 | #endif | |
1815bff5 A |
30 | |
31 | #ifndef __SLICK__ | |
32 | #define _PASSWD_FILE "/etc/master.passwd" | |
33 | #else | |
34 | #define _PASSWD_FILE "/etc/passwd" | |
35 | #endif | |
36 | ||
37 | #include <stdio.h> | |
38 | #include <errno.h> | |
39 | #include <pwd.h> | |
40 | #include <libc.h> | |
41 | #include <ctype.h> | |
42 | #include <string.h> | |
1815bff5 A |
43 | #include "stringops.h" |
44 | ||
45 | #ifdef __SLICK__ | |
46 | #define _PASSWORD_LEN 8 | |
47 | #endif | |
48 | ||
34d340d7 A |
49 | char* progname = "passwd"; |
50 | ||
1815bff5 A |
51 | static char *saltchars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./"; |
52 | ||
53 | extern int file_passwd(char *, char *); | |
1815bff5 | 54 | extern int nis_passwd(char *, char *); |
34d340d7 A |
55 | #ifdef INFO_OPEN_DIRECTORY |
56 | extern int od_passwd(char *, char *, char*); | |
57 | #endif | |
1815bff5 A |
58 | |
59 | void | |
60 | getpasswd(char *name, int isroot, int minlen, int mixcase, int nonalpha, | |
61 | char *old_pw, char **new_pw, char **old_clear, char **new_clear) | |
62 | { | |
63 | int i, tries, len, pw_ok, upper, lower, alpha, notalpha; | |
64 | int isNull; | |
65 | char *p; | |
66 | static char obuf[_PASSWORD_LEN+1]; | |
67 | static char nbuf[_PASSWORD_LEN+1]; | |
68 | char salt[9]; | |
69 | ||
70 | printf("Changing password for %s.\n", name); | |
71 | ||
72 | p = ""; | |
73 | isNull = 0; | |
74 | if (old_pw == NULL) isNull = 1; | |
75 | if ((isNull == 0) && (old_pw[0] == '\0')) isNull = 1; | |
76 | if ((isroot == 0) && (isNull == 0)) | |
77 | { | |
78 | p = getpass("Old password:"); | |
79 | if (strcmp(crypt(p, old_pw), old_pw)) | |
80 | { | |
81 | errno = EACCES; | |
82 | fprintf(stderr, "Sorry\n"); | |
83 | exit(1); | |
84 | } | |
85 | } | |
20e66415 A |
86 | //strcpy(obuf, p); |
87 | snprintf( obuf, sizeof(obuf), "%s", p ); | |
88 | ||
1815bff5 A |
89 | tries = 0; |
90 | nbuf[0] = '\0'; | |
91 | for (;;) | |
92 | { | |
93 | p = getpass("New password:"); | |
94 | if (!*p) | |
95 | { | |
96 | printf("Password unchanged.\n"); | |
97 | exit(0); | |
98 | } | |
99 | ||
100 | tries++; | |
101 | len = strlen(p); | |
102 | upper = 0; | |
103 | lower = 0; | |
104 | alpha = 0; | |
105 | notalpha = 0; | |
106 | for (i = 0; i < len; i++) | |
107 | { | |
108 | if (isupper(p[i])) upper++; | |
109 | if (islower(p[i])) lower++; | |
110 | if (isalpha(p[i])) alpha++; | |
111 | else notalpha++; | |
112 | } | |
113 | ||
114 | ||
115 | pw_ok = 1; | |
116 | if (len < minlen) pw_ok = 0; | |
117 | if ((mixcase == 1) && ((upper == 0) || (lower == 0))) pw_ok = 0; | |
118 | if ((nonalpha == 1) && (notalpha == 0)) pw_ok = 0; | |
119 | ||
120 | /* | |
121 | * An insistent root may override security options. | |
122 | */ | |
123 | if ((isroot == 1) && (tries > 2)) pw_ok = 1; | |
124 | ||
125 | /* | |
126 | * A very insistent user may override security options. | |
127 | */ | |
128 | if (tries > 4) pw_ok = 1; | |
129 | ||
130 | if (pw_ok == 0) | |
131 | { | |
132 | if (len < minlen) | |
133 | printf("Password must be at least %d characters long.\n", minlen); | |
134 | if ((mixcase == 1) && ((upper == 0) || (lower == 0))) | |
135 | printf("Password must contain both upper and lower case characters.\n"); | |
136 | if ((nonalpha == 1) && (notalpha == 0)) | |
137 | printf("Password must contain non-alphabetic characters.\n"); | |
138 | continue; | |
139 | } | |
140 | ||
20e66415 A |
141 | //strcpy(nbuf, p); |
142 | snprintf( nbuf, sizeof(nbuf), "%s", p ); | |
143 | ||
1815bff5 A |
144 | if (!strcmp(nbuf, getpass("Retype new password:"))) break; |
145 | ||
146 | printf("Mismatch; try again, EOF to quit.\n"); | |
147 | } | |
148 | ||
149 | /* | |
150 | * Create a random salt | |
151 | */ | |
152 | srandom((int)time((time_t *)NULL)); | |
153 | salt[0] = saltchars[random() % strlen(saltchars)]; | |
154 | salt[1] = saltchars[random() % strlen(saltchars)]; | |
155 | salt[2] = '\0'; | |
156 | *new_pw = crypt(nbuf, salt); | |
157 | ||
158 | *old_clear = obuf; | |
159 | *new_clear = nbuf; | |
160 | return; | |
161 | } | |
162 | ||
163 | void | |
164 | usage() | |
165 | { | |
34d340d7 A |
166 | fprintf(stderr, "usage: %s [-i infosystem] [-l location] [-u authname] [name]\n", progname); |
167 | fprintf(stderr, " infosystem:\n"); | |
1815bff5 | 168 | fprintf(stderr, " file\n"); |
34d340d7 A |
169 | fprintf(stderr, " NIS\n"); |
170 | fprintf(stderr, " OpenDirectory\n"); | |
171 | fprintf(stderr, " location (for infosystem):\n"); | |
172 | fprintf(stderr, " file location is path to file (default is %s)\n", _PASSWD_FILE); | |
173 | fprintf(stderr, " NIS location is NIS domain name\n"); | |
174 | fprintf(stderr, " OpenDirectory location is directory node name\n"); | |
1815bff5 A |
175 | exit(1); |
176 | } | |
177 | ||
178 | int | |
179 | main(int argc, char *argv[]) | |
180 | { | |
34d340d7 A |
181 | char* user = NULL; |
182 | char* locn = NULL; | |
183 | char* auth = NULL; | |
184 | int infosystem, ch; | |
2fc1e207 | 185 | int free_user = 0; |
20e66415 | 186 | |
34d340d7 A |
187 | #ifdef INFO_OPEN_DIRECTORY |
188 | /* since OpenDirectory works for most infosystems, make it the default */ | |
189 | infosystem = INFO_OPEN_DIRECTORY; | |
190 | #else | |
191 | infosystem = INFO_FILE; | |
192 | #endif | |
20e66415 | 193 | |
34d340d7 A |
194 | while ((ch = getopt(argc, argv, "i:l:u:")) != -1) |
195 | switch(ch) { | |
196 | case 'i': | |
197 | if (!strcasecmp(optarg, "file")) { | |
198 | infosystem = INFO_FILE; | |
916eb79e | 199 | #ifdef INFO_NIS |
34d340d7 A |
200 | } else if (!strcasecmp(optarg, "NIS")) { |
201 | infosystem = INFO_NIS; | |
202 | } else if (!strcasecmp(optarg, "YP")) { | |
203 | infosystem = INFO_NIS; | |
916eb79e | 204 | #endif |
34d340d7 A |
205 | #ifdef INFO_OPEN_DIRECTORY |
206 | } else if (!strcasecmp(optarg, "opendirectory")) { | |
207 | infosystem = INFO_OPEN_DIRECTORY; | |
208 | #endif | |
209 | } else { | |
210 | fprintf(stderr, "%s: Unknown info system \'%s\'.\n", | |
211 | progname, optarg); | |
1815bff5 A |
212 | usage(); |
213 | } | |
34d340d7 A |
214 | break; |
215 | case 'l': | |
216 | locn = optarg; | |
217 | break; | |
218 | case 'u': | |
219 | auth = optarg; | |
220 | break; | |
221 | case '?': | |
222 | default: | |
223 | usage(); | |
224 | break; | |
225 | } | |
226 | argc -= optind; | |
227 | argv += optind; | |
1815bff5 | 228 | |
34d340d7 A |
229 | if (argc > 1) { |
230 | usage(); | |
231 | } else if (argc == 1) { | |
232 | user = argv[0]; | |
1815bff5 A |
233 | } |
234 | ||
235 | if (user == NULL) | |
236 | { | |
2fc1e207 | 237 | /* |
1815bff5 A |
238 | * Verify that the login name exists. |
239 | * lukeh 24 Dec 1997 | |
240 | */ | |
2fc1e207 A |
241 | |
242 | /* getlogin() is the wrong thing to use here because it returns the wrong user after su */ | |
243 | /* sns 5 Jan 2005 */ | |
244 | ||
245 | struct passwd * userRec = getpwuid(getuid()); | |
246 | if (userRec != NULL && userRec->pw_name != NULL) { | |
247 | /* global static mem is volatile; must strdup */ | |
248 | user = strdup(userRec->pw_name); | |
249 | free_user = 1; | |
250 | } | |
251 | ||
252 | if (user == NULL) | |
1815bff5 A |
253 | { |
254 | fprintf(stderr, "you don't have a login name\n"); | |
255 | exit(1); | |
256 | } | |
257 | } | |
258 | ||
259 | switch (infosystem) | |
260 | { | |
1815bff5 A |
261 | case INFO_FILE: |
262 | file_passwd(user, locn); | |
263 | break; | |
916eb79e | 264 | #ifdef INFO_NIS |
1815bff5 A |
265 | case INFO_NIS: |
266 | nis_passwd(user, locn); | |
267 | break; | |
916eb79e | 268 | #endif |
34d340d7 A |
269 | #ifdef INFO_OPEN_DIRECTORY |
270 | case INFO_OPEN_DIRECTORY: | |
271 | od_passwd(user, locn, auth); | |
20e66415 | 272 | break; |
34d340d7 | 273 | #endif |
1815bff5 | 274 | } |
2fc1e207 A |
275 | |
276 | if (free_user == 1) | |
277 | free(user); | |
278 | ||
1815bff5 A |
279 | exit(0); |
280 | } | |
281 |