libwbclient: Add wbcAllocateMemory()
[Samba/gebeck_regimport.git] / nsswitch / libwbclient / wbclient.c
blob6b076ad499d0e6a021c61c93015fe73e010d41df
1 /*
2 Unix SMB/CIFS implementation.
4 Winbind client API
6 Copyright (C) Gerald (Jerry) Carter 2007
9 This library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Lesser General Public
11 License as published by the Free Software Foundation; either
12 version 3 of the License, or (at your option) any later version.
14 This library is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Library General Public License for more details.
19 You should have received a copy of the GNU Lesser General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 /* Required Headers */
25 #include "replace.h"
26 #include "libwbclient.h"
28 /* From wb_common.c */
30 NSS_STATUS winbindd_request_response(int req_type,
31 struct winbindd_request *request,
32 struct winbindd_response *response);
33 NSS_STATUS winbindd_priv_request_response(int req_type,
34 struct winbindd_request *request,
35 struct winbindd_response *response);
37 /** @brief Wrapper around Winbind's send/receive API call
39 * @param cmd Winbind command operation to perform
40 * @param request Send structure
41 * @param response Receive structure
43 * @return #wbcErr
44 **/
46 /**********************************************************************
47 result == NSS_STATUS_UNAVAIL: winbind not around
48 result == NSS_STATUS_NOTFOUND: winbind around, but domain missing
50 Due to a bad API NSS_STATUS_NOTFOUND is returned both when winbind_off
51 and when winbind return WINBINDD_ERROR. So the semantics of this
52 routine depends on winbind_on. Grepping for winbind_off I just
53 found 3 places where winbind is turned off, and this does not conflict
54 (as far as I have seen) with the callers of is_trusted_domains.
56 --Volker
57 **********************************************************************/
59 static wbcErr wbcRequestResponseInt(
60 int cmd,
61 struct winbindd_request *request,
62 struct winbindd_response *response,
63 NSS_STATUS (*fn)(int req_type,
64 struct winbindd_request *request,
65 struct winbindd_response *response))
67 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
68 NSS_STATUS nss_status;
70 /* for some calls the request and/or response can be NULL */
72 nss_status = fn(cmd, request, response);
74 switch (nss_status) {
75 case NSS_STATUS_SUCCESS:
76 wbc_status = WBC_ERR_SUCCESS;
77 break;
78 case NSS_STATUS_UNAVAIL:
79 wbc_status = WBC_ERR_WINBIND_NOT_AVAILABLE;
80 break;
81 case NSS_STATUS_NOTFOUND:
82 wbc_status = WBC_ERR_DOMAIN_NOT_FOUND;
83 break;
84 default:
85 wbc_status = WBC_ERR_NSS_ERROR;
86 break;
89 return wbc_status;
92 wbcErr wbcRequestResponse(int cmd,
93 struct winbindd_request *request,
94 struct winbindd_response *response)
96 return wbcRequestResponseInt(cmd, request, response,
97 winbindd_request_response);
100 wbcErr wbcRequestResponsePriv(int cmd,
101 struct winbindd_request *request,
102 struct winbindd_response *response)
104 return wbcRequestResponseInt(cmd, request, response,
105 winbindd_priv_request_response);
108 /** @brief Translate an error value into a string
110 * @param error
112 * @return a pointer to a static string
114 const char *wbcErrorString(wbcErr error)
116 switch (error) {
117 case WBC_ERR_SUCCESS:
118 return "WBC_ERR_SUCCESS";
119 case WBC_ERR_NOT_IMPLEMENTED:
120 return "WBC_ERR_NOT_IMPLEMENTED";
121 case WBC_ERR_UNKNOWN_FAILURE:
122 return "WBC_ERR_UNKNOWN_FAILURE";
123 case WBC_ERR_NO_MEMORY:
124 return "WBC_ERR_NO_MEMORY";
125 case WBC_ERR_INVALID_SID:
126 return "WBC_ERR_INVALID_SID";
127 case WBC_ERR_INVALID_PARAM:
128 return "WBC_ERR_INVALID_PARAM";
129 case WBC_ERR_WINBIND_NOT_AVAILABLE:
130 return "WBC_ERR_WINBIND_NOT_AVAILABLE";
131 case WBC_ERR_DOMAIN_NOT_FOUND:
132 return "WBC_ERR_DOMAIN_NOT_FOUND";
133 case WBC_ERR_INVALID_RESPONSE:
134 return "WBC_ERR_INVALID_RESPONSE";
135 case WBC_ERR_NSS_ERROR:
136 return "WBC_ERR_NSS_ERROR";
137 case WBC_ERR_UNKNOWN_USER:
138 return "WBC_ERR_UNKNOWN_USER";
139 case WBC_ERR_UNKNOWN_GROUP:
140 return "WBC_ERR_UNKNOWN_GROUP";
141 case WBC_ERR_AUTH_ERROR:
142 return "WBC_ERR_AUTH_ERROR";
143 case WBC_ERR_PWD_CHANGE_FAILED:
144 return "WBC_ERR_PWD_CHANGE_FAILED";
147 return "unknown wbcErr value";
150 #define WBC_MAGIC (0x7a2b0e1e)
152 struct wbcMemPrefix {
153 uint32_t magic;
154 void (*destructor)(void *ptr);
157 static size_t wbcPrefixLen(void)
159 size_t result = sizeof(struct wbcMemPrefix);
160 return (result + 15) & ~15;
163 static struct wbcMemPrefix *wbcMemToPrefix(void *ptr)
165 return (struct wbcMemPrefix *)(((char *)ptr) - wbcPrefixLen());
168 void *wbcAllocateMemory(size_t nelem, size_t elsize,
169 void (*destructor)(void *ptr))
171 struct wbcMemPrefix *result;
173 if (nelem >= (2<<24)/elsize) {
174 /* basic protection against integer wrap */
175 return NULL;
178 result = (struct wbcMemPrefix *)calloc(
179 1, nelem*elsize + wbcPrefixLen());
180 if (result == NULL) {
181 return NULL;
183 result->magic = WBC_MAGIC;
184 result->destructor = destructor;
185 return ((char *)result) + wbcPrefixLen();
188 /* Free library allocated memory */
189 void wbcFreeMemory(void *p)
191 struct wbcMemPrefix *wbcMem;
193 if (p == NULL) {
194 return;
196 wbcMem = wbcMemToPrefix(p);
197 if (wbcMem->magic != WBC_MAGIC) {
198 talloc_free(p);
199 return;
201 if (wbcMem->destructor != NULL) {
202 wbcMem->destructor(p);
204 free(wbcMem);
205 return;
208 wbcErr wbcLibraryDetails(struct wbcLibraryDetails **_details)
210 struct wbcLibraryDetails *info;
212 info = talloc(NULL, struct wbcLibraryDetails);
213 if (info == NULL) {
214 return WBC_ERR_NO_MEMORY;
217 info->major_version = WBCLIENT_MAJOR_VERSION;
218 info->minor_version = WBCLIENT_MINOR_VERSION;
219 info->vendor_version = WBCLIENT_VENDOR_VERSION;
221 *_details = info;
222 return WBC_ERR_SUCCESS;