437b4c1b96e8856ce3b2964dfeaa07dab7c948da
[obnox/samba/samba-obnox.git] / source3 / utils / net_dns.c
1 /* 
2    Samba Unix/Linux Dynamic DNS Update
3    net ads commands
4
5    Copyright (C) Krishna Ganugapati (krishnag@centeris.com)         2006
6    Copyright (C) Gerald Carter                                      2006
7
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.
12
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.
17
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/>.  
20 */
21
22 #include "includes.h"
23 #include "utils/net.h"
24 #include "../lib/addns/dns.h"
25 #include "utils/net_dns.h"
26
27 #if defined(WITH_DNS_UPDATES)
28
29 /*********************************************************************
30 *********************************************************************/
31
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)
36 {
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;
42
43         if ( (num_addrs <= 0) || !sslist ) {
44                 return ERROR_DNS_INVALID_PARAMETER;
45         }
46
47         if (!(mem_ctx = talloc_init("DoDNSUpdate"))) {
48                 return ERROR_DNS_NO_MEMORY;
49         }
50
51         err = dns_open_connection( pszServerName, DNS_TCP, mem_ctx, &conn );
52         if (!ERR_DNS_IS_OK(err)) {
53                 goto error;
54         }
55
56         /*
57          * Probe if everything's fine
58          */
59
60         err = dns_create_probe(mem_ctx, pszDomainName, pszHostName,
61                                num_addrs, sslist, &req);
62         if (!ERR_DNS_IS_OK(err)) goto error;
63
64         err = dns_update_transaction(mem_ctx, conn, req, &resp);
65         if (!ERR_DNS_IS_OK(err)) goto error;
66
67         if (dns_response_code(resp->flags) == DNS_NO_ERROR) {
68                 TALLOC_FREE(mem_ctx);
69                 return ERROR_DNS_SUCCESS;
70         }
71
72         /*
73          * First try without signing
74          */
75
76         err = dns_create_update_request(mem_ctx, pszDomainName, pszHostName,
77                                         sslist, num_addrs, &req);
78         if (!ERR_DNS_IS_OK(err)) goto error;
79
80         err = dns_update_transaction(mem_ctx, conn, req, &resp);
81         if (!ERR_DNS_IS_OK(err)) goto error;
82
83         if (dns_response_code(resp->flags) == DNS_NO_ERROR) {
84                 TALLOC_FREE(mem_ctx);
85                 return ERROR_DNS_SUCCESS;
86         }
87
88         /*
89          * Okay, we have to try with signing
90          */
91         {
92                 gss_ctx_id_t gss_context;
93                 char *keyname;
94
95                 if (!(keyname = dns_generate_keyname( mem_ctx ))) {
96                         err = ERROR_DNS_NO_MEMORY;
97                         goto error;
98                 }
99
100                 err = dns_negotiate_sec_ctx( pszDomainName, pszServerName,
101                                              keyname, &gss_context, DNS_SRV_ANY );
102
103                 /* retry using the Windows 2000 DNS hack */
104                 if (!ERR_DNS_IS_OK(err)) {
105                         err = dns_negotiate_sec_ctx( pszDomainName, pszServerName,
106                                                      keyname, &gss_context, 
107                                                      DNS_SRV_WIN2000 );
108                 }
109
110                 if (!ERR_DNS_IS_OK(err))
111                         goto error;
112
113                 err = dns_sign_update(req, gss_context, keyname,
114                                       "gss.microsoft.com", time(NULL), 3600);
115
116                 gss_delete_sec_context(&minor, &gss_context, GSS_C_NO_BUFFER);
117
118                 if (!ERR_DNS_IS_OK(err)) goto error;
119
120                 err = dns_update_transaction(mem_ctx, conn, req, &resp);
121                 if (!ERR_DNS_IS_OK(err)) goto error;
122
123                 err = (dns_response_code(resp->flags) == DNS_NO_ERROR) ?
124                         ERROR_DNS_SUCCESS : ERROR_DNS_UPDATE_FAILED;
125         }
126
127
128 error:
129         TALLOC_FREE(mem_ctx);
130         return err;
131 }
132
133 /*********************************************************************
134 *********************************************************************/
135
136 int get_my_ip_address( struct sockaddr_storage **pp_ss )
137
138 {
139         int i, n;
140         struct sockaddr_storage *list = NULL;
141         int count = 0;
142
143         /* Honor the configured list of interfaces to register */
144
145         load_interfaces();
146         n = iface_count();
147
148         if (n <= 0) {
149                 return -1;
150         }
151
152         if ( (list = SMB_MALLOC_ARRAY( struct sockaddr_storage, n )) == NULL ) {
153                 return -1;
154         }
155
156         for ( i=0; i<n; i++ ) {
157                 const struct sockaddr_storage *nic_sa_storage = NULL;
158
159                 if ((nic_sa_storage = iface_n_sockaddr_storage(i)) == NULL)
160                         continue;
161
162                 /* Don't register loopback addresses */
163                 if (is_loopback_addr((const struct sockaddr *)nic_sa_storage)) {
164                         continue;
165                 }
166
167                 /* Don't register link-local addresses */
168                 if (is_linklocal_addr(nic_sa_storage)) {
169                         continue;
170                 }
171
172                 memcpy(&list[count++], nic_sa_storage, sizeof(struct sockaddr_storage));
173         }
174         *pp_ss = list;
175
176         return count;
177 }
178
179 DNS_ERROR do_gethostbyname(const char *server, const char *host)
180 {
181         struct dns_connection *conn;
182         struct dns_request *req, *resp;
183         DNS_ERROR err;
184
185         err = dns_open_connection(server, DNS_UDP, NULL, &conn);
186         if (!ERR_DNS_IS_OK(err)) goto error;
187
188         err = dns_create_query(conn, host, QTYPE_A, DNS_CLASS_IN, &req);
189         if (!ERR_DNS_IS_OK(err)) goto error;
190
191         err = dns_transaction(conn, conn, req, &resp);
192
193  error:
194         TALLOC_FREE(conn);
195         return err;
196 }
197
198 #endif  /* defined(WITH_DNS_UPDATES) */