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/>.
23 #include "system/passwd.h"
25 #include "nsswitch/winbind_client.h"
27 #include "lib/winbind_util.h"
28 #include "libcli/security/dom_sid.h"
31 #define DBGC_CLASS DBGC_IDMAP
33 /*****************************
34 Initialise idmap database.
35 *****************************/
37 static NTSTATUS idmap_nss_int_init(struct idmap_domain *dom)
42 /**********************************
43 lookup a set of unix ids.
44 **********************************/
46 static NTSTATUS idmap_nss_unixids_to_sids(struct idmap_domain *dom, struct id_map **ids)
50 /* initialize the status to avoid suprise */
51 for (i = 0; ids[i]; i++) {
52 ids[i]->status = ID_UNKNOWN;
55 for (i = 0; ids[i]; i++) {
60 enum lsa_SidType type;
63 switch (ids[i]->xid.type) {
65 pw = getpwuid((uid_t)ids[i]->xid.id);
68 ids[i]->status = ID_UNMAPPED;
74 gr = getgrgid((gid_t)ids[i]->xid.id);
77 ids[i]->status = ID_UNMAPPED;
83 ids[i]->status = ID_UNKNOWN;
87 /* by default calls to winbindd are disabled
88 the following call will not recurse so this is safe */
90 /* Lookup name from PDC using lsa_lookup_names() */
91 ret = winbind_lookup_name(dom->name, name, &sid, &type);
95 /* TODO: how do we know if the name is really not mapped,
96 * or something just failed ? */
97 ids[i]->status = ID_UNMAPPED;
103 if (ids[i]->xid.type == ID_TYPE_UID) {
104 sid_copy(ids[i]->sid, &sid);
105 ids[i]->status = ID_MAPPED;
109 case SID_NAME_DOM_GRP:
111 case SID_NAME_WKN_GRP:
112 if (ids[i]->xid.type == ID_TYPE_GID) {
113 sid_copy(ids[i]->sid, &sid);
114 ids[i]->status = ID_MAPPED;
119 ids[i]->status = ID_UNKNOWN;
126 /**********************************
127 lookup a set of sids.
128 **********************************/
130 static NTSTATUS idmap_nss_sids_to_unixids(struct idmap_domain *dom, struct id_map **ids)
134 /* initialize the status to avoid suprise */
135 for (i = 0; ids[i]; i++) {
136 ids[i]->status = ID_UNKNOWN;
139 for (i = 0; ids[i]; i++) {
141 enum lsa_SidType type;
142 const char *_domain = NULL;
143 const char *_name = NULL;
148 /* by default calls to winbindd are disabled
149 the following call will not recurse so this is safe */
151 ret = winbind_lookup_sid(talloc_tos(),
158 /* TODO: how do we know if the name is really not mapped,
159 * or something just failed ? */
160 ids[i]->status = ID_UNMAPPED;
164 domain = discard_const_p(char, _domain);
165 name = discard_const_p(char, _name);
167 if (!strequal(domain, dom->name)) {
168 struct dom_sid_buf buf;
169 DBG_ERR("DOMAIN[%s] ignoring SID[%s] belongs to %s [%s\\%s]\n",
170 dom->name, dom_sid_str_buf(ids[i]->sid, &buf),
171 sid_type_lookup(type), domain, name);
172 ids[i]->status = ID_UNMAPPED;
177 case SID_NAME_USER: {
180 /* this will find also all lower case name and use username level */
182 pw = Get_Pwnam_alloc(talloc_tos(), name);
184 ids[i]->xid.id = pw->pw_uid;
185 ids[i]->xid.type = ID_TYPE_UID;
186 ids[i]->status = ID_MAPPED;
192 case SID_NAME_DOM_GRP:
194 case SID_NAME_WKN_GRP:
198 ids[i]->xid.id = gr->gr_gid;
199 ids[i]->xid.type = ID_TYPE_GID;
200 ids[i]->status = ID_MAPPED;
205 ids[i]->status = ID_UNKNOWN;
214 /**********************************
215 Close the idmap tdb instance
216 **********************************/
218 static const 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,
224 NTSTATUS idmap_nss_init(TALLOC_CTX *mem_ctx)
226 return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION, "nss", &nss_methods);