]>
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)
15 * Sun Oct 15 13:18:34 1995 Martin Schulze <joey@finlandia.infodrom.north.de>
17 * I have completely rewritten the whole argument handling (what?)
18 * to support two things. First I wanted "passwd $user $pw" to
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)
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
30 * I have also included a DEBUG mode (-DDEBUG) to test the
31 * argument handling _without_ any write attempt to
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.)
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
48 * Usage: passwd [username [password]]
49 * Only root may use the one and two argument forms.
52 #include <sys/types.h>
66 #include <sys/resource.h>
75 # define _PATH_CHFN "/usr/bin/chfn"
76 # define _PATH_CHSH "/usr/bin/chsh"
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)+'.')
94 vfprintf(stderr
, str
, vlst
);
95 fprintf(stderr
, ": ");
102 * Do various checks for stupid passwords here...
104 * This would probably be the best place for checking against
108 check_passwd_string(char *passwd
, char *string
) {
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
)) {
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
)) {
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
)) {
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
)) {
155 check_passwd(char *passwd
, char *oldpasswd
, char *user
, char *gecos
) {
156 int ucase
, lcase
, digit
, other
;
159 if ( (strlen(passwd
) < 6) ) {
160 printf(_("The password must have at least 6 characters, try again.\n"));
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
);
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"));
179 if ( oldpasswd
[0] && !strncmp(oldpasswd
, crypt(passwd
, oldpasswd
), 13) ) {
180 printf(_("You cannot reuse the old password.\n"));
184 if ( !check_passwd_string(passwd
, user
) ) {
185 printf(_("Please don't use something like your username as password!\n"));
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
);
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"));
202 if ( !check_passwd_string(passwd
, g
) ) {
203 printf(_("Please don't use something like your realname as password!\n"));
212 * if ( !check_password_dict(passwd) ) ...
221 printf (_("Usage: passwd [username [password]]\n"));
222 printf(_("Only root may use the one and two argument forms.\n"));
227 main(int argc
, char *argv
[]) {
229 uid_t gotuid
= getuid();
230 char *pwdstr
= NULL
, *cryptstr
, *oldstr
;
235 int force_passwd
= 0;
239 int fullname
= 0, shell
= 0;
240 static const struct option long_options
[] =
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'},
252 setlocale(LC_ALL
, "");
253 bindtextdomain(PACKAGE
, LOCALEDIR
);
257 while ( (c
= getopt_long(argc
, argv
, "foqsvV", long_options
, &opt_index
)) != -1 ) {
273 printf("%s\n", util_linux_version
);
276 fprintf(stderr
, _("Usage: passwd [-foqsvV] [user [password]]\n"));
281 if (fullname
|| shell
) {
285 setuid(getuid()); /* drop special privs. */
287 args
[0] = _PATH_CHFN
;
289 args
[0] = _PATH_CHSH
;
291 for (i
= optind
, j
= 1; (i
< argc
) && (j
< 99); i
++, j
++)
295 execv(args
[0], args
);
297 fprintf(stderr
, _("Can't exec %s: %s\n"), args
[0], strerror(errsv
));
301 switch (argc
- optind
) {
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"));
315 printf(_("Only root can change the password for others.\n"));
322 printf(_("Only root can change the password for others.\n"));
326 pwdstr
= argv
[optind
+1];
330 printf(_("Too many arguments.\n"));
334 if(!(pe
= getpwnam(user
))) {
335 pexit(_("Can't find username anywhere. Is `%s' really a user?"), user
);
338 if (!(is_local(user
))) {
339 puts(_("Sorry, I can only change local passwords. Use yppasswd instead."));
343 /* if somebody got into changing utmp... */
344 if(gotuid
&& gotuid
!= pe
->pw_uid
) {
345 puts(_("UID and username does not match, imposter!"));
350 printf( _("Changing password for %s\n"), user
);
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."));
361 if ( pwdstr
) { /* already set on command line */
362 if ( !force_passwd
&& !check_passwd(pwdstr
, pe
->pw_passwd
, user
, pe
->pw_gecos
) )
365 /* password not set on command line by root, ask for it ... */
368 pwdstr
= getpass(_("Enter new password: "));
369 if (pwdstr
[0] == '\0') {
370 puts(_("Password not changed."));
374 if ( (gotuid
|| (!gotuid
&& !force_passwd
))
375 && !check_passwd(pwdstr
, pe
->pw_passwd
, user
, pe
->pw_gecos
) )
378 strncpy(pwdstr1
, pwdstr
, 9);
380 pwdstr
= getpass(_("Re-type new password: "));
382 if(strncmp(pwdstr
, pwdstr1
, 8)) {
383 puts(_("You misspelled it. Password not changed."));
386 } /* pwdstr i.e. password set on command line */
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
);
393 if (pwdstr
[0] == 0) cryptstr
= "";
396 openlog("passwd", 0, LOG_AUTH
);
398 syslog(LOG_NOTICE
,_("password changed, user %s"),user
);
400 if ( !strcmp(user
, "root") )
401 syslog(LOG_WARNING
,_("ROOT PASSWORD CHANGED"));
403 syslog(LOG_NOTICE
,_("password changed by root, user %s"),user
);
408 pe
->pw_passwd
= cryptstr
;
410 printf (_("calling setpwnam to set password.\n"));
412 if (setpwnam( pe
) < 0) {
413 perror( "setpwnam" );
414 printf( _("Password *NOT* changed. Try again later.\n" ));
420 printf(_("Password changed.\n"));