summaryrefslogtreecommitdiff
path: root/src/providers/proxy/proxy_netgroup.c
blob: cdcb2a857a4426a06fb487fca27b7e19462d85f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
    SSSD

    Proxy netgroup handler

    Authors:

        Sumit Bose <sbose@redhat.com>

    Copyright (C) 2010 Red Hat

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "providers/proxy/proxy.h"
#include "util/util.h"

#define BUFLEN  1024

static errno_t make_netgroup_attr(struct __netgrent netgrent,
                                  struct sysdb_attrs *attrs)
{
    int ret;
    char *dummy;

    if (netgrent.type == group_val) {
        ret =sysdb_attrs_add_string(attrs, SYSDB_NETGROUP_MEMBER,
                                    netgrent.val.group);
        if (ret != EOK) {
            DEBUG(1, ("sysdb_attrs_add_string failed.\n"));
            return ret;
        }
    } else if (netgrent.type == triple_val) {
        dummy = talloc_asprintf(attrs, "(%s,%s,%s)", netgrent.val.triple.host,
                                netgrent.val.triple.user,
                                netgrent.val.triple.domain);
        if (dummy == NULL) {
            DEBUG(1, ("talloc_asprintf failed.\n"));
            return ENOMEM;
        }

        ret = sysdb_attrs_add_string(attrs, SYSDB_NETGROUP_TRIPLE, dummy);
        if (ret != EOK) {
            DEBUG(1, ("sysdb_attrs_add_string failed.\n"));
            return ret;
        }
    } else {
        DEBUG(1, ("Unknown netgrent entry type [%d].\n", netgrent.type));
        return EINVAL;
    }

    return EOK;
}

errno_t get_netgroup(struct proxy_id_ctx *ctx,
                     struct sysdb_ctx *sysdb,
                     struct sss_domain_info *dom,
                     const char *name)
{
    struct __netgrent result;
    enum nss_status status;
    char buffer[BUFLEN];
    int ret;
    TALLOC_CTX *tmp_ctx;
    struct sysdb_attrs *attrs;

    memset(&result, 0 ,sizeof(result));
    status = ctx->ops.setnetgrent(name, &result);
    if (status != NSS_STATUS_SUCCESS) {
        DEBUG(5, ("setnetgrent failed for netgroup [%s].\n", name));
        return EIO;
    }

    tmp_ctx = talloc_new(NULL);
    if (tmp_ctx == NULL) {
        DEBUG(1, ("talloc_new failed.\n"));
        return ENOMEM;
    }

    attrs = sysdb_new_attrs(tmp_ctx);
    if (attrs == NULL) {
        DEBUG(1, ("sysdb_new_attrs failed.\n"));
        return ENOMEM;
    }

    do {
        status = ctx->ops.getnetgrent_r(&result, buffer, BUFLEN, &ret);
        if (status != NSS_STATUS_SUCCESS && status != NSS_STATUS_RETURN) {
            DEBUG(1, ("getnetgrent_r failed for netgroup [%s]: [%d][%s].\n",
                      name, ret, strerror(ret)));
            goto done;
        }

        if (status == NSS_STATUS_SUCCESS) {
            ret = make_netgroup_attr(result, attrs);
            if (ret != EOK) {
                DEBUG(1, ("make_netgroup_attr failed.\n"));
                goto done;
            }
        }
    } while (status != NSS_STATUS_RETURN);

    status = ctx->ops.endnetgrent(&result);
    if (status != NSS_STATUS_SUCCESS) {
        DEBUG(1, ("endnetgrent failed.\n"));
        ret = EIO;
        goto done;
    }

    ret = sysdb_add_netgroup(sysdb, dom, name, NULL, attrs,
                             ctx->entry_cache_timeout);
    if (ret != EOK) {
        DEBUG(1, ("sysdb_add_netgroup failed.\n"));
        goto done;
    }

    ret = EOK;

done:
    talloc_free(tmp_ctx);

    return ret;
}