1
0

callback_access.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. * clish_access_callback.c
  3. *
  4. *
  5. * callback hook to check whether the current user is a
  6. * member of the specified group (access string)
  7. */
  8. #include <stdlib.h>
  9. #include <unistd.h>
  10. #include <sys/types.h>
  11. #include <assert.h>
  12. #include <string.h>
  13. #include <grp.h>
  14. #include "lub/string.h"
  15. #include "lub/db.h"
  16. #include "internal.h"
  17. /*--------------------------------------------------------- */
  18. bool_t clish_access_callback(const clish_shell_t * shell, const char *access)
  19. {
  20. bool_t allowed = BOOL_FALSE; /* assume the user is not allowed */
  21. int num_groups;
  22. long ngroups_max;
  23. gid_t *group_list;
  24. int i;
  25. char *tmp_access, *full_access;
  26. char *saveptr;
  27. assert(access);
  28. full_access = lub_string_dup(access);
  29. ngroups_max = sysconf(_SC_NGROUPS_MAX) + 1;
  30. group_list = (gid_t *)malloc(ngroups_max * sizeof(gid_t));
  31. /* Get the groups for the current user */
  32. num_groups = getgroups(ngroups_max, group_list);
  33. assert(num_groups != -1);
  34. /* Now check these against the access provided */
  35. /* The external loop goes trough the list of valid groups */
  36. /* The allowed groups are indicated by a colon-separated (:) list. */
  37. for (tmp_access = strtok_r(full_access, ":", &saveptr);
  38. tmp_access; tmp_access = strtok_r(NULL, ":", &saveptr)) {
  39. /* The internal loop goes trough the system group list */
  40. for (i = 0; i < num_groups; i++) {
  41. struct group *ptr = lub_db_getgrgid(group_list[i]);
  42. if (0 == strcmp(ptr->gr_name, tmp_access)) {
  43. /* The current user is permitted to use this command */
  44. allowed = BOOL_TRUE;
  45. free(ptr);
  46. break;
  47. }
  48. free(ptr);
  49. }
  50. }
  51. lub_string_free(full_access);
  52. free(group_list);
  53. return allowed;
  54. }
  55. /*--------------------------------------------------------- */