3
0

uidgid_get.c 1007 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #include "busybox.h"
  2. unsigned uidgid_get(struct bb_uidgid_t *u, const char *ug /*, unsigned dogrp */)
  3. {
  4. struct passwd *pwd;
  5. struct group *gr;
  6. const char *g;
  7. /* g = 0; if (dogrp) g = strchr(ug, ':'); */
  8. g = strchr(ug, ':');
  9. if (g) {
  10. int sz = (++g) - ug;
  11. char buf[sz];
  12. safe_strncpy(buf, ug, sz);
  13. pwd = getpwnam(buf);
  14. } else
  15. pwd = getpwnam(ug);
  16. if (!pwd)
  17. return 0;
  18. u->uid = pwd->pw_uid;
  19. u->gid = pwd->pw_gid;
  20. if (g) {
  21. gr = getgrnam(g);
  22. if (!gr) return 0;
  23. u->gid = gr->gr_gid;
  24. }
  25. return 1;
  26. }
  27. #if 0
  28. #include <stdio.h>
  29. int main()
  30. {
  31. unsigned u;
  32. struct bb_uidgid_t ug;
  33. u = uidgid_get(&ug, "apache");
  34. printf("%u = %u:%u\n", u, ug.uid, ug.gid);
  35. ug.uid = ug.gid = 1111;
  36. u = uidgid_get(&ug, "apache");
  37. printf("%u = %u:%u\n", u, ug.uid, ug.gid);
  38. ug.uid = ug.gid = 1111;
  39. u = uidgid_get(&ug, "apache:users");
  40. printf("%u = %u:%u\n", u, ug.uid, ug.gid);
  41. ug.uid = ug.gid = 1111;
  42. u = uidgid_get(&ug, "apache:users");
  43. printf("%u = %u:%u\n", u, ug.uid, ug.gid);
  44. return 0;
  45. }
  46. #endif