Merge branch 'v3-2-test' of ssh://git.samba.org/data/git/samba into v3-2-test
[samba.git] / source3 / nsswitch / libwbclient / wbc_util.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
28
29 /** @brief Ping winbindd to see if the daemon is running
30  *
31  * @return #wbcErr
32  **/
33
34 wbcErr wbcPing(void)
35 {
36         struct winbindd_request request;
37         struct winbindd_response response;
38
39         /* Initialize request */
40
41         ZERO_STRUCT(request);
42         ZERO_STRUCT(response);
43
44         return wbcRequestResponse(WINBINDD_PING, &request, &response);
45 }
46
47 /** @brief Lookup the current status of a trusted domain
48  *
49  * @param domain      Domain to query
50  * @param *dinfo       Pointer to returned domain_info struct
51  *
52  * @return #wbcErr
53  *
54  **/
55
56
57 wbcErr wbcDomainInfo(const char *domain, struct wbcDomainInfo **dinfo)
58 {
59         struct winbindd_request request;
60         struct winbindd_response response;
61         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
62         struct wbcDomainInfo *info = NULL;
63
64         if (!domain || !dinfo) {
65                 wbc_status = WBC_ERR_INVALID_PARAM;
66                 BAIL_ON_WBC_ERROR(wbc_status);
67         }
68
69         /* Initialize request */
70
71         ZERO_STRUCT(request);
72         ZERO_STRUCT(response);
73
74         strncpy(request.domain_name, domain,
75                 sizeof(request.domain_name)-1);
76
77         wbc_status = wbcRequestResponse(WINBINDD_DOMAIN_INFO,
78                                         &request,
79                                         &response);
80         BAIL_ON_WBC_ERROR(wbc_status);
81
82         info = talloc(NULL, struct wbcDomainInfo);
83         BAIL_ON_PTR_ERROR(info, wbc_status);
84
85         info->short_name = talloc_strdup(info,
86                                          response.data.domain_info.name);
87         BAIL_ON_PTR_ERROR(info->short_name, wbc_status);
88
89         info->dns_name = talloc_strdup(info,
90                                        response.data.domain_info.alt_name);
91         BAIL_ON_PTR_ERROR(info->dns_name, wbc_status);
92
93         wbc_status = wbcStringToSid(response.data.domain_info.sid,
94                                     &info->sid);
95         BAIL_ON_WBC_ERROR(wbc_status);
96
97         if (response.data.domain_info.native_mode)
98                 info->flags |= WBC_DOMINFO_NATIVE;
99         if (response.data.domain_info.active_directory)
100                 info->flags |= WBC_DOMINFO_AD;
101         if (response.data.domain_info.primary)
102                 info->flags |= WBC_DOMINFO_PRIMARY;
103
104         *dinfo = info;
105
106         wbc_status = WBC_ERR_SUCCESS;
107
108  done:
109         if (!WBC_ERROR_IS_OK(wbc_status)) {
110                 talloc_free(info);
111         }
112
113         return wbc_status;
114 }