1
0

hook_access.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. #ifdef HAVE_CONFIG_H
  14. #include "config.h"
  15. #endif /* HAVE_CONFIG_H */
  16. #ifdef HAVE_GRP_H
  17. #include <grp.h>
  18. #endif
  19. #include "lub/string.h"
  20. #include "lub/db.h"
  21. #include "clish/shell.h"
  22. /*--------------------------------------------------------- */
  23. CLISH_HOOK_ACCESS(clish_hook_access)
  24. {
  25. bool_t allowed = BOOL_FALSE; /* assume the user is not allowed */
  26. #ifdef HAVE_GRP_H
  27. int num_groups;
  28. long ngroups_max;
  29. gid_t *group_list;
  30. int i;
  31. char *tmp_access, *full_access;
  32. char *saveptr;
  33. assert(access);
  34. full_access = lub_string_dup(access);
  35. ngroups_max = sysconf(_SC_NGROUPS_MAX) + 1;
  36. group_list = (gid_t *)malloc(ngroups_max * sizeof(gid_t));
  37. /* Get the groups for the current user */
  38. num_groups = getgroups(ngroups_max, group_list);
  39. assert(num_groups != -1);
  40. /* Now check these against the access provided */
  41. /* The external loop goes trough the list of valid groups */
  42. /* The allowed groups are indicated by a colon-separated (:) list. */
  43. for (tmp_access = strtok_r(full_access, ":", &saveptr);
  44. tmp_access; tmp_access = strtok_r(NULL, ":", &saveptr)) {
  45. /* The internal loop goes trough the system group list */
  46. for (i = 0; i < num_groups; i++) {
  47. struct group *ptr = lub_db_getgrgid(group_list[i]);
  48. if (!ptr)
  49. continue;
  50. if (0 == strcmp(ptr->gr_name, tmp_access)) {
  51. /* The current user is permitted to use this command */
  52. allowed = BOOL_TRUE;
  53. free(ptr);
  54. break;
  55. }
  56. free(ptr);
  57. }
  58. }
  59. lub_string_free(full_access);
  60. free(group_list);
  61. #endif
  62. return allowed;
  63. }
  64. /*--------------------------------------------------------- */