~ubuntu-branches/ubuntu/saucy/sssd/saucy

« back to all changes in this revision

Viewing changes to src/providers/proxy/proxy_netgroup.c

  • Committer: Stéphane Graber
  • Date: 2011-06-15 16:23:14 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: stgraber@ubuntu.com-20110615162314-rbhoppnpaxfqo5q7
Merge 1.5.8

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    SSSD
 
3
 
 
4
    Proxy netgroup handler
 
5
 
 
6
    Authors:
 
7
 
 
8
        Sumit Bose <sbose@redhat.com>
 
9
 
 
10
    Copyright (C) 2010 Red Hat
 
11
 
 
12
    This program is free software; you can redistribute it and/or modify
 
13
    it under the terms of the GNU General Public License as published by
 
14
    the Free Software Foundation; either version 3 of the License, or
 
15
    (at your option) any later version.
 
16
 
 
17
    This program is distributed in the hope that it will be useful,
 
18
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
19
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
20
    GNU General Public License for more details.
 
21
 
 
22
    You should have received a copy of the GNU General Public License
 
23
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
24
*/
 
25
 
 
26
#include "providers/proxy/proxy.h"
 
27
#include "util/util.h"
 
28
 
 
29
#define BUFLEN  1024
 
30
 
 
31
static errno_t make_netgroup_attr(struct __netgrent netgrent,
 
32
                                  struct sysdb_attrs *attrs)
 
33
{
 
34
    int ret;
 
35
    char *dummy;
 
36
 
 
37
    if (netgrent.type == group_val) {
 
38
        ret =sysdb_attrs_add_string(attrs, SYSDB_NETGROUP_MEMBER,
 
39
                                    netgrent.val.group);
 
40
        if (ret != EOK) {
 
41
            DEBUG(1, ("sysdb_attrs_add_string failed.\n"));
 
42
            return ret;
 
43
        }
 
44
    } else if (netgrent.type == triple_val) {
 
45
        dummy = talloc_asprintf(attrs, "(%s,%s,%s)", netgrent.val.triple.host,
 
46
                                netgrent.val.triple.user,
 
47
                                netgrent.val.triple.domain);
 
48
        if (dummy == NULL) {
 
49
            DEBUG(1, ("talloc_asprintf failed.\n"));
 
50
            return ENOMEM;
 
51
        }
 
52
 
 
53
        ret = sysdb_attrs_add_string(attrs, SYSDB_NETGROUP_TRIPLE, dummy);
 
54
        if (ret != EOK) {
 
55
            DEBUG(1, ("sysdb_attrs_add_string failed.\n"));
 
56
            return ret;
 
57
        }
 
58
    } else {
 
59
        DEBUG(1, ("Unknown netgrent entry type [%d].\n", netgrent.type));
 
60
        return EINVAL;
 
61
    }
 
62
 
 
63
    return EOK;
 
64
}
 
65
 
 
66
errno_t get_netgroup(struct proxy_id_ctx *ctx,
 
67
                     struct sysdb_ctx *sysdb,
 
68
                     struct sss_domain_info *dom,
 
69
                     const char *name)
 
70
{
 
71
    struct __netgrent result;
 
72
    enum nss_status status;
 
73
    char buffer[BUFLEN];
 
74
    int ret;
 
75
    TALLOC_CTX *tmp_ctx;
 
76
    struct sysdb_attrs *attrs;
 
77
 
 
78
    memset(&result, 0 ,sizeof(result));
 
79
    status = ctx->ops.setnetgrent(name, &result);
 
80
    if (status != NSS_STATUS_SUCCESS) {
 
81
        DEBUG(5, ("setnetgrent failed for netgroup [%s].\n", name));
 
82
        return EIO;
 
83
    }
 
84
 
 
85
    tmp_ctx = talloc_new(NULL);
 
86
    if (tmp_ctx == NULL) {
 
87
        DEBUG(1, ("talloc_new failed.\n"));
 
88
        return ENOMEM;
 
89
    }
 
90
 
 
91
    attrs = sysdb_new_attrs(tmp_ctx);
 
92
    if (attrs == NULL) {
 
93
        DEBUG(1, ("sysdb_new_attrs failed.\n"));
 
94
        return ENOMEM;
 
95
    }
 
96
 
 
97
    do {
 
98
        status = ctx->ops.getnetgrent_r(&result, buffer, BUFLEN, &ret);
 
99
        if (status != NSS_STATUS_SUCCESS && status != NSS_STATUS_RETURN) {
 
100
            DEBUG(1, ("getnetgrent_r failed for netgroup [%s]: [%d][%s].\n",
 
101
                      name, ret, strerror(ret)));
 
102
            goto done;
 
103
        }
 
104
 
 
105
        if (status == NSS_STATUS_SUCCESS) {
 
106
            ret = make_netgroup_attr(result, attrs);
 
107
            if (ret != EOK) {
 
108
                DEBUG(1, ("make_netgroup_attr failed.\n"));
 
109
                goto done;
 
110
            }
 
111
        }
 
112
    } while (status != NSS_STATUS_RETURN);
 
113
 
 
114
    status = ctx->ops.endnetgrent(&result);
 
115
    if (status != NSS_STATUS_SUCCESS) {
 
116
        DEBUG(1, ("endnetgrent failed.\n"));
 
117
        ret = EIO;
 
118
        goto done;
 
119
    }
 
120
 
 
121
    ret = sysdb_add_netgroup(sysdb, dom, name, NULL, attrs,
 
122
                             ctx->entry_cache_timeout);
 
123
    if (ret != EOK) {
 
124
        DEBUG(1, ("sysdb_add_netgroup failed.\n"));
 
125
        goto done;
 
126
    }
 
127
 
 
128
    ret = EOK;
 
129
 
 
130
done:
 
131
    talloc_free(tmp_ctx);
 
132
 
 
133
    return ret;
 
134
}