adduser.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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, const char *group_name)
  62. {
  63. char *cmd;
  64. if (group_name) /* Add user to existing group */
  65. cmd = xasprintf("addgroup '%s' '%s'", p->pw_name, group_name);
  66. else /* Add user to his own group with the first free gid found in passwd_study */
  67. cmd = xasprintf("addgroup -g %u '%s'", (unsigned)p->pw_gid, p->pw_name);
  68. /* Warning: to be compatible with external addgroup programs we should use --gid instead */
  69. system(cmd);
  70. free(cmd);
  71. }
  72. static void passwd_wrapper(const char *login) NORETURN;
  73. static void passwd_wrapper(const char *login)
  74. {
  75. BB_EXECLP("passwd", "passwd", login, NULL);
  76. bb_error_msg_and_die("can't execute passwd, you must set password manually");
  77. }
  78. #if ENABLE_FEATURE_ADDUSER_LONG_OPTIONS
  79. static const char adduser_longopts[] ALIGN1 =
  80. "home\0" Required_argument "h"
  81. "gecos\0" Required_argument "g"
  82. "shell\0" Required_argument "s"
  83. "ingroup\0" Required_argument "G"
  84. "disabled-password\0" No_argument "D"
  85. "empty-password\0" No_argument "D"
  86. "system\0" No_argument "S"
  87. "no-create-home\0" No_argument "H"
  88. "uid\0" Required_argument "u"
  89. ;
  90. #endif
  91. /*
  92. * adduser will take a login_name as its first parameter.
  93. * home, shell, gecos:
  94. * can be customized via command-line parameters.
  95. */
  96. int adduser_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  97. int adduser_main(int argc UNUSED_PARAM, char **argv)
  98. {
  99. struct passwd pw;
  100. const char *usegroup = NULL;
  101. char *p;
  102. unsigned opts;
  103. #if ENABLE_FEATURE_ADDUSER_LONG_OPTIONS
  104. applet_long_options = adduser_longopts;
  105. #endif
  106. /* got root? */
  107. if (geteuid()) {
  108. bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
  109. }
  110. pw.pw_gecos = (char *)"Linux User,,,";
  111. pw.pw_shell = (char *)DEFAULT_SHELL;
  112. pw.pw_dir = NULL;
  113. /* exactly one non-option arg */
  114. /* disable interactive passwd for system accounts */
  115. opt_complementary = "=1:SD:u+";
  116. if (sizeof(pw.pw_uid) == sizeof(int)) {
  117. opts = getopt32(argv, "h:g:s:G:DSHu:", &pw.pw_dir, &pw.pw_gecos, &pw.pw_shell, &usegroup, &pw.pw_uid);
  118. } else {
  119. unsigned uid;
  120. opts = getopt32(argv, "h:g:s:G:DSHu:", &pw.pw_dir, &pw.pw_gecos, &pw.pw_shell, &usegroup, &uid);
  121. if (opts & OPT_UID) {
  122. pw.pw_uid = uid;
  123. }
  124. }
  125. argv += optind;
  126. /* fill in the passwd struct */
  127. pw.pw_name = argv[0];
  128. die_if_bad_username(pw.pw_name);
  129. if (!pw.pw_dir) {
  130. /* create string for $HOME if not specified already */
  131. pw.pw_dir = xasprintf("/home/%s", argv[0]);
  132. }
  133. pw.pw_passwd = (char *)"x";
  134. if (opts & OPT_SYSTEM_ACCOUNT) {
  135. if (!usegroup) {
  136. usegroup = "nogroup";
  137. }
  138. if (!(opts & OPT_SHELL)) {
  139. pw.pw_shell = (char *) "/bin/false";
  140. }
  141. }
  142. pw.pw_gid = usegroup ? xgroup2gid(usegroup) : -1; /* exits on failure */
  143. /* make sure everything is kosher and setup uid && maybe gid */
  144. passwd_study(&pw);
  145. p = xasprintf("x:%u:%u:%s:%s:%s",
  146. (unsigned) pw.pw_uid, (unsigned) pw.pw_gid,
  147. pw.pw_gecos, pw.pw_dir, pw.pw_shell);
  148. if (update_passwd(bb_path_passwd_file, pw.pw_name, p, NULL) < 0) {
  149. return EXIT_FAILURE;
  150. }
  151. if (ENABLE_FEATURE_CLEAN_UP)
  152. free(p);
  153. #if ENABLE_FEATURE_SHADOWPASSWDS
  154. /* /etc/shadow fields:
  155. * 1. username
  156. * 2. encrypted password
  157. * 3. last password change (unix date (unix time/24*60*60))
  158. * 4. minimum days required between password changes
  159. * 5. maximum days password is valid
  160. * 6. days before password is to expire that user is warned
  161. * 7. days after password expires that account is disabled
  162. * 8. unix date when login expires (i.e. when it may no longer be used)
  163. */
  164. /* fields: 2 3 4 5 6 78 */
  165. p = xasprintf("!:%u:0:99999:7:::", (unsigned)(time(NULL)) / (24*60*60));
  166. /* ignore errors: if file is missing we suppose admin doesn't want it */
  167. update_passwd(bb_path_shadow_file, pw.pw_name, p, NULL);
  168. if (ENABLE_FEATURE_CLEAN_UP)
  169. free(p);
  170. #endif
  171. /* add to group */
  172. addgroup_wrapper(&pw, usegroup);
  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. int mkdir_err = mkdir(pw.pw_dir, 0755);
  181. if (mkdir_err == 0) {
  182. /* New home. Copy /etc/skel to it */
  183. const char *args[] = {
  184. "chown", "-R",
  185. xasprintf("%u:%u", (int)pw.pw_uid, (int)pw.pw_gid),
  186. pw.pw_dir, NULL
  187. };
  188. /* Be silent on any errors (like: no /etc/skel) */
  189. logmode = LOGMODE_NONE;
  190. copy_file("/etc/skel", pw.pw_dir, FILEUTILS_RECUR);
  191. logmode = LOGMODE_STDIO;
  192. chown_main(4, (char**)args);
  193. }
  194. if ((mkdir_err != 0 && errno != EEXIST)
  195. || chown(pw.pw_dir, pw.pw_uid, pw.pw_gid) != 0
  196. || chmod(pw.pw_dir, 02755) != 0 /* set setgid bit on homedir */
  197. ) {
  198. bb_simple_perror_msg(pw.pw_dir);
  199. }
  200. }
  201. if (!(opts & OPT_DONT_SET_PASS)) {
  202. /* interactively set passwd */
  203. passwd_wrapper(pw.pw_name);
  204. }
  205. return EXIT_SUCCESS;
  206. }