bb_pwd.c 4.1 KB

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