]> git.saurik.com Git - apple/system_cmds.git/blob - passwd.tproj/passwd.c
system_cmds-230.7.tar.gz
[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
29 #ifndef __SLICK__
30 #define _PASSWD_FILE "/etc/master.passwd"
31 #else
32 #define _PASSWD_FILE "/etc/passwd"
33 #endif
34
35 #include <stdio.h>
36 #include <errno.h>
37 #include <pwd.h>
38 #include <libc.h>
39 #include <ctype.h>
40 #include <string.h>
41 #include <netinfo/ni.h>
42 #include "stringops.h"
43
44 #ifdef __SLICK__
45 #define _PASSWORD_LEN 8
46 #endif
47
48 static char *saltchars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./";
49
50 extern int file_passwd(char *, char *);
51 extern int netinfo_passwd(char *, char *);
52 extern int nis_passwd(char *, char *);
53
54 void
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)
57 {
58 int i, tries, len, pw_ok, upper, lower, alpha, notalpha;
59 int isNull;
60 char *p;
61 static char obuf[_PASSWORD_LEN+1];
62 static char nbuf[_PASSWORD_LEN+1];
63 char salt[9];
64
65 printf("Changing password for %s.\n", name);
66
67 p = "";
68 isNull = 0;
69 if (old_pw == NULL) isNull = 1;
70 if ((isNull == 0) && (old_pw[0] == '\0')) isNull = 1;
71 if ((isroot == 0) && (isNull == 0))
72 {
73 p = getpass("Old password:");
74 if (strcmp(crypt(p, old_pw), old_pw))
75 {
76 errno = EACCES;
77 fprintf(stderr, "Sorry\n");
78 exit(1);
79 }
80 }
81 strcpy(obuf, p);
82
83 tries = 0;
84 nbuf[0] = '\0';
85 for (;;)
86 {
87 p = getpass("New password:");
88 if (!*p)
89 {
90 printf("Password unchanged.\n");
91 exit(0);
92 }
93
94 tries++;
95 len = strlen(p);
96 upper = 0;
97 lower = 0;
98 alpha = 0;
99 notalpha = 0;
100 for (i = 0; i < len; i++)
101 {
102 if (isupper(p[i])) upper++;
103 if (islower(p[i])) lower++;
104 if (isalpha(p[i])) alpha++;
105 else notalpha++;
106 }
107
108
109 pw_ok = 1;
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;
113
114 /*
115 * An insistent root may override security options.
116 */
117 if ((isroot == 1) && (tries > 2)) pw_ok = 1;
118
119 /*
120 * A very insistent user may override security options.
121 */
122 if (tries > 4) pw_ok = 1;
123
124 if (pw_ok == 0)
125 {
126 if (len < minlen)
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");
132 continue;
133 }
134
135 strcpy(nbuf, p);
136 if (!strcmp(nbuf, getpass("Retype new password:"))) break;
137
138 printf("Mismatch; try again, EOF to quit.\n");
139 }
140
141 /*
142 * Create a random salt
143 */
144 srandom((int)time((time_t *)NULL));
145 salt[0] = saltchars[random() % strlen(saltchars)];
146 salt[1] = saltchars[random() % strlen(saltchars)];
147 salt[2] = '\0';
148 *new_pw = crypt(nbuf, salt);
149
150 *old_clear = obuf;
151 *new_clear = nbuf;
152 return;
153 }
154
155 void
156 usage()
157 {
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",
165 _PASSWD_FILE);
166 fprintf(stderr, "for nis, location may be a NIS domainname\n");
167 exit(1);
168 }
169
170 int
171 main(int argc, char *argv[])
172 {
173 char *user, *locn;
174 int i, infosystem;
175
176 infosystem = INFO_NETINFO;
177 user = NULL;
178 locn = NULL;
179
180 for (i = 1; i < argc; i++)
181 {
182 if (!strcmp(argv[i], "-i"))
183 {
184 if (++i >= argc)
185 {
186 fprintf(stderr, "no argument for -i option\n");
187 usage();
188 }
189
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;
198 else
199 {
200 fprintf(stderr, "unknown info system \"%s\"\n", argv[i]);
201 usage();
202 }
203 }
204
205 else if (!strcmp(argv[i], "-l"))
206 {
207 if (++i >= argc)
208 {
209 fprintf(stderr, "no argument for -l option\n");
210 usage();
211 }
212 locn = argv[i];
213 }
214 else if (user == NULL) user = argv[i];
215 else usage();
216 }
217
218 if (user == NULL)
219 {
220 /*
221 * Verify that the login name exists.
222 * lukeh 24 Dec 1997
223 */
224 if ((user = getlogin()) == NULL)
225 {
226 fprintf(stderr, "you don't have a login name\n");
227 exit(1);
228 }
229 }
230
231 switch (infosystem)
232 {
233 case INFO_NETINFO:
234 netinfo_passwd(user, locn);
235 break;
236 case INFO_FILE:
237 file_passwd(user, locn);
238 break;
239 case INFO_NIS:
240 nis_passwd(user, locn);
241 break;
242 }
243
244 exit(0);
245 }
246