misc.idl: Add reference to the slightly odd representation of if_version
[abartlet/samba.git/.git] / source3 / winbindd / winbindd_group.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Winbind daemon for ntdom nss module
5
6    Copyright (C) Tim Potter 2000
7    Copyright (C) Jeremy Allison 2001.
8    Copyright (C) Gerald (Jerry) Carter 2003.
9    Copyright (C) Volker Lendecke 2005
10    
11    This program is free software; you can redistribute it and/or modify
12    it under the terms of the GNU General Public License as published by
13    the Free Software Foundation; either version 3 of the License, or
14    (at your option) any later version.
15    
16    This program is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19    GNU General Public License for more details.
20    
21    You should have received a copy of the GNU General Public License
22    along with this program.  If not, see <http://www.gnu.org/licenses/>.
23 */
24
25 #include "includes.h"
26 #include "winbindd.h"
27
28 #undef DBGC_CLASS
29 #define DBGC_CLASS DBGC_WINBIND
30
31 /* Fill a grent structure from various other information */
32
33 bool fill_grent(TALLOC_CTX *mem_ctx, struct winbindd_gr *gr,
34                 const char *dom_name, const char *gr_name, gid_t unix_gid)
35 {
36         fstring full_group_name;
37         char *mapped_name = NULL;
38         struct winbindd_domain *domain = find_domain_from_name_noinit(dom_name);
39         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
40
41         nt_status = normalize_name_map(mem_ctx, domain, gr_name,
42                                        &mapped_name);
43
44         /* Basic whitespace replacement */
45         if (NT_STATUS_IS_OK(nt_status)) {
46                 fill_domain_username(full_group_name, dom_name,
47                                      mapped_name, true);
48         }
49         /* Mapped to an aliase */
50         else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_FILE_RENAMED)) {
51                 fstrcpy(full_group_name, mapped_name);
52         }
53         /* no change */
54         else {
55                 fill_domain_username( full_group_name, dom_name,
56                                       gr_name, True );
57         }
58
59         gr->gr_gid = unix_gid;
60
61         /* Group name and password */
62
63         safe_strcpy(gr->gr_name, full_group_name, sizeof(gr->gr_name) - 1);
64         safe_strcpy(gr->gr_passwd, "x", sizeof(gr->gr_passwd) - 1);
65
66         return True;
67 }
68
69 struct getgr_countmem {
70         int num;
71         size_t len;
72 };
73
74 static int getgr_calc_memberlen(DATA_BLOB key, void *data, void *priv)
75 {
76         struct wbint_Principal *m = talloc_get_type_abort(
77                 data, struct wbint_Principal);
78         struct getgr_countmem *buf = (struct getgr_countmem *)priv;
79
80         buf->num += 1;
81         buf->len += strlen(m->name) + 1;
82         return 0;
83 }
84
85 struct getgr_stringmem {
86         size_t ofs;
87         char *buf;
88 };
89
90 static int getgr_unparse_members(DATA_BLOB key, void *data, void *priv)
91 {
92         struct wbint_Principal *m = talloc_get_type_abort(
93                 data, struct wbint_Principal);
94         struct getgr_stringmem *buf = (struct getgr_stringmem *)priv;
95         int len;
96
97         len = strlen(m->name);
98
99         memcpy(buf->buf + buf->ofs, m->name, len);
100         buf->ofs += len;
101         buf->buf[buf->ofs] = ',';
102         buf->ofs += 1;
103         return 0;
104 }
105
106 NTSTATUS winbindd_print_groupmembers(struct talloc_dict *members,
107                                      TALLOC_CTX *mem_ctx,
108                                      int *num_members, char **result)
109 {
110         struct getgr_countmem c;
111         struct getgr_stringmem m;
112         int res;
113
114         c.num = 0;
115         c.len = 0;
116
117         res = talloc_dict_traverse(members, getgr_calc_memberlen, &c);
118         if (res != 0) {
119                 DEBUG(5, ("talloc_dict_traverse failed\n"));
120                 return NT_STATUS_INTERNAL_ERROR;
121         }
122
123         m.ofs = 0;
124         m.buf = talloc_array(mem_ctx, char, c.len);
125         if (m.buf == NULL) {
126                 DEBUG(5, ("talloc failed\n"));
127                 return NT_STATUS_NO_MEMORY;
128         }
129
130         res = talloc_dict_traverse(members, getgr_unparse_members, &m);
131         if (res != 0) {
132                 DEBUG(5, ("talloc_dict_traverse failed\n"));
133                 TALLOC_FREE(m.buf);
134                 return NT_STATUS_INTERNAL_ERROR;
135         }
136         m.buf[c.len-1] = '\0';
137
138         *num_members = c.num;
139         *result = m.buf;
140         return NT_STATUS_OK;
141 }