groupinfo.c revision 251875
1259698Sdim/* Licensed to the Apache Software Foundation (ASF) under one or more
2259698Sdim * contributor license agreements.  See the NOTICE file distributed with
3259698Sdim * this work for additional information regarding copyright ownership.
4259698Sdim * The ASF licenses this file to You under the Apache License, Version 2.0
5259698Sdim * (the "License"); you may not use this file except in compliance with
6259698Sdim * the License.  You may obtain a copy of the License at
7259698Sdim *
8259698Sdim *     http://www.apache.org/licenses/LICENSE-2.0
9259698Sdim *
10259698Sdim * Unless required by applicable law or agreed to in writing, software
11259698Sdim * distributed under the License is distributed on an "AS IS" BASIS,
12259698Sdim * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13259698Sdim * See the License for the specific language governing permissions and
14259698Sdim * limitations under the License.
15259698Sdim */
16259698Sdim
17259698Sdim#include "apr_strings.h"
18259698Sdim#include "apr_portable.h"
19259698Sdim#include "apr_user.h"
20259698Sdim#include "apr_private.h"
21259698Sdim#ifdef HAVE_GRP_H
22259698Sdim#include <grp.h>
23259698Sdim#endif
24259698Sdim#if APR_HAVE_SYS_TYPES_H
25259698Sdim#include <sys/types.h>
26259698Sdim#endif
27259698Sdim#if APR_HAVE_UNISTD_H
28259698Sdim#include <unistd.h> /* for _POSIX_THREAD_SAFE_FUNCTIONS */
29259698Sdim#endif
30259698Sdim
31259698Sdim#define GRBUF_SIZE 8192
32259698Sdim
33259698SdimAPR_DECLARE(apr_status_t) apr_gid_name_get(char **groupname, apr_gid_t groupid,
34259698Sdim                                           apr_pool_t *p)
35259698Sdim{
36259698Sdim    struct group *gr;
37259698Sdim
38259698Sdim#if APR_HAS_THREADS && defined(_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETGRGID_R)
39259698Sdim    struct group grp;
40259698Sdim    char grbuf[GRBUF_SIZE];
41259698Sdim    apr_status_t rv;
42259698Sdim
43259698Sdim    /* See comment in getpwnam_safe on error handling. */
44259698Sdim    rv = getgrgid_r(groupid, &grp, grbuf, sizeof(grbuf), &gr);
45259698Sdim    if (rv) {
46259698Sdim        return rv;
47259698Sdim    }
48259698Sdim    if (gr == NULL) {
49259698Sdim        return APR_ENOENT;
50259698Sdim    }
51259698Sdim#else
52259698Sdim    errno = 0;
53259698Sdim    if ((gr = getgrgid(groupid)) == NULL) {
54259698Sdim        return errno ? errno : APR_ENOENT;
55259698Sdim    }
56259698Sdim#endif
57259698Sdim    *groupname = apr_pstrdup(p, gr->gr_name);
58259698Sdim    return APR_SUCCESS;
59259698Sdim}
60259698Sdim
61259698SdimAPR_DECLARE(apr_status_t) apr_gid_get(apr_gid_t *groupid,
62259698Sdim                                      const char *groupname, apr_pool_t *p)
63259698Sdim{
64259698Sdim    struct group *gr;
65259698Sdim
66259698Sdim#if APR_HAS_THREADS && defined(_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETGRNAM_R)
67259698Sdim    struct group grp;
68259698Sdim    char grbuf[GRBUF_SIZE];
69259698Sdim    apr_status_t rv;
70259698Sdim
71259698Sdim    /* See comment in getpwnam_safe on error handling. */
72259698Sdim    rv = getgrnam_r(groupname, &grp, grbuf, sizeof(grbuf), &gr);
73259698Sdim    if (rv) {
74259698Sdim        return rv;
75259698Sdim    }
76259698Sdim    if (gr == NULL) {
77259698Sdim        return APR_ENOENT;
78259698Sdim    }
79259698Sdim#else
80259698Sdim    errno = 0;
81259698Sdim    if ((gr = getgrnam(groupname)) == NULL) {
82259698Sdim        return errno ? errno : APR_ENOENT;
83259698Sdim    }
84#endif
85    *groupid = gr->gr_gid;
86    return APR_SUCCESS;
87}
88