diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2007-07-20 21:29:49 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2007-07-20 21:29:49 +0000 |
commit | 1ec5eaecba0a0323f214825b83fabcc18a41884d (patch) | |
tree | d2dee2f7f0d1e5f3dd62be4f6cc4a6d5e274ac5c /loginutils/chpasswd.c | |
parent | 21d1014b5b91d1a1319273945291b7a9f4717827 (diff) | |
download | busybox-w32-1ec5eaecba0a0323f214825b83fabcc18a41884d.tar.gz busybox-w32-1ec5eaecba0a0323f214825b83fabcc18a41884d.tar.bz2 busybox-w32-1ec5eaecba0a0323f214825b83fabcc18a41884d.zip |
chpasswd: now with svn add
Diffstat (limited to '')
-rw-r--r-- | loginutils/chpasswd.c | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/loginutils/chpasswd.c b/loginutils/chpasswd.c new file mode 100644 index 000000000..124fc86e2 --- /dev/null +++ b/loginutils/chpasswd.c | |||
@@ -0,0 +1,77 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
2 | /* | ||
3 | * chpasswd.c | ||
4 | * | ||
5 | * Written for SLIND (from passwd.c) by Alexander Shishkin <virtuoso@slind.org> | ||
6 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. | ||
7 | */ | ||
8 | |||
9 | #include "libbb.h" | ||
10 | |||
11 | #if ENABLE_GETOPT_LONG | ||
12 | #include <getopt.h> | ||
13 | |||
14 | static const struct option chpasswd_opts[] = { | ||
15 | { "encrypted", no_argument, NULL, 'e' }, | ||
16 | { "md5", no_argument, NULL, 'm' }, | ||
17 | { NULL, 0, NULL, 0 } | ||
18 | }; | ||
19 | #endif | ||
20 | |||
21 | #define OPT_ENC 1 | ||
22 | #define OPT_MD5 2 | ||
23 | |||
24 | int chpasswd_main(int argc, char **argv); | ||
25 | int chpasswd_main(int argc, char **argv) | ||
26 | { | ||
27 | char *name, *pass; | ||
28 | char salt[sizeof("$N$XXXXXXXX")]; | ||
29 | int opt, rc; | ||
30 | int rnd = rnd; /* we *want* it to be non-initialized! */ | ||
31 | const char *pwfile = bb_path_passwd_file; | ||
32 | |||
33 | if (getuid() != 0) | ||
34 | bb_error_msg_and_die(bb_msg_perm_denied_are_you_root); | ||
35 | |||
36 | #if ENABLE_FEATURE_SHADOWPASSWDS | ||
37 | if (access(bb_path_shadow_file, F_OK) == 0) | ||
38 | pwfile = bb_path_shadow_file; | ||
39 | #endif | ||
40 | |||
41 | opt_complementary = "m--e"; | ||
42 | USE_GETOPT_LONG(applet_long_options = chpasswd_opts;) | ||
43 | opt = getopt32(argc, argv, "em"); | ||
44 | |||
45 | while ((name = xmalloc_getline(stdin)) != NULL) { | ||
46 | pass = strchr(name, ':'); | ||
47 | if (!pass) | ||
48 | bb_error_msg_and_die("missing new password"); | ||
49 | *pass++ = '\0'; | ||
50 | |||
51 | //if (!getpwnam(name)) | ||
52 | // bb_error_msg_and_die("unknown user %s", name); | ||
53 | |||
54 | if (!(opt & OPT_ENC)) { | ||
55 | rnd = crypt_make_salt(salt, 1, rnd); | ||
56 | if (opt & OPT_MD5) { | ||
57 | strcpy(salt, "$1$"); | ||
58 | rnd = crypt_make_salt(salt + 3, 4, rnd); | ||
59 | } | ||
60 | pass = pw_encrypt(pass, salt); | ||
61 | } | ||
62 | |||
63 | rc = update_passwd(pwfile, name, pass); | ||
64 | /* LOGMODE_BOTH logs to syslog */ | ||
65 | logmode = LOGMODE_BOTH; | ||
66 | if (rc < 0) | ||
67 | bb_error_msg_and_die("an error occurred updating %s", pwfile); | ||
68 | if (rc > 0) | ||
69 | bb_info_msg("Password for '%s' changed", name); | ||
70 | else | ||
71 | bb_info_msg("User '%s' not found", name); | ||
72 | logmode = LOGMODE_STDIO; | ||
73 | free(name); | ||
74 | } | ||
75 | |||
76 | return 0; | ||
77 | } | ||