r23563: Add dump-event-list command to smbcontrol.
[sfrench/samba-autobuild/.git] / source / libads / kerberos.c
index c872508fe84d025ea9c96ac6c01295ac1aba4072..841674bea5785801ee6712a5c8f6384bb74e6cd9 100644 (file)
@@ -55,6 +55,127 @@ kerb_prompter(krb5_context ctx, void *data,
        return 0;
 }
 
+static BOOL smb_krb5_err_io_nstatus(TALLOC_CTX *mem_ctx, 
+                                   DATA_BLOB *edata_blob, 
+                                   KRB5_EDATA_NTSTATUS *edata)
+{
+       BOOL ret = False;
+       prs_struct ps;
+
+       if (!mem_ctx || !edata_blob || !edata) 
+               return False;
+
+       if (!prs_init(&ps, edata_blob->length, mem_ctx, UNMARSHALL))
+               return False;
+
+       if (!prs_copy_data_in(&ps, (char *)edata_blob->data, edata_blob->length))
+               goto out;
+
+       prs_set_offset(&ps, 0);
+
+       if (!prs_ntstatus("ntstatus", &ps, 1, &edata->ntstatus))
+               goto out;
+
+       if (!prs_uint32("unknown1", &ps, 1, &edata->unknown1))
+               goto out;
+
+       if (!prs_uint32("unknown2", &ps, 1, &edata->unknown2)) /* only seen 00000001 here */
+               goto out;
+
+       ret = True;
+ out:
+       prs_mem_free(&ps);
+
+       return ret;
+}
+
+ static BOOL smb_krb5_get_ntstatus_from_krb5_error(krb5_error *error,
+                                                  NTSTATUS *nt_status)
+{
+       DATA_BLOB edata;
+       DATA_BLOB unwrapped_edata;
+       TALLOC_CTX *mem_ctx;
+       KRB5_EDATA_NTSTATUS parsed_edata;
+
+#ifdef HAVE_E_DATA_POINTER_IN_KRB5_ERROR
+       edata = data_blob(error->e_data->data, error->e_data->length);
+#else
+       edata = data_blob(error->e_data.data, error->e_data.length);
+#endif /* HAVE_E_DATA_POINTER_IN_KRB5_ERROR */
+
+#ifdef DEVELOPER
+       dump_data(10, edata.data, edata.length);
+#endif /* DEVELOPER */
+
+       mem_ctx = talloc_init("smb_krb5_get_ntstatus_from_krb5_error");
+       if (mem_ctx == NULL) {
+               data_blob_free(&edata);
+               return False;
+       }
+
+       if (!unwrap_edata_ntstatus(mem_ctx, &edata, &unwrapped_edata)) {
+               data_blob_free(&edata);
+               TALLOC_FREE(mem_ctx);
+               return False;
+       }
+
+       data_blob_free(&edata);
+
+       if (!smb_krb5_err_io_nstatus(mem_ctx, &unwrapped_edata, &parsed_edata)) {
+               data_blob_free(&unwrapped_edata);
+               TALLOC_FREE(mem_ctx);
+               return False;
+       }
+
+       data_blob_free(&unwrapped_edata);
+
+       if (nt_status) {
+               *nt_status = parsed_edata.ntstatus;
+       }
+
+       TALLOC_FREE(mem_ctx);
+
+       return True;
+}
+
+ static BOOL smb_krb5_get_ntstatus_from_krb5_error_init_creds_opt(krb5_context ctx, 
+                                                                 krb5_get_init_creds_opt *opt, 
+                                                                 NTSTATUS *nt_status)
+{
+       BOOL ret = False;
+       krb5_error *error = NULL;
+
+#ifdef HAVE_KRB5_GET_INIT_CREDS_OPT_GET_ERROR
+       ret = krb5_get_init_creds_opt_get_error(ctx, opt, &error);
+       if (ret) {
+               DEBUG(1,("krb5_get_init_creds_opt_get_error gave: %s\n", 
+                       error_message(ret)));
+               return False;
+       }
+#endif /* HAVE_KRB5_GET_INIT_CREDS_OPT_GET_ERROR */
+
+       if (!error) {
+               DEBUG(1,("no krb5_error\n"));
+               return False;
+       }
+
+#ifdef HAVE_E_DATA_POINTER_IN_KRB5_ERROR
+       if (!error->e_data) {
+#else
+       if (error->e_data.data == NULL) {
+#endif /* HAVE_E_DATA_POINTER_IN_KRB5_ERROR */
+               DEBUG(1,("no edata in krb5_error\n")); 
+               krb5_free_error(ctx, error);
+               return False;
+       }
+
+       ret = smb_krb5_get_ntstatus_from_krb5_error(error, nt_status);
+
+       krb5_free_error(ctx, error);
+
+       return ret;
+}
+
 /*
   simulate a kinit, putting the tgt in the given cache location. If cache_name == NULL
   place in default cache location.
@@ -68,86 +189,76 @@ int kerberos_kinit_password_ext(const char *principal,
                                const char *cache_name,
                                BOOL request_pac,
                                BOOL add_netbios_addr,
-                               time_t renewable_time)
+                               time_t renewable_time,
+                               NTSTATUS *ntstatus)
 {
        krb5_context ctx = NULL;
        krb5_error_code code = 0;
        krb5_ccache cc = NULL;
-       krb5_principal me;
+       krb5_principal me = NULL;
        krb5_creds my_creds;
-       krb5_get_init_creds_opt opt;
+       krb5_get_init_creds_opt *opt = NULL;
        smb_krb5_addresses *addr = NULL;
 
+       ZERO_STRUCT(my_creds);
+
        initialize_krb5_error_table();
        if ((code = krb5_init_context(&ctx)))
-               return code;
+               goto out;
 
        if (time_offset != 0) {
                krb5_set_real_time(ctx, time(NULL) + time_offset, 0);
        }
 
-       DEBUG(10,("kerberos_kinit_password: using %s as ccache\n",
-                       cache_name ? cache_name: krb5_cc_default_name(ctx)));
+       DEBUG(10,("kerberos_kinit_password: using [%s] as ccache and config [%s]\n",
+                       cache_name ? cache_name: krb5_cc_default_name(ctx),
+                       getenv("KRB5_CONFIG")));
 
        if ((code = krb5_cc_resolve(ctx, cache_name ? cache_name : krb5_cc_default_name(ctx), &cc))) {
-               krb5_free_context(ctx);
-               return code;
+               goto out;
        }
        
        if ((code = smb_krb5_parse_name(ctx, principal, &me))) {
-               krb5_free_context(ctx); 
-               return code;
+               goto out;
        }
 
-       krb5_get_init_creds_opt_init(&opt);
-       krb5_get_init_creds_opt_set_renew_life(&opt, renewable_time);
-       krb5_get_init_creds_opt_set_forwardable(&opt, 1);
-       
-       if (request_pac) {
+       if ((code = smb_krb5_get_init_creds_opt_alloc(ctx, &opt))) {
+               goto out;
+       }
+
+       krb5_get_init_creds_opt_set_renew_life(opt, renewable_time);
+       krb5_get_init_creds_opt_set_forwardable(opt, True);
+#if 0
+       /* insane testing */
+       krb5_get_init_creds_opt_set_tkt_life(opt, 60);
+#endif
+
 #ifdef HAVE_KRB5_GET_INIT_CREDS_OPT_SET_PAC_REQUEST
-               code = krb5_get_init_creds_opt_set_pac_request(ctx, &opt, True);
-               if (code) {
-                       krb5_free_principal(ctx, me);
-                       krb5_free_context(ctx);
-                       return code;
+       if (request_pac) {
+               if ((code = krb5_get_init_creds_opt_set_pac_request(ctx, opt, (krb5_boolean)request_pac))) {
+                       goto out;
                }
-#endif
        }
-
+#endif
        if (add_netbios_addr) {
-               code = smb_krb5_gen_netbios_krb5_address(&addr);
-               if (code) {
-                       krb5_free_principal(ctx, me);
-                       krb5_free_context(ctx);         
-                       return code;    
+               if ((code = smb_krb5_gen_netbios_krb5_address(&addr))) {
+                       goto out;
                }
-               krb5_get_init_creds_opt_set_address_list(&opt, addr->addrs);
+               krb5_get_init_creds_opt_set_address_list(opt, addr->addrs);
        }
 
        if ((code = krb5_get_init_creds_password(ctx, &my_creds, me, CONST_DISCARD(char *,password), 
-                                                kerb_prompter, NULL, 0, NULL, &opt)))
-       {
-               smb_krb5_free_addresses(ctx, addr);
-               krb5_free_principal(ctx, me);
-               krb5_free_context(ctx);         
-               return code;
+                                                kerb_prompter, CONST_DISCARD(char *,password),
+                                                0, NULL, opt))) {
+               goto out;
        }
-       
+
        if ((code = krb5_cc_initialize(ctx, cc, me))) {
-               smb_krb5_free_addresses(ctx, addr);
-               krb5_free_cred_contents(ctx, &my_creds);
-               krb5_free_principal(ctx, me);
-               krb5_free_context(ctx);         
-               return code;
+               goto out;
        }
        
        if ((code = krb5_cc_store_cred(ctx, cc, &my_creds))) {
-               krb5_cc_close(ctx, cc);
-               smb_krb5_free_addresses(ctx, addr);
-               krb5_free_cred_contents(ctx, &my_creds);
-               krb5_free_principal(ctx, me);
-               krb5_free_context(ctx);         
-               return code;
+               goto out;
        }
 
        if (expire_time) {
@@ -157,14 +268,47 @@ int kerberos_kinit_password_ext(const char *principal,
        if (renew_till_time) {
                *renew_till_time = (time_t) my_creds.times.renew_till;
        }
+ out:
+       if (ntstatus) {
 
-       krb5_cc_close(ctx, cc);
-       smb_krb5_free_addresses(ctx, addr);
+               NTSTATUS status;
+
+               /* fast path */
+               if (code == 0) {
+                       *ntstatus = NT_STATUS_OK;
+                       goto cleanup;
+               }
+
+               /* try to get ntstatus code out of krb5_error when we have it
+                * inside the krb5_get_init_creds_opt - gd */
+
+               if (opt && smb_krb5_get_ntstatus_from_krb5_error_init_creds_opt(ctx, opt, &status)) {
+                       *ntstatus = status;
+                       goto cleanup;
+               }
+
+               /* fall back to self-made-mapping */
+               *ntstatus = krb5_to_nt_status(code);
+       }
+
+ cleanup:
        krb5_free_cred_contents(ctx, &my_creds);
-       krb5_free_principal(ctx, me);
-       krb5_free_context(ctx);         
-       
-       return 0;
+       if (me) {
+               krb5_free_principal(ctx, me);
+       }
+       if (addr) {
+               smb_krb5_free_addresses(ctx, addr);
+       }
+       if (opt) {
+               smb_krb5_get_init_creds_opt_free(ctx, opt);
+       }
+       if (cc) {
+               krb5_cc_close(ctx, cc);
+       }
+       if (ctx) {
+               krb5_free_context(ctx);
+       }
+       return code;
 }
 
 
@@ -202,7 +346,8 @@ int ads_kinit_password(ADS_STRUCT *ads)
        }
        
        ret = kerberos_kinit_password_ext(s, ads->auth.password, ads->auth.time_offset,
-                       &ads->auth.expire, NULL, NULL, False, False, ads->auth.renewable);
+                       &ads->auth.tgt_expire, NULL, NULL, False, False, ads->auth.renewable, 
+                       NULL);
 
        if (ret) {
                DEBUG(0,("kerberos_kinit_password %s failed: %s\n", 
@@ -461,7 +606,8 @@ int kerberos_kinit_password(const char *principal,
                                           cache_name,
                                           False,
                                           False,
-                                          0);
+                                          0,
+                                          NULL);
 }
 
 /************************************************************************
@@ -469,10 +615,11 @@ int kerberos_kinit_password(const char *principal,
  Does DNS queries.
 ************************************************************************/
 
-static char *get_kdc_ip_string(char *mem_ctx, const char *realm, struct in_addr primary_ip)
+static char *get_kdc_ip_string(char *mem_ctx, const char *realm, const char *sitename, struct in_addr primary_ip)
 {
-       struct ip_service *ip_srv;
-       int count, i;
+       struct ip_service *ip_srv_site;
+       struct ip_service *ip_srv_nonsite;
+       int count_site, count_nonsite, i;
        char *kdc_str = talloc_asprintf(mem_ctx, "\tkdc = %s\n",
                                        inet_ntoa(primary_ip));
 
@@ -480,24 +627,65 @@ static char *get_kdc_ip_string(char *mem_ctx, const char *realm, struct in_addr
                return NULL;
        }
 
-       if (!NT_STATUS_IS_OK(get_kdc_list(realm, &ip_srv, &count))) {
-               DEBUG(10,("get_kdc_ip_string: get_kdc_list failed. Returning %s\n",
-                       kdc_str ));
-               return kdc_str;
+       /* Get the KDC's only in this site. */
+
+       if (sitename) {
+
+               get_kdc_list(realm, sitename, &ip_srv_site, &count_site);
+
+               for (i = 0; i < count_site; i++) {
+                       if (ip_equal(ip_srv_site[i].ip, primary_ip)) {
+                               continue;
+                       }
+                       /* Append to the string - inefficient but not done often. */
+                       kdc_str = talloc_asprintf(mem_ctx, "%s\tkdc = %s\n",
+                               kdc_str, inet_ntoa(ip_srv_site[i].ip));
+                       if (!kdc_str) {
+                               SAFE_FREE(ip_srv_site);
+                               return NULL;
+                       }
+               }
        }
 
-       for (i = 0; i < count; i++) {
-               if (ip_equal(ip_srv[i].ip, primary_ip)) {
+       /* Get all KDC's. */
+
+       get_kdc_list(realm, NULL, &ip_srv_nonsite, &count_nonsite);
+
+       for (i = 0; i < count_nonsite; i++) {
+               int j;
+
+               if (ip_equal(ip_srv_nonsite[i].ip, primary_ip)) {
+                       continue;
+               }
+
+               /* Ensure this isn't an IP already seen (YUK! this is n*n....) */
+               for (j = 0; j < count_site; j++) {
+                       if (ip_equal(ip_srv_nonsite[i].ip, ip_srv_site[j].ip)) {
+                               break;
+                       }
+                       /* As the lists are sorted we can break early if nonsite > site. */
+                       if (ip_service_compare(&ip_srv_nonsite[i], &ip_srv_site[j]) > 0) {
+                               break;
+                       }
+               }
+               if (j != i) {
                        continue;
                }
+
                /* Append to the string - inefficient but not done often. */
                kdc_str = talloc_asprintf(mem_ctx, "%s\tkdc = %s\n",
-                       kdc_str, inet_ntoa(ip_srv[i].ip));
+                       kdc_str, inet_ntoa(ip_srv_nonsite[i].ip));
                if (!kdc_str) {
+                       SAFE_FREE(ip_srv_site);
+                       SAFE_FREE(ip_srv_nonsite);
                        return NULL;
                }
        }
 
+
+       SAFE_FREE(ip_srv_site);
+       SAFE_FREE(ip_srv_nonsite);
+
        DEBUG(10,("get_kdc_ip_string: Returning %s\n",
                kdc_str ));
 
@@ -511,17 +699,18 @@ static char *get_kdc_ip_string(char *mem_ctx, const char *realm, struct in_addr
  run as root or will fail (which is a good thing :-).
 ************************************************************************/
 
-BOOL create_local_private_krb5_conf_for_domain(const char *realm, const char *domain, struct in_addr ip)
+BOOL create_local_private_krb5_conf_for_domain(const char *realm, const char *domain,
+                                       const char *sitename, struct in_addr ip)
 {
-       XFILE *xfp = NULL;
        char *dname = talloc_asprintf(NULL, "%s/smb_krb5", lp_lockdir());
+       char *tmpname = NULL;
        char *fname = NULL;
        char *file_contents = NULL;
-       char *kdc_ip_string;
+       char *kdc_ip_string = NULL;
        size_t flen = 0;
-       size_t ret;
+       ssize_t ret;
+       int fd;
        char *realm_upper = NULL;
-       int loopcount = 0;
 
        if (!dname) {
                return False;
@@ -534,6 +723,12 @@ BOOL create_local_private_krb5_conf_for_domain(const char *realm, const char *do
                return False;
        }
 
+       tmpname = talloc_asprintf(dname, "%s/smb_tmp_krb5.XXXXXX", lp_lockdir());
+       if (!tmpname) {
+               TALLOC_FREE(dname);
+               return False;
+       }
+
        fname = talloc_asprintf(dname, "%s/krb5.conf.%s", dname, domain);
        if (!fname) {
                TALLOC_FREE(dname);
@@ -546,7 +741,7 @@ BOOL create_local_private_krb5_conf_for_domain(const char *realm, const char *do
        realm_upper = talloc_strdup(fname, realm);
        strupper_m(realm_upper);
 
-       kdc_ip_string = get_kdc_ip_string(dname, realm, ip);
+       kdc_ip_string = get_kdc_ip_string(dname, realm, sitename, ip);
        if (!kdc_ip_string) {
                TALLOC_FREE(dname);
                return False;
@@ -554,7 +749,7 @@ BOOL create_local_private_krb5_conf_for_domain(const char *realm, const char *do
                
        file_contents = talloc_asprintf(fname, "[libdefaults]\n\tdefault_realm = %s\n\n"
                                "[realms]\n\t%s = {\n"
-                               "\t\t%s\t}\n",
+                               "\t%s\t}\n",
                                realm_upper, realm_upper, kdc_ip_string);
 
        if (!file_contents) {
@@ -564,52 +759,46 @@ BOOL create_local_private_krb5_conf_for_domain(const char *realm, const char *do
 
        flen = strlen(file_contents);
 
-       while (loopcount < 10) {
-               SMB_STRUCT_STAT st;
-
-               xfp = x_fopen(fname, O_CREAT|O_WRONLY, 0644);
-               if (!xfp) {
-                       TALLOC_FREE(dname);
-                       return False;
-               }
-               /* Lock the file. */
-               if (!fcntl_lock(xfp->fd, F_SETLKW, 0, 1, F_WRLCK)) {
-                       unlink(fname);
-                       x_fclose(xfp);
-                       TALLOC_FREE(dname);
-                       return False;
-               }
+       fd = smb_mkstemp(tmpname);
+       if (fd == -1) {
+               DEBUG(0,("create_local_private_krb5_conf_for_domain: smb_mkstemp failed,"
+                       " for file %s. Errno %s\n",
+                       tmpname, strerror(errno) ));
+       }
 
-               /* We got the lock. Is the file still there ? */
-               if (sys_stat(fname,&st)==-1) {
-                       if (errno == ENOENT) {
-                               /* Nope - try again up to 10x */
-                               x_fclose(xfp);
-                               loopcount++;
-                               continue;       
-                       }
-                       unlink(fname);
-                       x_fclose(xfp);
-                       TALLOC_FREE(dname);
-                       return False;
-               }
-               break;
+       if (fchmod(fd, 0644)==-1) {
+               DEBUG(0,("create_local_private_krb5_conf_for_domain: fchmod failed for %s."
+                       " Errno %s\n",
+                       tmpname, strerror(errno) ));
+               unlink(tmpname);
+               close(fd);
+               TALLOC_FREE(dname);
+               return False;
        }
 
-       ret = x_fwrite(file_contents, 1, flen, xfp);
+       ret = write(fd, file_contents, flen);
        if (flen != ret) {
-               DEBUG(0,("create_local_private_krb5_conf_for_domain: x_fwrite failed,"
-                       " returned %u (should be %u). Errno %s\n",
-                       (unsigned int)ret, (unsigned int)flen, strerror(errno) ));
-               unlink(fname);
-               x_fclose(xfp);
+               DEBUG(0,("create_local_private_krb5_conf_for_domain: write failed,"
+                       " returned %d (should be %u). Errno %s\n",
+                       (int)ret, (unsigned int)flen, strerror(errno) ));
+               unlink(tmpname);
+               close(fd);
                TALLOC_FREE(dname);
                return False;
        }
-       if (x_fclose(xfp)==-1) {
-               DEBUG(0,("create_local_private_krb5_conf_for_domain: x_fclose failed."
+       if (close(fd)==-1) {
+               DEBUG(0,("create_local_private_krb5_conf_for_domain: close failed."
                        " Errno %s\n", strerror(errno) ));
-               unlink(fname);
+               unlink(tmpname);
+               TALLOC_FREE(dname);
+               return False;
+       }
+
+       if (rename(tmpname, fname) == -1) {
+               DEBUG(0,("create_local_private_krb5_conf_for_domain: rename "
+                       "of %s to %s failed. Errno %s\n",
+                       tmpname, fname, strerror(errno) ));
+               unlink(tmpname);
                TALLOC_FREE(dname);
                return False;
        }
@@ -620,6 +809,59 @@ BOOL create_local_private_krb5_conf_for_domain(const char *realm, const char *do
 
        /* Set the environment variable to this file. */
        setenv("KRB5_CONFIG", fname, 1);
+
+#if defined(OVERWRITE_SYSTEM_KRB5_CONF)
+
+#define SYSTEM_KRB5_CONF_PATH "/etc/krb5.conf"
+       /* Insanity, sheer insanity..... */
+
+       if (strequal(realm, lp_realm())) {
+               pstring linkpath;
+               int lret;
+
+               lret = readlink(SYSTEM_KRB5_CONF_PATH, linkpath, sizeof(linkpath)-1);
+               linkpath[sizeof(pstring)-1] = '\0';
+
+               if (lret == 0 || strcmp(linkpath, fname) == 0) {
+                       /* Symlink already exists. */
+                       TALLOC_FREE(dname);
+                       return True;
+               }
+
+               /* Try and replace with a symlink. */
+               if (symlink(fname, SYSTEM_KRB5_CONF_PATH) == -1) {
+                       if (errno != EEXIST) {
+                               DEBUG(0,("create_local_private_krb5_conf_for_domain: symlink "
+                                       "of %s to %s failed. Errno %s\n",
+                                       fname, SYSTEM_KRB5_CONF_PATH, strerror(errno) ));
+                               TALLOC_FREE(dname);
+                               return True; /* Not a fatal error. */
+                       }
+
+                       pstrcpy(linkpath, SYSTEM_KRB5_CONF_PATH);
+                       pstrcat(linkpath, ".saved");
+
+                       /* Yes, this is a race conditon... too bad. */
+                       if (rename(SYSTEM_KRB5_CONF_PATH, linkpath) == -1) {
+                               DEBUG(0,("create_local_private_krb5_conf_for_domain: rename "
+                                       "of %s to %s failed. Errno %s\n",
+                                       SYSTEM_KRB5_CONF_PATH, linkpath,
+                                       strerror(errno) ));
+                               TALLOC_FREE(dname);
+                               return True; /* Not a fatal error. */
+                       }
+
+                       if (symlink(fname, "/etc/krb5.conf") == -1) {
+                               DEBUG(0,("create_local_private_krb5_conf_for_domain: "
+                                       "forced symlink of %s to /etc/krb5.conf failed. Errno %s\n",
+                                       fname, strerror(errno) ));
+                               TALLOC_FREE(dname);
+                               return True; /* Not a fatal error. */
+                       }
+               }
+       }
+#endif
+
        TALLOC_FREE(dname);
 
        return True;