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"
28 #define DBGC_CLASS DBGC_IDMAP
30 /*****************************
31 Initialise idmap database.
32 *****************************/
34 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)
47 /* initialize the status to avoid suprise */
48 for (i = 0; ids[i]; i++) {
49 ids[i]->status = ID_UNKNOWN;
52 for (i = 0; ids[i]; i++) {
56 enum lsa_SidType type;
59 switch (ids[i]->xid.type) {
61 pw = getpwuid((uid_t)ids[i]->xid.id);
64 ids[i]->status = ID_UNMAPPED;
70 gr = getgrgid((gid_t)ids[i]->xid.id);
73 ids[i]->status = ID_UNMAPPED;
79 ids[i]->status = ID_UNKNOWN;
83 /* by default calls to winbindd are disabled
84 the following call will not recurse so this is safe */
86 /* Lookup name from PDC using lsa_lookup_names() */
87 ret = winbind_lookup_name(dom->name, name, ids[i]->sid, &type);
91 /* TODO: how do we know if the name is really not mapped,
92 * or something just failed ? */
93 ids[i]->status = ID_UNMAPPED;
99 if (ids[i]->xid.type == ID_TYPE_UID) {
100 ids[i]->status = ID_MAPPED;
104 case SID_NAME_DOM_GRP:
106 case SID_NAME_WKN_GRP:
107 if (ids[i]->xid.type == ID_TYPE_GID) {
108 ids[i]->status = ID_MAPPED;
113 ids[i]->status = ID_UNKNOWN;
120 /**********************************
121 lookup a set of sids.
122 **********************************/
124 static NTSTATUS idmap_nss_sids_to_unixids(struct idmap_domain *dom, struct id_map **ids)
128 /* initialize the status to avoid suprise */
129 for (i = 0; ids[i]; i++) {
130 ids[i]->status = ID_UNKNOWN;
133 for (i = 0; ids[i]; i++) {
135 enum lsa_SidType type;
139 /* by default calls to winbindd are disabled
140 the following call will not recurse so this is safe */
142 ret = winbind_lookup_sid(talloc_tos(), ids[i]->sid, NULL,
143 (const char **)&name, &type);
147 /* TODO: how do we know if the name is really not mapped,
148 * or something just failed ? */
149 ids[i]->status = ID_UNMAPPED;
154 case SID_NAME_USER: {
157 /* this will find also all lower case name and use username level */
159 pw = Get_Pwnam_alloc(talloc_tos(), name);
161 ids[i]->xid.id = pw->pw_uid;
162 ids[i]->xid.type = ID_TYPE_UID;
163 ids[i]->status = ID_MAPPED;
169 case SID_NAME_DOM_GRP:
171 case SID_NAME_WKN_GRP:
175 ids[i]->xid.id = gr->gr_gid;
176 ids[i]->xid.type = ID_TYPE_GID;
177 ids[i]->status = ID_MAPPED;
182 ids[i]->status = ID_UNKNOWN;
190 /**********************************
191 Close the idmap tdb instance
192 **********************************/
194 static struct idmap_methods nss_methods = {
196 .init = idmap_nss_int_init,
197 .unixids_to_sids = idmap_nss_unixids_to_sids,
198 .sids_to_unixids = idmap_nss_sids_to_unixids,
201 NTSTATUS idmap_nss_init(void)
203 return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION, "nss", &nss_methods);