2 Unix SMB/CIFS implementation.
6 Copyright (C) Simo Sorce 2006
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.
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.
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/>.
24 #include "nsswitch/winbind_client.h"
27 #define DBGC_CLASS DBGC_IDMAP
29 /*****************************
30 Initialise idmap database.
31 *****************************/
33 static NTSTATUS idmap_nss_int_init(struct idmap_domain *dom,
39 /**********************************
40 lookup a set of unix ids.
41 **********************************/
43 static NTSTATUS idmap_nss_unixids_to_sids(struct idmap_domain *dom, struct id_map **ids)
48 /* initialize the status to avoid suprise */
49 for (i = 0; ids[i]; i++) {
50 ids[i]->status = ID_UNKNOWN;
53 ctx = talloc_new(dom);
55 DEBUG(0, ("Out of memory!\n"));
56 return NT_STATUS_NO_MEMORY;
59 for (i = 0; ids[i]; i++) {
63 enum lsa_SidType type;
66 switch (ids[i]->xid.type) {
68 pw = getpwuid((uid_t)ids[i]->xid.id);
71 ids[i]->status = ID_UNMAPPED;
77 gr = getgrgid((gid_t)ids[i]->xid.id);
80 ids[i]->status = ID_UNMAPPED;
86 ids[i]->status = ID_UNKNOWN;
90 /* by default calls to winbindd are disabled
91 the following call will not recurse so this is safe */
93 /* Lookup name from PDC using lsa_lookup_names() */
94 ret = winbind_lookup_name(dom->name, name, ids[i]->sid, &type);
98 /* TODO: how do we know if the name is really not mapped,
99 * or something just failed ? */
100 ids[i]->status = ID_UNMAPPED;
106 if (ids[i]->xid.type == ID_TYPE_UID) {
107 ids[i]->status = ID_MAPPED;
111 case SID_NAME_DOM_GRP:
113 case SID_NAME_WKN_GRP:
114 if (ids[i]->xid.type == ID_TYPE_GID) {
115 ids[i]->status = ID_MAPPED;
120 ids[i]->status = ID_UNKNOWN;
130 /**********************************
131 lookup a set of sids.
132 **********************************/
134 static NTSTATUS idmap_nss_sids_to_unixids(struct idmap_domain *dom, struct id_map **ids)
139 /* initialize the status to avoid suprise */
140 for (i = 0; ids[i]; i++) {
141 ids[i]->status = ID_UNKNOWN;
144 ctx = talloc_new(dom);
146 DEBUG(0, ("Out of memory!\n"));
147 return NT_STATUS_NO_MEMORY;
150 for (i = 0; ids[i]; i++) {
152 enum lsa_SidType type;
153 const char *dom_name = NULL;
154 const char *name = NULL;
157 /* by default calls to winbindd are disabled
158 the following call will not recurse so this is safe */
160 ret = winbind_lookup_sid(ctx, ids[i]->sid, &dom_name, &name, &type);
164 /* TODO: how do we know if the name is really not mapped,
165 * or something just failed ? */
166 ids[i]->status = ID_UNMAPPED;
171 case SID_NAME_USER: {
174 /* this will find also all lower case name and use username level */
176 pw = Get_Pwnam_alloc(talloc_tos(), name);
178 ids[i]->xid.id = pw->pw_uid;
179 ids[i]->xid.type = ID_TYPE_UID;
180 ids[i]->status = ID_MAPPED;
186 case SID_NAME_DOM_GRP:
188 case SID_NAME_WKN_GRP:
192 ids[i]->xid.id = gr->gr_gid;
193 ids[i]->xid.type = ID_TYPE_GID;
194 ids[i]->status = ID_MAPPED;
199 ids[i]->status = ID_UNKNOWN;
208 /**********************************
209 Close the idmap tdb instance
210 **********************************/
212 static NTSTATUS idmap_nss_close(struct idmap_domain *dom)
217 static struct idmap_methods nss_methods = {
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
225 NTSTATUS idmap_nss_init(void)
227 return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION, "nss", &nss_methods);