groupadd.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* groupadd.c - create a new group
  2. *
  3. * Copyright 2013 Ashwini Kumar <ak.ashwini@gmail.com>
  4. * Copyright 2013 Kyungwan Han <asura321@gmail.com>
  5. *
  6. * See http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/groupadd.html
  7. USE_GROUPADD(NEWTOY(groupadd, "<1>2g#<0S", TOYFLAG_NEEDROOT|TOYFLAG_SBIN))
  8. USE_GROUPADD(OLDTOY(addgroup, groupadd, TOYFLAG_NEEDROOT|TOYFLAG_SBIN))
  9. config GROUPADD
  10. bool "groupadd"
  11. default n
  12. help
  13. usage: groupadd [-S] [-g GID] [USER] GROUP
  14. Add a group or add a user to a group
  15. -g GID Group id
  16. -S Create a system group
  17. */
  18. #define FOR_groupadd
  19. #include "toys.h"
  20. #define GROUP_PATH "/etc/group"
  21. #define SECURE_GROUP_PATH "/etc/gshadow"
  22. GLOBALS(
  23. long gid;
  24. )
  25. /* Add a new group to the system, if GID is given then that is validated
  26. * to be free, else a free GID is choosen by self.
  27. * SYSTEM IDs are considered in the range 100 ... 999
  28. * update_group(), updates the entries in /etc/group, /etc/gshadow files
  29. */
  30. static void new_group()
  31. {
  32. char *entry = NULL;
  33. if (FLAG(g)) {
  34. if (TT.gid > INT_MAX) error_exit("gid should be less than '%d' ", INT_MAX);
  35. if (getgrgid(TT.gid)) error_exit("group '%ld' is in use", TT.gid);
  36. } else {
  37. if (FLAG(S)) TT.gid = CFG_TOYBOX_UID_SYS;
  38. else TT.gid = CFG_TOYBOX_UID_USR;
  39. //find unused gid
  40. while (getgrgid(TT.gid)) TT.gid++;
  41. }
  42. entry = xmprintf("%s:%s:%ld:", *toys.optargs, "x", TT.gid);
  43. update_password(GROUP_PATH, *toys.optargs, entry);
  44. free(entry);
  45. entry = xmprintf("%s:%s::", *toys.optargs, "!");
  46. update_password(SECURE_GROUP_PATH, *toys.optargs, entry);
  47. free(entry);
  48. }
  49. void groupadd_main(void)
  50. {
  51. struct group *grp = NULL;
  52. char *entry = NULL;
  53. if (toys.optflags && toys.optc == 2)
  54. help_exit("options, user and group can't be together");
  55. if (toys.optc == 2) { //add user to group
  56. //toys.optargs[0]- user, toys.optargs[1] - group
  57. xgetpwnam(*toys.optargs);
  58. if (!(grp = getgrnam(toys.optargs[1])))
  59. error_exit("group '%s' does not exist", toys.optargs[1]);
  60. if (!grp->gr_mem) entry = xmprintf("%s", *toys.optargs);
  61. else {
  62. int i;
  63. for (i = 0; grp->gr_mem[i]; i++)
  64. if (!strcmp(grp->gr_mem[i], *toys.optargs)) return;
  65. entry = xstrdup("");
  66. for (i=0; grp->gr_mem[i]; i++) {
  67. entry = xrealloc(entry, strlen(entry) + strlen(grp->gr_mem[i]) + 2);
  68. strcat(entry, grp->gr_mem[i]);
  69. strcat(entry, ",");
  70. }
  71. entry = xrealloc(entry, strlen(entry) + strlen(*toys.optargs) + 1);
  72. strcat(entry, *toys.optargs);
  73. }
  74. update_password(GROUP_PATH, grp->gr_name, entry);
  75. update_password(SECURE_GROUP_PATH, grp->gr_name, entry);
  76. free(entry);
  77. } else { //new group to be created
  78. char *s = *toys.optargs;
  79. /* investigate the group to be created */
  80. if (getgrnam(s)) error_exit("'%s' in use", s);
  81. if (s[strcspn(s, ":/\n")] || strlen(s) > LOGIN_NAME_MAX)
  82. error_exit("bad name");
  83. new_group();
  84. }
  85. }