wbclient: "ev" is no longer used in wbc_xids_to_sids
[Samba.git] / source4 / libcli / wbclient / wbclient.c
blob69d8b439ad9a33f7606d900edb9cf07e86adfe5c
1 /*
2 Unix SMB/CIFS implementation.
4 Winbind client library.
6 Copyright (C) 2008 Kai Blin <kai@samba.org>
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 <tevent.h>
24 #include "nsswitch/winbind_client.h"
25 #include "libcli/wbclient/wbclient.h"
26 #include "libcli/security/dom_sid.h"
27 #include "nsswitch/libwbclient/wbclient.h"
29 NTSTATUS wbc_sids_to_xids(struct id_map *ids, uint32_t count)
31 TALLOC_CTX *mem_ctx;
32 uint32_t i;
33 struct wbcDomainSid *sids;
34 struct wbcUnixId *xids;
35 wbcErr result;
36 bool wb_off;
38 mem_ctx = talloc_new(NULL);
39 if (mem_ctx == NULL) {
40 return NT_STATUS_NO_MEMORY;
43 sids = talloc_array(mem_ctx, struct wbcDomainSid, count);
44 if (sids == NULL) {
45 TALLOC_FREE(mem_ctx);
46 return NT_STATUS_NO_MEMORY;
49 xids = talloc_array(mem_ctx, struct wbcUnixId, count);
50 if (xids == NULL) {
51 TALLOC_FREE(mem_ctx);
52 return NT_STATUS_NO_MEMORY;
55 for (i=0; i<count; i++) {
56 memcpy(&sids[i], ids[i].sid, sizeof(struct dom_sid));
59 wb_off = winbind_env_set();
60 if (wb_off) {
61 (void)winbind_on();
64 result = wbcSidsToUnixIds(sids, count, xids);
66 if (wb_off) {
67 (void)winbind_off();
70 if (!WBC_ERROR_IS_OK(result)) {
71 TALLOC_FREE(mem_ctx);
72 return NT_STATUS_INTERNAL_ERROR;
75 for (i=0; i<count; i++) {
76 struct wbcUnixId *xid = &xids[i];
77 struct unixid *id = &ids[i].xid;
79 switch (xid->type) {
80 case WBC_ID_TYPE_UID:
81 id->type = ID_TYPE_UID;
82 id->id = xid->id.uid;
83 break;
84 case WBC_ID_TYPE_GID:
85 id->type = ID_TYPE_GID;
86 id->id = xid->id.gid;
87 break;
88 case WBC_ID_TYPE_BOTH:
89 id->type = ID_TYPE_BOTH;
90 id->id = xid->id.uid;
91 break;
92 case WBC_ID_TYPE_NOT_SPECIFIED:
93 id->type = ID_TYPE_NOT_SPECIFIED;
94 id->id = UINT32_MAX;
95 break;
97 ids[i].status = ID_MAPPED;
100 TALLOC_FREE(mem_ctx);
102 return NT_STATUS_OK;
105 NTSTATUS wbc_xids_to_sids(struct id_map *ids, uint32_t count)
107 TALLOC_CTX *mem_ctx;
108 uint32_t i;
109 struct wbcDomainSid *sids;
110 struct wbcUnixId *xids;
111 wbcErr result;
112 bool wb_off;
114 mem_ctx = talloc_new(NULL);
115 if (mem_ctx == NULL) {
116 return NT_STATUS_NO_MEMORY;
119 sids = talloc_array(mem_ctx, struct wbcDomainSid, count);
120 if (sids == NULL) {
121 TALLOC_FREE(mem_ctx);
122 return NT_STATUS_NO_MEMORY;
125 xids = talloc_array(mem_ctx, struct wbcUnixId, count);
126 if (xids == NULL) {
127 TALLOC_FREE(mem_ctx);
128 return NT_STATUS_NO_MEMORY;
131 for (i=0; i<count; i++) {
132 struct id_map *id = &ids[i];
133 struct wbcUnixId *xid = &xids[i];
135 switch (id->xid.type) {
136 case ID_TYPE_UID:
137 *xid = (struct wbcUnixId) {
138 .type = WBC_ID_TYPE_UID,
139 .id.uid = id->xid.id
141 break;
142 case ID_TYPE_GID:
143 *xid = (struct wbcUnixId) {
144 .type = WBC_ID_TYPE_GID,
145 .id.uid = id->xid.id
147 break;
148 default:
149 TALLOC_FREE(mem_ctx);
150 return NT_STATUS_NOT_FOUND;
154 wb_off = winbind_env_set();
155 if (wb_off) {
156 (void)winbind_on();
159 result = wbcUnixIdsToSids(xids, count, sids);
161 if (wb_off) {
162 (void)winbind_off();
165 if (!WBC_ERROR_IS_OK(result)) {
166 TALLOC_FREE(mem_ctx);
167 return NT_STATUS_INTERNAL_ERROR;
170 for (i=0; i<count; i++) {
171 struct wbcDomainSid *sid = &sids[i];
172 struct wbcDomainSid null_sid = { 0 };
173 struct id_map *id = &ids[i];
175 if (memcmp(sid, &null_sid, sizeof(*sid)) != 0) {
176 struct dom_sid domsid;
177 id->status = ID_MAPPED;
179 memcpy(&domsid, sid, sizeof(struct dom_sid));
180 id->sid = dom_sid_dup(ids, &domsid);
181 if (id->sid == NULL) {
182 TALLOC_FREE(mem_ctx);
183 return NT_STATUS_NO_MEMORY;
185 } else {
186 id->status = ID_UNMAPPED;
187 id->sid = NULL;
191 TALLOC_FREE(mem_ctx);
192 return NT_STATUS_OK;