s3-build: only include nsswitch header where needed.
[samba.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
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,
34                                    const char *params)
35 {       
36         return NT_STATUS_OK;
37 }
38
39 /**********************************
40  lookup a set of unix ids. 
41 **********************************/
42
43 static NTSTATUS idmap_nss_unixids_to_sids(struct idmap_domain *dom, struct id_map **ids)
44 {
45         TALLOC_CTX *ctx;
46         int i;
47
48         /* initialize the status to avoid suprise */
49         for (i = 0; ids[i]; i++) {
50                 ids[i]->status = ID_UNKNOWN;
51         }
52         
53         ctx = talloc_new(dom);
54         if ( ! ctx) {
55                 DEBUG(0, ("Out of memory!\n"));
56                 return NT_STATUS_NO_MEMORY;
57         }
58
59         for (i = 0; ids[i]; i++) {
60                 struct passwd *pw;
61                 struct group *gr;
62                 const char *name;
63                 enum lsa_SidType type;
64                 bool ret;
65                 
66                 switch (ids[i]->xid.type) {
67                 case ID_TYPE_UID:
68                         pw = getpwuid((uid_t)ids[i]->xid.id);
69
70                         if (!pw) {
71                                 ids[i]->status = ID_UNMAPPED;
72                                 continue;
73                         }
74                         name = pw->pw_name;
75                         break;
76                 case ID_TYPE_GID:
77                         gr = getgrgid((gid_t)ids[i]->xid.id);
78
79                         if (!gr) {
80                                 ids[i]->status = ID_UNMAPPED;
81                                 continue;
82                         }
83                         name = gr->gr_name;
84                         break;
85                 default: /* ?? */
86                         ids[i]->status = ID_UNKNOWN;
87                         continue;
88                 }
89
90                 /* by default calls to winbindd are disabled
91                    the following call will not recurse so this is safe */
92                 (void)winbind_on();
93                 /* Lookup name from PDC using lsa_lookup_names() */
94                 ret = winbind_lookup_name(dom->name, name, ids[i]->sid, &type);
95                 (void)winbind_off();
96
97                 if (!ret) {
98                         /* TODO: how do we know if the name is really not mapped,
99                          * or something just failed ? */
100                         ids[i]->status = ID_UNMAPPED;
101                         continue;
102                 }
103
104                 switch (type) {
105                 case SID_NAME_USER:
106                         if (ids[i]->xid.type == ID_TYPE_UID) {
107                                 ids[i]->status = ID_MAPPED;
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]->status = ID_MAPPED;
116                         }
117                         break;
118
119                 default:
120                         ids[i]->status = ID_UNKNOWN;
121                         break;
122                 }
123         }
124
125
126         talloc_free(ctx);
127         return NT_STATUS_OK;
128 }
129
130 /**********************************
131  lookup a set of sids. 
132 **********************************/
133
134 static NTSTATUS idmap_nss_sids_to_unixids(struct idmap_domain *dom, struct id_map **ids)
135 {
136         TALLOC_CTX *ctx;
137         int i;
138
139         /* initialize the status to avoid suprise */
140         for (i = 0; ids[i]; i++) {
141                 ids[i]->status = ID_UNKNOWN;
142         }
143         
144         ctx = talloc_new(dom);
145         if ( ! ctx) {
146                 DEBUG(0, ("Out of memory!\n"));
147                 return NT_STATUS_NO_MEMORY;
148         }
149
150         for (i = 0; ids[i]; i++) {
151                 struct group *gr;
152                 enum lsa_SidType type;
153                 const char *dom_name = NULL;
154                 const char *name = NULL;
155                 bool ret;
156
157                 /* by default calls to winbindd are disabled
158                    the following call will not recurse so this is safe */
159                 (void)winbind_on();
160                 ret = winbind_lookup_sid(ctx, ids[i]->sid, &dom_name, &name, &type);
161                 (void)winbind_off();
162
163                 if (!ret) {
164                         /* TODO: how do we know if the name is really not mapped,
165                          * or something just failed ? */
166                         ids[i]->status = ID_UNMAPPED;
167                         continue;
168                 }
169
170                 switch (type) {
171                 case SID_NAME_USER: {
172                         struct passwd *pw;
173
174                         /* this will find also all lower case name and use username level */
175
176                         pw = Get_Pwnam_alloc(talloc_tos(), name);
177                         if (pw) {
178                                 ids[i]->xid.id = pw->pw_uid;
179                                 ids[i]->xid.type = ID_TYPE_UID;
180                                 ids[i]->status = ID_MAPPED;
181                         }
182                         TALLOC_FREE(pw);
183                         break;
184                 }
185
186                 case SID_NAME_DOM_GRP:
187                 case SID_NAME_ALIAS:
188                 case SID_NAME_WKN_GRP:
189
190                         gr = getgrnam(name);
191                         if (gr) {
192                                 ids[i]->xid.id = gr->gr_gid;
193                                 ids[i]->xid.type = ID_TYPE_GID;
194                                 ids[i]->status = ID_MAPPED;
195                         }
196                         break;
197
198                 default:
199                         ids[i]->status = ID_UNKNOWN;
200                         break;
201                 }
202         }
203
204         talloc_free(ctx);
205         return NT_STATUS_OK;
206 }
207
208 /**********************************
209  Close the idmap tdb instance
210 **********************************/
211
212 static NTSTATUS idmap_nss_close(struct idmap_domain *dom)
213 {
214         return NT_STATUS_OK;
215 }
216
217 static struct idmap_methods nss_methods = {
218
219         .init = idmap_nss_int_init,
220         .unixids_to_sids = idmap_nss_unixids_to_sids,
221         .sids_to_unixids = idmap_nss_sids_to_unixids,
222         .close_fn = idmap_nss_close
223 };
224
225 NTSTATUS idmap_nss_init(void)
226 {
227         return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION, "nss", &nss_methods);
228 }