winbindd: winbindd_priv_pipe_dir() -> bool_dispatch_table
[samba.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 #include "lib/dbwrap/dbwrap.h"
28
29 #undef DBGC_CLASS
30 #define DBGC_CLASS DBGC_WINBIND
31
32 /* Fill a grent structure from various other information */
33
34 bool fill_grent(TALLOC_CTX *mem_ctx, struct winbindd_gr *gr,
35                 const char *dom_name, const char *gr_name, gid_t unix_gid)
36 {
37         fstring full_group_name;
38         char *mapped_name = NULL;
39         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
40
41         nt_status = normalize_name_map(mem_ctx, dom_name, 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         strlcpy(gr->gr_name, full_group_name, sizeof(gr->gr_name));
64         strlcpy(gr->gr_passwd, "x", sizeof(gr->gr_passwd));
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(struct db_record *rec, void *private_data)
75 {
76         struct getgr_countmem *buf = private_data;
77         TDB_DATA data = dbwrap_record_get_value(rec);
78         size_t len;
79
80         buf->num += 1;
81
82         len = buf->len + data.dsize;
83         if (len < buf->len) {
84                 return 0;
85         }
86         buf->len = len;
87         return 0;
88 }
89
90 struct getgr_stringmem {
91         size_t ofs;
92         char *buf;
93 };
94
95 static int getgr_unparse_members(struct db_record *rec, void *private_data)
96 {
97         struct getgr_stringmem *buf = private_data;
98         TDB_DATA data = dbwrap_record_get_value(rec);
99         int len;
100
101         len = data.dsize-1;
102
103         memcpy(buf->buf + buf->ofs, data.dptr, len);
104         buf->ofs += len;
105         buf->buf[buf->ofs] = ',';
106         buf->ofs += 1;
107         return 0;
108 }
109
110 NTSTATUS winbindd_print_groupmembers(struct db_context *members,
111                                      TALLOC_CTX *mem_ctx,
112                                      int *num_members, char **result)
113 {
114         struct getgr_countmem c;
115         struct getgr_stringmem m;
116         int count;
117         NTSTATUS status;
118
119         c.num = 0;
120         c.len = 0;
121
122         status = dbwrap_traverse(members, getgr_calc_memberlen, &c, &count);
123         if (!NT_STATUS_IS_OK(status)) {
124                 DBG_NOTICE("dbwrap_traverse failed: %s\n", nt_errstr(status));
125                 return status;
126         }
127
128         m.ofs = 0;
129         m.buf = talloc_array(mem_ctx, char, c.len);
130         if (m.buf == NULL) {
131                 DEBUG(5, ("talloc failed\n"));
132                 return NT_STATUS_NO_MEMORY;
133         }
134
135         status = dbwrap_traverse(members, getgr_unparse_members, &m, &count);
136         if (!NT_STATUS_IS_OK(status)) {
137                 TALLOC_FREE(m.buf);
138                 DBG_NOTICE("dbwrap_traverse failed: %s\n", nt_errstr(status));
139                 return status;
140         }
141         m.buf[c.len-1] = '\0';
142
143         *num_members = c.num;
144         *result = m.buf;
145         return NT_STATUS_OK;
146 }