]> git.saurik.com Git - apple/system_cmds.git/blob - passwd.tproj/passwd.c
system_cmds-433.tar.gz
[apple/system_cmds.git] / passwd.tproj / passwd.c
1 /*
2 * Copyright (c) 1999-2006 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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.
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,
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23 #include <TargetConditionals.h>
24
25 #define INFO_FILE 1
26 #if !TARGET_OS_EMBEDDED
27 #define INFO_NIS 2
28 #define INFO_OPEN_DIRECTORY 3
29 #endif
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>
43 #include "stringops.h"
44
45 #ifdef __SLICK__
46 #define _PASSWORD_LEN 8
47 #endif
48
49 char* progname = "passwd";
50
51 static char *saltchars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./";
52
53 extern int file_passwd(char *, char *);
54 extern int nis_passwd(char *, char *);
55 #ifdef INFO_OPEN_DIRECTORY
56 extern int od_passwd(char *, char *, char*);
57 #endif
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 }
86 //strcpy(obuf, p);
87 snprintf( obuf, sizeof(obuf), "%s", p );
88
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
141 //strcpy(nbuf, p);
142 snprintf( nbuf, sizeof(nbuf), "%s", p );
143
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 {
166 fprintf(stderr, "usage: %s [-i infosystem] [-l location] [-u authname] [name]\n", progname);
167 fprintf(stderr, " infosystem:\n");
168 fprintf(stderr, " file\n");
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");
175 exit(1);
176 }
177
178 int
179 main(int argc, char *argv[])
180 {
181 char* user = NULL;
182 char* locn = NULL;
183 char* auth = NULL;
184 int infosystem, ch;
185 int free_user = 0;
186
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
193
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;
199 #ifdef INFO_NIS
200 } else if (!strcasecmp(optarg, "NIS")) {
201 infosystem = INFO_NIS;
202 } else if (!strcasecmp(optarg, "YP")) {
203 infosystem = INFO_NIS;
204 #endif
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);
212 usage();
213 }
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;
228
229 if (argc > 1) {
230 usage();
231 } else if (argc == 1) {
232 user = argv[0];
233 }
234
235 if (user == NULL)
236 {
237 /*
238 * Verify that the login name exists.
239 * lukeh 24 Dec 1997
240 */
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)
253 {
254 fprintf(stderr, "you don't have a login name\n");
255 exit(1);
256 }
257 }
258
259 switch (infosystem)
260 {
261 case INFO_FILE:
262 file_passwd(user, locn);
263 break;
264 #ifdef INFO_NIS
265 case INFO_NIS:
266 nis_passwd(user, locn);
267 break;
268 #endif
269 #ifdef INFO_OPEN_DIRECTORY
270 case INFO_OPEN_DIRECTORY:
271 od_passwd(user, locn, auth);
272 break;
273 #endif
274 }
275
276 if (free_user == 1)
277 free(user);
278
279 exit(0);
280 }
281