s3-winbind: remove global inclusion of libwbclient.
[samba.git] / source3 / winbindd / idmap_nss.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    idmap NSS 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 #include "lib/winbind_util.h"
27
28 #undef DBGC_CLASS
29 #define DBGC_CLASS DBGC_IDMAP
30
31 /*****************************
32  Initialise idmap database. 
33 *****************************/
34
35 static NTSTATUS idmap_nss_int_init(struct idmap_domain *dom)
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         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         for (i = 0; ids[i]; i++) {
54                 struct passwd *pw;
55                 struct group *gr;
56                 const char *name;
57                 enum lsa_SidType type;
58                 bool ret;
59
60                 switch (ids[i]->xid.type) {
61                 case ID_TYPE_UID:
62                         pw = getpwuid((uid_t)ids[i]->xid.id);
63
64                         if (!pw) {
65                                 ids[i]->status = ID_UNMAPPED;
66                                 continue;
67                         }
68                         name = pw->pw_name;
69                         break;
70                 case ID_TYPE_GID:
71                         gr = getgrgid((gid_t)ids[i]->xid.id);
72
73                         if (!gr) {
74                                 ids[i]->status = ID_UNMAPPED;
75                                 continue;
76                         }
77                         name = gr->gr_name;
78                         break;
79                 default: /* ?? */
80                         ids[i]->status = ID_UNKNOWN;
81                         continue;
82                 }
83
84                 /* by default calls to winbindd are disabled
85                    the following call will not recurse so this is safe */
86                 (void)winbind_on();
87                 /* Lookup name from PDC using lsa_lookup_names() */
88                 ret = winbind_lookup_name(dom->name, name, ids[i]->sid, &type);
89                 (void)winbind_off();
90
91                 if (!ret) {
92                         /* TODO: how do we know if the name is really not mapped,
93                          * or something just failed ? */
94                         ids[i]->status = ID_UNMAPPED;
95                         continue;
96                 }
97
98                 switch (type) {
99                 case SID_NAME_USER:
100                         if (ids[i]->xid.type == ID_TYPE_UID) {
101                                 ids[i]->status = ID_MAPPED;
102                         }
103                         break;
104
105                 case SID_NAME_DOM_GRP:
106                 case SID_NAME_ALIAS:
107                 case SID_NAME_WKN_GRP:
108                         if (ids[i]->xid.type == ID_TYPE_GID) {
109                                 ids[i]->status = ID_MAPPED;
110                         }
111                         break;
112
113                 default:
114                         ids[i]->status = ID_UNKNOWN;
115                         break;
116                 }
117         }
118         return NT_STATUS_OK;
119 }
120
121 /**********************************
122  lookup a set of sids. 
123 **********************************/
124
125 static NTSTATUS idmap_nss_sids_to_unixids(struct idmap_domain *dom, struct id_map **ids)
126 {
127         int i;
128
129         /* initialize the status to avoid suprise */
130         for (i = 0; ids[i]; i++) {
131                 ids[i]->status = ID_UNKNOWN;
132         }
133
134         for (i = 0; ids[i]; i++) {
135                 struct group *gr;
136                 enum lsa_SidType type;
137                 char *name = NULL;
138                 bool ret;
139
140                 /* by default calls to winbindd are disabled
141                    the following call will not recurse so this is safe */
142                 (void)winbind_on();
143                 ret = winbind_lookup_sid(talloc_tos(), ids[i]->sid, NULL,
144                                          (const char **)&name, &type);
145                 (void)winbind_off();
146
147                 if (!ret) {
148                         /* TODO: how do we know if the name is really not mapped,
149                          * or something just failed ? */
150                         ids[i]->status = ID_UNMAPPED;
151                         continue;
152                 }
153
154                 switch (type) {
155                 case SID_NAME_USER: {
156                         struct passwd *pw;
157
158                         /* this will find also all lower case name and use username level */
159
160                         pw = Get_Pwnam_alloc(talloc_tos(), name);
161                         if (pw) {
162                                 ids[i]->xid.id = pw->pw_uid;
163                                 ids[i]->xid.type = ID_TYPE_UID;
164                                 ids[i]->status = ID_MAPPED;
165                         }
166                         TALLOC_FREE(pw);
167                         break;
168                 }
169
170                 case SID_NAME_DOM_GRP:
171                 case SID_NAME_ALIAS:
172                 case SID_NAME_WKN_GRP:
173
174                         gr = getgrnam(name);
175                         if (gr) {
176                                 ids[i]->xid.id = gr->gr_gid;
177                                 ids[i]->xid.type = ID_TYPE_GID;
178                                 ids[i]->status = ID_MAPPED;
179                         }
180                         break;
181
182                 default:
183                         ids[i]->status = ID_UNKNOWN;
184                         break;
185                 }
186                 TALLOC_FREE(name);
187         }
188         return NT_STATUS_OK;
189 }
190
191 /**********************************
192  Close the idmap tdb instance
193 **********************************/
194
195 static struct idmap_methods nss_methods = {
196
197         .init = idmap_nss_int_init,
198         .unixids_to_sids = idmap_nss_unixids_to_sids,
199         .sids_to_unixids = idmap_nss_sids_to_unixids,
200 };
201
202 NTSTATUS idmap_nss_init(void)
203 {
204         return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION, "nss", &nss_methods);
205 }