Line data Source code
1 : /*
2 : Unix SMB/CIFS implementation.
3 :
4 : Winbind client API
5 :
6 : Copyright (C) Gerald (Jerry) Carter 2007
7 :
8 :
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.
13 :
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.
18 :
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/>.
21 : */
22 :
23 : /* Required Headers */
24 :
25 : #include "libwbclient.h"
26 :
27 : /** @brief Translate an error value into a string
28 : *
29 : * @param error
30 : *
31 : * @return a pointer to a static string
32 : **/
33 0 : const char *wbcErrorString(wbcErr error)
34 : {
35 0 : switch (error) {
36 : case WBC_ERR_SUCCESS:
37 0 : return "WBC_ERR_SUCCESS";
38 : case WBC_ERR_NOT_IMPLEMENTED:
39 0 : return "WBC_ERR_NOT_IMPLEMENTED";
40 : case WBC_ERR_UNKNOWN_FAILURE:
41 0 : return "WBC_ERR_UNKNOWN_FAILURE";
42 : case WBC_ERR_NO_MEMORY:
43 0 : return "WBC_ERR_NO_MEMORY";
44 : case WBC_ERR_INVALID_SID:
45 0 : return "WBC_ERR_INVALID_SID";
46 : case WBC_ERR_INVALID_PARAM:
47 0 : return "WBC_ERR_INVALID_PARAM";
48 : case WBC_ERR_WINBIND_NOT_AVAILABLE:
49 0 : return "WBC_ERR_WINBIND_NOT_AVAILABLE";
50 : case WBC_ERR_DOMAIN_NOT_FOUND:
51 0 : return "WBC_ERR_DOMAIN_NOT_FOUND";
52 : case WBC_ERR_INVALID_RESPONSE:
53 0 : return "WBC_ERR_INVALID_RESPONSE";
54 : case WBC_ERR_NSS_ERROR:
55 0 : return "WBC_ERR_NSS_ERROR";
56 : case WBC_ERR_UNKNOWN_USER:
57 0 : return "WBC_ERR_UNKNOWN_USER";
58 : case WBC_ERR_UNKNOWN_GROUP:
59 0 : return "WBC_ERR_UNKNOWN_GROUP";
60 : case WBC_ERR_AUTH_ERROR:
61 0 : return "WBC_ERR_AUTH_ERROR";
62 : case WBC_ERR_PWD_CHANGE_FAILED:
63 0 : return "WBC_ERR_PWD_CHANGE_FAILED";
64 : }
65 :
66 0 : return "unknown wbcErr value";
67 : }
68 :
69 : #define WBC_MAGIC (0x7a2b0e1e)
70 : #define WBC_MAGIC_FREE (0x875634fe)
71 :
72 : struct wbcMemPrefix {
73 : uint32_t magic;
74 : void (*destructor)(void *ptr);
75 : };
76 :
77 0 : static size_t wbcPrefixLen(void)
78 : {
79 0 : size_t result = sizeof(struct wbcMemPrefix);
80 0 : return (result + 15) & ~15;
81 : }
82 :
83 0 : static struct wbcMemPrefix *wbcMemToPrefix(void *ptr)
84 : {
85 0 : return (struct wbcMemPrefix *)((void *)(((char *)ptr) - wbcPrefixLen()));
86 : }
87 :
88 0 : void *wbcAllocateMemory(size_t nelem, size_t elsize,
89 : void (*destructor)(void *ptr))
90 : {
91 : struct wbcMemPrefix *result;
92 :
93 0 : if (nelem >= (2<<24)/elsize) {
94 : /* basic protection against integer wrap */
95 0 : return NULL;
96 : }
97 :
98 0 : result = (struct wbcMemPrefix *)calloc(
99 0 : 1, nelem*elsize + wbcPrefixLen());
100 0 : if (result == NULL) {
101 0 : return NULL;
102 : }
103 0 : result->magic = WBC_MAGIC;
104 0 : result->destructor = destructor;
105 0 : return ((char *)result) + wbcPrefixLen();
106 : }
107 :
108 : /* Free library allocated memory */
109 0 : void wbcFreeMemory(void *p)
110 : {
111 : struct wbcMemPrefix *wbcMem;
112 :
113 0 : if (p == NULL) {
114 0 : return;
115 : }
116 0 : wbcMem = wbcMemToPrefix(p);
117 0 : if (wbcMem->magic != WBC_MAGIC) {
118 0 : return;
119 : }
120 :
121 : /* paranoid check to ensure we don't double free */
122 0 : wbcMem->magic = WBC_MAGIC_FREE;
123 :
124 0 : if (wbcMem->destructor != NULL) {
125 0 : wbcMem->destructor(p);
126 : }
127 0 : free(wbcMem);
128 0 : return;
129 : }
130 :
131 0 : char *wbcStrDup(const char *str)
132 : {
133 : char *result;
134 : size_t len;
135 :
136 0 : len = strlen(str);
137 0 : result = (char *)wbcAllocateMemory(len+1, sizeof(char), NULL);
138 0 : if (result == NULL) {
139 0 : return NULL;
140 : }
141 0 : memcpy(result, str, len+1);
142 0 : return result;
143 : }
144 :
145 0 : static void wbcStringArrayDestructor(void *ptr)
146 : {
147 0 : char **p = (char **)ptr;
148 0 : while (*p != NULL) {
149 0 : free(*p);
150 0 : p += 1;
151 : }
152 0 : }
153 :
154 0 : const char **wbcAllocateStringArray(int num_strings)
155 : {
156 0 : return (const char **)wbcAllocateMemory(
157 0 : num_strings + 1, sizeof(const char *),
158 : wbcStringArrayDestructor);
159 : }
160 :
161 0 : wbcErr wbcLibraryDetails(struct wbcLibraryDetails **_details)
162 : {
163 : struct wbcLibraryDetails *info;
164 :
165 0 : info = (struct wbcLibraryDetails *)wbcAllocateMemory(
166 : 1, sizeof(struct wbcLibraryDetails), NULL);
167 :
168 0 : if (info == NULL) {
169 0 : return WBC_ERR_NO_MEMORY;
170 : }
171 :
172 0 : info->major_version = WBCLIENT_MAJOR_VERSION;
173 0 : info->minor_version = WBCLIENT_MINOR_VERSION;
174 0 : info->vendor_version = WBCLIENT_VENDOR_VERSION;
175 :
176 0 : *_details = info;
177 0 : return WBC_ERR_SUCCESS;
178 : }
|