adduser.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * adduser - add users to /etc/passwd and /etc/shadow
  4. *
  5. * Copyright (C) 1999 by Lineo, inc. and John Beppu
  6. * Copyright (C) 1999,2000,2001 by John Beppu <beppu@codepoet.org>
  7. *
  8. * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
  9. */
  10. #include "libbb.h"
  11. #if CONFIG_LAST_SYSTEM_ID < CONFIG_FIRST_SYSTEM_ID
  12. #error Bad LAST_SYSTEM_ID or FIRST_SYSTEM_ID in .config
  13. #endif
  14. /* #define OPT_HOME (1 << 0) */ /* unused */
  15. /* #define OPT_GECOS (1 << 1) */ /* unused */
  16. #define OPT_SHELL (1 << 2)
  17. #define OPT_GID (1 << 3)
  18. #define OPT_DONT_SET_PASS (1 << 4)
  19. #define OPT_SYSTEM_ACCOUNT (1 << 5)
  20. #define OPT_DONT_MAKE_HOME (1 << 6)
  21. #define OPT_UID (1 << 7)
  22. /* We assume UID_T_MAX == INT_MAX */
  23. /* remix */
  24. /* recoded such that the uid may be passed in *p */
  25. static void passwd_study(struct passwd *p)
  26. {
  27. int max = UINT_MAX;
  28. if (getpwnam(p->pw_name)) {
  29. bb_error_msg_and_die("%s '%s' in use", "user", p->pw_name);
  30. /* this format string is reused in adduser and addgroup */
  31. }
  32. if (!(option_mask32 & OPT_UID)) {
  33. if (option_mask32 & OPT_SYSTEM_ACCOUNT) {
  34. p->pw_uid = CONFIG_FIRST_SYSTEM_ID;
  35. max = CONFIG_LAST_SYSTEM_ID;
  36. } else {
  37. p->pw_uid = CONFIG_LAST_SYSTEM_ID + 1;
  38. max = 64999;
  39. }
  40. }
  41. /* check for a free uid (and maybe gid) */
  42. while (getpwuid(p->pw_uid) || (p->pw_gid == (gid_t)-1 && getgrgid(p->pw_uid))) {
  43. if (option_mask32 & OPT_UID) {
  44. /* -u N, cannot pick uid other than N: error */
  45. bb_error_msg_and_die("%s '%s' in use", "uid", itoa(p->pw_uid));
  46. /* this format string is reused in adduser and addgroup */
  47. }
  48. if (p->pw_uid == max) {
  49. bb_error_msg_and_die("no %cids left", 'u');
  50. }
  51. p->pw_uid++;
  52. }
  53. if (p->pw_gid == (gid_t)-1) {
  54. p->pw_gid = p->pw_uid; /* new gid = uid */
  55. if (getgrnam(p->pw_name)) {
  56. bb_error_msg_and_die("%s '%s' in use", "group", p->pw_name);
  57. /* this format string is reused in adduser and addgroup */
  58. }
  59. }
  60. }
  61. static void addgroup_wrapper(struct passwd *p)
  62. {
  63. char *cmd;
  64. cmd = xasprintf("addgroup -g %u '%s'", (unsigned)p->pw_gid, p->pw_name);
  65. system(cmd);
  66. free(cmd);
  67. }
  68. static void passwd_wrapper(const char *login) NORETURN;
  69. static void passwd_wrapper(const char *login)
  70. {
  71. static const char prog[] ALIGN1 = "passwd";
  72. BB_EXECLP(prog, prog, login, NULL);
  73. bb_error_msg_and_die("can't execute %s, you must set password manually", prog);
  74. }
  75. #if ENABLE_FEATURE_ADDUSER_LONG_OPTIONS
  76. static const char adduser_longopts[] ALIGN1 =
  77. "home\0" Required_argument "h"
  78. "gecos\0" Required_argument "g"
  79. "shell\0" Required_argument "s"
  80. "ingroup\0" Required_argument "G"
  81. "disabled-password\0" No_argument "D"
  82. "empty-password\0" No_argument "D"
  83. "system\0" No_argument "S"
  84. "no-create-home\0" No_argument "H"
  85. "uid\0" Required_argument "u"
  86. ;
  87. #endif
  88. /*
  89. * adduser will take a login_name as its first parameter.
  90. * home, shell, gecos:
  91. * can be customized via command-line parameters.
  92. */
  93. int adduser_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  94. int adduser_main(int argc UNUSED_PARAM, char **argv)
  95. {
  96. struct passwd pw;
  97. const char *usegroup = NULL;
  98. char *p;
  99. unsigned opts;
  100. #if ENABLE_FEATURE_ADDUSER_LONG_OPTIONS
  101. applet_long_options = adduser_longopts;
  102. #endif
  103. /* got root? */
  104. if (geteuid()) {
  105. bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
  106. }
  107. pw.pw_gecos = (char *)"Linux User,,,";
  108. pw.pw_shell = (char *)DEFAULT_SHELL;
  109. pw.pw_dir = NULL;
  110. /* exactly one non-option arg */
  111. /* disable interactive passwd for system accounts */
  112. opt_complementary = "=1:SD:u+";
  113. if (sizeof(pw.pw_uid) == sizeof(int)) {
  114. opts = getopt32(argv, "h:g:s:G:DSHu:", &pw.pw_dir, &pw.pw_gecos, &pw.pw_shell, &usegroup, &pw.pw_uid);
  115. } else {
  116. unsigned uid;
  117. opts = getopt32(argv, "h:g:s:G:DSHu:", &pw.pw_dir, &pw.pw_gecos, &pw.pw_shell, &usegroup, &uid);
  118. if (opts & OPT_UID) {
  119. pw.pw_uid = uid;
  120. }
  121. }
  122. argv += optind;
  123. /* fill in the passwd struct */
  124. pw.pw_name = argv[0];
  125. die_if_bad_username(pw.pw_name);
  126. if (!pw.pw_dir) {
  127. /* create string for $HOME if not specified already */
  128. pw.pw_dir = xasprintf("/home/%s", argv[0]);
  129. }
  130. pw.pw_passwd = (char *)"x";
  131. if (opts & OPT_SYSTEM_ACCOUNT) {
  132. if (!usegroup) {
  133. usegroup = "nogroup";
  134. }
  135. if (!(opts & OPT_SHELL)) {
  136. pw.pw_shell = (char *) "/bin/false";
  137. }
  138. }
  139. pw.pw_gid = usegroup ? xgroup2gid(usegroup) : -1; /* exits on failure */
  140. /* make sure everything is kosher and setup uid && maybe gid */
  141. passwd_study(&pw);
  142. p = xasprintf("x:%u:%u:%s:%s:%s",
  143. (unsigned) pw.pw_uid, (unsigned) pw.pw_gid,
  144. pw.pw_gecos, pw.pw_dir, pw.pw_shell);
  145. if (update_passwd(bb_path_passwd_file, pw.pw_name, p, NULL) < 0) {
  146. return EXIT_FAILURE;
  147. }
  148. if (ENABLE_FEATURE_CLEAN_UP)
  149. free(p);
  150. #if ENABLE_FEATURE_SHADOWPASSWDS
  151. /* /etc/shadow fields:
  152. * 1. username
  153. * 2. encrypted password
  154. * 3. last password change (unix date (unix time/24*60*60))
  155. * 4. minimum days required between password changes
  156. * 5. maximum days password is valid
  157. * 6. days before password is to expire that user is warned
  158. * 7. days after password expires that account is disabled
  159. * 8. unix date when login expires (i.e. when it may no longer be used)
  160. */
  161. /* fields: 2 3 4 5 6 78 */
  162. p = xasprintf("!:%u:0:99999:7:::", (unsigned)(time(NULL)) / (24*60*60));
  163. /* ignore errors: if file is missing we suppose admin doesn't want it */
  164. update_passwd(bb_path_shadow_file, pw.pw_name, p, NULL);
  165. if (ENABLE_FEATURE_CLEAN_UP)
  166. free(p);
  167. #endif
  168. /* add to group */
  169. /* addgroup should be responsible for dealing w/ gshadow */
  170. /* if using a pre-existing group, don't create one */
  171. if (!usegroup)
  172. addgroup_wrapper(&pw);
  173. /* clear the umask for this process so it doesn't
  174. * screw up the permissions on the mkdir and chown. */
  175. umask(0);
  176. if (!(opts & OPT_DONT_MAKE_HOME)) {
  177. /* set the owner and group so it is owned by the new user,
  178. * then fix up the permissions to 2755. Can't do it before
  179. * since chown will clear the setgid bit */
  180. if (mkdir(pw.pw_dir, 0755)
  181. || chown(pw.pw_dir, pw.pw_uid, pw.pw_gid)
  182. || chmod(pw.pw_dir, 02755) /* set setgid bit on homedir */
  183. ) {
  184. bb_simple_perror_msg(pw.pw_dir);
  185. }
  186. }
  187. if (!(opts & OPT_DONT_SET_PASS)) {
  188. /* interactively set passwd */
  189. passwd_wrapper(pw.pw_name);
  190. }
  191. return 0;
  192. }