bb_pwd.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * password utility routines.
  4. *
  5. * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
  6. * Copyright (C) 2008 by Tito Ragusa <farmatito@tiscali.it>
  7. *
  8. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  9. */
  10. #include "libbb.h"
  11. /* TODO: maybe change API to return malloced data?
  12. * This will allow to stop using libc functions returning
  13. * pointers to static data (getpwuid)
  14. */
  15. struct passwd* FAST_FUNC xgetpwnam(const char *name)
  16. {
  17. struct passwd *pw = getpwnam(name);
  18. if (!pw)
  19. bb_error_msg_and_die("unknown user %s", name);
  20. return pw;
  21. }
  22. struct group* FAST_FUNC xgetgrnam(const char *name)
  23. {
  24. struct group *gr = getgrnam(name);
  25. if (!gr)
  26. bb_error_msg_and_die("unknown group %s", name);
  27. return gr;
  28. }
  29. struct passwd* FAST_FUNC xgetpwuid(uid_t uid)
  30. {
  31. /* Note: used in nofork applets (whoami), be careful not to leak anything */
  32. struct passwd *pw = getpwuid(uid);
  33. if (!pw)
  34. bb_error_msg_and_die("unknown uid %u", (unsigned)uid);
  35. return pw;
  36. }
  37. struct group* FAST_FUNC xgetgrgid(gid_t gid)
  38. {
  39. struct group *gr = getgrgid(gid);
  40. if (!gr)
  41. bb_error_msg_and_die("unknown gid %u", (unsigned)gid);
  42. return gr;
  43. }
  44. char* FAST_FUNC xuid2uname(uid_t uid)
  45. {
  46. /* Note: used in nofork applets (whoami), be careful not to leak anything */
  47. struct passwd *pw = xgetpwuid(uid);
  48. return pw->pw_name;
  49. }
  50. char* FAST_FUNC xgid2group(gid_t gid)
  51. {
  52. struct group *gr = xgetgrgid(gid);
  53. return gr->gr_name;
  54. }
  55. char* FAST_FUNC uid2uname(uid_t uid)
  56. {
  57. struct passwd *pw = getpwuid(uid);
  58. return (pw) ? pw->pw_name : NULL;
  59. }
  60. char* FAST_FUNC gid2group(gid_t gid)
  61. {
  62. struct group *gr = getgrgid(gid);
  63. return (gr) ? gr->gr_name : NULL;
  64. }
  65. char* FAST_FUNC uid2uname_utoa(uid_t uid)
  66. {
  67. char *name = uid2uname(uid);
  68. return (name) ? name : utoa(uid);
  69. }
  70. char* FAST_FUNC gid2group_utoa(gid_t gid)
  71. {
  72. char *name = gid2group(gid);
  73. return (name) ? name : utoa(gid);
  74. }
  75. long FAST_FUNC xuname2uid(const char *name)
  76. {
  77. struct passwd *myuser;
  78. myuser = xgetpwnam(name);
  79. return myuser->pw_uid;
  80. }
  81. long FAST_FUNC xgroup2gid(const char *name)
  82. {
  83. struct group *mygroup;
  84. mygroup = xgetgrnam(name);
  85. return mygroup->gr_gid;
  86. }
  87. unsigned long FAST_FUNC get_ug_id(const char *s,
  88. long FAST_FUNC (*xname2id)(const char *))
  89. {
  90. unsigned long r;
  91. r = bb_strtoul(s, NULL, 10);
  92. if (errno)
  93. return xname2id(s);
  94. return r;
  95. }