3a9afad15d4c49ab0286c8e877bf2ed11bd73427
[jra/samba/.git] / source3 / nsswitch / libwbclient / wbclient.c
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 /* From wb_common.c */
28
29 NSS_STATUS winbindd_request_response(int req_type,
30                                      struct winbindd_request *request,
31                                      struct winbindd_response *response);
32
33 /** @brief Wrapper around Winbind's send/receive API call
34  *
35  * @param cmd       Winbind command operation to perform
36  * @param request   Send structure
37  * @param response  Receive structure
38  *
39  * @return #wbcErr
40  **/
41
42 /**********************************************************************
43  result == NSS_STATUS_UNAVAIL: winbind not around
44  result == NSS_STATUS_NOTFOUND: winbind around, but domain missing
45
46  Due to a bad API NSS_STATUS_NOTFOUND is returned both when winbind_off
47  and when winbind return WINBINDD_ERROR. So the semantics of this
48  routine depends on winbind_on. Grepping for winbind_off I just
49  found 3 places where winbind is turned off, and this does not conflict
50  (as far as I have seen) with the callers of is_trusted_domains.
51
52  --Volker
53 **********************************************************************/
54
55 wbcErr wbcRequestResponse(int cmd,
56                           struct winbindd_request *request,
57                           struct winbindd_response *response)
58 {
59         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
60         NSS_STATUS nss_status;
61
62         /* for some calls the request and/or response can be NULL */
63
64         nss_status = winbindd_request_response(cmd, request, response);
65
66         switch (nss_status) {
67         case NSS_STATUS_SUCCESS:
68                 wbc_status = WBC_ERR_SUCCESS;
69                 break;
70         case NSS_STATUS_UNAVAIL:
71                 wbc_status = WBC_ERR_WINBIND_NOT_AVAILABLE;
72                 break;
73         case NSS_STATUS_NOTFOUND:
74                 wbc_status = WBC_ERR_DOMAIN_NOT_FOUND;
75                 break;
76         default:
77                 wbc_status = WBC_ERR_NSS_ERROR;
78                 break;
79         }
80
81         return wbc_status;
82 }
83
84 /** @brief Translate an error value into a string
85  *
86  * @param error
87  *
88  * @return a pointer to a static string
89  **/
90 const char *wbcErrorString(wbcErr error)
91 {
92         switch (error) {
93         case WBC_ERR_SUCCESS:
94                 return "WBC_ERR_SUCCESS";
95         case WBC_ERR_NOT_IMPLEMENTED:
96                 return "WBC_ERR_NOT_IMPLEMENTED";
97         case WBC_ERR_UNKNOWN_FAILURE:
98                 return "WBC_ERR_UNKNOWN_FAILURE";
99         case WBC_ERR_NO_MEMORY:
100                 return "WBC_ERR_NO_MEMORY";
101         case WBC_ERR_INVALID_SID:
102                 return "WBC_ERR_INVALID_SID";
103         case WBC_ERR_INVALID_PARAM:
104                 return "WBC_ERR_INVALID_PARAM";
105         case WBC_ERR_WINBIND_NOT_AVAILABLE:
106                 return "WBC_ERR_WINBIND_NOT_AVAILABLE";
107         case WBC_ERR_DOMAIN_NOT_FOUND:
108                 return "WBC_ERR_DOMAIN_NOT_FOUND";
109         case WBC_ERR_INVALID_RESPONSE:
110                 return "WBC_ERR_INVALID_RESPONSE";
111         case WBC_ERR_NSS_ERROR:
112                 return "WBC_ERR_NSS_ERROR";
113         case WBC_ERR_UNKNOWN_USER:
114                 return "WBC_ERR_UNKNOWN_USER";
115         case WBC_ERR_UNKNOWN_GROUP:
116                 return "WBC_ERR_UNKNOWN_GROUP";
117         case WBC_ERR_AUTH_ERROR:
118                 return "WBC_ERR_AUTH_ERROR";
119         case WBC_ERR_PWD_CHANGE_FAILED:
120                 return "WBC_ERR_PWD_CHANGE_FAILED";
121         }
122
123         return "unknown wbcErr value";
124 }
125
126 /* Free library allocated memory */
127 void wbcFreeMemory(void *p)
128 {
129         if (p)
130                 talloc_free(p);
131
132         return;
133 }
134
135 wbcErr wbcLibraryDetails(struct wbcLibraryDetails **_details)
136 {
137         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
138         struct wbcLibraryDetails *info;
139
140         info = talloc(NULL, struct wbcLibraryDetails);
141         BAIL_ON_PTR_ERROR(info, wbc_status);
142
143         info->major_version = WBCLIENT_MAJOR_VERSION;
144         info->minor_version = WBCLIENT_MINOR_VERSION;
145         info->vendor_version = talloc_strdup(info,
146                                              WBCLIENT_VENDOR_VERSION);
147         BAIL_ON_PTR_ERROR(info->vendor_version, wbc_status);
148
149         *_details = info;
150         info = NULL;
151
152         wbc_status = WBC_ERR_SUCCESS;
153
154 done:
155         talloc_free(info);
156         return wbc_status;
157 }
158
159