3 Samba Unix/Linux Dynamic DNS Update
6 Copyright (C) Krishna Ganugapati (krishnag@centeris.com) 2006
7 Copyright (C) Gerald Carter 2006
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program 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
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "utils/net.h"
27 #if defined(WITH_DNS_UPDATES)
30 * Silly prototype to get rid of a warning
33 DNS_ERROR
DoDNSUpdate(char *pszServerName
,
34 const char *pszDomainName
, const char *pszHostName
,
35 const struct sockaddr_storage
*sslist
,
38 /*********************************************************************
39 *********************************************************************/
41 DNS_ERROR
DoDNSUpdate(char *pszServerName
,
42 const char *pszDomainName
, const char *pszHostName
,
43 const struct sockaddr_storage
*sslist
, size_t num_addrs
)
46 struct dns_connection
*conn
;
49 struct dns_update_request
*req
, *resp
;
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
)) {
65 * Probe if everything's fine
68 err
= dns_create_probe(mem_ctx
, pszDomainName
, pszHostName
,
69 num_addrs
, sslist
, &req
);
70 if (!ERR_DNS_IS_OK(err
)) goto error
;
72 err
= dns_update_transaction(mem_ctx
, conn
, req
, &resp
);
73 if (!ERR_DNS_IS_OK(err
)) goto error
;
75 if (dns_response_code(resp
->flags
) == DNS_NO_ERROR
) {
77 return ERROR_DNS_SUCCESS
;
81 * First try without signing
84 err
= dns_create_update_request(mem_ctx
, pszDomainName
, pszHostName
,
85 sslist
, num_addrs
, &req
);
86 if (!ERR_DNS_IS_OK(err
)) goto error
;
88 err
= dns_update_transaction(mem_ctx
, conn
, req
, &resp
);
89 if (!ERR_DNS_IS_OK(err
)) goto error
;
91 if (dns_response_code(resp
->flags
) == DNS_NO_ERROR
) {
93 return ERROR_DNS_SUCCESS
;
97 * Okay, we have to try with signing
100 gss_ctx_id_t gss_context
;
103 if (!(keyname
= dns_generate_keyname( mem_ctx
))) {
104 err
= ERROR_DNS_NO_MEMORY
;
108 err
= dns_negotiate_sec_ctx( pszDomainName
, pszServerName
,
109 keyname
, &gss_context
, DNS_SRV_ANY
);
111 /* retry using the Windows 2000 DNS hack */
112 if (!ERR_DNS_IS_OK(err
)) {
113 err
= dns_negotiate_sec_ctx( pszDomainName
, pszServerName
,
114 keyname
, &gss_context
,
118 if (!ERR_DNS_IS_OK(err
))
122 err
= dns_sign_update(req
, gss_context
, keyname
,
123 "gss.microsoft.com", time(NULL
), 3600);
125 gss_delete_sec_context(&minor
, &gss_context
, GSS_C_NO_BUFFER
);
127 if (!ERR_DNS_IS_OK(err
)) goto error
;
129 err
= dns_update_transaction(mem_ctx
, conn
, req
, &resp
);
130 if (!ERR_DNS_IS_OK(err
)) goto error
;
132 err
= (dns_response_code(resp
->flags
) == DNS_NO_ERROR
) ?
133 ERROR_DNS_SUCCESS
: ERROR_DNS_UPDATE_FAILED
;
138 TALLOC_FREE(mem_ctx
);
142 /*********************************************************************
143 *********************************************************************/
145 int get_my_ip_address( struct sockaddr_storage
**pp_ss
)
148 struct iface_struct nics
[MAX_INTERFACES
];
150 struct sockaddr_storage
*list
= NULL
;
153 /* find the first non-loopback address from our list of interfaces */
155 n
= get_interfaces(nics
, MAX_INTERFACES
);
161 if ( (list
= SMB_MALLOC_ARRAY( struct sockaddr_storage
, n
)) == NULL
) {
165 for ( i
=0; i
<n
; i
++ ) {
166 if (is_loopback_addr(&nics
[i
].ip
)) {
169 #if defined(HAVE_IPV6)
170 if ((nics
[i
].ip
.ss_family
== AF_INET
)) {
171 memcpy(&list
[count
++], &nics
[i
].ip
,
172 sizeof(struct sockaddr_storage
));
175 if (nics
[i
].ip
.ss_family
== AF_INET
) {
176 memcpy(&list
[count
++], &nics
[i
].ip
,
177 sizeof(struct sockaddr_storage
));
186 * Silly prototype to get rid of a warning
189 DNS_ERROR
do_gethostbyname(const char *server
, const char *host
);
191 DNS_ERROR
do_gethostbyname(const char *server
, const char *host
)
193 struct dns_connection
*conn
;
194 struct dns_request
*req
, *resp
;
197 err
= dns_open_connection(server
, DNS_UDP
, NULL
, &conn
);
198 if (!ERR_DNS_IS_OK(err
)) goto error
;
200 err
= dns_create_query(conn
, host
, QTYPE_A
, DNS_CLASS_IN
, &req
);
201 if (!ERR_DNS_IS_OK(err
)) goto error
;
203 err
= dns_transaction(conn
, conn
, req
, &resp
);
210 #endif /* defined(WITH_DNS_UPDATES) */