r19890: Fix klokwork ID 2188
[Samba/nascimento.git] / source3 / utils / net_dns.c
blobd372211a5ffab079c90457162858c8bc4002c15f
2 /*
3 Samba Unix/Linux Dynamic DNS Update
4 net ads commands
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 2 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, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "includes.h"
25 #include "utils/net.h"
26 #include "dns.h"
28 #if defined(WITH_DNS_UPDATES)
30 /*********************************************************************
31 *********************************************************************/
33 DNS_ERROR DoDNSUpdate(ADS_STRUCT *ads, char *pszServerName,
34 const char *pszDomainName, const char *pszHostName,
35 const struct in_addr *iplist, int num_addrs )
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;
43 if ( (num_addrs <= 0) || !iplist ) {
44 return ERROR_DNS_INVALID_PARAMETER;
47 if (!(mem_ctx = talloc_init("DoDNSUpdate"))) {
48 return ERROR_DNS_NO_MEMORY;
51 err = dns_open( pszServerName, DNS_TCP, mem_ctx, &conn );
52 if (!ERR_DNS_IS_OK(err)) {
53 goto error;
57 * Probe if everything's fine
60 err = dns_create_probe(mem_ctx, pszDomainName, pszHostName,
61 num_addrs, iplist, &req);
62 if (!ERR_DNS_IS_OK(err)) goto error;
64 err = dns_update_transaction(mem_ctx, conn, req, &resp);
65 if (!ERR_DNS_IS_OK(err)) goto error;
67 if (dns_response_code(resp->flags) == DNS_NO_ERROR) {
68 TALLOC_FREE(mem_ctx);
69 return ERROR_DNS_SUCCESS;
73 * First try without signing
76 err = dns_create_update_request(mem_ctx, pszDomainName, pszHostName,
77 iplist[0].s_addr, &req);
78 if (!ERR_DNS_IS_OK(err)) goto error;
80 err = dns_update_transaction(mem_ctx, conn, req, &resp);
81 if (!ERR_DNS_IS_OK(err)) goto error;
83 if (dns_response_code(resp->flags) == DNS_NO_ERROR) {
84 TALLOC_FREE(mem_ctx);
85 return ERROR_DNS_SUCCESS;
89 * Okay, we have to try with signing
92 ADS_STRUCT *ads_s;
93 gss_ctx_id_t gss_context;
94 int res;
95 char *keyname;
97 if (!(keyname = dns_generate_keyname( mem_ctx ))) {
98 err = ERROR_DNS_NO_MEMORY;
99 goto error;
102 if (!(ads_s = ads_init(ads->server.realm, ads->server.workgroup,
103 ads->server.ldap_server))) {
104 return ERROR_DNS_NO_MEMORY;
107 /* kinit with the machine password */
108 setenv(KRB5_ENV_CCNAME, "MEMORY:net_ads", 1);
109 asprintf( &ads_s->auth.user_name, "%s$", global_myname() );
110 ads_s->auth.password = secrets_fetch_machine_password(
111 lp_workgroup(), NULL, NULL );
112 ads_s->auth.realm = SMB_STRDUP( lp_realm() );
113 res = ads_kinit_password( ads_s );
114 ads_destroy(&ads_s);
115 if (res) {
116 err = ERROR_DNS_GSS_ERROR;
117 goto error;
120 err = dns_negotiate_sec_ctx( pszDomainName, pszServerName,
121 keyname, &gss_context );
122 if (!ERR_DNS_IS_OK(err)) goto error;
124 err = dns_sign_update(req, gss_context, keyname,
125 "gss.microsoft.com", time(NULL), 3600);
127 gss_delete_sec_context(&minor, &gss_context, GSS_C_NO_BUFFER);
129 if (!ERR_DNS_IS_OK(err)) goto error;
131 err = dns_update_transaction(mem_ctx, conn, req, &resp);
132 if (!ERR_DNS_IS_OK(err)) goto error;
134 err = (dns_response_code(resp->flags) == DNS_NO_ERROR) ?
135 ERROR_DNS_SUCCESS : ERROR_DNS_UPDATE_FAILED;
139 error:
140 TALLOC_FREE(mem_ctx);
141 return err;
144 /*********************************************************************
145 *********************************************************************/
147 int get_my_ip_address( struct in_addr **ips )
149 struct iface_struct nics[MAX_INTERFACES];
150 int i, n;
151 struct in_addr loopback_ip = *interpret_addr2("127.0.0.1");
152 struct in_addr *list;
153 int count = 0;
155 /* find the first non-loopback address from our list of interfaces */
157 n = get_interfaces(nics, MAX_INTERFACES);
159 if ( (list = SMB_MALLOC_ARRAY( struct in_addr, n )) == NULL ) {
160 return -1;
163 for ( i=0; i<n; i++ ) {
164 if ( nics[i].ip.s_addr != loopback_ip.s_addr ) {
165 memcpy( &list[count++], &nics[i].ip, sizeof( struct in_addr ) );
168 *ips = list;
170 return count;
173 DNS_ERROR do_gethostbyname(const char *server, const char *host)
175 struct dns_connection *conn;
176 struct dns_request *req, *resp;
177 DNS_ERROR err;
179 err = dns_open(server, DNS_UDP, NULL, &conn);
180 if (!ERR_DNS_IS_OK(err)) goto error;
182 err = dns_create_query(conn, host, QTYPE_A, DNS_CLASS_IN, &req);
183 if (!ERR_DNS_IS_OK(err)) goto error;
185 err = dns_transaction(conn, conn, req, &resp);
187 error:
188 TALLOC_FREE(conn);
189 return err;
192 #endif /* defined(WITH_DNS_UPDATES) */