s3:libads: Fix size types in kerberos functions
[samba.git] / source3 / libads / kerberos.c
1 /* 
2    Unix SMB/CIFS implementation.
3    kerberos utility library
4    Copyright (C) Andrew Tridgell 2001
5    Copyright (C) Remus Koos 2001
6    Copyright (C) Nalin Dahyabhai <nalin@redhat.com> 2004.
7    Copyright (C) Jeremy Allison 2004.
8    Copyright (C) Gerald Carter 2006.
9
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 3 of the License, or
13    (at your option) any later version.
14
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 */
23
24 #include "includes.h"
25 #include "system/filesys.h"
26 #include "smb_krb5.h"
27 #include "../librpc/gen_ndr/ndr_misc.h"
28 #include "libads/kerberos_proto.h"
29 #include "libads/cldap.h"
30 #include "secrets.h"
31 #include "../lib/tsocket/tsocket.h"
32 #include "lib/util/asn1.h"
33
34 #ifdef HAVE_KRB5
35
36 #define LIBADS_CCACHE_NAME "MEMORY:libads"
37
38 /*
39   we use a prompter to avoid a crash bug in the kerberos libs when 
40   dealing with empty passwords
41   this prompter is just a string copy ...
42 */
43 static krb5_error_code 
44 kerb_prompter(krb5_context ctx, void *data,
45                const char *name,
46                const char *banner,
47                int num_prompts,
48                krb5_prompt prompts[])
49 {
50         if (num_prompts == 0) return 0;
51         if (num_prompts == 2) {
52                 /*
53                  * only heimdal has a prompt type and we need to deal with it here to
54                  * avoid loops.
55                  *
56                  * removing the prompter completely is not an option as at least these
57                  * versions would crash: heimdal-1.0.2 and heimdal-1.1. Later heimdal
58                  * version have looping detection and return with a proper error code.
59                  */
60
61 #if HAVE_KRB5_PROMPT_TYPE /* Heimdal */
62                  if (prompts[0].type == KRB5_PROMPT_TYPE_NEW_PASSWORD &&
63                      prompts[1].type == KRB5_PROMPT_TYPE_NEW_PASSWORD_AGAIN) {
64                         /*
65                          * We don't want to change passwords here. We're
66                          * called from heimal when the KDC returns
67                          * KRB5KDC_ERR_KEY_EXPIRED, but at this point we don't
68                          * have the chance to ask the user for a new
69                          * password. If we return 0 (i.e. success), we will be
70                          * spinning in the endless for-loop in
71                          * change_password() in
72                          * source4/heimdal/lib/krb5/init_creds_pw.c:526ff
73                          */
74                         return KRB5KDC_ERR_KEY_EXPIRED;
75                 }
76 #elif defined(HAVE_KRB5_GET_PROMPT_TYPES) /* MIT */
77                 krb5_prompt_type *prompt_types = NULL;
78
79                 prompt_types = krb5_get_prompt_types(ctx);
80                 if (prompt_types != NULL) {
81                         if (prompt_types[0] == KRB5_PROMPT_TYPE_NEW_PASSWORD &&
82                             prompt_types[1] == KRB5_PROMPT_TYPE_NEW_PASSWORD_AGAIN) {
83                                 return KRB5KDC_ERR_KEY_EXP;
84                         }
85                 }
86 #endif
87         }
88
89         memset(prompts[0].reply->data, '\0', prompts[0].reply->length);
90         if (prompts[0].reply->length > 0) {
91                 if (data) {
92                         strncpy((char *)prompts[0].reply->data, (const char *)data,
93                                 prompts[0].reply->length-1);
94                         prompts[0].reply->length = strlen((const char *)prompts[0].reply->data);
95                 } else {
96                         prompts[0].reply->length = 0;
97                 }
98         }
99         return 0;
100 }
101
102 /*
103   simulate a kinit, putting the tgt in the given cache location. If cache_name == NULL
104   place in default cache location.
105   remus@snapserver.com
106 */
107 int kerberos_kinit_password_ext(const char *principal,
108                                 const char *password,
109                                 int time_offset,
110                                 time_t *expire_time,
111                                 time_t *renew_till_time,
112                                 const char *cache_name,
113                                 bool request_pac,
114                                 bool add_netbios_addr,
115                                 time_t renewable_time,
116                                 NTSTATUS *ntstatus)
117 {
118         krb5_context ctx = NULL;
119         krb5_error_code code = 0;
120         krb5_ccache cc = NULL;
121         krb5_principal me = NULL;
122         krb5_principal canon_princ = NULL;
123         krb5_creds my_creds;
124         krb5_get_init_creds_opt *opt = NULL;
125         smb_krb5_addresses *addr = NULL;
126
127         ZERO_STRUCT(my_creds);
128
129         initialize_krb5_error_table();
130         if ((code = krb5_init_context(&ctx)))
131                 goto out;
132
133         if (time_offset != 0) {
134                 krb5_set_real_time(ctx, time(NULL) + time_offset, 0);
135         }
136
137         DEBUG(10,("kerberos_kinit_password: as %s using [%s] as ccache and config [%s]\n",
138                         principal,
139                         cache_name ? cache_name: krb5_cc_default_name(ctx),
140                         getenv("KRB5_CONFIG")));
141
142         if ((code = krb5_cc_resolve(ctx, cache_name ? cache_name : krb5_cc_default_name(ctx), &cc))) {
143                 goto out;
144         }
145
146         if ((code = smb_krb5_parse_name(ctx, principal, &me))) {
147                 goto out;
148         }
149
150         if ((code = krb5_get_init_creds_opt_alloc(ctx, &opt))) {
151                 goto out;
152         }
153
154         krb5_get_init_creds_opt_set_renew_life(opt, renewable_time);
155         krb5_get_init_creds_opt_set_forwardable(opt, True);
156
157         /* Turn on canonicalization for lower case realm support */
158 #ifndef SAMBA4_USES_HEIMDAL /* MIT */
159         krb5_get_init_creds_opt_set_canonicalize(opt, true);
160 #endif /* MIT */
161 #if 0
162         /* insane testing */
163         krb5_get_init_creds_opt_set_tkt_life(opt, 60);
164 #endif
165
166 #ifdef HAVE_KRB5_GET_INIT_CREDS_OPT_SET_PAC_REQUEST
167         if (request_pac) {
168                 if ((code = krb5_get_init_creds_opt_set_pac_request(ctx, opt, (krb5_boolean)request_pac))) {
169                         goto out;
170                 }
171         }
172 #endif
173         if (add_netbios_addr) {
174                 if ((code = smb_krb5_gen_netbios_krb5_address(&addr,
175                                                         lp_netbios_name()))) {
176                         goto out;
177                 }
178                 krb5_get_init_creds_opt_set_address_list(opt, addr->addrs);
179         }
180
181         if ((code = krb5_get_init_creds_password(ctx, &my_creds, me, discard_const_p(char,password), 
182                                                  kerb_prompter, discard_const_p(char, password),
183                                                  0, NULL, opt))) {
184                 goto out;
185         }
186
187         canon_princ = me;
188 #ifndef SAMBA4_USES_HEIMDAL /* MIT */
189         canon_princ = my_creds.client;
190 #endif /* MIT */
191
192         if ((code = krb5_cc_initialize(ctx, cc, canon_princ))) {
193                 goto out;
194         }
195
196         if ((code = krb5_cc_store_cred(ctx, cc, &my_creds))) {
197                 goto out;
198         }
199
200         if (expire_time) {
201                 *expire_time = (time_t) my_creds.times.endtime;
202         }
203
204         if (renew_till_time) {
205                 *renew_till_time = (time_t) my_creds.times.renew_till;
206         }
207  out:
208         if (ntstatus) {
209                 /* fast path */
210                 if (code == 0) {
211                         *ntstatus = NT_STATUS_OK;
212                         goto cleanup;
213                 }
214
215                 /* fall back to self-made-mapping */
216                 *ntstatus = krb5_to_nt_status(code);
217         }
218
219  cleanup:
220         krb5_free_cred_contents(ctx, &my_creds);
221         if (me) {
222                 krb5_free_principal(ctx, me);
223         }
224         if (addr) {
225                 smb_krb5_free_addresses(ctx, addr);
226         }
227         if (opt) {
228                 krb5_get_init_creds_opt_free(ctx, opt);
229         }
230         if (cc) {
231                 krb5_cc_close(ctx, cc);
232         }
233         if (ctx) {
234                 krb5_free_context(ctx);
235         }
236         return code;
237 }
238
239 int ads_kdestroy(const char *cc_name)
240 {
241         krb5_error_code code;
242         krb5_context ctx = NULL;
243         krb5_ccache cc = NULL;
244
245         initialize_krb5_error_table();
246         if ((code = krb5_init_context (&ctx))) {
247                 DEBUG(3, ("ads_kdestroy: kdb5_init_context failed: %s\n", 
248                         error_message(code)));
249                 return code;
250         }
251
252         if (!cc_name) {
253                 if ((code = krb5_cc_default(ctx, &cc))) {
254                         krb5_free_context(ctx);
255                         return code;
256                 }
257         } else {
258                 if ((code = krb5_cc_resolve(ctx, cc_name, &cc))) {
259                         DEBUG(3, ("ads_kdestroy: krb5_cc_resolve failed: %s\n",
260                                   error_message(code)));
261                         krb5_free_context(ctx);
262                         return code;
263                 }
264         }
265
266         if ((code = krb5_cc_destroy (ctx, cc))) {
267                 DEBUG(3, ("ads_kdestroy: krb5_cc_destroy failed: %s\n", 
268                         error_message(code)));
269         }
270
271         krb5_free_context (ctx);
272         return code;
273 }
274
275 int create_kerberos_key_from_string(krb5_context context,
276                                         krb5_principal host_princ,
277                                         krb5_principal salt_princ,
278                                         krb5_data *password,
279                                         krb5_keyblock *key,
280                                         krb5_enctype enctype,
281                                         bool no_salt)
282 {
283         int ret;
284         /*
285          * Check if we've determined that the KDC is salting keys for this
286          * principal/enctype in a non-obvious way.  If it is, try to match
287          * its behavior.
288          */
289         if (no_salt) {
290                 KRB5_KEY_DATA(key) = (KRB5_KEY_DATA_CAST *)SMB_MALLOC(password->length);
291                 if (!KRB5_KEY_DATA(key)) {
292                         return ENOMEM;
293                 }
294                 memcpy(KRB5_KEY_DATA(key), password->data, password->length);
295                 KRB5_KEY_LENGTH(key) = password->length;
296                 KRB5_KEY_TYPE(key) = enctype;
297                 return 0;
298         }
299         ret = smb_krb5_create_key_from_string(context,
300                                               salt_princ ? salt_princ : host_princ,
301                                               NULL,
302                                               password,
303                                               enctype,
304                                               key);
305         return ret;
306 }
307
308 /************************************************************************
309 ************************************************************************/
310
311 int kerberos_kinit_password(const char *principal,
312                             const char *password,
313                             int time_offset,
314                             const char *cache_name)
315 {
316         return kerberos_kinit_password_ext(principal, 
317                                            password, 
318                                            time_offset, 
319                                            0, 
320                                            0,
321                                            cache_name,
322                                            False,
323                                            False,
324                                            0,
325                                            NULL);
326 }
327
328 /************************************************************************
329 ************************************************************************/
330
331 /************************************************************************
332  Create a string list of available kdc's, possibly searching by sitename.
333  Does DNS queries.
334
335  If "sitename" is given, the DC's in that site are listed first.
336
337 ************************************************************************/
338
339 static void add_sockaddr_unique(struct sockaddr_storage *addrs, size_t *num_addrs,
340                                 const struct sockaddr_storage *addr)
341 {
342         size_t i;
343
344         for (i=0; i<*num_addrs; i++) {
345                 if (sockaddr_equal((const struct sockaddr *)&addrs[i],
346                                    (const struct sockaddr *)addr)) {
347                         return;
348                 }
349         }
350         addrs[i] = *addr;
351         *num_addrs += 1;
352 }
353
354 /* print_canonical_sockaddr prints an ipv6 addr in the form of
355 * [ipv6.addr]. This string, when put in a generated krb5.conf file is not
356 * always properly dealt with by some older krb5 libraries. Adding the hard-coded
357 * portnumber workarounds the issue. - gd */
358
359 static char *print_canonical_sockaddr_with_port(TALLOC_CTX *mem_ctx,
360                                                 const struct sockaddr_storage *pss)
361 {
362         char *str = NULL;
363
364         str = print_canonical_sockaddr(mem_ctx, pss);
365         if (str == NULL) {
366                 return NULL;
367         }
368
369         if (pss->ss_family != AF_INET6) {
370                 return str;
371         }
372
373 #if defined(HAVE_IPV6)
374         str = talloc_asprintf_append(str, ":88");
375 #endif
376         return str;
377 }
378
379 static char *get_kdc_ip_string(char *mem_ctx,
380                 const char *realm,
381                 const char *sitename,
382                 const struct sockaddr_storage *pss)
383 {
384         TALLOC_CTX *frame = talloc_stackframe();
385         size_t i;
386         struct ip_service *ip_srv_site = NULL;
387         struct ip_service *ip_srv_nonsite = NULL;
388         int count_site = 0;
389         int count_nonsite;
390         size_t num_dcs;
391         struct sockaddr_storage *dc_addrs;
392         struct tsocket_address **dc_addrs2 = NULL;
393         const struct tsocket_address * const *dc_addrs3 = NULL;
394         char *result = NULL;
395         struct netlogon_samlogon_response **responses = NULL;
396         NTSTATUS status;
397         char *kdc_str = talloc_asprintf(mem_ctx, "%s\t\tkdc = %s\n", "",
398                                         print_canonical_sockaddr_with_port(mem_ctx, pss));
399
400         if (kdc_str == NULL) {
401                 TALLOC_FREE(frame);
402                 return NULL;
403         }
404
405         /*
406          * First get the KDC's only in this site, the rest will be
407          * appended later
408          */
409
410         if (sitename) {
411                 get_kdc_list(realm, sitename, &ip_srv_site, &count_site);
412                 DEBUG(10, ("got %d addresses from site %s search\n", count_site,
413                            sitename));
414         }
415
416         /* Get all KDC's. */
417
418         get_kdc_list(realm, NULL, &ip_srv_nonsite, &count_nonsite);
419         DEBUG(10, ("got %d addresses from site-less search\n", count_nonsite));
420
421         dc_addrs = talloc_array(talloc_tos(), struct sockaddr_storage,
422                                 count_site + count_nonsite);
423         if (dc_addrs == NULL) {
424                 goto out;
425         }
426
427         num_dcs = 0;
428
429         for (i = 0; i < count_site; i++) {
430                 if (!sockaddr_equal(
431                         (const struct sockaddr *)pss,
432                         (const struct sockaddr *)&ip_srv_site[i].ss)) {
433                         add_sockaddr_unique(dc_addrs, &num_dcs,
434                                             &ip_srv_site[i].ss);
435                 }
436         }
437
438         for (i = 0; i < count_nonsite; i++) {
439                 if (!sockaddr_equal(
440                         (const struct sockaddr *)pss,
441                         (const struct sockaddr *)&ip_srv_nonsite[i].ss)) {
442                         add_sockaddr_unique(dc_addrs, &num_dcs,
443                                             &ip_srv_nonsite[i].ss);
444                 }
445         }
446
447         dc_addrs2 = talloc_zero_array(talloc_tos(),
448                                       struct tsocket_address *,
449                                       num_dcs);
450
451         DBG_DEBUG("%zu additional KDCs to test\n", num_dcs);
452         if (num_dcs == 0) {
453                 goto out;
454         }
455         if (dc_addrs2 == NULL) {
456                 goto out;
457         }
458
459         for (i=0; i<num_dcs; i++) {
460                 char addr[INET6_ADDRSTRLEN];
461                 int ret;
462
463                 print_sockaddr(addr, sizeof(addr), &dc_addrs[i]);
464
465                 ret = tsocket_address_inet_from_strings(dc_addrs2, "ip",
466                                                         addr, LDAP_PORT,
467                                                         &dc_addrs2[i]);
468                 if (ret != 0) {
469                         status = map_nt_error_from_unix(errno);
470                         DEBUG(2,("Failed to create tsocket_address for %s - %s\n",
471                                  addr, nt_errstr(status)));
472                         goto out;
473                 }
474         }
475
476         dc_addrs3 = (const struct tsocket_address * const *)dc_addrs2;
477
478         status = cldap_multi_netlogon(talloc_tos(),
479                         dc_addrs3, num_dcs,
480                         realm, lp_netbios_name(),
481                         NETLOGON_NT_VERSION_5 | NETLOGON_NT_VERSION_5EX,
482                         MIN(num_dcs, 3), timeval_current_ofs(3, 0), &responses);
483         TALLOC_FREE(dc_addrs2);
484         dc_addrs3 = NULL;
485
486         if (!NT_STATUS_IS_OK(status)) {
487                 DEBUG(10,("get_kdc_ip_string: cldap_multi_netlogon failed: "
488                           "%s\n", nt_errstr(status)));
489                 goto out;
490         }
491
492         for (i=0; i<num_dcs; i++) {
493                 char *new_kdc_str;
494
495                 if (responses[i] == NULL) {
496                         continue;
497                 }
498
499                 /* Append to the string - inefficient but not done often. */
500                 new_kdc_str = talloc_asprintf(mem_ctx, "%s\t\tkdc = %s\n",
501                                               kdc_str,
502                                               print_canonical_sockaddr_with_port(mem_ctx, &dc_addrs[i]));
503                 if (new_kdc_str == NULL) {
504                         goto out;
505                 }
506                 TALLOC_FREE(kdc_str);
507                 kdc_str = new_kdc_str;
508         }
509
510 out:
511         DEBUG(10, ("get_kdc_ip_string: Returning %s\n", kdc_str));
512
513         result = kdc_str;
514         SAFE_FREE(ip_srv_site);
515         SAFE_FREE(ip_srv_nonsite);
516         TALLOC_FREE(frame);
517         return result;
518 }
519
520 /************************************************************************
521  Create  a specific krb5.conf file in the private directory pointing
522  at a specific kdc for a realm. Keyed off domain name. Sets
523  KRB5_CONFIG environment variable to point to this file. Must be
524  run as root or will fail (which is a good thing :-).
525 ************************************************************************/
526
527 #if !defined(SAMBA4_USES_HEIMDAL) /* MIT version */
528 static char *get_enctypes(TALLOC_CTX *mem_ctx)
529 {
530         char *aes_enctypes = NULL;
531         const char *legacy_enctypes = "";
532         char *enctypes = NULL;
533
534         aes_enctypes = talloc_strdup(mem_ctx, "");
535         if (aes_enctypes == NULL) {
536                 goto done;
537         }
538
539         if (lp_kerberos_encryption_types() == KERBEROS_ETYPES_ALL ||
540             lp_kerberos_encryption_types() == KERBEROS_ETYPES_STRONG) {
541 #ifdef HAVE_ENCTYPE_AES256_CTS_HMAC_SHA1_96
542                 aes_enctypes = talloc_asprintf_append(
543                     aes_enctypes, "%s", "aes256-cts-hmac-sha1-96 ");
544                 if (aes_enctypes == NULL) {
545                         goto done;
546                 }
547 #endif
548 #ifdef HAVE_ENCTYPE_AES128_CTS_HMAC_SHA1_96
549                 aes_enctypes = talloc_asprintf_append(
550                     aes_enctypes, "%s", "aes128-cts-hmac-sha1-96");
551                 if (aes_enctypes == NULL) {
552                         goto done;
553                 }
554 #endif
555         }
556
557         if (lp_kerberos_encryption_types() == KERBEROS_ETYPES_ALL ||
558             lp_kerberos_encryption_types() == KERBEROS_ETYPES_LEGACY) {
559                 legacy_enctypes = "RC4-HMAC DES-CBC-CRC DES-CBC-MD5";
560         }
561
562         enctypes =
563             talloc_asprintf(mem_ctx, "\tdefault_tgs_enctypes = %s %s\n"
564                                      "\tdefault_tkt_enctypes = %s %s\n"
565                                      "\tpreferred_enctypes = %s %s\n",
566                             aes_enctypes, legacy_enctypes, aes_enctypes,
567                             legacy_enctypes, aes_enctypes, legacy_enctypes);
568 done:
569         TALLOC_FREE(aes_enctypes);
570         return enctypes;
571 }
572 #else /* Heimdal version */
573 static char *get_enctypes(TALLOC_CTX *mem_ctx)
574 {
575         const char *aes_enctypes = "";
576         const char *legacy_enctypes = "";
577         char *enctypes = NULL;
578
579         if (lp_kerberos_encryption_types() == KERBEROS_ETYPES_ALL ||
580             lp_kerberos_encryption_types() == KERBEROS_ETYPES_STRONG) {
581                 aes_enctypes =
582                     "aes256-cts-hmac-sha1-96 aes128-cts-hmac-sha1-96";
583         }
584
585         if (lp_kerberos_encryption_types() == KERBEROS_ETYPES_ALL ||
586             lp_kerberos_encryption_types() == KERBEROS_ETYPES_LEGACY) {
587                 legacy_enctypes = "arcfour-hmac-md5 des-cbc-crc des-cbc-md5";
588         }
589
590         enctypes = talloc_asprintf(mem_ctx, "\tdefault_etypes = %s %s\n",
591                                    aes_enctypes, legacy_enctypes);
592
593         return enctypes;
594 }
595 #endif
596
597 bool create_local_private_krb5_conf_for_domain(const char *realm,
598                                                 const char *domain,
599                                                 const char *sitename,
600                                                 const struct sockaddr_storage *pss)
601 {
602         char *dname;
603         char *tmpname = NULL;
604         char *fname = NULL;
605         char *file_contents = NULL;
606         char *kdc_ip_string = NULL;
607         size_t flen = 0;
608         ssize_t ret;
609         int fd;
610         char *realm_upper = NULL;
611         bool result = false;
612         char *enctypes = NULL;
613         const char *include_system_krb5 = "";
614         mode_t mask;
615
616         if (!lp_create_krb5_conf()) {
617                 return false;
618         }
619
620         if (realm == NULL) {
621                 DEBUG(0, ("No realm has been specified! Do you really want to "
622                           "join an Active Directory server?\n"));
623                 return false;
624         }
625
626         if (domain == NULL || pss == NULL) {
627                 return false;
628         }
629
630         dname = lock_path("smb_krb5");
631         if (!dname) {
632                 return false;
633         }
634         if ((mkdir(dname, 0755)==-1) && (errno != EEXIST)) {
635                 DEBUG(0,("create_local_private_krb5_conf_for_domain: "
636                         "failed to create directory %s. Error was %s\n",
637                         dname, strerror(errno) ));
638                 goto done;
639         }
640
641         tmpname = lock_path("smb_tmp_krb5.XXXXXX");
642         if (!tmpname) {
643                 goto done;
644         }
645
646         fname = talloc_asprintf(dname, "%s/krb5.conf.%s", dname, domain);
647         if (!fname) {
648                 goto done;
649         }
650
651         DEBUG(10,("create_local_private_krb5_conf_for_domain: fname = %s, realm = %s, domain = %s\n",
652                 fname, realm, domain ));
653
654         realm_upper = talloc_strdup(fname, realm);
655         if (!strupper_m(realm_upper)) {
656                 goto done;
657         }
658
659         kdc_ip_string = get_kdc_ip_string(dname, realm, sitename, pss);
660         if (!kdc_ip_string) {
661                 goto done;
662         }
663
664         enctypes = get_enctypes(fname);
665         if (enctypes == NULL) {
666                 goto done;
667         }
668
669 #if !defined(SAMBA4_USES_HEIMDAL)
670         if (lp_include_system_krb5_conf()) {
671                 include_system_krb5 = "include /etc/krb5.conf";
672         }
673 #endif
674
675         file_contents =
676             talloc_asprintf(fname,
677                             "[libdefaults]\n\tdefault_realm = %s\n"
678                             "%s"
679                             "\tdns_lookup_realm = false\n\n"
680                             "[realms]\n\t%s = {\n"
681                             "%s\t}\n"
682                             "%s\n",
683                             realm_upper,
684                             enctypes,
685                             realm_upper,
686                             kdc_ip_string,
687                             include_system_krb5);
688
689         if (!file_contents) {
690                 goto done;
691         }
692
693         flen = strlen(file_contents);
694
695         mask = umask(S_IRWXO | S_IRWXG);
696         fd = mkstemp(tmpname);
697         umask(mask);
698         if (fd == -1) {
699                 DEBUG(0,("create_local_private_krb5_conf_for_domain: smb_mkstemp failed,"
700                         " for file %s. Errno %s\n",
701                         tmpname, strerror(errno) ));
702                 goto done;
703         }
704
705         if (fchmod(fd, 0644)==-1) {
706                 DEBUG(0,("create_local_private_krb5_conf_for_domain: fchmod failed for %s."
707                         " Errno %s\n",
708                         tmpname, strerror(errno) ));
709                 unlink(tmpname);
710                 close(fd);
711                 goto done;
712         }
713
714         ret = write(fd, file_contents, flen);
715         if (flen != ret) {
716                 DEBUG(0,("create_local_private_krb5_conf_for_domain: write failed,"
717                         " returned %d (should be %u). Errno %s\n",
718                         (int)ret, (unsigned int)flen, strerror(errno) ));
719                 unlink(tmpname);
720                 close(fd);
721                 goto done;
722         }
723         if (close(fd)==-1) {
724                 DEBUG(0,("create_local_private_krb5_conf_for_domain: close failed."
725                         " Errno %s\n", strerror(errno) ));
726                 unlink(tmpname);
727                 goto done;
728         }
729
730         if (rename(tmpname, fname) == -1) {
731                 DEBUG(0,("create_local_private_krb5_conf_for_domain: rename "
732                         "of %s to %s failed. Errno %s\n",
733                         tmpname, fname, strerror(errno) ));
734                 unlink(tmpname);
735                 goto done;
736         }
737
738         DEBUG(5,("create_local_private_krb5_conf_for_domain: wrote "
739                 "file %s with realm %s KDC list = %s\n",
740                 fname, realm_upper, kdc_ip_string));
741
742         /* Set the environment variable to this file. */
743         setenv("KRB5_CONFIG", fname, 1);
744
745         result = true;
746
747 #if defined(OVERWRITE_SYSTEM_KRB5_CONF)
748
749 #define SYSTEM_KRB5_CONF_PATH "/etc/krb5.conf"
750         /* Insanity, sheer insanity..... */
751
752         if (strequal(realm, lp_realm())) {
753                 SMB_STRUCT_STAT sbuf;
754
755                 if (sys_lstat(SYSTEM_KRB5_CONF_PATH, &sbuf, false) == 0) {
756                         if (S_ISLNK(sbuf.st_ex_mode) && sbuf.st_ex_size) {
757                                 int lret;
758                                 size_t alloc_size = sbuf.st_ex_size + 1;
759                                 char *linkpath = talloc_array(talloc_tos(), char,
760                                                 alloc_size);
761                                 if (!linkpath) {
762                                         goto done;
763                                 }
764                                 lret = readlink(SYSTEM_KRB5_CONF_PATH, linkpath,
765                                                 alloc_size - 1);
766                                 if (lret == -1) {
767                                         TALLOC_FREE(linkpath);
768                                         goto done;
769                                 }
770                                 linkpath[lret] = '\0';
771
772                                 if (strcmp(linkpath, fname) == 0) {
773                                         /* Symlink already exists. */
774                                         TALLOC_FREE(linkpath);
775                                         goto done;
776                                 }
777                                 TALLOC_FREE(linkpath);
778                         }
779                 }
780
781                 /* Try and replace with a symlink. */
782                 if (symlink(fname, SYSTEM_KRB5_CONF_PATH) == -1) {
783                         const char *newpath = SYSTEM_KRB5_CONF_PATH ".saved";
784                         if (errno != EEXIST) {
785                                 DEBUG(0,("create_local_private_krb5_conf_for_domain: symlink "
786                                         "of %s to %s failed. Errno %s\n",
787                                         fname, SYSTEM_KRB5_CONF_PATH, strerror(errno) ));
788                                 goto done; /* Not a fatal error. */
789                         }
790
791                         /* Yes, this is a race conditon... too bad. */
792                         if (rename(SYSTEM_KRB5_CONF_PATH, newpath) == -1) {
793                                 DEBUG(0,("create_local_private_krb5_conf_for_domain: rename "
794                                         "of %s to %s failed. Errno %s\n",
795                                         SYSTEM_KRB5_CONF_PATH, newpath,
796                                         strerror(errno) ));
797                                 goto done; /* Not a fatal error. */
798                         }
799
800                         if (symlink(fname, SYSTEM_KRB5_CONF_PATH) == -1) {
801                                 DEBUG(0,("create_local_private_krb5_conf_for_domain: "
802                                         "forced symlink of %s to /etc/krb5.conf failed. Errno %s\n",
803                                         fname, strerror(errno) ));
804                                 goto done; /* Not a fatal error. */
805                         }
806                 }
807         }
808 #endif
809
810 done:
811         TALLOC_FREE(tmpname);
812         TALLOC_FREE(dname);
813
814         return result;
815 }
816 #endif