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