bb_pwd.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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, see the file LICENSE in this tarball.
  8. */
  9. #ifdef L_bb_getgrgid
  10. /* Hacked by Tito Ragusa (c) 2004 <farmatito@tiscali.it> to make it more
  11. * flexible :
  12. *
  13. * if bufsize is > 0 char *group cannot be set to NULL.
  14. * On success groupname is written on static allocated buffer
  15. * group (and a pointer to it is returned).
  16. * On failure gid as string is written to static allocated
  17. * buffer group and NULL is returned.
  18. * if bufsize is = 0 char *group can be set to NULL.
  19. * On success groupname is returned.
  20. * On failure NULL is returned.
  21. * if bufsize is < 0 char *group can be set to NULL.
  22. * On success groupname is returned.
  23. * On failure an error message is printed and
  24. * the program exits.
  25. */
  26. #include "libbb.h"
  27. #include "grp_.h"
  28. /* gets a groupname given a gid */
  29. char * bb_getgrgid(char *group, long gid, int bufsize)
  30. {
  31. struct group *mygroup = getgrgid(gid);
  32. return bb_getug(group, (mygroup) ?
  33. mygroup->gr_name : (char *)mygroup, gid, bufsize, 'g');
  34. }
  35. #endif /* L_bb_getgrgid */
  36. #ifdef L_bb_xgetgrnam
  37. #include <stdio.h>
  38. #include <string.h>
  39. #include "libbb.h"
  40. #include "pwd_.h"
  41. #include "grp_.h"
  42. /* returns a gid given a group name */
  43. long bb_xgetgrnam(const char *name)
  44. {
  45. struct group *mygroup;
  46. mygroup = getgrnam(name);
  47. if (mygroup==NULL)
  48. bb_error_msg_and_die("unknown group name: %s", name);
  49. return (mygroup->gr_gid);
  50. }
  51. #endif /* L_bb_xgetgrnam */
  52. #ifdef L_bb_xgetpwnam
  53. #include <stdio.h>
  54. #include <string.h>
  55. #include "libbb.h"
  56. #include "pwd_.h"
  57. #include "grp_.h"
  58. /* returns a uid given a username */
  59. long bb_xgetpwnam(const char *name)
  60. {
  61. struct passwd *myuser;
  62. myuser = getpwnam(name);
  63. if (myuser==NULL)
  64. bb_error_msg_and_die("unknown user name: %s", name);
  65. return myuser->pw_uid;
  66. }
  67. #endif /* L_bb_xgetpwnam */
  68. #ifdef L_bb_getpwuid
  69. /* Hacked by Tito Ragusa (c) 2004 <farmatito@tiscali.it> to make it more
  70. * flexible :
  71. *
  72. * if bufsize is > 0 char *name can not be set to NULL.
  73. * On success username is written on the static allocated
  74. * buffer name (and a pointer to it is returned).
  75. * On failure uid as string is written to the static
  76. * allocated buffer name and NULL is returned.
  77. * if bufsize is = 0 char *name can be set to NULL.
  78. * On success username is returned.
  79. * On failure NULL is returned.
  80. * if bufsize is < 0 char *name can be set to NULL
  81. * On success username is returned.
  82. * On failure an error message is printed and
  83. * the program exits.
  84. */
  85. #include "libbb.h"
  86. #include "pwd_.h"
  87. /* gets a username given a uid */
  88. char * bb_getpwuid(char *name, long uid, int bufsize)
  89. {
  90. struct passwd *myuser = getpwuid(uid);
  91. return bb_getug(name, (myuser) ?
  92. myuser->pw_name : (char *)myuser , uid, bufsize, 'u');
  93. }
  94. #endif /* L_bb_getpwuid */
  95. #ifdef L_bb_getug
  96. /*
  97. * if bufsize is > 0 char *buffer can not be set to NULL.
  98. * If idname is not NULL it is written on the static
  99. * allocated buffer (and a pointer to it is returned).
  100. * if idname is NULL, id as string is written to the static
  101. * allocated buffer and NULL is returned.
  102. * if bufsize is = 0 char *buffer can be set to NULL.
  103. * If idname exists a pointer to it is returned,
  104. * else NULL is returned.
  105. * if bufsize is < 0 char *buffer can be set to NULL.
  106. * If idname exists a pointer to it is returned,
  107. * else an error message is printed and the program exits.
  108. */
  109. #include <stdio.h>
  110. #include <assert.h>
  111. #include "libbb.h"
  112. /* internal function for bb_getpwuid and bb_getgrgid */
  113. char * bb_getug(char *buffer, char *idname, long id, int bufsize, char prefix)
  114. {
  115. if(bufsize > 0 ) {
  116. assert(buffer!=NULL);
  117. if(idname) {
  118. return safe_strncpy(buffer, idname, bufsize);
  119. }
  120. snprintf(buffer, bufsize, "%ld", id);
  121. } else if(bufsize < 0 && !idname) {
  122. bb_error_msg_and_die("unknown %cid %ld", prefix, id);
  123. }
  124. return idname;
  125. }
  126. #endif /* L_bb_getug */
  127. #ifdef L_get_ug_id
  128. /* indirect dispatcher for pwd helpers. */
  129. #include <stdlib.h>
  130. #include "libbb.h"
  131. unsigned long get_ug_id(const char *s,
  132. long (*__bb_getxxnam)(const char *))
  133. {
  134. unsigned long r;
  135. char *p;
  136. r = strtoul(s, &p, 10);
  137. if (*p || (s == p)) {
  138. r = __bb_getxxnam(s);
  139. }
  140. return r;
  141. }
  142. #endif /* L_get_ug_id */
  143. /* END CODE */
  144. /*
  145. Local Variables:
  146. c-file-style: "linux"
  147. c-basic-offset: 4
  148. tab-width: 4
  149. End:
  150. */