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 2 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, const char *compat_params)
38 /**********************************
39 lookup a set of unix ids.
40 **********************************/
42 static NTSTATUS idmap_nss_unixids_to_sids(struct idmap_domain *dom, struct id_map **ids)
45 struct winbindd_domain *wdom;
49 wdom = find_lookup_domain_from_name(dom->name);
51 DEBUG(2, ("Can't lookup domain %s\n", dom->name));
52 return NT_STATUS_NO_SUCH_DOMAIN;
55 ctx = talloc_new(dom);
57 DEBUG(0, ("Out of memory!\n"));
58 return NT_STATUS_NO_MEMORY;
61 /* avoid any possible recursion in winbindd,
62 * these calls are aimed at getting info
63 * out of alternative nss dbs anyway */
64 winbind_env = winbind_env_set();
67 for (i = 0; ids[i]; i++) {
71 enum lsa_SidType type;
73 switch (ids[i]->xid.type) {
75 pw = getpwuid((uid_t)ids[i]->xid.id);
77 ids[i]->mapped = False;
83 gr = getgrgid((gid_t)ids[i]->xid.id);
85 ids[i]->mapped = False;
91 ids[i]->mapped = False;
95 /* Lookup name from PDC using lsa_lookup_names() */
96 if (!winbindd_lookup_sid_by_name(ctx, wdom, dom->name, name, ids[i]->sid, &type)) {
97 ids[i]->mapped = False;
101 /* make sure it is marked as unmapped if types do not match */
102 ids[i]->mapped = False;
106 if (ids[i]->xid.type == ID_TYPE_UID) {
107 ids[i]->mapped = True;
111 case SID_NAME_DOM_GRP:
113 case SID_NAME_WKN_GRP:
114 if (ids[i]->xid.type == ID_TYPE_GID) {
115 ids[i]->mapped = True;
124 /* allow winbindd calls again, if they were enabled */
133 /**********************************
134 lookup a set of sids.
135 **********************************/
137 static NTSTATUS idmap_nss_sids_to_unixids(struct idmap_domain *dom, struct id_map **ids)
143 ctx = talloc_new(dom);
145 DEBUG(0, ("Out of memory!\n"));
146 return NT_STATUS_NO_MEMORY;
149 /* avoid any possible recursion in winbindd,
150 * these calls are aimed at getting info
151 * out of alternative nss dbs anyway */
152 winbind_env = winbind_env_set();
155 for (i = 0; ids[i]; i++) {
158 enum lsa_SidType type;
159 char *dom_name = NULL;
162 if (!winbindd_lookup_name_by_sid(ctx, ids[i]->sid, &dom_name, &name, &type)) {
163 ids[i]->mapped = False;
167 /* make sure it is marked as unmapped if types do not match */
168 ids[i]->mapped = False;
173 /* 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]->mapped = True;
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]->mapped = True;
198 TALLOC_FREE(dom_name);
202 /* allow winbindd calls again, if they were enabled */
211 /**********************************
212 Close the idmap tdb instance
213 **********************************/
215 static NTSTATUS idmap_nss_close(struct idmap_domain *dom)
220 static struct idmap_methods nss_methods = {
222 .init = idmap_nss_int_init,
223 .unixids_to_sids = idmap_nss_unixids_to_sids,
224 .sids_to_unixids = idmap_nss_sids_to_unixids,
225 .close_fn = idmap_nss_close
228 NTSTATUS idmap_nss_init(void)
230 return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION, "nss", &nss_methods);