r23784: use the GPLv3 boilerplate as recommended by the FSF and the license text
[samba.git] / source3 / utils / net_dns.c
1
2 /* 
3    Samba Unix/Linux Dynamic DNS Update
4    net ads commands
5
6    Copyright (C) Krishna Ganugapati (krishnag@centeris.com)         2006
7    Copyright (C) Gerald Carter                                      2006
8
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.
13    
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.
18    
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/>.  
21 */
22
23 #include "includes.h"
24 #include "utils/net.h"
25 #include "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 in_addr *iplist, size_t num_addrs )
35 {
36         DNS_ERROR err;
37         struct dns_connection *conn;
38         TALLOC_CTX *mem_ctx;
39         OM_uint32 minor;
40         struct dns_update_request *req, *resp;
41
42         if ( (num_addrs <= 0) || !iplist ) {
43                 return ERROR_DNS_INVALID_PARAMETER;
44         }
45
46         if (!(mem_ctx = talloc_init("DoDNSUpdate"))) {
47                 return ERROR_DNS_NO_MEMORY;
48         }
49                 
50         err = dns_open_connection( pszServerName, DNS_TCP, mem_ctx, &conn );
51         if (!ERR_DNS_IS_OK(err)) {
52                 goto error;
53         }
54
55         /*
56          * Probe if everything's fine
57          */
58
59         err = dns_create_probe(mem_ctx, pszDomainName, pszHostName,
60                                num_addrs, iplist, &req);
61         if (!ERR_DNS_IS_OK(err)) goto error;
62
63         err = dns_update_transaction(mem_ctx, conn, req, &resp);
64         if (!ERR_DNS_IS_OK(err)) goto error;
65
66         if (dns_response_code(resp->flags) == DNS_NO_ERROR) {
67                 TALLOC_FREE(mem_ctx);
68                 return ERROR_DNS_SUCCESS;
69         }
70
71         /*
72          * First try without signing
73          */
74
75         err = dns_create_update_request(mem_ctx, pszDomainName, pszHostName,
76                                         iplist, num_addrs, &req);
77         if (!ERR_DNS_IS_OK(err)) goto error;
78
79         err = dns_update_transaction(mem_ctx, conn, req, &resp);
80         if (!ERR_DNS_IS_OK(err)) goto error;
81
82         if (dns_response_code(resp->flags) == DNS_NO_ERROR) {
83                 TALLOC_FREE(mem_ctx);
84                 return ERROR_DNS_SUCCESS;
85         }
86
87         /*
88          * Okay, we have to try with signing
89          */
90         {
91                 gss_ctx_id_t gss_context;
92                 char *keyname;
93
94                 if (!(keyname = dns_generate_keyname( mem_ctx ))) {
95                         err = ERROR_DNS_NO_MEMORY;
96                         goto error;
97                 }
98
99                 err = dns_negotiate_sec_ctx( pszDomainName, pszServerName,
100                                              keyname, &gss_context, DNS_SRV_ANY );
101
102                 /* retry using the Windows 2000 DNS hack */
103                 if (!ERR_DNS_IS_OK(err)) {
104                         err = dns_negotiate_sec_ctx( pszDomainName, pszServerName,
105                                                      keyname, &gss_context, 
106                                                      DNS_SRV_WIN2000 );
107                 }
108                 
109                 if (!ERR_DNS_IS_OK(err))
110                         goto error;
111                 
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 in_addr **ips )
137 {
138         struct iface_struct nics[MAX_INTERFACES];
139         int i, n;
140         struct in_addr loopback_ip = *interpret_addr2("127.0.0.1");
141         struct in_addr *list;
142         int count = 0;
143
144         /* find the first non-loopback address from our list of interfaces */
145
146         n = get_interfaces(nics, MAX_INTERFACES);
147         
148         if ( (list = SMB_MALLOC_ARRAY( struct in_addr, n )) == NULL ) {
149                 return -1;
150         }
151
152         for ( i=0; i<n; i++ ) {
153                 if ( nics[i].ip.s_addr != loopback_ip.s_addr ) {
154                         memcpy( &list[count++], &nics[i].ip, sizeof( struct in_addr ) );
155                 }
156         }
157         *ips = list;
158
159         return count;
160 }
161
162 DNS_ERROR do_gethostbyname(const char *server, const char *host)
163 {
164         struct dns_connection *conn;
165         struct dns_request *req, *resp;
166         DNS_ERROR err;
167
168         err = dns_open_connection(server, DNS_UDP, NULL, &conn);
169         if (!ERR_DNS_IS_OK(err)) goto error;
170
171         err = dns_create_query(conn, host, QTYPE_A, DNS_CLASS_IN, &req);
172         if (!ERR_DNS_IS_OK(err)) goto error;
173
174         err = dns_transaction(conn, conn, req, &resp);
175
176  error:
177         TALLOC_FREE(conn);
178         return err;
179 }
180
181 #endif  /* defined(WITH_DNS_UPDATES) */