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