2 Unix SMB/CIFS implementation.
6 Copyright (C) Gerald (Jerry) Carter 2007
7 Copyright (C) Matthew Newton 2015
10 This library is free software; you can redistribute it and/or
11 modify it under the terms of the GNU Lesser General Public
12 License as published by the Free Software Foundation; either
13 version 3 of the License, or (at your option) any later version.
15 This library is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Library General Public License for more details.
20 You should have received a copy of the GNU Lesser General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 /* Required Headers */
27 #include "libwbclient.h"
29 /* From wb_common.c */
31 struct winbindd_context;
33 NSS_STATUS winbindd_request_response(struct winbindd_context *wbctx,
35 struct winbindd_request *request,
36 struct winbindd_response *response);
37 NSS_STATUS winbindd_priv_request_response(struct winbindd_context *wbctx,
39 struct winbindd_request *request,
40 struct winbindd_response *response);
41 struct winbindd_context *winbindd_ctx_create(void);
42 void winbindd_ctx_free(struct winbindd_context *ctx);
46 result == NSS_STATUS_UNAVAIL: winbind not around
47 result == NSS_STATUS_NOTFOUND: winbind around, but domain missing
49 Due to a bad API NSS_STATUS_NOTFOUND is returned both when winbind_off
50 and when winbind return WINBINDD_ERROR. So the semantics of this
51 routine depends on winbind_on. Grepping for winbind_off I just
52 found 3 places where winbind is turned off, and this does not conflict
53 (as far as I have seen) with the callers of is_trusted_domains.
58 static wbcErr wbcRequestResponseInt(
60 struct winbindd_request *request,
61 struct winbindd_response *response,
62 NSS_STATUS (*fn)(struct winbindd_context *wbctx, int req_type,
63 struct winbindd_request *request,
64 struct winbindd_response *response))
66 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
67 NSS_STATUS nss_status;
69 /* for some calls the request and/or response can be NULL */
71 nss_status = fn(NULL, cmd, request, response);
74 case NSS_STATUS_SUCCESS:
75 wbc_status = WBC_ERR_SUCCESS;
77 case NSS_STATUS_UNAVAIL:
78 wbc_status = WBC_ERR_WINBIND_NOT_AVAILABLE;
80 case NSS_STATUS_NOTFOUND:
81 wbc_status = WBC_ERR_DOMAIN_NOT_FOUND;
84 wbc_status = WBC_ERR_NSS_ERROR;
92 * @brief Wrapper around Winbind's send/receive API call
94 * @param cmd Winbind command operation to perform
95 * @param request Send structure
96 * @param response Receive structure
100 wbcErr wbcRequestResponse(int cmd,
101 struct winbindd_request *request,
102 struct winbindd_response *response)
104 return wbcRequestResponseInt(cmd, request, response,
105 winbindd_request_response);
108 wbcErr wbcRequestResponsePriv(int cmd,
109 struct winbindd_request *request,
110 struct winbindd_response *response)
112 return wbcRequestResponseInt(cmd, request, response,
113 winbindd_priv_request_response);
116 /** @brief Translate an error value into a string
120 * @return a pointer to a static string
122 const char *wbcErrorString(wbcErr error)
125 case WBC_ERR_SUCCESS:
126 return "WBC_ERR_SUCCESS";
127 case WBC_ERR_NOT_IMPLEMENTED:
128 return "WBC_ERR_NOT_IMPLEMENTED";
129 case WBC_ERR_UNKNOWN_FAILURE:
130 return "WBC_ERR_UNKNOWN_FAILURE";
131 case WBC_ERR_NO_MEMORY:
132 return "WBC_ERR_NO_MEMORY";
133 case WBC_ERR_INVALID_SID:
134 return "WBC_ERR_INVALID_SID";
135 case WBC_ERR_INVALID_PARAM:
136 return "WBC_ERR_INVALID_PARAM";
137 case WBC_ERR_WINBIND_NOT_AVAILABLE:
138 return "WBC_ERR_WINBIND_NOT_AVAILABLE";
139 case WBC_ERR_DOMAIN_NOT_FOUND:
140 return "WBC_ERR_DOMAIN_NOT_FOUND";
141 case WBC_ERR_INVALID_RESPONSE:
142 return "WBC_ERR_INVALID_RESPONSE";
143 case WBC_ERR_NSS_ERROR:
144 return "WBC_ERR_NSS_ERROR";
145 case WBC_ERR_UNKNOWN_USER:
146 return "WBC_ERR_UNKNOWN_USER";
147 case WBC_ERR_UNKNOWN_GROUP:
148 return "WBC_ERR_UNKNOWN_GROUP";
149 case WBC_ERR_AUTH_ERROR:
150 return "WBC_ERR_AUTH_ERROR";
151 case WBC_ERR_PWD_CHANGE_FAILED:
152 return "WBC_ERR_PWD_CHANGE_FAILED";
155 return "unknown wbcErr value";
158 #define WBC_MAGIC (0x7a2b0e1e)
159 #define WBC_MAGIC_FREE (0x875634fe)
161 struct wbcMemPrefix {
163 void (*destructor)(void *ptr);
166 static size_t wbcPrefixLen(void)
168 size_t result = sizeof(struct wbcMemPrefix);
169 return (result + 15) & ~15;
172 static struct wbcMemPrefix *wbcMemToPrefix(void *ptr)
174 return (struct wbcMemPrefix *)(((char *)ptr) - wbcPrefixLen());
177 void *wbcAllocateMemory(size_t nelem, size_t elsize,
178 void (*destructor)(void *ptr))
180 struct wbcMemPrefix *result;
182 if (nelem >= (2<<24)/elsize) {
183 /* basic protection against integer wrap */
187 result = (struct wbcMemPrefix *)calloc(
188 1, nelem*elsize + wbcPrefixLen());
189 if (result == NULL) {
192 result->magic = WBC_MAGIC;
193 result->destructor = destructor;
194 return ((char *)result) + wbcPrefixLen();
197 /* Free library allocated memory */
198 void wbcFreeMemory(void *p)
200 struct wbcMemPrefix *wbcMem;
205 wbcMem = wbcMemToPrefix(p);
206 if (wbcMem->magic != WBC_MAGIC) {
210 /* paranoid check to ensure we don't double free */
211 wbcMem->magic = WBC_MAGIC_FREE;
213 if (wbcMem->destructor != NULL) {
214 wbcMem->destructor(p);
220 char *wbcStrDup(const char *str)
226 result = (char *)wbcAllocateMemory(len+1, sizeof(char), NULL);
227 if (result == NULL) {
230 memcpy(result, str, len+1);
234 static void wbcStringArrayDestructor(void *ptr)
236 char **p = (char **)ptr;
243 const char **wbcAllocateStringArray(int num_strings)
245 return (const char **)wbcAllocateMemory(
246 num_strings + 1, sizeof(const char *),
247 wbcStringArrayDestructor);
250 wbcErr wbcLibraryDetails(struct wbcLibraryDetails **_details)
252 struct wbcLibraryDetails *info;
254 info = (struct wbcLibraryDetails *)wbcAllocateMemory(
255 1, sizeof(struct wbcLibraryDetails), NULL);
258 return WBC_ERR_NO_MEMORY;
261 info->major_version = WBCLIENT_MAJOR_VERSION;
262 info->minor_version = WBCLIENT_MINOR_VERSION;
263 info->vendor_version = WBCLIENT_VENDOR_VERSION;
266 return WBC_ERR_SUCCESS;
269 /* Context handling functions */
271 static void wbcContextDestructor(void *ptr)
273 struct wbcContext *ctx = (struct wbcContext *)ptr;
275 winbindd_ctx_free(ctx->winbindd_ctx);
278 struct wbcContext *wbcCtxCreate(void)
280 struct wbcContext *ctx;
281 struct winbindd_context *wbctx;
283 ctx = (struct wbcContext *)wbcAllocateMemory(
284 1, sizeof(struct wbcContext), wbcContextDestructor);
290 wbctx = winbindd_ctx_create();
297 ctx->winbindd_ctx = wbctx;
302 void wbcCtxFree(struct wbcContext *ctx)