summaryrefslogtreecommitdiffstats
path: root/src/providers/proxy/proxy_netgroup.c
blob: bb0bc171b8c3688a92c14f711669f752653a39f0 (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
    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

#define get_triple_el(s) ((s) ? (s) : "")

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)",
                                get_triple_el(netgrent.val.triple.host),
                                get_triple_el(netgrent.val.triple.user),
                                get_triple_el(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;
}

static errno_t save_netgroup(struct sysdb_ctx *sysdb,
                             struct sss_domain_info *domain,
                             const char *name,
                             struct sysdb_attrs *attrs,
                             bool lowercase,
                             uint64_t cache_timeout)
{
    errno_t ret;

    if (lowercase) {
        ret = sysdb_attrs_add_lc_name_alias(attrs, name);
        if (ret) {
            DEBUG(SSSDBG_OP_FAILURE, ("Could not add name alias\n"));
            return ret;
        }
    }

    ret = sysdb_add_netgroup(sysdb, domain, name, NULL,
                             attrs, NULL, cache_timeout, 0);
    if (ret != EOK) {
        DEBUG(SSSDBG_OP_FAILURE, ("sysdb_add_netgroup failed.\n"));
        return ret;
    }

    return EOK;
}

static errno_t handle_error(enum nss_status status, struct sysdb_ctx *sysdb,
                            struct sss_domain_info *domain, const char *name)
{
    errno_t ret;

    switch (status) {
    case NSS_STATUS_SUCCESS:
        DEBUG(SSSDBG_TRACE_INTERNAL, ("Netgroup lookup succeeded\n"));
        ret = EOK;
        break;

    case NSS_STATUS_NOTFOUND:
        DEBUG(SSSDBG_MINOR_FAILURE, ("The netgroup was not found\n"));
        ret = sysdb_delete_netgroup(sysdb, domain, name);
        if (ret != EOK) {
            DEBUG(SSSDBG_CRIT_FAILURE, ("Cannot delete netgroup: %d\n", ret));
            ret = EIO;
        }
        break;

    case NSS_STATUS_UNAVAIL:
        DEBUG(SSSDBG_TRACE_LIBS,
              ("The proxy target did not respond, going offline\n"));
        ret = ENXIO;
        break;

    default:
        DEBUG(SSSDBG_CRIT_FAILURE, ("Unexpected error looking up netgroup\n"));
        ret = EIO;
        break;
    }

    return ret;
}

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 = NULL;
    struct sysdb_attrs *attrs;

    memset(&result, 0, sizeof(result));
    status = ctx->ops.setnetgrent(name, &result);
    if (status != NSS_STATUS_SUCCESS) {
        DEBUG(SSSDBG_OP_FAILURE,
              ("setnetgrent failed for netgroup [%s].\n", name));
        ret = handle_error(status, sysdb, dom, name);
        goto done;
    }

    tmp_ctx = talloc_new(NULL);
    if (tmp_ctx == NULL) {
        DEBUG(SSSDBG_CRIT_FAILURE, ("talloc_new failed.\n"));
        ret = ENOMEM;
        goto done;
    }

    attrs = sysdb_new_attrs(tmp_ctx);
    if (attrs == NULL) {
        DEBUG(SSSDBG_CRIT_FAILURE, ("sysdb_new_attrs failed.\n"));
        ret = ENOMEM;
        goto done;
    }

    do {
        status = ctx->ops.getnetgrent_r(&result, buffer, BUFLEN, &ret);
        if (status != NSS_STATUS_SUCCESS &&
            status != NSS_STATUS_RETURN &&
            status != NSS_STATUS_NOTFOUND) {
            ret = handle_error(status, sysdb, dom, name);
            DEBUG(SSSDBG_OP_FAILURE,
                  ("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(SSSDBG_CRIT_FAILURE, ("make_netgroup_attr failed.\n"));
                goto done;
            }
        }
    } while (status != NSS_STATUS_RETURN && status != NSS_STATUS_NOTFOUND);

    status = ctx->ops.endnetgrent(&result);
    if (status != NSS_STATUS_SUCCESS) {
        DEBUG(SSSDBG_OP_FAILURE, ("endnetgrent failed.\n"));
        ret = handle_error(status, sysdb, dom, name);
        goto done;
    }

    ret = save_netgroup(sysdb, dom, name, attrs,
                        !dom->case_sensitive,
                        dom->netgroup_timeout);
    if (ret != EOK) {
        DEBUG(SSSDBG_OP_FAILURE, ("sysdb_add_netgroup failed.\n"));
        goto done;
    }

    ret = EOK;

done:
    talloc_free(tmp_ctx);
    return ret;
}