net_idmap: use wbcSet[U|G]idMapping() and wbcSet[U|G]idHwm() functions
[Samba.git] / source / winbindd / idmap_nss.c
blobe4acd9ce65a2c2269e6ad1ba75894a8e78388a9a
1 /*
2 Unix SMB/CIFS implementation.
4 idmap PASSDB backend
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/>.
22 #include "includes.h"
23 #include "winbindd.h"
25 #undef DBGC_CLASS
26 #define DBGC_CLASS DBGC_IDMAP
28 /*****************************
29 Initialise idmap database.
30 *****************************/
32 static NTSTATUS idmap_nss_int_init(struct idmap_domain *dom)
34 dom->initialized = True;
35 return NT_STATUS_OK;
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)
44 TALLOC_CTX *ctx;
45 int i;
47 if (! dom->initialized) {
48 return NT_STATUS_UNSUCCESSFUL;
51 ctx = talloc_new(dom);
52 if ( ! ctx) {
53 DEBUG(0, ("Out of memory!\n"));
54 return NT_STATUS_NO_MEMORY;
57 for (i = 0; ids[i]; i++) {
58 struct passwd *pw;
59 struct group *gr;
60 const char *name;
61 enum lsa_SidType type;
62 bool ret;
64 switch (ids[i]->xid.type) {
65 case ID_TYPE_UID:
66 pw = getpwuid((uid_t)ids[i]->xid.id);
68 if (!pw) {
69 ids[i]->status = ID_UNMAPPED;
70 continue;
72 name = pw->pw_name;
73 break;
74 case ID_TYPE_GID:
75 gr = getgrgid((gid_t)ids[i]->xid.id);
77 if (!gr) {
78 ids[i]->status = ID_UNMAPPED;
79 continue;
81 name = gr->gr_name;
82 break;
83 default: /* ?? */
84 ids[i]->status = ID_UNKNOWN;
85 continue;
88 /* by default calls to winbindd are disabled
89 the following call will not recurse so this is safe */
90 (void)winbind_on();
91 /* Lookup name from PDC using lsa_lookup_names() */
92 ret = winbind_lookup_name(dom->name, name, ids[i]->sid, &type);
93 (void)winbind_off();
95 if (!ret) {
96 /* TODO: how do we know if the name is really not mapped,
97 * or something just failed ? */
98 ids[i]->status = ID_UNMAPPED;
99 continue;
102 switch (type) {
103 case SID_NAME_USER:
104 if (ids[i]->xid.type == ID_TYPE_UID) {
105 ids[i]->status = ID_MAPPED;
107 break;
109 case SID_NAME_DOM_GRP:
110 case SID_NAME_ALIAS:
111 case SID_NAME_WKN_GRP:
112 if (ids[i]->xid.type == ID_TYPE_GID) {
113 ids[i]->status = ID_MAPPED;
115 break;
117 default:
118 ids[i]->status = ID_UNKNOWN;
119 break;
124 talloc_free(ctx);
125 return NT_STATUS_OK;
128 /**********************************
129 lookup a set of sids.
130 **********************************/
132 static NTSTATUS idmap_nss_sids_to_unixids(struct idmap_domain *dom, struct id_map **ids)
134 TALLOC_CTX *ctx;
135 int i;
137 if (! dom->initialized) {
138 return NT_STATUS_UNSUCCESSFUL;
141 ctx = talloc_new(dom);
142 if ( ! ctx) {
143 DEBUG(0, ("Out of memory!\n"));
144 return NT_STATUS_NO_MEMORY;
147 for (i = 0; ids[i]; i++) {
148 struct group *gr;
149 enum lsa_SidType type;
150 const char *dom_name = NULL;
151 const char *name = NULL;
152 bool ret;
154 /* by default calls to winbindd are disabled
155 the following call will not recurse so this is safe */
156 (void)winbind_on();
157 ret = winbind_lookup_sid(ctx, ids[i]->sid, &dom_name, &name, &type);
158 (void)winbind_off();
160 if (!ret) {
161 /* TODO: how do we know if the name is really not mapped,
162 * or something just failed ? */
163 ids[i]->status = ID_UNMAPPED;
164 continue;
167 switch (type) {
168 case SID_NAME_USER: {
169 struct passwd *pw;
171 /* this will find also all lower case name and use username level */
173 pw = Get_Pwnam_alloc(talloc_tos(), name);
174 if (pw) {
175 ids[i]->xid.id = pw->pw_uid;
176 ids[i]->xid.type = ID_TYPE_UID;
177 ids[i]->status = ID_MAPPED;
179 TALLOC_FREE(pw);
180 break;
183 case SID_NAME_DOM_GRP:
184 case SID_NAME_ALIAS:
185 case SID_NAME_WKN_GRP:
187 gr = getgrnam(name);
188 if (gr) {
189 ids[i]->xid.id = gr->gr_gid;
190 ids[i]->xid.type = ID_TYPE_GID;
191 ids[i]->status = ID_MAPPED;
193 break;
195 default:
196 ids[i]->status = ID_UNKNOWN;
197 break;
201 talloc_free(ctx);
202 return NT_STATUS_OK;
205 /**********************************
206 Close the idmap tdb instance
207 **********************************/
209 static NTSTATUS idmap_nss_close(struct idmap_domain *dom)
211 return NT_STATUS_OK;
214 static struct idmap_methods nss_methods = {
216 .init = idmap_nss_int_init,
217 .unixids_to_sids = idmap_nss_unixids_to_sids,
218 .sids_to_unixids = idmap_nss_sids_to_unixids,
219 .close_fn = idmap_nss_close
222 NTSTATUS idmap_nss_init(void)
224 return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION, "nss", &nss_methods);