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 source tree. 7 */ 8//config:config CHPASSWD 9//config: bool "chpasswd (18 kb)" 10//config: default y 11//config: help 12//config: Reads a file of user name and password pairs from standard input 13//config: and uses this information to update a group of existing users. 14//config: 15//config:config FEATURE_DEFAULT_PASSWD_ALGO 16//config: string "Default encryption method (passwd -a, cryptpw -m, chpasswd -c ALG)" 17//config: default "des" 18//config: depends on PASSWD || CRYPTPW || CHPASSWD 19//config: help 20//config: Possible choices are "d[es]", "m[d5]", "s[ha256]" or "sha512". 21 22//applet:IF_CHPASSWD(APPLET(chpasswd, BB_DIR_USR_SBIN, BB_SUID_DROP)) 23 24//kbuild:lib-$(CONFIG_CHPASSWD) += chpasswd.o 25 26//usage:#define chpasswd_trivial_usage 27//usage: IF_LONG_OPTS("[--md5|--encrypted|--crypt-method|--root]") IF_NOT_LONG_OPTS("[-m|-e|-c|-R]") 28//usage:#define chpasswd_full_usage "\n\n" 29//usage: "Read user:password from stdin and update /etc/passwd\n" 30//usage: IF_LONG_OPTS( 31//usage: "\n -e,--encrypted Supplied passwords are in encrypted form" 32//usage: "\n -m,--md5 Encrypt using md5, not des" 33//usage: "\n -c,--crypt-method ALG "CRYPT_METHODS_HELP_STR 34//usage: "\n -R,--root DIR Directory to chroot into" 35//usage: ) 36//usage: IF_NOT_LONG_OPTS( 37//usage: "\n -e Supplied passwords are in encrypted form" 38//usage: "\n -m Encrypt using md5, not des" 39//usage: "\n -c ALG "CRYPT_METHODS_HELP_STR 40//usage: "\n -R DIR Directory to chroot into" 41//usage: ) 42 43#include "libbb.h" 44 45#if ENABLE_LONG_OPTS 46static const char chpasswd_longopts[] ALIGN1 = 47 "encrypted\0" No_argument "e" 48 "md5\0" No_argument "m" 49 "crypt-method\0" Required_argument "c" 50 "root\0" Required_argument "R" 51 ; 52#endif 53 54#define OPT_ENC 1 55#define OPT_MD5 2 56 57int chpasswd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; 58int chpasswd_main(int argc UNUSED_PARAM, char **argv) 59{ 60 char *name; 61 const char *algo = CONFIG_FEATURE_DEFAULT_PASSWD_ALGO; 62 const char *root = NULL; 63 int opt; 64 65 if (getuid() != 0) 66 bb_error_msg_and_die(bb_msg_perm_denied_are_you_root); 67 68 opt = getopt32long(argv, "^" "emc:R:" "\0" "m--ec:e--mc:c--em", 69 chpasswd_longopts, 70 &algo, &root 71 ); 72 73 if (root) { 74 xchroot(root); 75 } 76 77 while ((name = xmalloc_fgetline(stdin)) != NULL) { 78 char *free_me; 79 char *pass; 80 int rc; 81 82 pass = strchr(name, ':'); 83 if (!pass) 84 bb_error_msg_and_die("missing new password"); 85 *pass++ = '\0'; 86 87 xuname2uid(name); /* dies if there is no such user */ 88 89 free_me = NULL; 90 if (!(opt & OPT_ENC)) { 91 char salt[MAX_PW_SALT_LEN]; 92 93 if (opt & OPT_MD5) { 94 /* Force MD5 if the -m flag is set */ 95 algo = "md5"; 96 } 97 98 crypt_make_pw_salt(salt, algo); 99 free_me = pass = pw_encrypt(pass, salt, 0); 100 } 101 102 /* This is rather complex: if user is not found in /etc/shadow, 103 * we try to find & change his passwd in /etc/passwd */ 104#if ENABLE_FEATURE_SHADOWPASSWDS 105 rc = update_passwd(bb_path_shadow_file, name, pass, NULL); 106 if (rc > 0) /* password in /etc/shadow was updated */ 107 pass = (char*)"x"; 108 if (rc >= 0) 109 /* 0 = /etc/shadow missing (not an error), >0 = passwd changed in /etc/shadow */ 110#endif 111 rc = update_passwd(bb_path_passwd_file, name, pass, NULL); 112 /* LOGMODE_BOTH logs to syslog also */ 113 logmode = LOGMODE_BOTH; 114 if (rc < 0) 115 bb_error_msg_and_die("an error occurred updating password for %s", name); 116 if (rc) 117 bb_error_msg("password for '%s' changed", name); 118 logmode = LOGMODE_STDIO; 119 free(name); 120 free(free_me); 121 } 122 return EXIT_SUCCESS; 123} 124