| 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 "replace.h" | 
|---|
| 26 | #include "libwbclient.h" | 
|---|
| 27 |  | 
|---|
| 28 | /* From wb_common.c */ | 
|---|
| 29 |  | 
|---|
| 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); | 
|---|
| 36 |  | 
|---|
| 37 | /* | 
|---|
| 38 | result == NSS_STATUS_UNAVAIL: winbind not around | 
|---|
| 39 | result == NSS_STATUS_NOTFOUND: winbind around, but domain missing | 
|---|
| 40 |  | 
|---|
| 41 | Due to a bad API NSS_STATUS_NOTFOUND is returned both when winbind_off | 
|---|
| 42 | and when winbind return WINBINDD_ERROR. So the semantics of this | 
|---|
| 43 | routine depends on winbind_on. Grepping for winbind_off I just | 
|---|
| 44 | found 3 places where winbind is turned off, and this does not conflict | 
|---|
| 45 | (as far as I have seen) with the callers of is_trusted_domains. | 
|---|
| 46 |  | 
|---|
| 47 | --Volker | 
|---|
| 48 | */ | 
|---|
| 49 |  | 
|---|
| 50 | static wbcErr wbcRequestResponseInt( | 
|---|
| 51 | int cmd, | 
|---|
| 52 | struct winbindd_request *request, | 
|---|
| 53 | struct winbindd_response *response, | 
|---|
| 54 | NSS_STATUS (*fn)(int req_type, | 
|---|
| 55 | struct winbindd_request *request, | 
|---|
| 56 | struct winbindd_response *response)) | 
|---|
| 57 | { | 
|---|
| 58 | wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE; | 
|---|
| 59 | NSS_STATUS nss_status; | 
|---|
| 60 |  | 
|---|
| 61 | /* for some calls the request and/or response can be NULL */ | 
|---|
| 62 |  | 
|---|
| 63 | nss_status = fn(cmd, request, response); | 
|---|
| 64 |  | 
|---|
| 65 | switch (nss_status) { | 
|---|
| 66 | case NSS_STATUS_SUCCESS: | 
|---|
| 67 | wbc_status = WBC_ERR_SUCCESS; | 
|---|
| 68 | break; | 
|---|
| 69 | case NSS_STATUS_UNAVAIL: | 
|---|
| 70 | wbc_status = WBC_ERR_WINBIND_NOT_AVAILABLE; | 
|---|
| 71 | break; | 
|---|
| 72 | case NSS_STATUS_NOTFOUND: | 
|---|
| 73 | wbc_status = WBC_ERR_DOMAIN_NOT_FOUND; | 
|---|
| 74 | break; | 
|---|
| 75 | default: | 
|---|
| 76 | wbc_status = WBC_ERR_NSS_ERROR; | 
|---|
| 77 | break; | 
|---|
| 78 | } | 
|---|
| 79 |  | 
|---|
| 80 | return wbc_status; | 
|---|
| 81 | } | 
|---|
| 82 |  | 
|---|
| 83 | /** | 
|---|
| 84 | * @brief Wrapper around Winbind's send/receive API call | 
|---|
| 85 | * | 
|---|
| 86 | * @param cmd       Winbind command operation to perform | 
|---|
| 87 | * @param request   Send structure | 
|---|
| 88 | * @param response  Receive structure | 
|---|
| 89 | * | 
|---|
| 90 | * @return #wbcErr | 
|---|
| 91 | */ | 
|---|
| 92 | wbcErr wbcRequestResponse(int cmd, | 
|---|
| 93 | struct winbindd_request *request, | 
|---|
| 94 | struct winbindd_response *response) | 
|---|
| 95 | { | 
|---|
| 96 | return wbcRequestResponseInt(cmd, request, response, | 
|---|
| 97 | winbindd_request_response); | 
|---|
| 98 | } | 
|---|
| 99 |  | 
|---|
| 100 | wbcErr wbcRequestResponsePriv(int cmd, | 
|---|
| 101 | struct winbindd_request *request, | 
|---|
| 102 | struct winbindd_response *response) | 
|---|
| 103 | { | 
|---|
| 104 | return wbcRequestResponseInt(cmd, request, response, | 
|---|
| 105 | winbindd_priv_request_response); | 
|---|
| 106 | } | 
|---|
| 107 |  | 
|---|
| 108 | /** @brief Translate an error value into a string | 
|---|
| 109 | * | 
|---|
| 110 | * @param error | 
|---|
| 111 | * | 
|---|
| 112 | * @return a pointer to a static string | 
|---|
| 113 | **/ | 
|---|
| 114 | const char *wbcErrorString(wbcErr error) | 
|---|
| 115 | { | 
|---|
| 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"; | 
|---|
| 145 | } | 
|---|
| 146 |  | 
|---|
| 147 | return "unknown wbcErr value"; | 
|---|
| 148 | } | 
|---|
| 149 |  | 
|---|
| 150 | #define WBC_MAGIC (0x7a2b0e1e) | 
|---|
| 151 | #define WBC_MAGIC_FREE (0x875634fe) | 
|---|
| 152 |  | 
|---|
| 153 | struct wbcMemPrefix { | 
|---|
| 154 | uint32_t magic; | 
|---|
| 155 | void (*destructor)(void *ptr); | 
|---|
| 156 | }; | 
|---|
| 157 |  | 
|---|
| 158 | static size_t wbcPrefixLen(void) | 
|---|
| 159 | { | 
|---|
| 160 | size_t result = sizeof(struct wbcMemPrefix); | 
|---|
| 161 | return (result + 15) & ~15; | 
|---|
| 162 | } | 
|---|
| 163 |  | 
|---|
| 164 | static struct wbcMemPrefix *wbcMemToPrefix(void *ptr) | 
|---|
| 165 | { | 
|---|
| 166 | return (struct wbcMemPrefix *)(((char *)ptr) - wbcPrefixLen()); | 
|---|
| 167 | } | 
|---|
| 168 |  | 
|---|
| 169 | void *wbcAllocateMemory(size_t nelem, size_t elsize, | 
|---|
| 170 | void (*destructor)(void *ptr)) | 
|---|
| 171 | { | 
|---|
| 172 | struct wbcMemPrefix *result; | 
|---|
| 173 |  | 
|---|
| 174 | if (nelem >= (2<<24)/elsize) { | 
|---|
| 175 | /* basic protection against integer wrap */ | 
|---|
| 176 | return NULL; | 
|---|
| 177 | } | 
|---|
| 178 |  | 
|---|
| 179 | result = (struct wbcMemPrefix *)calloc( | 
|---|
| 180 | 1, nelem*elsize + wbcPrefixLen()); | 
|---|
| 181 | if (result == NULL) { | 
|---|
| 182 | return NULL; | 
|---|
| 183 | } | 
|---|
| 184 | result->magic = WBC_MAGIC; | 
|---|
| 185 | result->destructor = destructor; | 
|---|
| 186 | return ((char *)result) + wbcPrefixLen(); | 
|---|
| 187 | } | 
|---|
| 188 |  | 
|---|
| 189 | /* Free library allocated memory */ | 
|---|
| 190 | void wbcFreeMemory(void *p) | 
|---|
| 191 | { | 
|---|
| 192 | struct wbcMemPrefix *wbcMem; | 
|---|
| 193 |  | 
|---|
| 194 | if (p == NULL) { | 
|---|
| 195 | return; | 
|---|
| 196 | } | 
|---|
| 197 | wbcMem = wbcMemToPrefix(p); | 
|---|
| 198 | if (wbcMem->magic != WBC_MAGIC) { | 
|---|
| 199 | return; | 
|---|
| 200 | } | 
|---|
| 201 |  | 
|---|
| 202 | /* paranoid check to ensure we don't double free */ | 
|---|
| 203 | wbcMem->magic = WBC_MAGIC_FREE; | 
|---|
| 204 |  | 
|---|
| 205 | if (wbcMem->destructor != NULL) { | 
|---|
| 206 | wbcMem->destructor(p); | 
|---|
| 207 | } | 
|---|
| 208 | free(wbcMem); | 
|---|
| 209 | return; | 
|---|
| 210 | } | 
|---|
| 211 |  | 
|---|
| 212 | char *wbcStrDup(const char *str) | 
|---|
| 213 | { | 
|---|
| 214 | char *result; | 
|---|
| 215 | size_t len; | 
|---|
| 216 |  | 
|---|
| 217 | len = strlen(str); | 
|---|
| 218 | result = (char *)wbcAllocateMemory(len+1, sizeof(char), NULL); | 
|---|
| 219 | if (result == NULL) { | 
|---|
| 220 | return NULL; | 
|---|
| 221 | } | 
|---|
| 222 | memcpy(result, str, len+1); | 
|---|
| 223 | return result; | 
|---|
| 224 | } | 
|---|
| 225 |  | 
|---|
| 226 | static void wbcStringArrayDestructor(void *ptr) | 
|---|
| 227 | { | 
|---|
| 228 | char **p = (char **)ptr; | 
|---|
| 229 | while (*p != NULL) { | 
|---|
| 230 | free(*p); | 
|---|
| 231 | p += 1; | 
|---|
| 232 | } | 
|---|
| 233 | } | 
|---|
| 234 |  | 
|---|
| 235 | const char **wbcAllocateStringArray(int num_strings) | 
|---|
| 236 | { | 
|---|
| 237 | return (const char **)wbcAllocateMemory( | 
|---|
| 238 | num_strings + 1, sizeof(const char *), | 
|---|
| 239 | wbcStringArrayDestructor); | 
|---|
| 240 | } | 
|---|
| 241 |  | 
|---|
| 242 | wbcErr wbcLibraryDetails(struct wbcLibraryDetails **_details) | 
|---|
| 243 | { | 
|---|
| 244 | struct wbcLibraryDetails *info; | 
|---|
| 245 |  | 
|---|
| 246 | info = (struct wbcLibraryDetails *)wbcAllocateMemory( | 
|---|
| 247 | 1, sizeof(struct wbcLibraryDetails), NULL); | 
|---|
| 248 |  | 
|---|
| 249 | if (info == NULL) { | 
|---|
| 250 | return WBC_ERR_NO_MEMORY; | 
|---|
| 251 | } | 
|---|
| 252 |  | 
|---|
| 253 | info->major_version = WBCLIENT_MAJOR_VERSION; | 
|---|
| 254 | info->minor_version = WBCLIENT_MINOR_VERSION; | 
|---|
| 255 | info->vendor_version = WBCLIENT_VENDOR_VERSION; | 
|---|
| 256 |  | 
|---|
| 257 | *_details = info; | 
|---|
| 258 | return WBC_ERR_SUCCESS; | 
|---|
| 259 | } | 
|---|