aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2006-09-23 12:30:03 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2006-09-23 12:30:03 +0000
commitd78b433414fead8ef8de0da63484edaee7e3f80a (patch)
treef7bef7a8ab3a2d0e024f9d837aed372e10c36c2f
parent6429aabbf14be7ce1585fb07b1edb11795dbefc2 (diff)
downloadbusybox-w32-d78b433414fead8ef8de0da63484edaee7e3f80a.tar.gz
busybox-w32-d78b433414fead8ef8de0da63484edaee7e3f80a.tar.bz2
busybox-w32-d78b433414fead8ef8de0da63484edaee7e3f80a.zip
passwd: getopt_ulflags'isation
-rw-r--r--loginutils/passwd.c113
1 files changed, 52 insertions, 61 deletions
diff --git a/loginutils/passwd.c b/loginutils/passwd.c
index b0770e65c..f48f15f89 100644
--- a/loginutils/passwd.c
+++ b/loginutils/passwd.c
@@ -46,7 +46,8 @@ static int update_passwd(const struct passwd *pw, const char *crypt_pw)
46 snprintf(filename, sizeof filename, "%s", bb_path_passwd_file); 46 snprintf(filename, sizeof filename, "%s", bb_path_passwd_file);
47 } 47 }
48 48
49 if (((fp = fopen(filename, "r+")) == 0) || (fstat(fileno(fp), &sb))) { 49 fp = fopen(filename, "r+");
50 if (fp == 0 || fstat(fileno(fp), &sb)) {
50 /* return 0; */ 51 /* return 0; */
51 return 1; 52 return 1;
52 } 53 }
@@ -57,7 +58,7 @@ static int update_passwd(const struct passwd *pw, const char *crypt_pw)
57 lock.l_start = 0; 58 lock.l_start = 0;
58 lock.l_len = 0; 59 lock.l_len = 0;
59 if (fcntl(fileno(fp), F_SETLK, &lock) < 0) { 60 if (fcntl(fileno(fp), F_SETLK, &lock) < 0) {
60 fprintf(stderr, "%s: %s\n", filename, strerror(errno)); 61 bb_perror_msg("%s", filename);
61 return 1; 62 return 1;
62 } 63 }
63 lock.l_type = F_UNLCK; 64 lock.l_type = F_UNLCK;
@@ -126,88 +127,76 @@ static int update_passwd(const struct passwd *pw, const char *crypt_pw)
126 127
127int passwd_main(int argc, char **argv) 128int passwd_main(int argc, char **argv)
128{ 129{
130 enum {
131 OPT_algo = 0x1, /* -a - password algorithm */
132 OPT_lock = 0x2, /* -l - lock account */
133 OPT_unlock = 0x4, /* -u - unlock account */
134 OPT_delete = 0x8, /* -d - delete password */
135 OPT_lud = 0xe,
136 };
137 unsigned long opt;
138 char *opt_a;
129 int amroot; 139 int amroot;
130 char *cp; 140 char *cp;
131 char *np; 141 char *np;
132 char *name; 142 char *name;
133 char *myname; 143 char *myname;
134 int flag; 144 int algo = 1;
135 int algo = 1; /* -a - password algorithm */
136 int lflg = 0; /* -l - lock account */
137 int uflg = 0; /* -u - unlock account */
138 int dflg = 0; /* -d - delete password */
139 const struct passwd *pw; 145 const struct passwd *pw;
140 146
141 amroot = (getuid() == 0); 147 amroot = (getuid() == 0);
142 openlog("passwd", LOG_PID | LOG_CONS | LOG_NOWAIT, LOG_AUTH); 148 openlog("passwd", LOG_PID | LOG_CONS | LOG_NOWAIT, LOG_AUTH);
143 while ((flag = getopt(argc, argv, "a:dlu")) != EOF) { 149 opt = bb_getopt_ulflags(argc, argv, "a:lud", &opt_a);
144 switch (flag) { 150 argc -= optind;
145 case 'a': 151 argv += optind;
146 algo = get_algo(optarg); 152 if (opt & OPT_algo) algo = get_algo(opt_a); // -a
147 break; 153 if ((opt & OPT_lud) && (!argc || !amroot))
148 case 'd':
149 dflg++;
150 break;
151 case 'l':
152 lflg++;
153 break;
154 case 'u':
155 uflg++;
156 break;
157 default:
158 bb_show_usage();
159 }
160 }
161 myname = (char *) xstrdup(bb_getpwuid(NULL, getuid(), -1));
162 /* exits on error */
163 if (optind < argc) {
164 name = argv[optind];
165 } else {
166 name = myname;
167 }
168 if ((lflg || uflg || dflg) && (optind >= argc || !amroot)) {
169 bb_show_usage(); 154 bb_show_usage();
170 } 155
156 myname = xstrdup(bb_getpwuid(NULL, getuid(), -1));
157 name = myname;
158 if (argc) name = argv[0];
159
171 pw = getpwnam(name); 160 pw = getpwnam(name);
172 if (!pw) { 161 if (!pw) {
173 bb_error_msg_and_die("Unknown user %s", name); 162 bb_error_msg_and_die("unknown user %s", name);
174 } 163 }
175 if (!amroot && pw->pw_uid != getuid()) { 164 if (!amroot && pw->pw_uid != getuid()) {
176 syslog(LOG_WARNING, "can't change pwd for `%s'", name); 165 syslog(LOG_WARNING, "can't change pwd for '%s'", name);
177 bb_error_msg_and_die("Permission denied."); 166 bb_error_msg_and_die("permission denied");
178 } 167 }
179 if (ENABLE_FEATURE_SHADOWPASSWDS) { 168 if (ENABLE_FEATURE_SHADOWPASSWDS) {
180 struct spwd *sp = getspnam(name); 169 struct spwd *sp = getspnam(name);
181 if (!sp) bb_error_msg_and_die("Unknown user %s", name); 170 if (!sp) bb_error_msg_and_die("unknown user %s", name);
182 cp = sp->sp_pwdp; 171 cp = sp->sp_pwdp;
183 } else cp = pw->pw_passwd; 172 } else cp = pw->pw_passwd;
184 173
185 np = name; 174 np = name;
186 safe_strncpy(crypt_passwd, cp, sizeof(crypt_passwd)); 175 safe_strncpy(crypt_passwd, cp, sizeof(crypt_passwd));
187 if (!(dflg || lflg || uflg)) { 176 if (!(opt & OPT_lud)) {
188 if (!amroot) { 177 if (!amroot) {
189 if (cp[0] == '!') { 178 if (cp[0] == '!') {
190 syslog(LOG_WARNING, "password locked for `%s'", np); 179 syslog(LOG_WARNING, "password locked for '%s'", np);
191 bb_error_msg_and_die( "The password for `%s' cannot be changed.", np); 180 bb_error_msg_and_die("the password for %s cannot be changed", np);
192 } 181 }
193 } 182 }
194 printf("Changing password for %s\n", name); 183 printf("Changing password for %s\n", name);
195 if (new_password(pw, amroot, algo)) { 184 if (new_password(pw, amroot, algo)) {
196 bb_error_msg_and_die( "The password for %s is unchanged.", name); 185 bb_error_msg_and_die("the password for %s is unchanged", name);
197 } 186 }
198 } else if (lflg) { 187 } else if (opt & OPT_lock) {
199 if (crypt_passwd[0] != '!') { 188 if (crypt_passwd[0] != '!') {
200 memmove(&crypt_passwd[1], crypt_passwd, 189 memmove(&crypt_passwd[1], crypt_passwd,
201 sizeof crypt_passwd - 1); 190 sizeof crypt_passwd - 1);
202 crypt_passwd[sizeof crypt_passwd - 1] = '\0'; 191 crypt_passwd[sizeof crypt_passwd - 1] = '\0';
203 crypt_passwd[0] = '!'; 192 crypt_passwd[0] = '!';
204 } 193 }
205 } else if (uflg) { 194 } else if (opt & OPT_unlock) {
206 if (crypt_passwd[0] == '!') { 195 if (crypt_passwd[0] == '!') {
207 memmove(crypt_passwd, &crypt_passwd[1], 196 memmove(crypt_passwd, &crypt_passwd[1],
208 sizeof crypt_passwd - 1); 197 sizeof crypt_passwd - 1);
209 } 198 }
210 } else if (dflg) { 199 } else if (opt & OPT_delete) {
211 crypt_passwd[0] = '\0'; 200 crypt_passwd[0] = '\0';
212 } 201 }
213 set_filesize_limit(30000); 202 set_filesize_limit(30000);
@@ -217,15 +206,15 @@ int passwd_main(int argc, char **argv)
217 umask(077); 206 umask(077);
218 xsetuid(0); 207 xsetuid(0);
219 if (!update_passwd(pw, crypt_passwd)) { 208 if (!update_passwd(pw, crypt_passwd)) {
220 syslog(LOG_INFO, "password for `%s' changed by user `%s'", name, 209 syslog(LOG_INFO, "password for '%s' changed by user '%s'", name,
221 myname); 210 myname);
222 printf("Password changed.\n"); 211 puts("Password changed");
223 } else { 212 } else {
224 syslog(LOG_WARNING, "an error occurred updating the password file"); 213 syslog(LOG_WARNING, "cannot update password file");
225 bb_error_msg_and_die("An error occurred updating the password file."); 214 bb_error_msg_and_die("cannot update password file");
226 } 215 }
227 if (ENABLE_FEATURE_CLEAN_UP) free(myname); 216 if (ENABLE_FEATURE_CLEAN_UP) free(myname);
228 return (0); 217 return 0;
229} 218}
230 219
231 220
@@ -309,16 +298,17 @@ static int new_password(const struct passwd *pw, int amroot, int algo)
309 char pass[200]; 298 char pass[200];
310 299
311 if (!amroot && crypt_passwd[0]) { 300 if (!amroot && crypt_passwd[0]) {
312 if (!(clear = bb_askpass(0, "Old password:"))) { 301 clear = bb_askpass(0, "Old password:");
302 if (!clear) {
313 /* return -1; */ 303 /* return -1; */
314 return 1; 304 return 1;
315 } 305 }
316 cipher = pw_encrypt(clear, crypt_passwd); 306 cipher = pw_encrypt(clear, crypt_passwd);
317 if (strcmp(cipher, crypt_passwd) != 0) { 307 if (strcmp(cipher, crypt_passwd) != 0) {
318 syslog(LOG_WARNING, "incorrect password for `%s'", 308 syslog(LOG_WARNING, "incorrect password for '%s'",
319 pw->pw_name); 309 pw->pw_name);
320 bb_do_delay(FAIL_DELAY); 310 bb_do_delay(FAIL_DELAY);
321 fprintf(stderr, "Incorrect password.\n"); 311 puts("Incorrect password");
322 /* return -1; */ 312 /* return -1; */
323 return 1; 313 return 1;
324 } 314 }
@@ -328,10 +318,10 @@ static int new_password(const struct passwd *pw, int amroot, int algo)
328 } else { 318 } else {
329 orig[0] = '\0'; 319 orig[0] = '\0';
330 } 320 }
331 if (! (cp=bb_askpass(0, "Enter the new password (minimum of 5, maximum of 8 characters)\n" 321 cp = bb_askpass(0, "Enter the new password (minimum of 5, maximum of 8 characters).\n"
332 "Please use a combination of upper and lower case letters and numbers.\n" 322 "Please use a combination of upper and lower case letters and numbers.\n"
333 "Enter new password: "))) 323 "Enter new password: ");
334 { 324 if (!cp ) {
335 memset(orig, 0, sizeof orig); 325 memset(orig, 0, sizeof orig);
336 /* return -1; */ 326 /* return -1; */
337 return 1; 327 return 1;
@@ -341,19 +331,20 @@ static int new_password(const struct passwd *pw, int amroot, int algo)
341 /* if (!obscure(orig, pass, pw)) { */ 331 /* if (!obscure(orig, pass, pw)) { */
342 if (obscure(orig, pass, pw)) { 332 if (obscure(orig, pass, pw)) {
343 if (amroot) { 333 if (amroot) {
344 printf("\nWarning: weak password (continuing).\n"); 334 puts("\nWarning: weak password (continuing)");
345 } else { 335 } else {
346 /* return -1; */ 336 /* return -1; */
347 return 1; 337 return 1;
348 } 338 }
349 } 339 }
350 if (!(cp = bb_askpass(0, "Re-enter new password: "))) { 340 cp = bb_askpass(0, "Re-enter new password: ");
341 if (!cp) {
351 memset(orig, 0, sizeof orig); 342 memset(orig, 0, sizeof orig);
352 /* return -1; */ 343 /* return -1; */
353 return 1; 344 return 1;
354 } 345 }
355 if (strcmp(cp, pass)) { 346 if (strcmp(cp, pass)) {
356 fprintf(stderr, "Passwords do not match.\n"); 347 puts("Passwords do not match");
357 /* return -1; */ 348 /* return -1; */
358 return 1; 349 return 1;
359 } 350 }