gpo: Apply kerberos settings
[nivanova/samba-autobuild/.git] / lib / param / util.c
index 472096f60d295ccdd08b071a7165de82d09406db..cd8e74b9d8f387fcb0354530a2f473ef3a75b3ad 100644 (file)
@@ -28,6 +28,8 @@
 #include "system/filesys.h"
 #include "system/dir.h"
 #include "param/param.h"
+#include "libds/common/roles.h"
+#include "tdb.h"
 
 /**
  * @file
@@ -57,13 +59,13 @@ bool lpcfg_is_myname(struct loadparm_context *lp_ctx, const char *name)
        const char **aliases;
        int i;
 
-       if (strcasecmp(name, lpcfg_netbios_name(lp_ctx)) == 0) {
+       if (strcasecmp_m(name, lpcfg_netbios_name(lp_ctx)) == 0) {
                return true;
        }
 
        aliases = lpcfg_netbios_aliases(lp_ctx);
        for (i=0; aliases && aliases[i]; i++) {
-               if (strcasecmp(name, aliases[i]) == 0) {
+               if (strcasecmp_m(name, aliases[i]) == 0) {
                        return true;
                }
        }
@@ -71,14 +73,13 @@ bool lpcfg_is_myname(struct loadparm_context *lp_ctx, const char *name)
        return false;
 }
 
-
-/**
- A useful function for returning a path in the Samba lock directory.
-**/
-char *lpcfg_lock_path(TALLOC_CTX* mem_ctx, struct loadparm_context *lp_ctx,
-                        const char *name)
+static char *lpcfg_common_path(TALLOC_CTX* mem_ctx,
+                              const char *parent,
+                              const char *name)
 {
        char *fname, *dname;
+       bool ok;
+
        if (name == NULL) {
                return NULL;
        }
@@ -86,46 +87,45 @@ char *lpcfg_lock_path(TALLOC_CTX* mem_ctx, struct loadparm_context *lp_ctx,
                return talloc_strdup(mem_ctx, name);
        }
 
-       dname = talloc_strdup(mem_ctx, lpcfg_lockdir(lp_ctx));
+       dname = talloc_strdup(mem_ctx, parent);
+       if (dname == NULL) {
+               return NULL;
+       }
        trim_string(dname,"","/");
-       
-       if (!directory_exist(dname)) {
-               mkdir(dname,0755);
+
+       ok = directory_create_or_exist(dname, 0755);
+       if (!ok) {
+               DEBUG(1, ("Unable to create directory %s for file %s. "
+                         "Error was %s\n", dname, name, strerror(errno)));
+               return NULL;
        }
-       
-       fname = talloc_asprintf(mem_ctx, "%s/%s", dname, name);
 
+       fname = talloc_asprintf(mem_ctx, "%s/%s", dname, name);
+       if (fname == NULL) {
+               return dname;
+       }
        talloc_free(dname);
 
        return fname;
 }
 
+
+/**
+ A useful function for returning a path in the Samba lock directory.
+**/
+char *lpcfg_lock_path(TALLOC_CTX* mem_ctx, struct loadparm_context *lp_ctx,
+                        const char *name)
+{
+       return lpcfg_common_path(mem_ctx, lpcfg_lock_directory(lp_ctx), name);
+}
+
 /**
  A useful function for returning a path in the Samba state directory.
 **/
 char *lpcfg_state_path(TALLOC_CTX* mem_ctx, struct loadparm_context *lp_ctx,
                       const char *name)
 {
-       char *fname, *dname;
-       if (name == NULL) {
-               return NULL;
-       }
-       if (name[0] == 0 || name[0] == '/' || strstr(name, ":/")) {
-               return talloc_strdup(mem_ctx, name);
-       }
-
-       dname = talloc_strdup(mem_ctx, lpcfg_statedir(lp_ctx));
-       trim_string(dname,"","/");
-
-       if (!directory_exist(dname)) {
-               mkdir(dname,0755);
-       }
-
-       fname = talloc_asprintf(mem_ctx, "%s/%s", dname, name);
-
-       talloc_free(dname);
-
-       return fname;
+       return lpcfg_common_path(mem_ctx, lpcfg_state_directory(lp_ctx), name);
 }
 
 /**
@@ -134,26 +134,7 @@ char *lpcfg_state_path(TALLOC_CTX* mem_ctx, struct loadparm_context *lp_ctx,
 char *lpcfg_cache_path(TALLOC_CTX* mem_ctx, struct loadparm_context *lp_ctx,
                       const char *name)
 {
-       char *fname, *dname;
-       if (name == NULL) {
-               return NULL;
-       }
-       if (name[0] == 0 || name[0] == '/' || strstr(name, ":/")) {
-               return talloc_strdup(mem_ctx, name);
-       }
-
-       dname = talloc_strdup(mem_ctx, lpcfg_cachedir(lp_ctx));
-       trim_string(dname,"","/");
-
-       if (!directory_exist(dname)) {
-               mkdir(dname,0755);
-       }
-
-       fname = talloc_asprintf(mem_ctx, "%s/%s", dname, name);
-
-       talloc_free(dname);
-
-       return fname;
+       return lpcfg_common_path(mem_ctx, lpcfg_cache_directory(lp_ctx), name);
 }
 
 /**
@@ -210,6 +191,23 @@ char *lpcfg_private_path(TALLOC_CTX* mem_ctx,
        return fname;
 }
 
+/**
+ * @brief Returns an absolute path to a NTDB or TDB file in the Samba
+ * private directory.
+ *
+ * @param name File to find, relative to PRIVATEDIR, without .tdb extension.
+ *
+ * @retval Pointer to a talloc'ed string containing the full path, for
+ * use with dbwrap_local_open().
+ **/
+char *lpcfg_private_db_path(TALLOC_CTX *mem_ctx,
+                           struct loadparm_context *lp_ctx,
+                           const char *name)
+{
+       return talloc_asprintf(mem_ctx, "%s/%s.tdb",
+                              lpcfg_private_dir(lp_ctx), name);
+}
+
 /**
   return a path in the smbd.tmp directory, where all temporary file
   for smbd go. If NULL is passed for name then return the directory 
@@ -220,10 +218,16 @@ char *smbd_tmp_path(TALLOC_CTX *mem_ctx,
                             const char *name)
 {
        char *fname, *dname;
+       bool ok;
 
        dname = lpcfg_private_path(mem_ctx, lp_ctx, "smbd.tmp");
-       if (!directory_exist(dname)) {
-               mkdir(dname,0755);
+       if (dname == NULL) {
+               return NULL;
+       }
+
+       ok = directory_create_or_exist(dname, 0755);
+       if (!ok) {
+               return NULL;
        }
 
        if (name == NULL) {
@@ -231,6 +235,9 @@ char *smbd_tmp_path(TALLOC_CTX *mem_ctx,
        }
 
        fname = talloc_asprintf(mem_ctx, "%s/%s", dname, name);
+       if (fname == NULL) {
+               return dname;
+       }
        talloc_free(dname);
 
        return fname;
@@ -242,25 +249,78 @@ const char *lpcfg_imessaging_path(TALLOC_CTX *mem_ctx,
        return smbd_tmp_path(mem_ctx, lp_ctx, "msg");
 }
 
-struct smb_iconv_handle *smb_iconv_handle_reinit_lp(TALLOC_CTX *mem_ctx,
-                                                             struct loadparm_context *lp_ctx,
-                                                             struct smb_iconv_handle *old_ic)
-{
-       return smb_iconv_handle_reinit(mem_ctx, lpcfg_dos_charset(lp_ctx),
-                                      lpcfg_unix_charset(lp_ctx),
-                                      true,
-                                      old_ic);
-}
-
-
 const char *lpcfg_sam_name(struct loadparm_context *lp_ctx)
 {
        switch (lpcfg_server_role(lp_ctx)) {
        case ROLE_DOMAIN_BDC:
        case ROLE_DOMAIN_PDC:
+       case ROLE_ACTIVE_DIRECTORY_DC:
                return lpcfg_workgroup(lp_ctx);
        default:
                return lpcfg_netbios_name(lp_ctx);
        }
 }
 
+const char *lpcfg_sam_dnsname(struct loadparm_context *lp_ctx)
+{
+       switch (lpcfg_server_role(lp_ctx)) {
+       case ROLE_ACTIVE_DIRECTORY_DC:
+               return lpcfg_dnsdomain(lp_ctx);
+       default:
+               return NULL;
+       }
+}
+
+static long tdb_fetch_lifetime(TALLOC_CTX *mem_ctx, struct tdb_context *tdb, const char *keystr)
+{
+       TDB_DATA key;
+       TDB_DATA ret;
+       char *tmp = NULL;
+       long result;
+
+       key.dptr = discard_const_p(unsigned char, keystr);
+       key.dsize = strlen(keystr);
+
+       if (!key.dptr)
+               return -1;
+
+       ret = tdb_fetch(tdb, key);
+       if (ret.dsize == 0)
+               return -1;
+
+       tmp = talloc_realloc(mem_ctx, tmp, char, ret.dsize+1);
+       memset(tmp, 0, ret.dsize+1);
+       memcpy(tmp, ret.dptr, ret.dsize);
+       free(ret.dptr);
+
+       result = atol(tmp);
+       talloc_free(tmp);
+       return result;
+}
+
+void lpcfg_default_kdc_policy(TALLOC_CTX *mem_ctx,
+                               struct loadparm_context *lp_ctx,
+                               time_t *svc_tkt_lifetime,
+                               time_t *usr_tkt_lifetime,
+                               time_t *renewal_lifetime)
+{
+       long val;
+       TDB_CONTEXT *ctx = NULL;
+       const char *kdc_tdb = NULL;
+
+       kdc_tdb = lpcfg_cache_path(mem_ctx, lp_ctx, "gpo.tdb");
+       if (kdc_tdb)
+               ctx = tdb_open(kdc_tdb, 0, TDB_DEFAULT, O_RDWR, 0600);
+
+       if (!ctx || ( val = tdb_fetch_lifetime(mem_ctx, ctx, "kdc:service_ticket_lifetime") ) == -1 )
+               val = lpcfg_parm_long(lp_ctx, NULL, "kdc", "service ticket lifetime", 10);
+       *svc_tkt_lifetime = val * 60 * 60;
+
+       if (!ctx || ( val = tdb_fetch_lifetime(mem_ctx, ctx, "kdc:user_ticket_lifetime") ) == -1 )
+               val = lpcfg_parm_long(lp_ctx, NULL, "kdc", "user ticket lifetime", 10);
+       *usr_tkt_lifetime = val * 60 * 60;
+
+       if (!ctx || ( val = tdb_fetch_lifetime(mem_ctx, ctx, "kdc:renewal_lifetime") ) == -1 )
+               val = lpcfg_parm_long(lp_ctx, NULL, "kdc", "renewal lifetime", 24 * 7);
+       *renewal_lifetime = val * 60 * 60;
+}