1
0

callback_access.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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 <assert.h>
  9. #include <string.h>
  10. #include <unistd.h>
  11. #include <sys/types.h>
  12. #include <grp.h>
  13. #include "internal.h"
  14. /*--------------------------------------------------------- */
  15. bool_t clish_access_callback(const clish_shell_t * shell, const char *access)
  16. {
  17. bool_t allowed = BOOL_FALSE;
  18. assert(access);
  19. /* There is an access restricion on this element */
  20. int num_groups;
  21. #define MAX_GROUPS 10
  22. gid_t group_list[MAX_GROUPS];
  23. int i;
  24. /* assume the worst */
  25. allowed = BOOL_FALSE;
  26. /* get the groups for the current user */
  27. num_groups = getgroups(MAX_GROUPS, group_list);
  28. assert(num_groups != -1);
  29. /* now check these against the access provided */
  30. for (i = 0; i < num_groups; i++) {
  31. struct group *ptr = getgrgid(group_list[i]);
  32. if (0 == strcmp(ptr->gr_name, access)) {
  33. /* The current user is permitted to use this command */
  34. allowed = BOOL_TRUE;
  35. break;
  36. }
  37. }
  38. return allowed;
  39. }
  40. /*--------------------------------------------------------- */