Attempt to fix bug #3617. Mix of patches from Volker and
[ira/wip.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  * The char* members of the struct wbcDomainInfo* are malloc()'d
55  * and it the the responsibility of the caller to free the members
56  * before  discarding the struct.
57  *
58  **/
59
60
61 wbcErr wbcDomainInfo(const char *domain, struct wbcDomainInfo **dinfo)
62 {
63         struct winbindd_request request;
64         struct winbindd_response response;
65         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
66         struct wbcDomainInfo *info = NULL;
67         
68         if (!domain || !dinfo) {
69                 wbc_status = WBC_ERR_INVALID_PARAM;
70                 BAIL_ON_WBC_ERROR(wbc_status);
71         }
72
73         /* Initialize request */
74
75         ZERO_STRUCT(request);
76         ZERO_STRUCT(response);
77
78         strncpy(request.domain_name, domain, 
79                 sizeof(request.domain_name)-1);
80
81         wbc_status = wbcRequestResponse(WINBINDD_DOMAIN_INFO,
82                                         &request,
83                                         &response);
84         BAIL_ON_WBC_ERROR(wbc_status);
85
86         info = talloc(NULL, struct wbcDomainInfo);
87         BAIL_ON_PTR_ERROR(info, wbc_status);
88
89         info->short_name = talloc_strdup(info, 
90                                          response.data.domain_info.name);
91         BAIL_ON_PTR_ERROR(info->short_name, wbc_status);
92
93         info->dns_name = talloc_strdup(info, 
94                                        response.data.domain_info.alt_name);
95         BAIL_ON_PTR_ERROR(info->dns_name, wbc_status);
96
97         wbc_status = wbcStringToSid(response.data.domain_info.sid, 
98                                     &info->sid);
99         BAIL_ON_WBC_ERROR(wbc_status);
100
101         if (response.data.domain_info.native_mode)
102                 info->flags |= WBC_DOMINFO_NATIVE;
103         if (response.data.domain_info.active_directory)
104                 info->flags |= WBC_DOMINFO_AD;
105         if (response.data.domain_info.primary)
106                 info->flags |= WBC_DOMINFO_PRIMARY;
107
108         *dinfo = info;
109         
110         wbc_status = WBC_ERR_SUCCESS;
111
112  done:
113         if (!WBC_ERROR_IS_OK(wbc_status)) {
114                 talloc_free(info);
115         }
116
117         return wbc_status;
118 }