bb_getpwuid, bb_getgrgid: change order of arguments to more intuitive one;
[platform/upstream/busybox.git] / loginutils / passwd.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
4  */
5
6 #include "libbb.h"
7 #include <syslog.h>
8
9
10 static void nuke_str(char *str)
11 {
12         if (str) memset(str, 0, strlen(str));
13 }
14
15 static char* new_password(const struct passwd *pw, uid_t myuid, int algo)
16 {
17         char salt[sizeof("$N$XXXXXXXX")]; /* "$N$XXXXXXXX" or "XX" */
18         char *orig = (char*)"";
19         char *newp = NULL;
20         char *cipher = NULL;
21         char *cp = NULL;
22         char *ret = NULL; /* failure so far */
23
24         if (myuid && pw->pw_passwd[0]) {
25                 orig = bb_askpass(0, "Old password:"); /* returns ptr to static */
26                 if (!orig)
27                         goto err_ret;
28                 cipher = pw_encrypt(orig, pw->pw_passwd); /* returns ptr to static */
29                 if (strcmp(cipher, pw->pw_passwd) != 0) {
30                         syslog(LOG_WARNING, "incorrect password for '%s'",
31                                 pw->pw_name);
32                         bb_do_delay(FAIL_DELAY);
33                         puts("Incorrect password");
34                         goto err_ret;
35                 }
36         }
37         orig = xstrdup(orig); /* or else bb_askpass() will destroy it */
38         newp = bb_askpass(0, "New password:"); /* returns ptr to static */
39         if (!newp)
40                 goto err_ret;
41         newp = xstrdup(newp); /* we are going to bb_askpass() again, so save it */
42         if (ENABLE_FEATURE_PASSWD_WEAK_CHECK
43          && obscure(orig, newp, pw) && myuid)
44                 goto err_ret; /* non-root is not allowed to have weak passwd */
45
46         cp = bb_askpass(0, "Retype password:");
47         if (!cp)
48                 goto err_ret;
49         if (strcmp(cp, newp)) {
50                 puts("Passwords don't match");
51                 goto err_ret;
52         }
53
54         crypt_make_salt(salt, 1, 0); /* des */
55         if (algo) { /* MD5 */
56                 strcpy(salt, "$1$");
57                 crypt_make_salt(salt + 3, 4, 0);
58         }
59         /* pw_encrypt returns ptr to static */
60         ret = xstrdup(pw_encrypt(newp, salt));
61         /* whee, success! */
62
63  err_ret:
64         nuke_str(orig);
65         if (ENABLE_FEATURE_CLEAN_UP) free(orig);
66         nuke_str(newp);
67         if (ENABLE_FEATURE_CLEAN_UP) free(newp);
68         nuke_str(cipher);
69         nuke_str(cp);
70         return ret;
71 }
72
73 int passwd_main(int argc, char **argv);
74 int passwd_main(int argc, char **argv)
75 {
76         enum {
77                 OPT_algo = 0x1, /* -a - password algorithm */
78                 OPT_lock = 0x2, /* -l - lock account */
79                 OPT_unlock = 0x4, /* -u - unlock account */
80                 OPT_delete = 0x8, /* -d - delete password */
81                 OPT_lud = 0xe,
82                 STATE_ALGO_md5 = 0x10,
83                 //STATE_ALGO_des = 0x20, not needed yet
84         };
85         unsigned opt;
86         int rc;
87         const char *opt_a = "";
88         const char *filename;
89         char *myname;
90         char *name;
91         char *newp;
92         struct passwd *pw;
93         uid_t myuid;
94         struct rlimit rlimit_fsize;
95         char c;
96
97 #if ENABLE_FEATURE_SHADOWPASSWDS
98         /* Using _r function to avoid pulling in static buffers */
99         struct spwd spw;
100         struct spwd *result;
101         char buffer[256];
102 #endif
103
104         logmode = LOGMODE_BOTH;
105         openlog(applet_name, LOG_NOWAIT, LOG_AUTH);
106         opt = getopt32(argc, argv, "a:lud", &opt_a);
107         //argc -= optind;
108         argv += optind;
109
110         if (strcasecmp(opt_a, "des") != 0) /* -a */
111                 opt |= STATE_ALGO_md5;
112         //else
113         //      opt |= STATE_ALGO_des;
114         myuid = getuid();
115         /* -l, -u, -d require root priv and username argument */
116         if ((opt & OPT_lud) && (myuid || !argv[0]))
117                 bb_show_usage();
118
119         /* Will complain and die if username not found */
120         myname = xstrdup(bb_getpwuid(NULL, -1, myuid));
121         name = argv[0] ? argv[0] : myname;
122
123         pw = getpwnam(name);
124         if (!pw) bb_error_msg_and_die("unknown user %s", name);
125         if (myuid && pw->pw_uid != myuid) {
126                 /* LOGMODE_BOTH */
127                 bb_error_msg_and_die("%s can't change password for %s", myname, name);
128         }
129
130         filename = bb_path_passwd_file;
131 #if ENABLE_FEATURE_SHADOWPASSWDS
132         if (getspnam_r(pw->pw_name, &spw, buffer, sizeof(buffer), &result)) {
133                 /* LOGMODE_BOTH */
134                 bb_error_msg("no record of %s in %s, using %s",
135                                 name, bb_path_shadow_file,
136                                 bb_path_passwd_file);
137         } else {
138                 filename = bb_path_shadow_file;
139                 pw->pw_passwd = spw.sp_pwdp;
140         }
141 #endif
142
143         /* Decide what the new password will be */
144         newp = NULL;
145         c = pw->pw_passwd[0] - '!';
146         if (!(opt & OPT_lud)) {
147                 if (myuid && !c) { /* passwd starts with '!' */
148                         /* LOGMODE_BOTH */
149                         bb_error_msg_and_die("cannot change "
150                                         "locked password for %s", name);
151                 }
152                 printf("Changing password for %s\n", name);
153                 newp = new_password(pw, myuid, opt & STATE_ALGO_md5);
154                 if (!newp) {
155                         logmode = LOGMODE_STDIO;
156                         bb_error_msg_and_die("password for %s is unchanged", name);
157                 }
158         } else if (opt & OPT_lock) {
159                 if (!c) goto skip; /* passwd starts with '!' */
160                 newp = xasprintf("!%s", pw->pw_passwd);
161         } else if (opt & OPT_unlock) {
162                 if (c) goto skip; /* not '!' */
163                 /* pw->pw_passwd pints to static storage,
164                  * strdup'ing to avoid nasty surprizes */
165                 newp = xstrdup(&pw->pw_passwd[1]);
166         } else if (opt & OPT_delete) {
167                 //newp = xstrdup("");
168                 newp = (char*)"";
169         }
170
171         rlimit_fsize.rlim_cur = rlimit_fsize.rlim_max = 512L * 30000;
172         setrlimit(RLIMIT_FSIZE, &rlimit_fsize);
173         signal(SIGHUP, SIG_IGN);
174         signal(SIGINT, SIG_IGN);
175         signal(SIGQUIT, SIG_IGN);
176         umask(077);
177         xsetuid(0);
178         rc = update_passwd(filename, name, newp);
179         logmode = LOGMODE_BOTH;
180         if (rc < 0)
181                 bb_error_msg_and_die("cannot update password file %s",
182                                 filename);
183         bb_info_msg("Password for %s changed by %s", name, myname);
184
185         //if (ENABLE_FEATURE_CLEAN_UP) free(newp);
186  skip:
187         if (!newp) {
188                 bb_error_msg_and_die("password for %s is already %slocked",
189                         name, (opt & OPT_unlock) ? "un" : "");
190         }
191         if (ENABLE_FEATURE_CLEAN_UP) free(myname);
192         return 0;
193 }