]> git.saurik.com Git - apple/system_cmds.git/blob - passwd.tproj/passwd.c
3d2b19d063c1e8c1a13a10600b80aa4f87d8e694
[apple/system_cmds.git] / passwd.tproj / passwd.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
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
13 * file.
14 *
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.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 #define INFO_NETINFO 0
26 #define INFO_FILE 1
27 #define INFO_NIS 2
28 #define INFO_DIRECTORYSERVICES 3
29
30 #ifndef __SLICK__
31 #define _PASSWD_FILE "/etc/master.passwd"
32 #else
33 #define _PASSWD_FILE "/etc/passwd"
34 #endif
35
36 #include <stdio.h>
37 #include <errno.h>
38 #include <pwd.h>
39 #include <libc.h>
40 #include <ctype.h>
41 #include <string.h>
42 #include <netinfo/ni.h>
43 #include "stringops.h"
44
45 #ifdef __SLICK__
46 #define _PASSWORD_LEN 8
47 #endif
48
49 static char *saltchars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./";
50
51 extern int file_passwd(char *, char *);
52 extern int netinfo_passwd(char *, char *);
53 extern int nis_passwd(char *, char *);
54 extern int ds_passwd(char *, char *);
55
56 void
57 getpasswd(char *name, int isroot, int minlen, int mixcase, int nonalpha,
58 char *old_pw, char **new_pw, char **old_clear, char **new_clear)
59 {
60 int i, tries, len, pw_ok, upper, lower, alpha, notalpha;
61 int isNull;
62 char *p;
63 static char obuf[_PASSWORD_LEN+1];
64 static char nbuf[_PASSWORD_LEN+1];
65 char salt[9];
66
67 printf("Changing password for %s.\n", name);
68
69 p = "";
70 isNull = 0;
71 if (old_pw == NULL) isNull = 1;
72 if ((isNull == 0) && (old_pw[0] == '\0')) isNull = 1;
73 if ((isroot == 0) && (isNull == 0))
74 {
75 p = getpass("Old password:");
76 if (strcmp(crypt(p, old_pw), old_pw))
77 {
78 errno = EACCES;
79 fprintf(stderr, "Sorry\n");
80 exit(1);
81 }
82 }
83 //strcpy(obuf, p);
84 snprintf( obuf, sizeof(obuf), "%s", p );
85
86 tries = 0;
87 nbuf[0] = '\0';
88 for (;;)
89 {
90 p = getpass("New password:");
91 if (!*p)
92 {
93 printf("Password unchanged.\n");
94 exit(0);
95 }
96
97 tries++;
98 len = strlen(p);
99 upper = 0;
100 lower = 0;
101 alpha = 0;
102 notalpha = 0;
103 for (i = 0; i < len; i++)
104 {
105 if (isupper(p[i])) upper++;
106 if (islower(p[i])) lower++;
107 if (isalpha(p[i])) alpha++;
108 else notalpha++;
109 }
110
111
112 pw_ok = 1;
113 if (len < minlen) pw_ok = 0;
114 if ((mixcase == 1) && ((upper == 0) || (lower == 0))) pw_ok = 0;
115 if ((nonalpha == 1) && (notalpha == 0)) pw_ok = 0;
116
117 /*
118 * An insistent root may override security options.
119 */
120 if ((isroot == 1) && (tries > 2)) pw_ok = 1;
121
122 /*
123 * A very insistent user may override security options.
124 */
125 if (tries > 4) pw_ok = 1;
126
127 if (pw_ok == 0)
128 {
129 if (len < minlen)
130 printf("Password must be at least %d characters long.\n", minlen);
131 if ((mixcase == 1) && ((upper == 0) || (lower == 0)))
132 printf("Password must contain both upper and lower case characters.\n");
133 if ((nonalpha == 1) && (notalpha == 0))
134 printf("Password must contain non-alphabetic characters.\n");
135 continue;
136 }
137
138 //strcpy(nbuf, p);
139 snprintf( nbuf, sizeof(nbuf), "%s", p );
140
141 if (!strcmp(nbuf, getpass("Retype new password:"))) break;
142
143 printf("Mismatch; try again, EOF to quit.\n");
144 }
145
146 /*
147 * Create a random salt
148 */
149 srandom((int)time((time_t *)NULL));
150 salt[0] = saltchars[random() % strlen(saltchars)];
151 salt[1] = saltchars[random() % strlen(saltchars)];
152 salt[2] = '\0';
153 *new_pw = crypt(nbuf, salt);
154
155 *old_clear = obuf;
156 *new_clear = nbuf;
157 return;
158 }
159
160 void
161 usage()
162 {
163 fprintf(stderr, "usage: passwd [-i infosystem] [-l location] [name]\n");
164 fprintf(stderr, "supported infosystems are:\n");
165 fprintf(stderr, " netinfo\n");
166 fprintf(stderr, " file\n");
167 fprintf(stderr, " nis\n");
168 fprintf(stderr, " opendirectory\n");
169 fprintf(stderr, "for netinfo, location may be a domain name or server/tag\n");
170 fprintf(stderr, "for file, location may be a file name (%s is the default)\n",
171 _PASSWD_FILE);
172 fprintf(stderr, "for nis, location may be a NIS domainname\n");
173 fprintf(stderr, "for opendirectory, location may be a directory node name\n");
174 exit(1);
175 }
176
177 int
178 main(int argc, char *argv[])
179 {
180 char *user, *locn;
181 int i, infosystem;
182
183 /* since DS works for most infosystems, make it the default */
184 //infosystem = INFO_NETINFO;
185 infosystem = INFO_DIRECTORYSERVICES;
186 user = NULL;
187 locn = NULL;
188
189 for (i = 1; i < argc; i++)
190 {
191 if (!strcmp(argv[i], "-i"))
192 {
193 if (++i >= argc)
194 {
195 fprintf(stderr, "no argument for -i option\n");
196 usage();
197 }
198
199 if (!strcmp(argv[i], "NetInfo")) infosystem = INFO_NETINFO;
200 else if (!strcmp(argv[i], "netinfo")) infosystem = INFO_NETINFO;
201 else if (!strcmp(argv[i], "File")) infosystem = INFO_FILE;
202 else if (!strcmp(argv[i], "file")) infosystem = INFO_FILE;
203 else if (!strcmp(argv[i], "NIS")) infosystem = INFO_NIS;
204 else if (!strcmp(argv[i], "nis")) infosystem = INFO_NIS;
205 else if (!strcmp(argv[i], "YP")) infosystem = INFO_NIS;
206 else if (!strcmp(argv[i], "yp")) infosystem = INFO_NIS;
207 else if (!strcasecmp(argv[i], "opendirectory")) infosystem = INFO_DIRECTORYSERVICES;
208 else
209 {
210 fprintf(stderr, "unknown info system \"%s\"\n", argv[i]);
211 usage();
212 }
213 }
214
215 else if (!strcmp(argv[i], "-l"))
216 {
217 if (++i >= argc)
218 {
219 fprintf(stderr, "no argument for -l option\n");
220 usage();
221 }
222 locn = argv[i];
223 }
224 else if (user == NULL) user = argv[i];
225 else usage();
226 }
227
228 if (user == NULL)
229 {
230 /*
231 * Verify that the login name exists.
232 * lukeh 24 Dec 1997
233 */
234 if ((user = getlogin()) == NULL)
235 {
236 fprintf(stderr, "you don't have a login name\n");
237 exit(1);
238 }
239 }
240
241 switch (infosystem)
242 {
243 case INFO_NETINFO:
244 netinfo_passwd(user, locn);
245 break;
246 case INFO_FILE:
247 file_passwd(user, locn);
248 break;
249 case INFO_NIS:
250 nis_passwd(user, locn);
251 break;
252 case INFO_DIRECTORYSERVICES:
253 ds_passwd(user, locn);
254 break;
255 }
256
257 exit(0);
258 }
259