libwbclient: Make source4/ use nsswitch/libwbclient
[Samba.git] / source4 / libcli / wbclient / wbclient.c
blobf306556ff40ad8168492327a9551d24fb45f8f64
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 tevent_context *ev, struct id_map *ids,
30 uint32_t count)
32 TALLOC_CTX *mem_ctx;
33 uint32_t i;
34 struct wbcDomainSid *sids;
35 struct wbcUnixId *xids;
36 wbcErr result;
37 bool wb_off;
39 mem_ctx = talloc_new(NULL);
40 if (mem_ctx == NULL) {
41 return NT_STATUS_NO_MEMORY;
44 sids = talloc_array(mem_ctx, struct wbcDomainSid, count);
45 if (sids == NULL) {
46 TALLOC_FREE(mem_ctx);
47 return NT_STATUS_NO_MEMORY;
50 xids = talloc_array(mem_ctx, struct wbcUnixId, count);
51 if (xids == NULL) {
52 TALLOC_FREE(mem_ctx);
53 return NT_STATUS_NO_MEMORY;
56 for (i=0; i<count; i++) {
57 memcpy(&sids[i], ids[i].sid, sizeof(struct dom_sid));
60 wb_off = winbind_env_set();
61 if (wb_off) {
62 (void)winbind_on();
65 result = wbcSidsToUnixIds(sids, count, xids);
67 if (wb_off) {
68 (void)winbind_off();
71 if (!WBC_ERROR_IS_OK(result)) {
72 TALLOC_FREE(mem_ctx);
73 return NT_STATUS_INTERNAL_ERROR;
76 for (i=0; i<count; i++) {
77 struct wbcUnixId *xid = &xids[i];
78 struct unixid *id = &ids[i].xid;
80 switch (xid->type) {
81 case WBC_ID_TYPE_UID:
82 id->type = ID_TYPE_UID;
83 id->id = xid->id.uid;
84 break;
85 case WBC_ID_TYPE_GID:
86 id->type = ID_TYPE_GID;
87 id->id = xid->id.gid;
88 break;
89 case WBC_ID_TYPE_BOTH:
90 id->type = ID_TYPE_BOTH;
91 id->id = xid->id.uid;
92 break;
93 case WBC_ID_TYPE_NOT_SPECIFIED:
94 id->type = ID_TYPE_NOT_SPECIFIED;
95 id->id = UINT32_MAX;
96 break;
98 ids[i].status = ID_MAPPED;
101 TALLOC_FREE(mem_ctx);
103 return NT_STATUS_OK;
106 NTSTATUS wbc_xids_to_sids(struct tevent_context *ev, struct id_map *ids,
107 uint32_t count)
109 TALLOC_CTX *mem_ctx;
110 uint32_t i;
111 struct wbcDomainSid *sids;
112 struct wbcUnixId *xids;
113 wbcErr result;
114 bool wb_off;
116 mem_ctx = talloc_new(NULL);
117 if (mem_ctx == NULL) {
118 return NT_STATUS_NO_MEMORY;
121 sids = talloc_array(mem_ctx, struct wbcDomainSid, count);
122 if (sids == NULL) {
123 TALLOC_FREE(mem_ctx);
124 return NT_STATUS_NO_MEMORY;
127 xids = talloc_array(mem_ctx, struct wbcUnixId, count);
128 if (xids == NULL) {
129 TALLOC_FREE(mem_ctx);
130 return NT_STATUS_NO_MEMORY;
133 for (i=0; i<count; i++) {
134 struct id_map *id = &ids[i];
135 struct wbcUnixId *xid = &xids[i];
137 switch (id->xid.type) {
138 case ID_TYPE_UID:
139 *xid = (struct wbcUnixId) {
140 .type = WBC_ID_TYPE_UID,
141 .id.uid = id->xid.id
143 break;
144 case ID_TYPE_GID:
145 *xid = (struct wbcUnixId) {
146 .type = WBC_ID_TYPE_GID,
147 .id.uid = id->xid.id
149 break;
150 default:
151 TALLOC_FREE(mem_ctx);
152 return NT_STATUS_NOT_FOUND;
156 wb_off = winbind_env_set();
157 if (wb_off) {
158 (void)winbind_on();
161 result = wbcUnixIdsToSids(xids, count, sids);
163 if (wb_off) {
164 (void)winbind_off();
167 if (!WBC_ERROR_IS_OK(result)) {
168 TALLOC_FREE(mem_ctx);
169 return NT_STATUS_INTERNAL_ERROR;
172 for (i=0; i<count; i++) {
173 struct wbcDomainSid *sid = &sids[i];
174 struct wbcDomainSid null_sid = { 0 };
175 struct id_map *id = &ids[i];
177 if (memcmp(sid, &null_sid, sizeof(*sid)) != 0) {
178 struct dom_sid domsid;
179 id->status = ID_MAPPED;
181 memcpy(&domsid, sid, sizeof(struct dom_sid));
182 id->sid = dom_sid_dup(ids, &domsid);
183 if (id->sid == NULL) {
184 TALLOC_FREE(mem_ctx);
185 return NT_STATUS_NO_MEMORY;
187 } else {
188 id->status = ID_UNMAPPED;
189 id->sid = NULL;
193 TALLOC_FREE(mem_ctx);
194 return NT_STATUS_OK;