Add wbcContext struct, create and free functions
[samba.git] / nsswitch / libwbclient / wbclient.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    Winbind client API
5
6    Copyright (C) Gerald (Jerry) Carter 2007
7    Copyright (C) Matthew Newton 2015
8
9
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.
14
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.
19
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/>.
22 */
23
24 /* Required Headers */
25
26 #include "replace.h"
27 #include "libwbclient.h"
28
29 /* From wb_common.c */
30
31 struct winbindd_context;
32
33 NSS_STATUS winbindd_request_response(struct winbindd_context *wbctx,
34                                      int req_type,
35                                      struct winbindd_request *request,
36                                      struct winbindd_response *response);
37 NSS_STATUS winbindd_priv_request_response(struct winbindd_context *wbctx,
38                                           int req_type,
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);
43
44
45 /*
46  result == NSS_STATUS_UNAVAIL: winbind not around
47  result == NSS_STATUS_NOTFOUND: winbind around, but domain missing
48
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.
54
55  --Volker
56 */
57
58 static wbcErr wbcRequestResponseInt(
59         int cmd,
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))
65 {
66         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
67         NSS_STATUS nss_status;
68
69         /* for some calls the request and/or response can be NULL */
70
71         nss_status = fn(NULL, cmd, request, response);
72
73         switch (nss_status) {
74         case NSS_STATUS_SUCCESS:
75                 wbc_status = WBC_ERR_SUCCESS;
76                 break;
77         case NSS_STATUS_UNAVAIL:
78                 wbc_status = WBC_ERR_WINBIND_NOT_AVAILABLE;
79                 break;
80         case NSS_STATUS_NOTFOUND:
81                 wbc_status = WBC_ERR_DOMAIN_NOT_FOUND;
82                 break;
83         default:
84                 wbc_status = WBC_ERR_NSS_ERROR;
85                 break;
86         }
87
88         return wbc_status;
89 }
90
91 /**
92  * @brief Wrapper around Winbind's send/receive API call
93  *
94  * @param cmd       Winbind command operation to perform
95  * @param request   Send structure
96  * @param response  Receive structure
97  *
98  * @return #wbcErr
99  */
100 wbcErr wbcRequestResponse(int cmd,
101                           struct winbindd_request *request,
102                           struct winbindd_response *response)
103 {
104         return wbcRequestResponseInt(cmd, request, response,
105                                      winbindd_request_response);
106 }
107
108 wbcErr wbcRequestResponsePriv(int cmd,
109                               struct winbindd_request *request,
110                               struct winbindd_response *response)
111 {
112         return wbcRequestResponseInt(cmd, request, response,
113                                      winbindd_priv_request_response);
114 }
115
116 /** @brief Translate an error value into a string
117  *
118  * @param error
119  *
120  * @return a pointer to a static string
121  **/
122 const char *wbcErrorString(wbcErr error)
123 {
124         switch (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";
153         }
154
155         return "unknown wbcErr value";
156 }
157
158 #define WBC_MAGIC (0x7a2b0e1e)
159 #define WBC_MAGIC_FREE (0x875634fe)
160
161 struct wbcMemPrefix {
162         uint32_t magic;
163         void (*destructor)(void *ptr);
164 };
165
166 static size_t wbcPrefixLen(void)
167 {
168         size_t result = sizeof(struct wbcMemPrefix);
169         return (result + 15) & ~15;
170 }
171
172 static struct wbcMemPrefix *wbcMemToPrefix(void *ptr)
173 {
174         return (struct wbcMemPrefix *)(((char *)ptr) - wbcPrefixLen());
175 }
176
177 void *wbcAllocateMemory(size_t nelem, size_t elsize,
178                         void (*destructor)(void *ptr))
179 {
180         struct wbcMemPrefix *result;
181
182         if (nelem >= (2<<24)/elsize) {
183                 /* basic protection against integer wrap */
184                 return NULL;
185         }
186
187         result = (struct wbcMemPrefix *)calloc(
188                 1, nelem*elsize + wbcPrefixLen());
189         if (result == NULL) {
190                 return NULL;
191         }
192         result->magic = WBC_MAGIC;
193         result->destructor = destructor;
194         return ((char *)result) + wbcPrefixLen();
195 }
196
197 /* Free library allocated memory */
198 void wbcFreeMemory(void *p)
199 {
200         struct wbcMemPrefix *wbcMem;
201
202         if (p == NULL) {
203                 return;
204         }
205         wbcMem = wbcMemToPrefix(p);
206         if (wbcMem->magic != WBC_MAGIC) {
207                 return;
208         }
209
210         /* paranoid check to ensure we don't double free */
211         wbcMem->magic = WBC_MAGIC_FREE;
212
213         if (wbcMem->destructor != NULL) {
214                 wbcMem->destructor(p);
215         }
216         free(wbcMem);
217         return;
218 }
219
220 char *wbcStrDup(const char *str)
221 {
222         char *result;
223         size_t len;
224
225         len = strlen(str);
226         result = (char *)wbcAllocateMemory(len+1, sizeof(char), NULL);
227         if (result == NULL) {
228                 return NULL;
229         }
230         memcpy(result, str, len+1);
231         return result;
232 }
233
234 static void wbcStringArrayDestructor(void *ptr)
235 {
236         char **p = (char **)ptr;
237         while (*p != NULL) {
238                 free(*p);
239                 p += 1;
240         }
241 }
242
243 const char **wbcAllocateStringArray(int num_strings)
244 {
245         return (const char **)wbcAllocateMemory(
246                 num_strings + 1, sizeof(const char *),
247                 wbcStringArrayDestructor);
248 }
249
250 wbcErr wbcLibraryDetails(struct wbcLibraryDetails **_details)
251 {
252         struct wbcLibraryDetails *info;
253
254         info = (struct wbcLibraryDetails *)wbcAllocateMemory(
255                 1, sizeof(struct wbcLibraryDetails), NULL);
256
257         if (info == NULL) {
258                 return WBC_ERR_NO_MEMORY;
259         }
260
261         info->major_version = WBCLIENT_MAJOR_VERSION;
262         info->minor_version = WBCLIENT_MINOR_VERSION;
263         info->vendor_version = WBCLIENT_VENDOR_VERSION;
264
265         *_details = info;
266         return WBC_ERR_SUCCESS;
267 }
268
269 /* Context handling functions */
270
271 static void wbcContextDestructor(void *ptr)
272 {
273         struct wbcContext *ctx = (struct wbcContext *)ptr;
274
275         winbindd_ctx_free(ctx->winbindd_ctx);
276 }
277
278 struct wbcContext *wbcCtxCreate(void)
279 {
280         struct wbcContext *ctx;
281         struct winbindd_context *wbctx;
282
283         ctx = (struct wbcContext *)wbcAllocateMemory(
284                 1, sizeof(struct wbcContext), wbcContextDestructor);
285
286         if (!ctx) {
287                 return NULL;
288         }
289
290         wbctx = winbindd_ctx_create();
291
292         if (!wbctx) {
293                 wbcFreeMemory(ctx);
294                 return NULL;
295         }
296
297         ctx->winbindd_ctx = wbctx;
298
299         return ctx;
300 }
301
302 void wbcCtxFree(struct wbcContext *ctx)
303 {
304         wbcFreeMemory(ctx);
305 }