s3:winbind: Make wcache_lookup_usergroups externally visible
[Samba/aatanasov.git] / source3 / libaddns / dnsgss.c
blobe7ea0418477b7431632877a730296d9afc4cf910
1 /*
2 Public Interface file for Linux DNS client library implementation
4 Copyright (C) 2006 Krishna Ganugapati <krishnag@centeris.com>
5 Copyright (C) 2006 Gerald Carter <jerry@samba.org>
7 ** NOTE! The following LGPL license applies to the libaddns
8 ** library. This does NOT imply that all of Samba is released
9 ** under the LGPL
11 This library is free software; you can redistribute it and/or
12 modify it under the terms of the GNU Lesser General Public
13 License as published by the Free Software Foundation; either
14 version 2.1 of the License, or (at your option) any later version.
16 This library is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 Lesser General Public License for more details.
21 You should have received a copy of the GNU Lesser General Public
22 License along with this library; if not, see <http://www.gnu.org/licenses/>.
25 #include "dns.h"
26 #include <ctype.h>
29 #ifdef HAVE_GSSAPI_SUPPORT
31 /*********************************************************************
32 *********************************************************************/
34 static int strupr( char *szDomainName )
36 if ( !szDomainName ) {
37 return ( 0 );
39 while ( *szDomainName != '\0' ) {
40 *szDomainName = toupper( *szDomainName );
41 szDomainName++;
43 return ( 0 );
46 #if 0
47 /*********************************************************************
48 *********************************************************************/
50 static void display_status_1( const char *m, OM_uint32 code, int type )
52 OM_uint32 maj_stat, min_stat;
53 gss_buffer_desc msg;
54 OM_uint32 msg_ctx;
56 msg_ctx = 0;
57 while ( 1 ) {
58 maj_stat = gss_display_status( &min_stat, code,
59 type, GSS_C_NULL_OID,
60 &msg_ctx, &msg );
61 fprintf( stdout, "GSS-API error %s: %s\n", m,
62 ( char * ) msg.value );
63 ( void ) gss_release_buffer( &min_stat, &msg );
65 if ( !msg_ctx )
66 break;
70 /*********************************************************************
71 *********************************************************************/
73 void display_status( const char *msg, OM_uint32 maj_stat, OM_uint32 min_stat )
75 display_status_1( msg, maj_stat, GSS_C_GSS_CODE );
76 display_status_1( msg, min_stat, GSS_C_MECH_CODE );
78 #endif
80 static DNS_ERROR dns_negotiate_gss_ctx_int( TALLOC_CTX *mem_ctx,
81 struct dns_connection *conn,
82 const char *keyname,
83 const gss_name_t target_name,
84 gss_ctx_id_t *ctx,
85 enum dns_ServerType srv_type )
87 struct gss_buffer_desc_struct input_desc, *input_ptr, output_desc;
88 OM_uint32 major, minor;
89 OM_uint32 ret_flags;
90 DNS_ERROR err;
92 gss_OID_desc krb5_oid_desc =
93 { 9, (char *)"\x2a\x86\x48\x86\xf7\x12\x01\x02\x02" };
95 *ctx = GSS_C_NO_CONTEXT;
96 input_ptr = NULL;
98 do {
99 major = gss_init_sec_context(
100 &minor, NULL, ctx, target_name, &krb5_oid_desc,
101 GSS_C_REPLAY_FLAG | GSS_C_MUTUAL_FLAG |
102 GSS_C_SEQUENCE_FLAG | GSS_C_CONF_FLAG |
103 GSS_C_INTEG_FLAG | GSS_C_DELEG_FLAG,
104 0, NULL, input_ptr, NULL, &output_desc,
105 &ret_flags, NULL );
107 if (input_ptr != NULL) {
108 TALLOC_FREE(input_desc.value);
111 if (output_desc.length != 0) {
113 struct dns_request *req;
114 struct dns_rrec *rec;
115 struct dns_buffer *buf;
117 time_t t = time(NULL);
119 err = dns_create_query(mem_ctx, keyname, QTYPE_TKEY,
120 DNS_CLASS_IN, &req);
121 if (!ERR_DNS_IS_OK(err)) goto error;
123 err = dns_create_tkey_record(
124 req, keyname, "gss.microsoft.com", t,
125 t + 86400, DNS_TKEY_MODE_GSSAPI, 0,
126 output_desc.length, (uint8 *)output_desc.value,
127 &rec );
128 if (!ERR_DNS_IS_OK(err)) goto error;
130 /* Windows 2000 DNS is broken and requires the
131 TKEY payload in the Answer section instead
132 of the Additional seciton like Windows 2003 */
134 if ( srv_type == DNS_SRV_WIN2000 ) {
135 err = dns_add_rrec(req, rec, &req->num_answers,
136 &req->answers);
137 } else {
138 err = dns_add_rrec(req, rec, &req->num_additionals,
139 &req->additionals);
142 if (!ERR_DNS_IS_OK(err)) goto error;
144 err = dns_marshall_request(req, req, &buf);
145 if (!ERR_DNS_IS_OK(err)) goto error;
147 err = dns_send(conn, buf);
148 if (!ERR_DNS_IS_OK(err)) goto error;
150 TALLOC_FREE(req);
153 gss_release_buffer(&minor, &output_desc);
155 if ((major != GSS_S_COMPLETE) &&
156 (major != GSS_S_CONTINUE_NEEDED)) {
157 return ERROR_DNS_GSS_ERROR;
160 if (major == GSS_S_CONTINUE_NEEDED) {
162 struct dns_request *resp;
163 struct dns_buffer *buf;
164 struct dns_tkey_record *tkey;
166 err = dns_receive(mem_ctx, conn, &buf);
167 if (!ERR_DNS_IS_OK(err)) goto error;
169 err = dns_unmarshall_request(buf, buf, &resp);
170 if (!ERR_DNS_IS_OK(err)) goto error;
173 * TODO: Compare id and keyname
176 if ((resp->num_additionals != 1) ||
177 (resp->num_answers == 0) ||
178 (resp->answers[0]->type != QTYPE_TKEY)) {
179 err = ERROR_DNS_INVALID_MESSAGE;
180 goto error;
183 err = dns_unmarshall_tkey_record(
184 mem_ctx, resp->answers[0], &tkey);
185 if (!ERR_DNS_IS_OK(err)) goto error;
187 input_desc.length = tkey->key_length;
188 input_desc.value = talloc_move(mem_ctx, &tkey->key);
190 input_ptr = &input_desc;
192 TALLOC_FREE(buf);
195 } while ( major == GSS_S_CONTINUE_NEEDED );
197 /* If we arrive here, we have a valid security context */
199 err = ERROR_DNS_SUCCESS;
201 error:
203 return err;
206 DNS_ERROR dns_negotiate_sec_ctx( const char *target_realm,
207 const char *servername,
208 const char *keyname,
209 gss_ctx_id_t *gss_ctx,
210 enum dns_ServerType srv_type )
212 OM_uint32 major, minor;
214 char *upcaserealm, *targetname;
215 DNS_ERROR err;
217 gss_buffer_desc input_name;
218 struct dns_connection *conn;
220 gss_name_t targ_name;
222 gss_OID_desc nt_host_oid_desc =
223 {10, (char *)"\x2a\x86\x48\x86\xf7\x12\x01\x02\x02\x01"};
225 TALLOC_CTX *mem_ctx;
227 if (!(mem_ctx = talloc_init("dns_negotiate_sec_ctx"))) {
228 return ERROR_DNS_NO_MEMORY;
231 err = dns_open_connection( servername, DNS_TCP, mem_ctx, &conn );
232 if (!ERR_DNS_IS_OK(err)) goto error;
234 if (!(upcaserealm = talloc_strdup(mem_ctx, target_realm))) {
235 err = ERROR_DNS_NO_MEMORY;
236 goto error;
239 strupr(upcaserealm);
241 if (!(targetname = talloc_asprintf(mem_ctx, "dns/%s@%s",
242 servername, upcaserealm))) {
243 err = ERROR_DNS_NO_MEMORY;
244 goto error;
247 input_name.value = targetname;
248 input_name.length = strlen(targetname);
250 major = gss_import_name( &minor, &input_name,
251 &nt_host_oid_desc, &targ_name );
253 if (major) {
254 err = ERROR_DNS_GSS_ERROR;
255 goto error;
258 err = dns_negotiate_gss_ctx_int(mem_ctx, conn, keyname,
259 targ_name, gss_ctx, srv_type );
261 gss_release_name( &minor, &targ_name );
263 error:
264 TALLOC_FREE(mem_ctx);
266 return err;
269 DNS_ERROR dns_sign_update(struct dns_update_request *req,
270 gss_ctx_id_t gss_ctx,
271 const char *keyname,
272 const char *algorithmname,
273 time_t time_signed, uint16 fudge)
275 struct dns_buffer *buf;
276 DNS_ERROR err;
277 struct dns_domain_name *key, *algorithm;
278 struct gss_buffer_desc_struct msg, mic;
279 OM_uint32 major, minor;
280 struct dns_rrec *rec;
282 err = dns_marshall_update_request(req, req, &buf);
283 if (!ERR_DNS_IS_OK(err)) return err;
285 err = dns_domain_name_from_string(buf, keyname, &key);
286 if (!ERR_DNS_IS_OK(err)) goto error;
288 err = dns_domain_name_from_string(buf, algorithmname, &algorithm);
289 if (!ERR_DNS_IS_OK(err)) goto error;
291 dns_marshall_domain_name(buf, key);
292 dns_marshall_uint16(buf, DNS_CLASS_ANY);
293 dns_marshall_uint32(buf, 0); /* TTL */
294 dns_marshall_domain_name(buf, algorithm);
295 dns_marshall_uint16(buf, 0); /* Time prefix for 48-bit time_t */
296 dns_marshall_uint32(buf, time_signed);
297 dns_marshall_uint16(buf, fudge);
298 dns_marshall_uint16(buf, 0); /* error */
299 dns_marshall_uint16(buf, 0); /* other len */
301 err = buf->error;
302 if (!ERR_DNS_IS_OK(buf->error)) goto error;
304 msg.value = (void *)buf->data;
305 msg.length = buf->offset;
307 major = gss_get_mic(&minor, gss_ctx, 0, &msg, &mic);
308 if (major != 0) {
309 err = ERROR_DNS_GSS_ERROR;
310 goto error;
313 if (mic.length > 0xffff) {
314 gss_release_buffer(&minor, &mic);
315 err = ERROR_DNS_GSS_ERROR;
316 goto error;
319 err = dns_create_tsig_record(buf, keyname, algorithmname, time_signed,
320 fudge, mic.length, (uint8 *)mic.value,
321 req->id, 0, &rec);
322 gss_release_buffer(&minor, &mic);
323 if (!ERR_DNS_IS_OK(err)) goto error;
325 err = dns_add_rrec(req, rec, &req->num_additionals, &req->additionals);
327 error:
328 TALLOC_FREE(buf);
329 return err;
332 #endif /* HAVE_GSSAPI_SUPPORT */