cryptpw.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * cryptpw.c - output a crypt(3)ed password to stdout.
  4. *
  5. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  6. *
  7. * Cooked from passwd.c by Thomas Lundquist <thomasez@zelow.no>
  8. * mkpasswd compatible options added by Bernhard Reutner-Fischer
  9. *
  10. * Licensed under GPLv2, see file LICENSE in this source tree.
  11. */
  12. //config:config CRYPTPW
  13. //config: bool "cryptpw"
  14. //config: default y
  15. //config: help
  16. //config: Encrypts the given password with the crypt(3) libc function
  17. //config: using the given salt.
  18. //config:
  19. //config:config MKPASSWD
  20. //config: bool "mkpasswd"
  21. //config: default y
  22. //config: help
  23. //config: Encrypts the given password with the crypt(3) libc function
  24. //config: using the given salt. Debian has this utility under mkpasswd
  25. //config: name. Busybox provides mkpasswd as an alias for cryptpw.
  26. //applet:IF_CRYPTPW(APPLET(cryptpw, BB_DIR_USR_BIN, BB_SUID_DROP))
  27. // APPLET_ODDNAME:name main location suid_type help
  28. //applet:IF_MKPASSWD(APPLET_ODDNAME(mkpasswd, cryptpw, BB_DIR_USR_BIN, BB_SUID_DROP, cryptpw))
  29. //kbuild:lib-$(CONFIG_CRYPTPW) += cryptpw.o
  30. //kbuild:lib-$(CONFIG_MKPASSWD) += cryptpw.o
  31. //usage:#define cryptpw_trivial_usage
  32. //usage: "[OPTIONS] [PASSWORD] [SALT]"
  33. /* We do support -s, we just don't mention it */
  34. //usage:#define cryptpw_full_usage "\n\n"
  35. //usage: "Crypt PASSWORD using crypt(3)\n"
  36. //usage: IF_LONG_OPTS(
  37. //usage: "\n -P,--password-fd=N Read password from fd N"
  38. /* //usage: "\n -s,--stdin Use stdin; like -P0" */
  39. //usage: "\n -m,--method=TYPE Encryption method"
  40. //usage: "\n -S,--salt=SALT"
  41. //usage: )
  42. //usage: IF_NOT_LONG_OPTS(
  43. //usage: "\n -P N Read password from fd N"
  44. /* //usage: "\n -s Use stdin; like -P0" */
  45. //usage: "\n -m TYPE Encryption method TYPE"
  46. //usage: "\n -S SALT"
  47. //usage: )
  48. #include "libbb.h"
  49. /* Debian has 'mkpasswd' utility, manpage says:
  50. NAME
  51. mkpasswd - Overfeatured front end to crypt(3)
  52. SYNOPSIS
  53. mkpasswd PASSWORD SALT
  54. ...
  55. OPTIONS
  56. -S, --salt=STRING
  57. Use the STRING as salt. It must not contain prefixes such as
  58. $1$.
  59. -R, --rounds=NUMBER
  60. Use NUMBER rounds. This argument is ignored if the method
  61. choosen does not support variable rounds. For the OpenBSD Blowfish
  62. method this is the logarithm of the number of rounds.
  63. -m, --method=TYPE
  64. Compute the password using the TYPE method. If TYPE is 'help'
  65. then the available methods are printed.
  66. -P, --password-fd=NUM
  67. Read the password from file descriptor NUM instead of using getpass(3).
  68. If the file descriptor is not connected to a tty then
  69. no other message than the hashed password is printed on stdout.
  70. -s, --stdin
  71. Like --password-fd=0.
  72. ENVIRONMENT
  73. $MKPASSWD_OPTIONS
  74. A list of options which will be evaluated before the ones
  75. specified on the command line.
  76. BUGS
  77. This programs suffers of a bad case of featuritis.
  78. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  79. Very true...
  80. cryptpw was in bbox before this gem, so we retain it, and alias mkpasswd
  81. to cryptpw. -a option (alias for -m) came from cryptpw.
  82. */
  83. int cryptpw_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  84. int cryptpw_main(int argc UNUSED_PARAM, char **argv)
  85. {
  86. char salt[MAX_PW_SALT_LEN];
  87. char *salt_ptr;
  88. char *password;
  89. const char *opt_m, *opt_S;
  90. int fd;
  91. #if ENABLE_LONG_OPTS
  92. static const char mkpasswd_longopts[] ALIGN1 =
  93. "stdin\0" No_argument "s"
  94. "password-fd\0" Required_argument "P"
  95. "salt\0" Required_argument "S"
  96. "method\0" Required_argument "m"
  97. ;
  98. applet_long_options = mkpasswd_longopts;
  99. #endif
  100. fd = STDIN_FILENO;
  101. opt_m = CONFIG_FEATURE_DEFAULT_PASSWD_ALGO;
  102. opt_S = NULL;
  103. /* at most two non-option arguments; -P NUM */
  104. opt_complementary = "?2";
  105. getopt32(argv, "sP:+S:m:a:", &fd, &opt_S, &opt_m, &opt_m);
  106. argv += optind;
  107. /* have no idea how to handle -s... */
  108. if (argv[0] && !opt_S)
  109. opt_S = argv[1];
  110. salt_ptr = crypt_make_pw_salt(salt, opt_m);
  111. if (opt_S)
  112. safe_strncpy(salt_ptr, opt_S, sizeof(salt) - (sizeof("$N$")-1));
  113. xmove_fd(fd, STDIN_FILENO);
  114. password = argv[0];
  115. if (!password) {
  116. /* Only mkpasswd, and only from tty, prompts.
  117. * Otherwise it is a plain read. */
  118. password = (ENABLE_MKPASSWD && isatty(STDIN_FILENO) && applet_name[0] == 'm')
  119. ? bb_ask_stdin("Password: ")
  120. : xmalloc_fgetline(stdin)
  121. ;
  122. /* may still be NULL on EOF/error */
  123. }
  124. if (password)
  125. puts(pw_encrypt(password, salt, 1));
  126. return EXIT_SUCCESS;
  127. }