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, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #define DBGC_CLASS DBGC_IDMAP
29 /*****************************
30 Initialise idmap database.
31 *****************************/
33 static NTSTATUS idmap_nss_int_init(struct idmap_domain *dom)
35 dom->initialized = True;
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 if (! dom->initialized) {
49 return NT_STATUS_UNSUCCESSFUL;
52 ctx = talloc_new(dom);
54 DEBUG(0, ("Out of memory!\n"));
55 return NT_STATUS_NO_MEMORY;
58 for (i = 0; ids[i]; i++) {
62 enum lsa_SidType type;
65 switch (ids[i]->xid.type) {
67 pw = getpwuid((uid_t)ids[i]->xid.id);
70 ids[i]->status = ID_UNMAPPED;
76 gr = getgrgid((gid_t)ids[i]->xid.id);
79 ids[i]->status = ID_UNMAPPED;
85 ids[i]->status = ID_UNKNOWN;
89 /* by default calls to winbindd are disabled
90 the following call will not recurse so this is safe */
92 /* Lookup name from PDC using lsa_lookup_names() */
93 ret = winbind_lookup_name(dom->name, name, ids[i]->sid, &type);
97 /* TODO: how do we know if the name is really not mapped,
98 * or something just failed ? */
99 ids[i]->status = ID_UNMAPPED;
105 if (ids[i]->xid.type == ID_TYPE_UID) {
106 ids[i]->status = ID_MAPPED;
110 case SID_NAME_DOM_GRP:
112 case SID_NAME_WKN_GRP:
113 if (ids[i]->xid.type == ID_TYPE_GID) {
114 ids[i]->status = ID_MAPPED;
119 ids[i]->status = ID_UNKNOWN;
129 /**********************************
130 lookup a set of sids.
131 **********************************/
133 static NTSTATUS idmap_nss_sids_to_unixids(struct idmap_domain *dom, struct id_map **ids)
138 if (! dom->initialized) {
139 return NT_STATUS_UNSUCCESSFUL;
142 ctx = talloc_new(dom);
144 DEBUG(0, ("Out of memory!\n"));
145 return NT_STATUS_NO_MEMORY;
148 for (i = 0; ids[i]; i++) {
151 enum lsa_SidType type;
152 const char *dom_name = NULL;
153 const char *name = NULL;
156 /* by default calls to winbindd are disabled
157 the following call will not recurse so this is safe */
159 ret = winbind_lookup_sid(ctx, ids[i]->sid, &dom_name, &name, &type);
163 /* TODO: how do we know if the name is really not mapped,
164 * or something just failed ? */
165 ids[i]->status = ID_UNMAPPED;
172 /* this will find also all lower case name and use username level */
174 pw = Get_Pwnam(name);
176 ids[i]->xid.id = pw->pw_uid;
177 ids[i]->xid.type = ID_TYPE_UID;
178 ids[i]->status = ID_MAPPED;
182 case SID_NAME_DOM_GRP:
184 case SID_NAME_WKN_GRP:
188 ids[i]->xid.id = gr->gr_gid;
189 ids[i]->xid.type = ID_TYPE_GID;
190 ids[i]->status = ID_MAPPED;
195 ids[i]->status = ID_UNKNOWN;
204 /**********************************
205 Close the idmap tdb instance
206 **********************************/
208 static NTSTATUS idmap_nss_close(struct idmap_domain *dom)
213 static struct idmap_methods nss_methods = {
215 .init = idmap_nss_int_init,
216 .unixids_to_sids = idmap_nss_unixids_to_sids,
217 .sids_to_unixids = idmap_nss_sids_to_unixids,
218 .close_fn = idmap_nss_close
221 NTSTATUS idmap_nss_init(void)
223 return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION, "nss", &nss_methods);