chpasswd.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. //applet:IF_CHPASSWD(APPLET(chpasswd, BB_DIR_USR_SBIN, BB_SUID_DROP))
  22. //kbuild:lib-$(CONFIG_CHPASSWD) += chpasswd.o
  23. //usage:#define chpasswd_trivial_usage
  24. //usage: IF_LONG_OPTS("[--md5|--encrypted|--crypt-method]") IF_NOT_LONG_OPTS("[-m|-e|-c]")
  25. //usage:#define chpasswd_full_usage "\n\n"
  26. //usage: "Read user:password from stdin and update /etc/passwd\n"
  27. //usage: IF_LONG_OPTS(
  28. //usage: "\n -e,--encrypted Supplied passwords are in encrypted form"
  29. //usage: "\n -m,--md5 Eencrypt using md5, not des"
  30. //usage: "\n -c,--crypt-method ALG "CRYPT_METHODS_HELP_STR
  31. //usage: )
  32. //usage: IF_NOT_LONG_OPTS(
  33. //usage: "\n -e Supplied passwords are in encrypted form"
  34. //usage: "\n -m Eencrypt using md5, not des"
  35. //usage: "\n -c ALG "CRYPT_METHODS_HELP_STR
  36. //usage: )
  37. #include "libbb.h"
  38. #if ENABLE_LONG_OPTS
  39. static const char chpasswd_longopts[] ALIGN1 =
  40. "encrypted\0" No_argument "e"
  41. "md5\0" No_argument "m"
  42. "crypt-method\0" Required_argument "c"
  43. ;
  44. #endif
  45. #define OPT_ENC 1
  46. #define OPT_MD5 2
  47. int chpasswd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  48. int chpasswd_main(int argc UNUSED_PARAM, char **argv)
  49. {
  50. char *name;
  51. const char *algo = CONFIG_FEATURE_DEFAULT_PASSWD_ALGO;
  52. int opt;
  53. if (getuid() != 0)
  54. bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
  55. opt = getopt32long(argv, "^" "emc:" "\0" "m--ec:e--mc:c--em",
  56. chpasswd_longopts,
  57. &algo
  58. );
  59. while ((name = xmalloc_fgetline(stdin)) != NULL) {
  60. char *free_me;
  61. char *pass;
  62. int rc;
  63. pass = strchr(name, ':');
  64. if (!pass)
  65. bb_error_msg_and_die("missing new password");
  66. *pass++ = '\0';
  67. xuname2uid(name); /* dies if there is no such user */
  68. free_me = NULL;
  69. if (!(opt & OPT_ENC)) {
  70. char salt[MAX_PW_SALT_LEN];
  71. if (opt & OPT_MD5) {
  72. /* Force MD5 if the -m flag is set */
  73. algo = "md5";
  74. }
  75. crypt_make_pw_salt(salt, algo);
  76. free_me = pass = pw_encrypt(pass, salt, 0);
  77. }
  78. /* This is rather complex: if user is not found in /etc/shadow,
  79. * we try to find & change his passwd in /etc/passwd */
  80. #if ENABLE_FEATURE_SHADOWPASSWDS
  81. rc = update_passwd(bb_path_shadow_file, name, pass, NULL);
  82. if (rc > 0) /* password in /etc/shadow was updated */
  83. pass = (char*)"x";
  84. if (rc >= 0)
  85. /* 0 = /etc/shadow missing (not an error), >0 = passwd changed in /etc/shadow */
  86. #endif
  87. rc = update_passwd(bb_path_passwd_file, name, pass, NULL);
  88. /* LOGMODE_BOTH logs to syslog also */
  89. logmode = LOGMODE_BOTH;
  90. if (rc < 0)
  91. bb_error_msg_and_die("an error occurred updating password for %s", name);
  92. if (rc)
  93. bb_error_msg("password for '%s' changed", name);
  94. logmode = LOGMODE_STDIO;
  95. free(name);
  96. free(free_me);
  97. }
  98. return EXIT_SUCCESS;
  99. }