]> git.ipfire.org Git - thirdparty/util-linux.git/blob - login-utils/passwd.c
Imported from util-linux-2.10s tarball.
[thirdparty/util-linux.git] / login-utils / passwd.c
1 /*
2 * passwd.c - change password on an account
3 *
4 * Initially written for Linux by Peter Orbaek <poe@daimi.aau.dk>
5 * Currently maintained at ftp://ftp.daimi.aau.dk/pub/linux/poe/
6 *
7 * Hacked by Alvaro Martinez Echevarria, alvaro@enano.etsit.upm.es,
8 * to allow peaceful coexistence with yp. Nov 94.
9 *
10 * Hacked to allow root to set passwd from command line.
11 * by Arpad Magossanyi (mag@tas.vein.hu)
12 */
13
14 /*
15 * Sun Oct 15 13:18:34 1995 Martin Schulze <joey@finlandia.infodrom.north.de>
16 *
17 * I have completely rewritten the whole argument handling (what?)
18 * to support two things. First I wanted "passwd $user $pw" to
19
20 (a very bad idea; command lines are visible to people doing ps
21 or running a background job that just collects all command lines)
22
23 * work and second I wanted simplicity checks to be done for
24 * root, too. Only root can turn this off using the -f
25 * switch. Okay, I started with this to support -V version
26 * information, but one thing comes to the next. *sigh*
27 * In a later step perhaps we'll be able to support shadow
28 * passwords. (?)
29 *
30 * I have also included a DEBUG mode (-DDEBUG) to test the
31 * argument handling _without_ any write attempt to
32 * /etc/passwd.
33 *
34 * If you're paranoid about security on your system, you may want
35 * to add -DLOGALL to CFLAGS. This will turn on additional syslog
36 * logging of every password change. (user changes are logged as
37 * auth.notice, but changing root's password is logged as
38 * auth.warning. (Of course, the password itself is not logged.)
39 */
40
41 /* 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@misiek.eu.org>
42 * - added Native Language Support
43 * Sun Mar 21 1999 - Arnaldo Carvalho de Melo <acme@conectiva.com.br>
44 * - fixed strerr(errno) in gettext calls
45 */
46
47 /*
48 * Usage: passwd [username [password]]
49 * Only root may use the one and two argument forms.
50 */
51
52 #include <sys/types.h>
53 #include <sys/stat.h>
54 #include <stdio.h>
55 #include <unistd.h>
56 #include <stdarg.h>
57 #include <termios.h>
58 #include <getopt.h>
59 #include <malloc.h>
60 #include <fcntl.h>
61 #include <pwd.h>
62 #include <ctype.h>
63 #include <time.h>
64 #include <string.h>
65 #include <errno.h>
66 #include <sys/resource.h>
67 #include <stdlib.h>
68 #include "my_crypt.h"
69 #include "setpwnam.h"
70 #include "islocal.h"
71 #include "nls.h"
72 #include "env.h"
73
74 #ifndef _PATH_CHFN
75 # define _PATH_CHFN "/usr/bin/chfn"
76 # define _PATH_CHSH "/usr/bin/chsh"
77 #endif
78
79 #define LOGALL
80
81 #ifdef LOGALL
82 #include <syslog.h>
83 #endif /* LOGALL */
84
85 #define ascii_to_bin(c) ((c)>='a'?(c-59):(c)>='A'?((c)-53):(c)-'.')
86 #define bin_to_ascii(c) ((c)>=38?((c)-38+'a'):(c)>=12?((c)-12+'A'):(c)+'.')
87
88 static void
89 pexit(char *str, ...)
90 {
91 va_list vlst;
92
93 va_start(vlst, str);
94 vfprintf(stderr, str, vlst);
95 fprintf(stderr, ": ");
96 perror("");
97 va_end(vlst);
98 exit(1);
99 }
100
101 /*
102 * Do various checks for stupid passwords here...
103 *
104 * This would probably be the best place for checking against
105 * dictionaries. :-)
106 */
107 static int
108 check_passwd_string(char *passwd, char *string) {
109 int r;
110 char *p, *q;
111
112 r = 0;
113 /* test for string at the beginning of passwd */
114 for (p = passwd, q = string; *q && *p; q++, p++) {
115 if(tolower(*p) != tolower(*q)) {
116 r++;
117 break;
118 }
119 }
120
121 /* test for reverse string at the beginning of passwd */
122 for (p = passwd, q = string + strlen(string)-1;
123 *p && q >= string; p++, q--) {
124 if(tolower(*p) != tolower(*q)) {
125 r++;
126 break;
127 }
128 }
129
130 /* test for string at the end of passwd */
131 for (p = passwd + strlen(passwd)-1, q = string + strlen(string)-1;
132 q >= string && p >= passwd; q--, p--) {
133 if(tolower(*p) != tolower(*q)) {
134 r++;
135 break;
136 }
137 }
138
139 /* test for reverse string at the beginning of passwd */
140 for (p = passwd + strlen(passwd)-1, q = string;
141 p >= passwd && *q; p--, q++) {
142 if(tolower(*p) != tolower(*q)) {
143 r++;
144 break;
145 }
146 }
147
148 if (r != 4) {
149 return 0;
150 }
151 return 1;
152 }
153
154 static int
155 check_passwd(char *passwd, char *oldpasswd, char *user, char *gecos) {
156 int ucase, lcase, digit, other;
157 char *c, *g, *p;
158
159 if ( (strlen(passwd) < 6) ) {
160 printf(_("The password must have at least 6 characters, try again.\n"));
161 return 0;
162 }
163
164 other = digit = ucase = lcase = 0;
165 for (p = passwd; *p; p++) {
166 ucase = ucase || isupper(*p);
167 lcase = lcase || islower(*p);
168 digit = digit || isdigit(*p);
169 other = other || !isalnum(*p);
170 }
171
172 if ( (other + digit + ucase + lcase) < 2) {
173 printf(_("The password must contain characters out of two of the following\n"));
174 printf(_("classes: upper and lower case letters, digits and non alphanumeric\n"));
175 printf(_("characters. See passwd(1) for more information.\n"));
176 return 0;
177 }
178
179 if ( oldpasswd[0] && !strncmp(oldpasswd, crypt(passwd, oldpasswd), 13) ) {
180 printf(_("You cannot reuse the old password.\n"));
181 return 0;
182 }
183
184 if ( !check_passwd_string(passwd, user) ) {
185 printf(_("Please don't use something like your username as password!\n"));
186 return 0;
187 }
188
189 /* check against realname */
190 if ( (c = index(gecos, ',')) ) {
191 if ( c-gecos && (g = (char *)malloc (c-gecos+1)) ) {
192 strncpy (g, gecos, c-gecos);
193 g[c-gecos] = 0;
194 while ( (c=rindex(g, ' ')) ) {
195 if ( !check_passwd_string(passwd, c+1) ) {
196 printf(_("Please don't use something like your realname as password!\n"));
197 free (g);
198 return 0;
199 }
200 *c = '\0';
201 } /* while */
202 if ( !check_passwd_string(passwd, g) ) {
203 printf(_("Please don't use something like your realname as password!\n"));
204 free (g);
205 return 0;
206 }
207 free (g);
208 } /* if malloc */
209 }
210
211 /*
212 * if ( !check_password_dict(passwd) ) ...
213 */
214
215 return 1; /* fine */
216 }
217
218 #if 0
219 static void
220 usage(void) {
221 printf (_("Usage: passwd [username [password]]\n"));
222 printf(_("Only root may use the one and two argument forms.\n"));
223 }
224 #endif
225
226 int
227 main(int argc, char *argv[]) {
228 struct passwd *pe;
229 uid_t gotuid = getuid();
230 char *pwdstr = NULL, *cryptstr, *oldstr;
231 char pwdstr1[10];
232 char *user;
233 time_t tm;
234 char salt[2];
235 int force_passwd = 0;
236 int silent = 0;
237 char c;
238 int opt_index;
239 int fullname = 0, shell = 0;
240 static const struct option long_options[] =
241 {
242 {"fullname", no_argument, 0, 'f'},
243 {"shell", no_argument, 0, 's'},
244 {"force", no_argument, 0, 'o'},
245 {"quiet", no_argument, 0, 'q'},
246 {"silent", no_argument, 0, 'q'},
247 {"version", no_argument, 0, 'v'},
248 {0, 0, 0, 0}
249 };
250
251 sanitize_env();
252 setlocale(LC_ALL, "");
253 bindtextdomain(PACKAGE, LOCALEDIR);
254 textdomain(PACKAGE);
255
256 optind = 0;
257 while ( (c = getopt_long(argc, argv, "foqsvV", long_options, &opt_index)) != -1 ) {
258 switch (c) {
259 case 'f':
260 fullname = 1;
261 break;
262 case 's':
263 shell = 1;
264 break;
265 case 'o':
266 force_passwd = 1;
267 break;
268 case 'q':
269 silent = 1;
270 break;
271 case 'V':
272 case 'v':
273 printf("%s\n", util_linux_version);
274 exit(0);
275 default:
276 fprintf(stderr, _("Usage: passwd [-foqsvV] [user [password]]\n"));
277 exit(1);
278 } /* switch (c) */
279 } /* while */
280
281 if (fullname || shell) {
282 char *args[100];
283 int i, j, errsv;
284
285 setuid(getuid()); /* drop special privs. */
286 if (fullname)
287 args[0] = _PATH_CHFN;
288 else
289 args[0] = _PATH_CHSH;
290
291 for (i = optind, j = 1; (i < argc) && (j < 99); i++, j++)
292 args[j] = argv[i];
293
294 args[j] = NULL;
295 execv(args[0], args);
296 errsv = errno;
297 fprintf(stderr, _("Can't exec %s: %s\n"), args[0], strerror(errsv));
298 exit(1);
299 }
300
301 switch (argc - optind) {
302 case 0:
303 /* Why use getlogin()? Some systems allow having several
304 usernames with the same uid, especially several root accounts.
305 One changes the password for the username, not the uid. */
306 if ( !(user = getlogin()) || !*user ) {
307 if ( !(pe = getpwuid( getuid() )) ) {
308 pexit(_("Cannot find login name"));
309 } else
310 user = pe->pw_name;
311 }
312 break;
313 case 1:
314 if(gotuid) {
315 printf(_("Only root can change the password for others.\n"));
316 exit (1);
317 } else
318 user = argv[optind];
319 break;
320 case 2:
321 if(gotuid) {
322 printf(_("Only root can change the password for others.\n"));
323 exit(1);
324 } else {
325 user = argv[optind];
326 pwdstr = argv[optind+1];
327 }
328 break;
329 default:
330 printf(_("Too many arguments.\n"));
331 exit (1);
332 } /* switch */
333
334 if(!(pe = getpwnam(user))) {
335 pexit(_("Can't find username anywhere. Is `%s' really a user?"), user);
336 }
337
338 if (!(is_local(user))) {
339 puts(_("Sorry, I can only change local passwords. Use yppasswd instead."));
340 exit(1);
341 }
342
343 /* if somebody got into changing utmp... */
344 if(gotuid && gotuid != pe->pw_uid) {
345 puts(_("UID and username does not match, imposter!"));
346 exit(1);
347 }
348
349 if ( !silent )
350 printf( _("Changing password for %s\n"), user );
351
352 if ( (gotuid && pe->pw_passwd && pe->pw_passwd[0])
353 || (!gotuid && !strcmp(user,"root")) ) {
354 oldstr = getpass(_("Enter old password: "));
355 if(strncmp(pe->pw_passwd, crypt(oldstr, pe->pw_passwd), 13)) {
356 puts(_("Illegal password, imposter."));
357 exit(1);
358 }
359 }
360
361 if ( pwdstr ) { /* already set on command line */
362 if ( !force_passwd && !check_passwd(pwdstr, pe->pw_passwd, user, pe->pw_gecos) )
363 exit (1);
364 } else {
365 /* password not set on command line by root, ask for it ... */
366
367 redo_it:
368 pwdstr = getpass(_("Enter new password: "));
369 if (pwdstr[0] == '\0') {
370 puts(_("Password not changed."));
371 exit(1);
372 }
373
374 if ( (gotuid || (!gotuid && !force_passwd))
375 && !check_passwd(pwdstr, pe->pw_passwd, user, pe->pw_gecos) )
376 goto redo_it;
377
378 strncpy(pwdstr1, pwdstr, 9);
379 pwdstr1[9] = 0;
380 pwdstr = getpass(_("Re-type new password: "));
381
382 if(strncmp(pwdstr, pwdstr1, 8)) {
383 puts(_("You misspelled it. Password not changed."));
384 exit(1);
385 }
386 } /* pwdstr i.e. password set on command line */
387
388 time(&tm); tm ^= getpid();
389 salt[0] = bin_to_ascii(tm & 0x3f);
390 salt[1] = bin_to_ascii((tm >> 6) & 0x3f);
391 cryptstr = crypt(pwdstr, salt);
392
393 if (pwdstr[0] == 0) cryptstr = "";
394
395 #ifdef LOGALL
396 openlog("passwd", 0, LOG_AUTH);
397 if (gotuid)
398 syslog(LOG_NOTICE,_("password changed, user %s"),user);
399 else {
400 if ( !strcmp(user, "root") )
401 syslog(LOG_WARNING,_("ROOT PASSWORD CHANGED"));
402 else
403 syslog(LOG_NOTICE,_("password changed by root, user %s"),user);
404 }
405 closelog();
406 #endif /* LOGALL */
407
408 pe->pw_passwd = cryptstr;
409 #ifdef DEBUG
410 printf (_("calling setpwnam to set password.\n"));
411 #else
412 if (setpwnam( pe ) < 0) {
413 perror( "setpwnam" );
414 printf( _("Password *NOT* changed. Try again later.\n" ));
415 exit( 1 );
416 }
417 #endif
418
419 if ( !silent )
420 printf(_("Password changed.\n"));
421 exit(0);
422 }