userinfo.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /* Licensed to the Apache Software Foundation (ASF) under one or more
  2. * contributor license agreements. See the NOTICE file distributed with
  3. * this work for additional information regarding copyright ownership.
  4. * The ASF licenses this file to You under the Apache License, Version 2.0
  5. * (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "fspr_strings.h"
  17. #include "fspr_portable.h"
  18. #include "fspr_user.h"
  19. #include "fspr_private.h"
  20. #ifdef HAVE_PWD_H
  21. #include <pwd.h>
  22. #endif
  23. #if APR_HAVE_SYS_TYPES_H
  24. #include <sys/types.h>
  25. #endif
  26. #if APR_HAVE_UNISTD_H
  27. #include <unistd.h> /* for _POSIX_THREAD_SAFE_FUNCTIONS */
  28. #endif
  29. #define APR_WANT_MEMFUNC
  30. #include "fspr_want.h"
  31. #define PWBUF_SIZE 512
  32. static fspr_status_t getpwnam_safe(const char *username,
  33. struct passwd *pw,
  34. char pwbuf[PWBUF_SIZE])
  35. {
  36. struct passwd *pwptr;
  37. #if APR_HAS_THREADS && defined(_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWNAM_R)
  38. fspr_status_t rv;
  39. /* POSIX defines getpwnam_r() et al to return the error number
  40. * rather than set errno, and requires pwptr to be set to NULL if
  41. * the entry is not found, imply that "not found" is not an error
  42. * condition; some implementations do return 0 with pwptr set to
  43. * NULL. */
  44. rv = getpwnam_r(username, pw, pwbuf, PWBUF_SIZE, &pwptr);
  45. if (rv) {
  46. return rv;
  47. }
  48. if (pwptr == NULL) {
  49. return APR_ENOENT;
  50. }
  51. #else
  52. /* Some platforms (e.g. FreeBSD 4.x) do not set errno on NULL "not
  53. * found" return values for the non-threadsafe function either. */
  54. errno = 0;
  55. if ((pwptr = getpwnam(username)) != NULL) {
  56. memcpy(pw, pwptr, sizeof *pw);
  57. }
  58. else {
  59. return errno ? errno : APR_ENOENT;
  60. }
  61. #endif
  62. return APR_SUCCESS;
  63. }
  64. APR_DECLARE(fspr_status_t) fspr_uid_homepath_get(char **dirname,
  65. const char *username,
  66. fspr_pool_t *p)
  67. {
  68. struct passwd pw;
  69. char pwbuf[PWBUF_SIZE];
  70. fspr_status_t rv;
  71. if ((rv = getpwnam_safe(username, &pw, pwbuf)) != APR_SUCCESS)
  72. return rv;
  73. #ifdef OS2
  74. /* Need to manually add user name for OS/2 */
  75. *dirname = fspr_pstrcat(p, pw.pw_dir, pw.pw_name, NULL);
  76. #else
  77. *dirname = fspr_pstrdup(p, pw.pw_dir);
  78. #endif
  79. return APR_SUCCESS;
  80. }
  81. APR_DECLARE(fspr_status_t) fspr_uid_current(fspr_uid_t *uid,
  82. fspr_gid_t *gid,
  83. fspr_pool_t *p)
  84. {
  85. *uid = getuid();
  86. *gid = getgid();
  87. return APR_SUCCESS;
  88. }
  89. APR_DECLARE(fspr_status_t) fspr_uid_get(fspr_uid_t *uid, fspr_gid_t *gid,
  90. const char *username, fspr_pool_t *p)
  91. {
  92. struct passwd pw;
  93. char pwbuf[PWBUF_SIZE];
  94. fspr_status_t rv;
  95. if ((rv = getpwnam_safe(username, &pw, pwbuf)) != APR_SUCCESS)
  96. return rv;
  97. *uid = pw.pw_uid;
  98. *gid = pw.pw_gid;
  99. return APR_SUCCESS;
  100. }
  101. APR_DECLARE(fspr_status_t) fspr_uid_name_get(char **username, fspr_uid_t userid,
  102. fspr_pool_t *p)
  103. {
  104. struct passwd *pw;
  105. #if APR_HAS_THREADS && defined(_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWUID_R)
  106. struct passwd pwd;
  107. char pwbuf[PWBUF_SIZE];
  108. fspr_status_t rv;
  109. rv = getpwuid_r(userid, &pwd, pwbuf, sizeof(pwbuf), &pw);
  110. if (rv) {
  111. return rv;
  112. }
  113. if (pw == NULL) {
  114. return APR_ENOENT;
  115. }
  116. #else
  117. errno = 0;
  118. if ((pw = getpwuid(userid)) == NULL) {
  119. return errno ? errno : APR_ENOENT;
  120. }
  121. #endif
  122. *username = fspr_pstrdup(p, pw->pw_name);
  123. return APR_SUCCESS;
  124. }