bb_pwd.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. *
  7. * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
  8. */
  9. #include "libbb.h"
  10. #define assert(x) ((void)0)
  11. /* internal function for bb_getpwuid and bb_getgrgid */
  12. /* Hacked by Tito Ragusa (c) 2004 <farmatito@tiscali.it> to make it more
  13. * flexible:
  14. *
  15. * bufsize > 0: If idname is not NULL it is copied to buffer,
  16. * and buffer is returned. Else id as string is written
  17. * to buffer, and NULL is returned.
  18. *
  19. * bufsize == 0: idname is returned.
  20. *
  21. * bufsize < 0: If idname is not NULL it is returned.
  22. * Else an error message is printed and the program exits.
  23. */
  24. static char* bb_getug(char *buffer, int bufsize, char *idname, long id, char prefix)
  25. {
  26. if (bufsize > 0) {
  27. assert(buffer != NULL);
  28. if (idname) {
  29. return safe_strncpy(buffer, idname, bufsize);
  30. }
  31. snprintf(buffer, bufsize, "%ld", id);
  32. } else if (bufsize < 0 && !idname) {
  33. bb_error_msg_and_die("unknown %cid %ld", prefix, id);
  34. }
  35. return idname;
  36. }
  37. /* bb_getpwuid, bb_getgrgid:
  38. * bb_getXXXid(buf, bufsz, id) - copy user/group name or id
  39. * as a string to buf, return user/group name or NULL
  40. * bb_getXXXid(NULL, 0, id) - return user/group name or NULL
  41. * bb_getXXXid(NULL, -1, id) - return user/group name or exit
  42. */
  43. /* gets a username given a uid */
  44. char* bb_getpwuid(char *name, int bufsize, long uid)
  45. {
  46. struct passwd *myuser = getpwuid(uid);
  47. return bb_getug(name, bufsize,
  48. (myuser ? myuser->pw_name : (char*)myuser),
  49. uid, 'u');
  50. }
  51. /* gets a groupname given a gid */
  52. char* bb_getgrgid(char *group, int bufsize, long gid)
  53. {
  54. struct group *mygroup = getgrgid(gid);
  55. return bb_getug(group, bufsize,
  56. (mygroup ? mygroup->gr_name : (char*)mygroup),
  57. gid, 'g');
  58. }
  59. /* returns a gid given a group name */
  60. long xgroup2gid(const char *name)
  61. {
  62. struct group *mygroup;
  63. mygroup = getgrnam(name);
  64. if (mygroup == NULL)
  65. bb_error_msg_and_die("unknown group name: %s", name);
  66. return mygroup->gr_gid;
  67. }
  68. /* returns a uid given a username */
  69. long xuname2uid(const char *name)
  70. {
  71. struct passwd *myuser;
  72. myuser = getpwnam(name);
  73. if (myuser == NULL)
  74. bb_error_msg_and_die("unknown user name: %s", name);
  75. return myuser->pw_uid;
  76. }
  77. unsigned long get_ug_id(const char *s,
  78. long (*xname2id)(const char *))
  79. {
  80. unsigned long r;
  81. r = bb_strtoul(s, NULL, 10);
  82. if (errno)
  83. return xname2id(s);
  84. return r;
  85. }