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