summaryrefslogtreecommitdiff
path: root/nsswitch/libwbclient/wbclient.c
blob: ab1159ad9377ee166b04c6d613586caca7eff28c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
   Unix SMB/CIFS implementation.

   Winbind client API

   Copyright (C) Gerald (Jerry) Carter 2007
   Copyright (C) Matthew Newton 2015


   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 3 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Lesser General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/* Required Headers */

#include "replace.h"
#include "libwbclient.h"

/* From wb_common.c */

struct winbindd_context;

NSS_STATUS winbindd_request_response(struct winbindd_context *wbctx,
				     int req_type,
				     struct winbindd_request *request,
				     struct winbindd_response *response);
NSS_STATUS winbindd_priv_request_response(struct winbindd_context *wbctx,
					  int req_type,
					  struct winbindd_request *request,
					  struct winbindd_response *response);
struct winbindd_context *winbindd_ctx_create(void);
void winbindd_ctx_free(struct winbindd_context *ctx);


/*
 result == NSS_STATUS_UNAVAIL: winbind not around
 result == NSS_STATUS_NOTFOUND: winbind around, but domain missing

 Due to a bad API NSS_STATUS_NOTFOUND is returned both when winbind_off
 and when winbind return WINBINDD_ERROR. So the semantics of this
 routine depends on winbind_on. Grepping for winbind_off I just
 found 3 places where winbind is turned off, and this does not conflict
 (as far as I have seen) with the callers of is_trusted_domains.

 --Volker
*/

static wbcErr wbcRequestResponseInt(
	struct winbindd_context *wbctx,
	int cmd,
	struct winbindd_request *request,
	struct winbindd_response *response,
	NSS_STATUS (*fn)(struct winbindd_context *wbctx, int req_type,
			 struct winbindd_request *request,
			 struct winbindd_response *response))
{
	wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
	NSS_STATUS nss_status;

	/* for some calls the request and/or response can be NULL */

	nss_status = fn(wbctx, cmd, request, response);

	switch (nss_status) {
	case NSS_STATUS_SUCCESS:
		wbc_status = WBC_ERR_SUCCESS;
		break;
	case NSS_STATUS_UNAVAIL:
		wbc_status = WBC_ERR_WINBIND_NOT_AVAILABLE;
		break;
	case NSS_STATUS_NOTFOUND:
		wbc_status = WBC_ERR_DOMAIN_NOT_FOUND;
		break;
	default:
		wbc_status = WBC_ERR_NSS_ERROR;
		break;
	}

	return wbc_status;
}

/**
 * @brief Wrapper around Winbind's send/receive API call
 *
 * @param ctx       Context
 * @param cmd       Winbind command operation to perform
 * @param request   Send structure
 * @param response  Receive structure
 *
 * @return #wbcErr
 */
wbcErr wbcRequestResponse(struct wbcContext *ctx, int cmd,
			  struct winbindd_request *request,
			  struct winbindd_response *response)
{
	struct winbindd_context *wbctx = NULL;

	if (ctx) {
		wbctx = ctx->winbindd_ctx;
	}

	return wbcRequestResponseInt(wbctx, cmd, request, response,
				     winbindd_request_response);
}

wbcErr wbcRequestResponsePriv(struct wbcContext *ctx, int cmd,
			      struct winbindd_request *request,
			      struct winbindd_response *response)
{
	struct winbindd_context *wbctx = NULL;

	if (ctx) {
		wbctx = ctx->winbindd_ctx;
	}

	return wbcRequestResponseInt(wbctx, cmd, request, response,
				     winbindd_priv_request_response);
}

/** @brief Translate an error value into a string
 *
 * @param error
 *
 * @return a pointer to a static string
 **/
const char *wbcErrorString(wbcErr error)
{
	switch (error) {
	case WBC_ERR_SUCCESS:
		return "WBC_ERR_SUCCESS";
	case WBC_ERR_NOT_IMPLEMENTED:
		return "WBC_ERR_NOT_IMPLEMENTED";
	case WBC_ERR_UNKNOWN_FAILURE:
		return "WBC_ERR_UNKNOWN_FAILURE";
	case WBC_ERR_NO_MEMORY:
		return "WBC_ERR_NO_MEMORY";
	case WBC_ERR_INVALID_SID:
		return "WBC_ERR_INVALID_SID";
	case WBC_ERR_INVALID_PARAM:
		return "WBC_ERR_INVALID_PARAM";
	case WBC_ERR_WINBIND_NOT_AVAILABLE:
		return "WBC_ERR_WINBIND_NOT_AVAILABLE";
	case WBC_ERR_DOMAIN_NOT_FOUND:
		return "WBC_ERR_DOMAIN_NOT_FOUND";
	case WBC_ERR_INVALID_RESPONSE:
		return "WBC_ERR_INVALID_RESPONSE";
	case WBC_ERR_NSS_ERROR:
		return "WBC_ERR_NSS_ERROR";
	case WBC_ERR_UNKNOWN_USER:
		return "WBC_ERR_UNKNOWN_USER";
	case WBC_ERR_UNKNOWN_GROUP:
		return "WBC_ERR_UNKNOWN_GROUP";
	case WBC_ERR_AUTH_ERROR:
		return "WBC_ERR_AUTH_ERROR";
	case WBC_ERR_PWD_CHANGE_FAILED:
		return "WBC_ERR_PWD_CHANGE_FAILED";
	}

	return "unknown wbcErr value";
}

#define WBC_MAGIC (0x7a2b0e1e)
#define WBC_MAGIC_FREE (0x875634fe)

struct wbcMemPrefix {
	uint32_t magic;
	void (*destructor)(void *ptr);
};

static size_t wbcPrefixLen(void)
{
	size_t result = sizeof(struct wbcMemPrefix);
	return (result + 15) & ~15;
}

static struct wbcMemPrefix *wbcMemToPrefix(void *ptr)
{
	return (struct wbcMemPrefix *)(((char *)ptr) - wbcPrefixLen());
}

void *wbcAllocateMemory(size_t nelem, size_t elsize,
			void (*destructor)(void *ptr))
{
	struct wbcMemPrefix *result;

	if (nelem >= (2<<24)/elsize) {
		/* basic protection against integer wrap */
		return NULL;
	}

	result = (struct wbcMemPrefix *)calloc(
		1, nelem*elsize + wbcPrefixLen());
	if (result == NULL) {
		return NULL;
	}
	result->magic = WBC_MAGIC;
	result->destructor = destructor;
	return ((char *)result) + wbcPrefixLen();
}

/* Free library allocated memory */
void wbcFreeMemory(void *p)
{
	struct wbcMemPrefix *wbcMem;

	if (p == NULL) {
		return;
	}
	wbcMem = wbcMemToPrefix(p);
	if (wbcMem->magic != WBC_MAGIC) {
		return;
	}

	/* paranoid check to ensure we don't double free */
	wbcMem->magic = WBC_MAGIC_FREE;

	if (wbcMem->destructor != NULL) {
		wbcMem->destructor(p);
	}
	free(wbcMem);
	return;
}

char *wbcStrDup(const char *str)
{
	char *result;
	size_t len;

	len = strlen(str);
	result = (char *)wbcAllocateMemory(len+1, sizeof(char), NULL);
	if (result == NULL) {
		return NULL;
	}
	memcpy(result, str, len+1);
	return result;
}

static void wbcStringArrayDestructor(void *ptr)
{
	char **p = (char **)ptr;
	while (*p != NULL) {
		free(*p);
		p += 1;
	}
}

const char **wbcAllocateStringArray(int num_strings)
{
	return (const char **)wbcAllocateMemory(
		num_strings + 1, sizeof(const char *),
		wbcStringArrayDestructor);
}

wbcErr wbcLibraryDetails(struct wbcLibraryDetails **_details)
{
	struct wbcLibraryDetails *info;

	info = (struct wbcLibraryDetails *)wbcAllocateMemory(
		1, sizeof(struct wbcLibraryDetails), NULL);

	if (info == NULL) {
		return WBC_ERR_NO_MEMORY;
	}

	info->major_version = WBCLIENT_MAJOR_VERSION;
	info->minor_version = WBCLIENT_MINOR_VERSION;
	info->vendor_version = WBCLIENT_VENDOR_VERSION;

	*_details = info;
	return WBC_ERR_SUCCESS;
}

/* Context handling functions */

static void wbcContextDestructor(void *ptr)
{
	struct wbcContext *ctx = (struct wbcContext *)ptr;

	winbindd_ctx_free(ctx->winbindd_ctx);
}

struct wbcContext *wbcCtxCreate(void)
{
	struct wbcContext *ctx;
	struct winbindd_context *wbctx;

	ctx = (struct wbcContext *)wbcAllocateMemory(
		1, sizeof(struct wbcContext), wbcContextDestructor);

	if (!ctx) {
		return NULL;
	}

	wbctx = winbindd_ctx_create();

	if (!wbctx) {
		wbcFreeMemory(ctx);
		return NULL;
	}

	ctx->winbindd_ctx = wbctx;

	return ctx;
}

void wbcCtxFree(struct wbcContext *ctx)
{
	wbcFreeMemory(ctx);
}