r17994: Add debugs that showed me why my site code wasn't
[kai/samba.git] / source3 / libads / kerberos.c
index 4e4e4cfebf8db4bf26f67fd73a71b0287e43af2b..fb15ace7c7016b0d5909a2a4cfd4449292c1f00e 100644 (file)
@@ -477,15 +477,23 @@ BOOL create_local_private_krb5_conf_for_domain(const char *realm, const char *do
        char *fname = talloc_asprintf(NULL, "%s/smb_krb5.conf.%s", lp_private_dir(), domain);
        char *file_contents = NULL;
        size_t flen = 0;
+       char *realm_upper = NULL;
+       int loopcount = 0;
 
        if (!fname) {
                return False;
        }
 
+       DEBUG(10,("create_local_private_krb5_conf_for_domain: fname = %s, realm = %s, domain = %s\n",
+               fname, realm, domain ));
+
+       realm_upper = talloc_strdup(fname, realm);
+       strupper_m(realm_upper);
+
        file_contents = talloc_asprintf(fname, "[libdefaults]\n\tdefault_realm = %s\n"
                                "[realms]\n\t%s = {\n"
                                "\t\tkdc = %s\n]\n",
-                               realm, realm, inet_ntoa(ip));
+                               realm_upper, realm_upper, inet_ntoa(ip));
 
        if (!file_contents) {
                TALLOC_FREE(fname);
@@ -493,17 +501,37 @@ BOOL create_local_private_krb5_conf_for_domain(const char *realm, const char *do
        }
 
        flen = strlen(file_contents);
-       xfp = x_fopen(fname, O_CREAT|O_WRONLY, 0600);
-       if (!xfp) {
-               TALLOC_FREE(fname);
-               return False;
-       }
-       /* Lock the file. */
-       if (!fcntl_lock(xfp->fd, F_SETLKW, 0, 1, F_WRLCK)) {
-               unlink(fname);
-               x_fclose(xfp);
-               TALLOC_FREE(fname);
-               return False;
+
+       while (loopcount < 10) {
+               SMB_STRUCT_STAT st;
+
+               xfp = x_fopen(fname, O_CREAT|O_WRONLY, 0600);
+               if (!xfp) {
+                       TALLOC_FREE(fname);
+                       return False;
+               }
+               /* Lock the file. */
+               if (!fcntl_lock(xfp->fd, F_SETLKW, 0, 1, F_WRLCK)) {
+                       unlink(fname);
+                       x_fclose(xfp);
+                       TALLOC_FREE(fname);
+                       return False;
+               }
+
+               /* 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(fname);
+                       return False;
+               }
+               break;
        }
 
        if (x_fwrite(file_contents, flen, 1, xfp) != flen) {
@@ -520,6 +548,11 @@ 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);
        TALLOC_FREE(fname);
+
+       DEBUG(5,("create_local_private_krb5_conf_for_domain: wrote "
+               "file %s with realm %s KDC = %s\n",
+               fname, realm_upper, inet_ntoa(ip) ));
+
        return True;
 }
 #endif