2748141d3b56ad2b2134b489279009524216417f
[samba.git] / source3 / nsswitch / 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 2 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, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "winbindd.h"
25
26 #undef DBGC_CLASS
27 #define DBGC_CLASS DBGC_IDMAP
28
29 /*****************************
30  Initialise idmap database. 
31 *****************************/
32
33 static NTSTATUS idmap_nss_int_init(struct idmap_domain *dom, const char *compat_params)
34 {       
35         return NT_STATUS_OK;
36 }
37
38 /**********************************
39  lookup a set of unix ids. 
40 **********************************/
41
42 static NTSTATUS idmap_nss_unixids_to_sids(struct idmap_domain *dom, struct id_map **ids)
43 {
44         TALLOC_CTX *ctx;
45         struct winbindd_domain *wdom;
46         BOOL winbind_env;
47         int i;
48
49         wdom = find_lookup_domain_from_name(dom->name);
50         if (!wdom) {
51                 DEBUG(2, ("Can't lookup domain %s\n", dom->name));
52                 return NT_STATUS_NO_SUCH_DOMAIN;
53         }
54
55         ctx = talloc_new(dom);
56         if ( ! ctx) {
57                 DEBUG(0, ("Out of memory!\n"));
58                 return NT_STATUS_NO_MEMORY;
59         }
60
61         /* avoid any possible recursion in winbindd,
62          * these calls are aimed at getting info
63          * out of alternative nss dbs anyway */
64         winbind_env = winbind_env_set();
65         winbind_off();
66
67         for (i = 0; ids[i]; i++) {
68                 struct passwd *pw;
69                 struct group *gr;
70                 const char *name;
71                 enum lsa_SidType type;
72                 
73                 switch (ids[i]->xid.type) {
74                 case ID_TYPE_UID:
75                         pw = getpwuid((uid_t)ids[i]->xid.id);
76                         if (!pw) {
77                                 ids[i]->mapped = False;
78                                 continue;
79                         }
80                         name = pw->pw_name;
81                         break;
82                 case ID_TYPE_GID:
83                         gr = getgrgid((gid_t)ids[i]->xid.id);
84                         if (!gr) {
85                                 ids[i]->mapped = False;
86                                 continue;
87                         }
88                         name = gr->gr_name;
89                         break;
90                 default: /* ?? */
91                         ids[i]->mapped = False;
92                         continue;
93                 }
94
95                 /* Lookup name from PDC using lsa_lookup_names() */
96                 if (!winbindd_lookup_sid_by_name(ctx, wdom, dom->name, name, ids[i]->sid, &type)) {
97                         ids[i]->mapped = False;
98                         continue;
99                 }
100
101                 /* make sure it is marked as unmapped if types do not match */
102                 ids[i]->mapped = False;
103
104                 switch (type) {
105                 case SID_NAME_USER:
106                         if (ids[i]->xid.type == ID_TYPE_UID) {
107                                 ids[i]->mapped = True;
108                         }
109                         break;
110
111                 case SID_NAME_DOM_GRP:
112                 case SID_NAME_ALIAS:
113                 case SID_NAME_WKN_GRP:
114                         if (ids[i]->xid.type == ID_TYPE_GID) {
115                                 ids[i]->mapped = True;
116                         }
117                         break;
118
119                 default:
120                         break;
121                 }
122         }
123
124         /* allow winbindd calls again, if they were enabled */
125         if (!winbind_env) {
126                 winbind_on();
127         }
128
129         talloc_free(ctx);
130         return NT_STATUS_OK;
131 }
132
133 /**********************************
134  lookup a set of sids. 
135 **********************************/
136
137 static NTSTATUS idmap_nss_sids_to_unixids(struct idmap_domain *dom, struct id_map **ids)
138 {
139         TALLOC_CTX *ctx;
140         BOOL winbind_env;
141         int i;
142
143         ctx = talloc_new(dom);
144         if ( ! ctx) {
145                 DEBUG(0, ("Out of memory!\n"));
146                 return NT_STATUS_NO_MEMORY;
147         }
148
149         /* avoid any possible recursion in winbindd,
150          * these calls are aimed at getting info
151          * out of alternative nss dbs anyway */
152         winbind_env = winbind_env_set();
153         winbind_off();
154
155         for (i = 0; ids[i]; i++) {
156                 struct passwd *pw;
157                 struct group *gr;
158                 enum lsa_SidType type;
159                 char *dom_name = NULL;
160                 char *name = NULL;
161
162                 if (!winbindd_lookup_name_by_sid(ctx, ids[i]->sid, &dom_name, &name, &type)) {
163                         ids[i]->mapped = False;
164                         continue;
165                 }
166
167                 /* make sure it is marked as unmapped if types do not match */
168                 ids[i]->mapped = False;
169
170                 switch (type) {
171                 case SID_NAME_USER:
172
173                         /* this will find also all lower case name and use username level */
174                         pw = Get_Pwnam(name);
175                         if (pw) {
176                                 ids[i]->xid.id = pw->pw_uid;
177                                 ids[i]->xid.type = ID_TYPE_UID;
178                                 ids[i]->mapped = True;
179                         }
180                         break;
181
182                 case SID_NAME_DOM_GRP:
183                 case SID_NAME_ALIAS:
184                 case SID_NAME_WKN_GRP:
185
186                         gr = getgrnam(name);
187                         if (gr) {
188                                 ids[i]->xid.id = gr->gr_gid;
189                                 ids[i]->xid.type = ID_TYPE_GID;
190                                 ids[i]->mapped = True;
191                         }
192                         break;
193
194                 default:
195                         break;
196                 }
197
198                 TALLOC_FREE(dom_name);
199                 TALLOC_FREE(name);
200         }
201
202         /* allow winbindd calls again, if they were enabled */
203         if (!winbind_env) {
204                 winbind_on();
205         }
206
207         talloc_free(ctx);
208         return NT_STATUS_OK;
209 }
210
211 /**********************************
212  Close the idmap tdb instance
213 **********************************/
214
215 static NTSTATUS idmap_nss_close(struct idmap_domain *dom)
216 {
217         return NT_STATUS_OK;
218 }
219
220 static struct idmap_methods nss_methods = {
221
222         .init = idmap_nss_int_init,
223         .unixids_to_sids = idmap_nss_unixids_to_sids,
224         .sids_to_unixids = idmap_nss_sids_to_unixids,
225         .close_fn = idmap_nss_close
226 };
227
228 NTSTATUS idmap_nss_init(void)
229 {
230         return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION, "nss", &nss_methods);
231 }