s3: Fix some nonempty blank lines
[obnox/samba/samba-obnox.git] / source3 / winbindd / idmap_nss.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    idmap PASSDB backend
5
6    Copyright (C) Simo Sorce 2006
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "winbindd.h"
24 #include "nsswitch/winbind_client.h"
25 #include "idmap.h"
26
27 #undef DBGC_CLASS
28 #define DBGC_CLASS DBGC_IDMAP
29
30 /*****************************
31  Initialise idmap database. 
32 *****************************/
33
34 static NTSTATUS idmap_nss_int_init(struct idmap_domain *dom,
35                                    const char *params)
36 {       
37         return NT_STATUS_OK;
38 }
39
40 /**********************************
41  lookup a set of unix ids. 
42 **********************************/
43
44 static NTSTATUS idmap_nss_unixids_to_sids(struct idmap_domain *dom, struct id_map **ids)
45 {
46         TALLOC_CTX *ctx;
47         int i;
48
49         /* initialize the status to avoid suprise */
50         for (i = 0; ids[i]; i++) {
51                 ids[i]->status = ID_UNKNOWN;
52         }
53
54         ctx = talloc_new(dom);
55         if ( ! ctx) {
56                 DEBUG(0, ("Out of memory!\n"));
57                 return NT_STATUS_NO_MEMORY;
58         }
59
60         for (i = 0; ids[i]; i++) {
61                 struct passwd *pw;
62                 struct group *gr;
63                 const char *name;
64                 enum lsa_SidType type;
65                 bool ret;
66
67                 switch (ids[i]->xid.type) {
68                 case ID_TYPE_UID:
69                         pw = getpwuid((uid_t)ids[i]->xid.id);
70
71                         if (!pw) {
72                                 ids[i]->status = ID_UNMAPPED;
73                                 continue;
74                         }
75                         name = pw->pw_name;
76                         break;
77                 case ID_TYPE_GID:
78                         gr = getgrgid((gid_t)ids[i]->xid.id);
79
80                         if (!gr) {
81                                 ids[i]->status = ID_UNMAPPED;
82                                 continue;
83                         }
84                         name = gr->gr_name;
85                         break;
86                 default: /* ?? */
87                         ids[i]->status = ID_UNKNOWN;
88                         continue;
89                 }
90
91                 /* by default calls to winbindd are disabled
92                    the following call will not recurse so this is safe */
93                 (void)winbind_on();
94                 /* Lookup name from PDC using lsa_lookup_names() */
95                 ret = winbind_lookup_name(dom->name, name, ids[i]->sid, &type);
96                 (void)winbind_off();
97
98                 if (!ret) {
99                         /* TODO: how do we know if the name is really not mapped,
100                          * or something just failed ? */
101                         ids[i]->status = ID_UNMAPPED;
102                         continue;
103                 }
104
105                 switch (type) {
106                 case SID_NAME_USER:
107                         if (ids[i]->xid.type == ID_TYPE_UID) {
108                                 ids[i]->status = ID_MAPPED;
109                         }
110                         break;
111
112                 case SID_NAME_DOM_GRP:
113                 case SID_NAME_ALIAS:
114                 case SID_NAME_WKN_GRP:
115                         if (ids[i]->xid.type == ID_TYPE_GID) {
116                                 ids[i]->status = ID_MAPPED;
117                         }
118                         break;
119
120                 default:
121                         ids[i]->status = ID_UNKNOWN;
122                         break;
123                 }
124         }
125
126
127         talloc_free(ctx);
128         return NT_STATUS_OK;
129 }
130
131 /**********************************
132  lookup a set of sids. 
133 **********************************/
134
135 static NTSTATUS idmap_nss_sids_to_unixids(struct idmap_domain *dom, struct id_map **ids)
136 {
137         TALLOC_CTX *ctx;
138         int i;
139
140         /* initialize the status to avoid suprise */
141         for (i = 0; ids[i]; i++) {
142                 ids[i]->status = ID_UNKNOWN;
143         }
144
145         ctx = talloc_new(dom);
146         if ( ! ctx) {
147                 DEBUG(0, ("Out of memory!\n"));
148                 return NT_STATUS_NO_MEMORY;
149         }
150
151         for (i = 0; ids[i]; i++) {
152                 struct group *gr;
153                 enum lsa_SidType type;
154                 const char *dom_name = NULL;
155                 const char *name = NULL;
156                 bool ret;
157
158                 /* by default calls to winbindd are disabled
159                    the following call will not recurse so this is safe */
160                 (void)winbind_on();
161                 ret = winbind_lookup_sid(ctx, ids[i]->sid, &dom_name, &name, &type);
162                 (void)winbind_off();
163
164                 if (!ret) {
165                         /* TODO: how do we know if the name is really not mapped,
166                          * or something just failed ? */
167                         ids[i]->status = ID_UNMAPPED;
168                         continue;
169                 }
170
171                 switch (type) {
172                 case SID_NAME_USER: {
173                         struct passwd *pw;
174
175                         /* this will find also all lower case name and use username level */
176
177                         pw = Get_Pwnam_alloc(talloc_tos(), name);
178                         if (pw) {
179                                 ids[i]->xid.id = pw->pw_uid;
180                                 ids[i]->xid.type = ID_TYPE_UID;
181                                 ids[i]->status = ID_MAPPED;
182                         }
183                         TALLOC_FREE(pw);
184                         break;
185                 }
186
187                 case SID_NAME_DOM_GRP:
188                 case SID_NAME_ALIAS:
189                 case SID_NAME_WKN_GRP:
190
191                         gr = getgrnam(name);
192                         if (gr) {
193                                 ids[i]->xid.id = gr->gr_gid;
194                                 ids[i]->xid.type = ID_TYPE_GID;
195                                 ids[i]->status = ID_MAPPED;
196                         }
197                         break;
198
199                 default:
200                         ids[i]->status = ID_UNKNOWN;
201                         break;
202                 }
203         }
204
205         talloc_free(ctx);
206         return NT_STATUS_OK;
207 }
208
209 /**********************************
210  Close the idmap tdb instance
211 **********************************/
212
213 static NTSTATUS idmap_nss_close(struct idmap_domain *dom)
214 {
215         return NT_STATUS_OK;
216 }
217
218 static struct idmap_methods nss_methods = {
219
220         .init = idmap_nss_int_init,
221         .unixids_to_sids = idmap_nss_unixids_to_sids,
222         .sids_to_unixids = idmap_nss_sids_to_unixids,
223         .close_fn = idmap_nss_close
224 };
225
226 NTSTATUS idmap_nss_init(void)
227 {
228         return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION, "nss", &nss_methods);
229 }