]>
git.ipfire.org Git - thirdparty/util-linux.git/blob - login-utils/passwd.c
2 * passwd.c - change password on an account
4 * Initially written for Linux by Peter Orbaek <poe@daimi.aau.dk>
5 * Currently maintained at ftp://ftp.daimi.aau.dk/pub/linux/poe/
7 Hacked by Alvaro Martinez Echevarria, alvaro@enano.etsit.upm.es,
8 to allow peaceful coexistence with yp. Nov 94.
10 Hacked to allow root to set passwd from command line.
11 by Arpad Magossanyi (mag@tas.vein.hu)
13 Hacked by Peter Breitenlohner, peb@mppmu.mpg.de,
14 moved Alvaro's changes to setpwnam.c (so they get used
15 by chsh and chfn as well). Oct 5, 96.
20 * Sun Oct 15 13:18:34 1995 Martin Schulze <joey@finlandia.infodrom.north.de>
22 * I have completely rewritten the whole argument handlig (what?)
23 * to support two things. First I wanted "passwd $user $pw" to
24 * work and second I wanted simplicity checks to be done for
25 * root, too. Only root can turn this of using the -f
26 * switch. Okay, I started with this to support -V version
27 * information, but one thing comes to the next. *sigh*
28 * In a later step perhaps we'll be able to support shadow
31 * I have also included a DEBUG mode (-DDEBUG) to test the
32 * argument handling _without_ any write attempt to
35 * If you're paranoid about security on your system, you may want
36 * to add -DLOGALL to CFLAGS. This will turn on additional syslog
37 * logging of every password change. (user changes are logged as
38 * auth.notice, but changing root's password is logged as
39 * auth.warning. (Of course, the password itself is not logged.)
43 * Usage: passwd [username [password]]
44 * Only root may use the one and two argument forms.
47 #include <sys/types.h>
61 #include <sys/resource.h>
63 #if defined (__GNU_LIBRARY__) && __GNU_LIBRARY__ > 1
68 # include "../version.h"
70 char version
[] = "admutil 1.18, 15-Oct-95";
74 # define _PATH_CHFN "/usr/bin/chfn"
75 # define _PATH_CHSH "/usr/bin/chsh"
84 extern int is_local(char *); /* islocal.c */
85 extern int setpwnam(struct passwd
*); /* setpwnam.c */
87 #define ascii_to_bin(c) ((c)>='a'?(c-59):(c)>='A'?((c)-53):(c)-'.')
88 #define bin_to_ascii(c) ((c)>=38?((c)-38+'a'):(c)>=12?((c)-12+'A'):(c)+'.')
96 vfprintf(stderr
, str
, vlst
);
97 fprintf(stderr
, ": ");
104 * Do various checks for stupid passwords here...
106 * This would probably be the best place for checking against
110 int check_passwd_string(char *passwd
, char *string
)
116 /* test for string at the beginning of passwd */
117 for (p
= passwd
, q
= string
; *q
&& *p
; q
++, p
++) {
118 if(tolower(*p
) != tolower(*q
)) {
124 /* test for reverse string at the beginning of passwd */
125 for (p
= passwd
, q
= string
+ strlen(string
)-1;
126 *p
&& q
>= string
; p
++, q
--) {
127 if(tolower(*p
) != tolower(*q
)) {
133 /* test for string at the end of passwd */
134 for (p
= passwd
+ strlen(passwd
)-1, q
= string
+ strlen(string
)-1;
135 q
>= string
&& p
>= passwd
; q
--, p
--) {
136 if(tolower(*p
) != tolower(*q
)) {
142 /* test for reverse string at the beginning of passwd */
143 for (p
= passwd
+ strlen(passwd
)-1, q
= string
;
144 p
>= passwd
&& *q
; p
--, q
++) {
145 if(tolower(*p
) != tolower(*q
)) {
157 int check_passwd(char *passwd
, char *oldpasswd
, char *user
, char *gecos
)
159 int ucase
, lcase
, digit
, other
;
162 if ( (strlen(passwd
) < 6) ) {
163 printf("The password must have at least 6 characters, try again.\n");
167 other
= digit
= ucase
= lcase
= 0;
168 for (p
= passwd
; *p
; p
++) {
169 ucase
= ucase
|| isupper(*p
);
170 lcase
= lcase
|| islower(*p
);
171 digit
= digit
|| isdigit(*p
);
172 other
= other
|| !isalnum(*p
);
175 if ( (other
+ digit
+ ucase
+ lcase
) < 2) {
176 printf("The password must contain characters out of two of the following\n");
177 printf("classes: upper and lower case letters, digits and non alphanumeric\n");
178 printf("characters. See passwd(1) for more information.\n");
182 if ( oldpasswd
[0] && !strncmp(oldpasswd
, crypt(passwd
, oldpasswd
), 13) ) {
183 printf("You cannot reuse the old password.\n");
187 if ( !check_passwd_string(passwd
, user
) ) {
188 printf("Please don't use something like your username as password!\n");
192 /* check against realname */
193 if ( (c
= index(gecos
, ',')) ) {
194 if ( c
-gecos
&& (g
= (char *)malloc (c
-gecos
+1)) ) {
195 strncpy (g
, gecos
, c
-gecos
);
197 while ( (c
=rindex(g
, ' ')) ) {
198 if ( !check_passwd_string(passwd
, c
+1) ) {
199 printf("Please don't use something like your realname as password!\n");
205 if ( !check_passwd_string(passwd
, g
) ) {
206 printf("Please don't use something like your realname as password!\n");
215 * if ( !check_password_dict(passwd) ) ...
223 printf ("Usage: passwd [username [password]]\n");
224 printf("Only root may use the one and two argument forms.\n");
233 uid_t gotuid
= getuid();
234 char *pwdstr
= NULL
, *cryptstr
, *oldstr
;
239 int force_passwd
= 0;
243 int fullname
= 0, shell
= 0;
244 static const struct option long_options
[] =
246 {"fullname", no_argument
, 0, 'f'},
247 {"shell", no_argument
, 0, 's'},
248 {"force", no_argument
, 0, 'o'},
249 {"quiet", no_argument
, 0, 'q'},
250 {"silent", no_argument
, 0, 'q'},
251 {"version", no_argument
, 0, 'v'},
256 while ( (c
= getopt_long(argc
, argv
, "foqsvV", long_options
, &opt_index
)) != -1 ) {
272 printf("%s\n", version
);
275 fprintf(stderr
, "Usage: passwd [-foqsvV] [user [password]]\n");
280 if (fullname
|| shell
) {
284 setuid(getuid()); /* drop special privs. */
286 args
[0] = _PATH_CHFN
;
288 args
[0] = _PATH_CHSH
;
290 for (i
= optind
, j
= 1; (i
< argc
) && (j
< 99); i
++, j
++)
294 execv(args
[0], args
);
295 fprintf(stderr
, "Can't exec %s: %s\n", args
[0], strerror(errno
));
299 switch (argc
- optind
) {
301 if ( !(user
= getlogin()) ) {
302 if ( !(pe
= getpwuid( getuid() )) ) {
303 pexit("Cannot find login name");
310 printf("Only root can change the password for others.\n");
317 printf("Only root can change the password for others.\n");
321 pwdstr
= argv
[optind
+1];
325 printf("Too many arguments.\n");
329 if(!(pe
= getpwnam(user
))) {
330 pexit("Can't find username anywhere. Is `%s' really a user?", user
);
333 if (!(is_local(user
))) {
334 puts("Sorry, I can only change local passwords. Use yppasswd instead.");
338 /* if somebody got into changing utmp... */
339 if(gotuid
&& gotuid
!= pe
->pw_uid
) {
340 puts("UID and username does not match, imposter!");
345 printf( "Changing password for %s\n", user
);
347 if ( (gotuid
&& pe
->pw_passwd
&& pe
->pw_passwd
[0])
348 || (!gotuid
&& !strcmp(user
,"root")) ) {
349 oldstr
= getpass("Enter old password: ");
350 if(strncmp(pe
->pw_passwd
, crypt(oldstr
, pe
->pw_passwd
), 13)) {
351 puts("Illegal password, imposter.");
356 if ( pwdstr
) { /* already set on command line */
357 if ( !force_passwd
&& !check_passwd(pwdstr
, pe
->pw_passwd
, user
, pe
->pw_gecos
) )
360 /* password not set on command line by root, ask for it ... */
363 pwdstr
= getpass("Enter new password: ");
364 if (pwdstr
[0] == '\0') {
365 puts("Password not changed.");
369 if ( (gotuid
|| (!gotuid
&& !force_passwd
))
370 && !check_passwd(pwdstr
, pe
->pw_passwd
, user
, pe
->pw_gecos
) )
373 strncpy(pwdstr1
, pwdstr
, 9);
375 pwdstr
= getpass("Re-type new password: ");
377 if(strncmp(pwdstr
, pwdstr1
, 8)) {
378 puts("You misspelled it. Password not changed.");
381 } /* pwdstr i.e. password set on command line */
383 time(&tm
); tm
^= getpid();
384 salt
[0] = bin_to_ascii(tm
& 0x3f);
385 salt
[1] = bin_to_ascii((tm
>> 6) & 0x3f);
386 cryptstr
= crypt(pwdstr
, salt
);
388 if (pwdstr
[0] == 0) cryptstr
= "";
391 openlog("passwd", 0, LOG_AUTH
);
393 syslog(LOG_NOTICE
,"password changed, user %s",user
);
395 if ( !strcmp(user
, "root") )
396 syslog(LOG_WARNING
,"ROOT PASSWORD CHANGED");
398 syslog(LOG_NOTICE
,"password changed by root, user %s",user
);
403 pe
->pw_passwd
= cryptstr
;
405 printf ("calling setpwnam to set password.\n");
407 if (setpwnam( pe
) < 0) {
408 perror( "setpwnam" );
409 printf( "Password *NOT* changed. Try again later.\n" );
415 printf("Password changed.\n");