2
0

groupinfo.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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_GRP_H
  21. #include <grp.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. APR_DECLARE(fspr_status_t) fspr_gid_name_get(char **groupname, fspr_gid_t groupid,
  30. fspr_pool_t *p)
  31. {
  32. struct group *gr;
  33. #if APR_HAS_THREADS && defined(_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETGRGID_R)
  34. struct group grp;
  35. char grbuf[512];
  36. fspr_status_t rv;
  37. /* See comment in getpwnam_safe on error handling. */
  38. rv = getgrgid_r(groupid, &grp, grbuf, sizeof(grbuf), &gr);
  39. if (rv) {
  40. return rv;
  41. }
  42. if (gr == NULL) {
  43. return APR_ENOENT;
  44. }
  45. #else
  46. errno = 0;
  47. if ((gr = getgrgid(groupid)) == NULL) {
  48. return errno ? errno : APR_ENOENT;
  49. }
  50. #endif
  51. *groupname = fspr_pstrdup(p, gr->gr_name);
  52. return APR_SUCCESS;
  53. }
  54. APR_DECLARE(fspr_status_t) fspr_gid_get(fspr_gid_t *groupid,
  55. const char *groupname, fspr_pool_t *p)
  56. {
  57. struct group *gr;
  58. #if APR_HAS_THREADS && defined(_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETGRNAM_R)
  59. struct group grp;
  60. char grbuf[512];
  61. fspr_status_t rv;
  62. /* See comment in getpwnam_safe on error handling. */
  63. rv = getgrnam_r(groupname, &grp, grbuf, sizeof(grbuf), &gr);
  64. if (rv) {
  65. return rv;
  66. }
  67. if (gr == NULL) {
  68. return APR_ENOENT;
  69. }
  70. #else
  71. errno = 0;
  72. if ((gr = getgrnam(groupname)) == NULL) {
  73. return errno ? errno : APR_ENOENT;
  74. }
  75. #endif
  76. *groupid = gr->gr_gid;
  77. return APR_SUCCESS;
  78. }