3
0

bb_pwd.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. /*
  12. * if bufsize is > 0 char *buffer cannot be set to NULL.
  13. * If idname is not NULL it is written on the static
  14. * allocated buffer (and a pointer to it is returned).
  15. * if idname is NULL, id as string is written to the static
  16. * allocated buffer and NULL is returned.
  17. * if bufsize is = 0 char *buffer can be set to NULL.
  18. * If idname exists a pointer to it is returned,
  19. * else NULL is returned.
  20. * if bufsize is < 0 char *buffer can be set to NULL.
  21. * If idname exists a pointer to it is returned,
  22. * else an error message is printed and the program exits.
  23. */
  24. /* internal function for bb_getpwuid and bb_getgrgid */
  25. static char* bb_getug(char *buffer, char *idname, long id, int bufsize, char prefix)
  26. {
  27. if (bufsize > 0 ) {
  28. assert(buffer != NULL);
  29. if (idname) {
  30. return safe_strncpy(buffer, idname, bufsize);
  31. }
  32. snprintf(buffer, bufsize, "%ld", id);
  33. } else if (bufsize < 0 && !idname) {
  34. bb_error_msg_and_die("unknown %cid %ld", prefix, id);
  35. }
  36. return idname;
  37. }
  38. /* Hacked by Tito Ragusa (c) 2004 <farmatito@tiscali.it> to make it more
  39. * flexible :
  40. *
  41. * if bufsize is > 0 char *group cannot be set to NULL.
  42. * On success groupname is written on static allocated buffer
  43. * group (and a pointer to it is returned).
  44. * On failure gid as string is written to static allocated
  45. * buffer group and NULL is returned.
  46. * if bufsize is = 0 char *group can be set to NULL.
  47. * On success groupname is returned.
  48. * On failure NULL is returned.
  49. * if bufsize is < 0 char *group can be set to NULL.
  50. * On success groupname is returned.
  51. * On failure an error message is printed and
  52. * the program exits.
  53. */
  54. /* gets a groupname given a gid */
  55. char* bb_getgrgid(char *group, long gid, int bufsize)
  56. {
  57. struct group *mygroup = getgrgid(gid);
  58. return bb_getug(group,
  59. mygroup ? mygroup->gr_name : (char *)mygroup,
  60. gid, bufsize, 'g');
  61. }
  62. /* returns a gid given a group name */
  63. long xgroup2gid(const char *name)
  64. {
  65. struct group *mygroup;
  66. mygroup = getgrnam(name);
  67. if (mygroup == NULL)
  68. bb_error_msg_and_die("unknown group name: %s", name);
  69. return mygroup->gr_gid;
  70. }
  71. /* returns a uid given a username */
  72. long xuname2uid(const char *name)
  73. {
  74. struct passwd *myuser;
  75. myuser = getpwnam(name);
  76. if (myuser == NULL)
  77. bb_error_msg_and_die("unknown user name: %s", name);
  78. return myuser->pw_uid;
  79. }
  80. /* Hacked by Tito Ragusa (c) 2004 <farmatito@tiscali.it> to make it more
  81. * flexible :
  82. *
  83. * if bufsize is > 0 char *name cannot be set to NULL.
  84. * On success username is written on the static allocated
  85. * buffer name (and a pointer to it is returned).
  86. * On failure uid as string is written to the static
  87. * allocated buffer name and NULL is returned.
  88. * if bufsize is = 0 char *name can be set to NULL.
  89. * On success username is returned.
  90. * On failure NULL is returned.
  91. * if bufsize is < 0 char *name can be set to NULL
  92. * On success username is returned.
  93. * On failure an error message is printed and
  94. * the program exits.
  95. */
  96. /* gets a username given a uid */
  97. char* bb_getpwuid(char *name, long uid, int bufsize)
  98. {
  99. struct passwd *myuser = getpwuid(uid);
  100. return bb_getug(name, myuser ? myuser->pw_name : (char *)myuser,
  101. uid, bufsize, 'u');
  102. }
  103. unsigned long get_ug_id(const char *s,
  104. long (*xname2id)(const char *))
  105. {
  106. unsigned long r;
  107. r = bb_strtoul(s, NULL, 10);
  108. if (errno)
  109. return xname2id(s);
  110. return r;
  111. }