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 2 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, write to the Free Software
|
---|
21 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
22 | */
|
---|
23 |
|
---|
24 | #include "includes.h"
|
---|
25 | #include "utils/net.h"
|
---|
26 | #include "dns.h"
|
---|
27 |
|
---|
28 | #if defined(WITH_DNS_UPDATES)
|
---|
29 |
|
---|
30 | /*********************************************************************
|
---|
31 | *********************************************************************/
|
---|
32 |
|
---|
33 | DNS_ERROR DoDNSUpdate(char *pszServerName,
|
---|
34 | const char *pszDomainName, const char *pszHostName,
|
---|
35 | const struct in_addr *iplist, size_t num_addrs )
|
---|
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) || !iplist ) {
|
---|
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, iplist, &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 | iplist, 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 |
|
---|
114 | err = dns_sign_update(req, gss_context, keyname,
|
---|
115 | "gss.microsoft.com", time(NULL), 3600);
|
---|
116 |
|
---|
117 | gss_delete_sec_context(&minor, &gss_context, GSS_C_NO_BUFFER);
|
---|
118 |
|
---|
119 | if (!ERR_DNS_IS_OK(err)) goto error;
|
---|
120 |
|
---|
121 | err = dns_update_transaction(mem_ctx, conn, req, &resp);
|
---|
122 | if (!ERR_DNS_IS_OK(err)) goto error;
|
---|
123 |
|
---|
124 | err = (dns_response_code(resp->flags) == DNS_NO_ERROR) ?
|
---|
125 | ERROR_DNS_SUCCESS : ERROR_DNS_UPDATE_FAILED;
|
---|
126 | }
|
---|
127 |
|
---|
128 |
|
---|
129 | error:
|
---|
130 | TALLOC_FREE(mem_ctx);
|
---|
131 | return err;
|
---|
132 | }
|
---|
133 |
|
---|
134 | /*********************************************************************
|
---|
135 | *********************************************************************/
|
---|
136 |
|
---|
137 | int get_my_ip_address( struct in_addr **ips )
|
---|
138 | {
|
---|
139 | struct iface_struct nics[MAX_INTERFACES];
|
---|
140 | int i, n;
|
---|
141 | struct in_addr loopback_ip = *interpret_addr2("127.0.0.1");
|
---|
142 | struct in_addr *list;
|
---|
143 | int count = 0;
|
---|
144 |
|
---|
145 | /* find the first non-loopback address from our list of interfaces */
|
---|
146 |
|
---|
147 | n = get_interfaces(nics, MAX_INTERFACES);
|
---|
148 |
|
---|
149 | if ( (list = SMB_MALLOC_ARRAY( struct in_addr, n )) == NULL ) {
|
---|
150 | return -1;
|
---|
151 | }
|
---|
152 |
|
---|
153 | for ( i=0; i<n; i++ ) {
|
---|
154 | if ( nics[i].ip.s_addr != loopback_ip.s_addr ) {
|
---|
155 | memcpy( &list[count++], &nics[i].ip, sizeof( struct in_addr ) );
|
---|
156 | }
|
---|
157 | }
|
---|
158 | *ips = list;
|
---|
159 |
|
---|
160 | return count;
|
---|
161 | }
|
---|
162 |
|
---|
163 | DNS_ERROR do_gethostbyname(const char *server, const char *host)
|
---|
164 | {
|
---|
165 | struct dns_connection *conn;
|
---|
166 | struct dns_request *req, *resp;
|
---|
167 | DNS_ERROR err;
|
---|
168 |
|
---|
169 | err = dns_open_connection(server, DNS_UDP, NULL, &conn);
|
---|
170 | if (!ERR_DNS_IS_OK(err)) goto error;
|
---|
171 |
|
---|
172 | err = dns_create_query(conn, host, QTYPE_A, DNS_CLASS_IN, &req);
|
---|
173 | if (!ERR_DNS_IS_OK(err)) goto error;
|
---|
174 |
|
---|
175 | err = dns_transaction(conn, conn, req, &resp);
|
---|
176 |
|
---|
177 | error:
|
---|
178 | TALLOC_FREE(conn);
|
---|
179 | return err;
|
---|
180 | }
|
---|
181 |
|
---|
182 | #endif /* defined(WITH_DNS_UPDATES) */
|
---|