s3: Fix some nonempty blank lines
[mat/samba.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 "dns.h"
25
26 #if defined(WITH_DNS_UPDATES)
27
28 /*
29  * Silly prototype to get rid of a warning
30  */
31
32 DNS_ERROR DoDNSUpdate(char *pszServerName,
33                       const char *pszDomainName, const char *pszHostName,
34                       const struct sockaddr_storage *sslist,
35                       size_t num_addrs );
36
37 /*********************************************************************
38 *********************************************************************/
39
40 DNS_ERROR DoDNSUpdate(char *pszServerName,
41                       const char *pszDomainName, const char *pszHostName,
42                       const struct sockaddr_storage *sslist, size_t num_addrs )
43 {
44         DNS_ERROR err;
45         struct dns_connection *conn;
46         TALLOC_CTX *mem_ctx;
47         OM_uint32 minor;
48         struct dns_update_request *req, *resp;
49
50         if ( (num_addrs <= 0) || !sslist ) {
51                 return ERROR_DNS_INVALID_PARAMETER;
52         }
53
54         if (!(mem_ctx = talloc_init("DoDNSUpdate"))) {
55                 return ERROR_DNS_NO_MEMORY;
56         }
57
58         err = dns_open_connection( pszServerName, DNS_TCP, mem_ctx, &conn );
59         if (!ERR_DNS_IS_OK(err)) {
60                 goto error;
61         }
62
63         /*
64          * Probe if everything's fine
65          */
66
67         err = dns_create_probe(mem_ctx, pszDomainName, pszHostName,
68                                num_addrs, sslist, &req);
69         if (!ERR_DNS_IS_OK(err)) goto error;
70
71         err = dns_update_transaction(mem_ctx, conn, req, &resp);
72         if (!ERR_DNS_IS_OK(err)) goto error;
73
74         if (dns_response_code(resp->flags) == DNS_NO_ERROR) {
75                 TALLOC_FREE(mem_ctx);
76                 return ERROR_DNS_SUCCESS;
77         }
78
79         /*
80          * First try without signing
81          */
82
83         err = dns_create_update_request(mem_ctx, pszDomainName, pszHostName,
84                                         sslist, num_addrs, &req);
85         if (!ERR_DNS_IS_OK(err)) goto error;
86
87         err = dns_update_transaction(mem_ctx, conn, req, &resp);
88         if (!ERR_DNS_IS_OK(err)) goto error;
89
90         if (dns_response_code(resp->flags) == DNS_NO_ERROR) {
91                 TALLOC_FREE(mem_ctx);
92                 return ERROR_DNS_SUCCESS;
93         }
94
95         /*
96          * Okay, we have to try with signing
97          */
98         {
99                 gss_ctx_id_t gss_context;
100                 char *keyname;
101
102                 if (!(keyname = dns_generate_keyname( mem_ctx ))) {
103                         err = ERROR_DNS_NO_MEMORY;
104                         goto error;
105                 }
106
107                 err = dns_negotiate_sec_ctx( pszDomainName, pszServerName,
108                                              keyname, &gss_context, DNS_SRV_ANY );
109
110                 /* retry using the Windows 2000 DNS hack */
111                 if (!ERR_DNS_IS_OK(err)) {
112                         err = dns_negotiate_sec_ctx( pszDomainName, pszServerName,
113                                                      keyname, &gss_context, 
114                                                      DNS_SRV_WIN2000 );
115                 }
116
117                 if (!ERR_DNS_IS_OK(err))
118                         goto error;
119
120                 err = dns_sign_update(req, gss_context, keyname,
121                                       "gss.microsoft.com", time(NULL), 3600);
122
123                 gss_delete_sec_context(&minor, &gss_context, GSS_C_NO_BUFFER);
124
125                 if (!ERR_DNS_IS_OK(err)) goto error;
126
127                 err = dns_update_transaction(mem_ctx, conn, req, &resp);
128                 if (!ERR_DNS_IS_OK(err)) goto error;
129
130                 err = (dns_response_code(resp->flags) == DNS_NO_ERROR) ?
131                         ERROR_DNS_SUCCESS : ERROR_DNS_UPDATE_FAILED;
132         }
133
134
135 error:
136         TALLOC_FREE(mem_ctx);
137         return err;
138 }
139
140 /*********************************************************************
141 *********************************************************************/
142
143 int get_my_ip_address( struct sockaddr_storage **pp_ss )
144
145 {
146         int i, n;
147         struct sockaddr_storage *list = NULL;
148         int count = 0;
149
150         /* Honor the configured list of interfaces to register */
151
152         load_interfaces();
153         n = iface_count();
154
155         if (n <= 0) {
156                 return -1;
157         }
158
159         if ( (list = SMB_MALLOC_ARRAY( struct sockaddr_storage, n )) == NULL ) {
160                 return -1;
161         }
162
163         for ( i=0; i<n; i++ ) {
164                 const struct sockaddr_storage *nic_sa_storage = NULL;
165
166                 if ((nic_sa_storage = iface_n_sockaddr_storage(i)) == NULL)
167                         continue;
168
169                 /* Don't register loopback addresses */
170                 if (is_loopback_addr((struct sockaddr *)nic_sa_storage)) {
171                         continue;
172                 }
173
174                 memcpy(&list[count++], nic_sa_storage, sizeof(struct sockaddr_storage));
175         }
176         *pp_ss = list;
177
178         return count;
179 }
180
181 /*
182  * Silly prototype to get rid of a warning
183  */
184
185 DNS_ERROR do_gethostbyname(const char *server, const char *host);
186
187 DNS_ERROR do_gethostbyname(const char *server, const char *host)
188 {
189         struct dns_connection *conn;
190         struct dns_request *req, *resp;
191         DNS_ERROR err;
192
193         err = dns_open_connection(server, DNS_UDP, NULL, &conn);
194         if (!ERR_DNS_IS_OK(err)) goto error;
195
196         err = dns_create_query(conn, host, QTYPE_A, DNS_CLASS_IN, &req);
197         if (!ERR_DNS_IS_OK(err)) goto error;
198
199         err = dns_transaction(conn, conn, req, &resp);
200
201  error:
202         TALLOC_FREE(conn);
203         return err;
204 }
205
206 #endif  /* defined(WITH_DNS_UPDATES) */