ctdb-ib: make sure the tevent_fd is removed before the fd is closed
[Samba.git] / source3 / utils / net_dns.c
blob7a04459c2ced93143217a22ee7bcb49ca046f3ad
1 /*
2 Samba Unix/Linux Dynamic DNS Update
3 net ads commands
5 Copyright (C) Krishna Ganugapati (krishnag@centeris.com) 2006
6 Copyright (C) Gerald Carter 2006
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
23 #include "utils/net.h"
24 #include "../lib/addns/dns.h"
25 #include "utils/net_dns.h"
27 #if defined(WITH_DNS_UPDATES)
29 /*********************************************************************
30 *********************************************************************/
32 DNS_ERROR DoDNSUpdate(char *pszServerName,
33 const char *pszDomainName, const char *pszHostName,
34 const struct sockaddr_storage *sslist, size_t num_addrs,
35 uint32_t flags)
37 DNS_ERROR err;
38 struct dns_connection *conn;
39 TALLOC_CTX *mem_ctx;
40 OM_uint32 minor;
41 struct dns_update_request *req, *resp;
43 DEBUG(10,("DoDNSUpdate called with flags: 0x%08x\n", flags));
45 if (!(flags & DNS_UPDATE_SIGNED) &&
46 !(flags & DNS_UPDATE_UNSIGNED) &&
47 !(flags & DNS_UPDATE_PROBE)) {
48 return ERROR_DNS_INVALID_PARAMETER;
51 if ( (num_addrs <= 0) || !sslist ) {
52 return ERROR_DNS_INVALID_PARAMETER;
55 if (!(mem_ctx = talloc_init("DoDNSUpdate"))) {
56 return ERROR_DNS_NO_MEMORY;
59 err = dns_open_connection( pszServerName, DNS_TCP, mem_ctx, &conn );
60 if (!ERR_DNS_IS_OK(err)) {
61 goto error;
64 if (flags & DNS_UPDATE_PROBE) {
67 * Probe if everything's fine
70 err = dns_create_probe(mem_ctx, pszDomainName, pszHostName,
71 num_addrs, sslist, &req);
72 if (!ERR_DNS_IS_OK(err)) goto error;
74 err = dns_update_transaction(mem_ctx, conn, req, &resp);
75 if (!ERR_DNS_IS_OK(err)) goto error;
77 if (!ERR_DNS_IS_OK(err)) {
78 DEBUG(3,("DoDNSUpdate: failed to probe DNS\n"));
81 if ((dns_response_code(resp->flags) == DNS_NO_ERROR) &&
82 (flags & DNS_UPDATE_PROBE_SUFFICIENT)) {
83 TALLOC_FREE(mem_ctx);
84 return ERROR_DNS_SUCCESS;
88 if (flags & DNS_UPDATE_UNSIGNED) {
91 * First try without signing
94 err = dns_create_update_request(mem_ctx, pszDomainName, pszHostName,
95 sslist, num_addrs, &req);
96 if (!ERR_DNS_IS_OK(err)) goto error;
98 err = dns_update_transaction(mem_ctx, conn, req, &resp);
99 if (!ERR_DNS_IS_OK(err)) goto error;
101 if (!ERR_DNS_IS_OK(err)) {
102 DEBUG(3,("DoDNSUpdate: unsigned update failed\n"));
105 if ((dns_response_code(resp->flags) == DNS_NO_ERROR) &&
106 (flags & DNS_UPDATE_UNSIGNED_SUFFICIENT)) {
107 TALLOC_FREE(mem_ctx);
108 return ERROR_DNS_SUCCESS;
113 * Okay, we have to try with signing
115 if (flags & DNS_UPDATE_SIGNED) {
116 gss_ctx_id_t gss_context;
117 char *keyname;
119 err = dns_create_update_request(mem_ctx, pszDomainName, pszHostName,
120 sslist, num_addrs, &req);
121 if (!ERR_DNS_IS_OK(err)) goto error;
123 if (!(keyname = dns_generate_keyname( mem_ctx ))) {
124 err = ERROR_DNS_NO_MEMORY;
125 goto error;
128 err = dns_negotiate_sec_ctx( pszDomainName, pszServerName,
129 keyname, &gss_context, DNS_SRV_ANY );
131 /* retry using the Windows 2000 DNS hack */
132 if (!ERR_DNS_IS_OK(err)) {
133 err = dns_negotiate_sec_ctx( pszDomainName, pszServerName,
134 keyname, &gss_context,
135 DNS_SRV_WIN2000 );
138 if (!ERR_DNS_IS_OK(err))
139 goto error;
141 err = dns_sign_update(req, gss_context, keyname,
142 "gss.microsoft.com", time(NULL), 3600);
144 gss_delete_sec_context(&minor, &gss_context, GSS_C_NO_BUFFER);
146 if (!ERR_DNS_IS_OK(err)) goto error;
148 err = dns_update_transaction(mem_ctx, conn, req, &resp);
149 if (!ERR_DNS_IS_OK(err)) goto error;
151 err = (dns_response_code(resp->flags) == DNS_NO_ERROR) ?
152 ERROR_DNS_SUCCESS : ERROR_DNS_UPDATE_FAILED;
154 if (!ERR_DNS_IS_OK(err)) {
155 DEBUG(3,("DoDNSUpdate: signed update failed\n"));
160 error:
161 TALLOC_FREE(mem_ctx);
162 return err;
165 /*********************************************************************
166 *********************************************************************/
168 int get_my_ip_address( struct sockaddr_storage **pp_ss )
171 int i, n;
172 struct sockaddr_storage *list = NULL;
173 int count = 0;
175 /* Honor the configured list of interfaces to register */
177 load_interfaces();
178 n = iface_count();
180 if (n <= 0) {
181 return -1;
184 if ( (list = SMB_MALLOC_ARRAY( struct sockaddr_storage, n )) == NULL ) {
185 return -1;
188 for ( i=0; i<n; i++ ) {
189 const struct sockaddr_storage *nic_sa_storage = NULL;
191 if ((nic_sa_storage = iface_n_sockaddr_storage(i)) == NULL)
192 continue;
194 /* Don't register loopback addresses */
195 if (is_loopback_addr((const struct sockaddr *)nic_sa_storage)) {
196 continue;
199 /* Don't register link-local addresses */
200 if (is_linklocal_addr(nic_sa_storage)) {
201 continue;
204 memcpy(&list[count++], nic_sa_storage, sizeof(struct sockaddr_storage));
206 *pp_ss = list;
208 return count;
211 DNS_ERROR do_gethostbyname(const char *server, const char *host)
213 struct dns_connection *conn = NULL;
214 struct dns_request *req, *resp;
215 DNS_ERROR err;
217 err = dns_open_connection(server, DNS_UDP, NULL, &conn);
218 if (!ERR_DNS_IS_OK(err)) goto error;
220 err = dns_create_query(conn, host, QTYPE_A, DNS_CLASS_IN, &req);
221 if (!ERR_DNS_IS_OK(err)) goto error;
223 err = dns_transaction(conn, conn, req, &resp);
225 error:
226 TALLOC_FREE(conn);
227 return err;
230 #endif /* defined(WITH_DNS_UPDATES) */