LCOV - code coverage report
Current view: top level - lib/param - util.c (source / functions) Hit Total Coverage
Test: coverage report for master 2b515b7d Lines: 108 134 80.6 %
Date: 2024-02-28 12:06:22 Functions: 16 17 94.1 %

          Line data    Source code
       1             : /* 
       2             :    Unix SMB/CIFS implementation.
       3             :    Samba utility functions
       4             :    Copyright (C) Andrew Tridgell 1992-1998
       5             :    Copyright (C) Jeremy Allison 2001-2002
       6             :    Copyright (C) Simo Sorce 2001
       7             :    Copyright (C) Jim McDonough (jmcd@us.ibm.com)  2003.
       8             :    Copyright (C) James J Myers 2003
       9             :    Copyright (C) Jelmer Vernooij 2005-2007
      10             :    
      11             :    This program is free software; you can redistribute it and/or modify
      12             :    it under the terms of the GNU General Public License as published by
      13             :    the Free Software Foundation; either version 3 of the License, or
      14             :    (at your option) any later version.
      15             :    
      16             :    This program 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
      19             :    GNU General Public License for more details.
      20             :    
      21             :    You should have received a copy of the GNU General Public License
      22             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.
      23             : */
      24             : 
      25             : #include "includes.h"
      26             : #include "dynconfig/dynconfig.h"
      27             : #include "system/network.h"
      28             : #include "system/filesys.h"
      29             : #include "system/dir.h"
      30             : #include "param/param.h"
      31             : #include "libds/common/roles.h"
      32             : #include "tdb.h"
      33             : 
      34             : /**
      35             :  * @file
      36             :  * @brief Misc utility functions
      37             :  */
      38             : 
      39             : 
      40        8186 : bool lpcfg_is_mydomain(struct loadparm_context *lp_ctx,
      41             :                              const char *domain)
      42             : {
      43        8186 :         return strequal(lpcfg_workgroup(lp_ctx), domain);
      44             : }
      45             : 
      46      565645 : bool lpcfg_is_my_domain_or_realm(struct loadparm_context *lp_ctx,
      47             :                              const char *domain)
      48             : {
      49     1097884 :         return strequal(lpcfg_workgroup(lp_ctx), domain) ||
      50      532239 :                 strequal(lpcfg_realm(lp_ctx), domain);
      51             : }
      52             : 
      53             : /**
      54             :   see if a string matches either our primary or one of our secondary 
      55             :   netbios aliases. do a case insensitive match
      56             : */
      57       28099 : bool lpcfg_is_myname(struct loadparm_context *lp_ctx, const char *name)
      58             : {
      59        1412 :         const char **aliases;
      60        1412 :         int i;
      61             : 
      62       28099 :         if (strcasecmp_m(name, lpcfg_netbios_name(lp_ctx)) == 0) {
      63         106 :                 return true;
      64             :         }
      65             : 
      66       27992 :         aliases = lpcfg_netbios_aliases(lp_ctx);
      67       32214 :         for (i=0; aliases && aliases[i]; i++) {
      68        2811 :                 if (strcasecmp_m(name, aliases[i]) == 0) {
      69           0 :                         return true;
      70             :                 }
      71             :         }
      72             : 
      73       26581 :         return false;
      74             : }
      75             : 
      76      411451 : static char *lpcfg_common_path(TALLOC_CTX* mem_ctx,
      77             :                                const char *parent,
      78             :                                const char *name)
      79             : {
      80       17048 :         char *fname, *dname;
      81       17048 :         bool ok;
      82             : 
      83      411451 :         if (name == NULL) {
      84           0 :                 return NULL;
      85             :         }
      86      411451 :         if (name[0] == 0 || name[0] == '/' || strstr(name, ":/")) {
      87           0 :                 return talloc_strdup(mem_ctx, name);
      88             :         }
      89             : 
      90      411451 :         dname = talloc_strdup(mem_ctx, parent);
      91      411451 :         if (dname == NULL) {
      92           0 :                 return NULL;
      93             :         }
      94      411451 :         trim_string(dname,"","/");
      95             : 
      96      411451 :         ok = directory_create_or_exist(dname, 0755);
      97      411451 :         if (!ok) {
      98           0 :                 DEBUG(1, ("Unable to create directory %s for file %s. "
      99             :                           "Error was %s\n", dname, name, strerror(errno)));
     100           0 :                 return NULL;
     101             :         }
     102             : 
     103      411451 :         fname = talloc_asprintf(mem_ctx, "%s/%s", dname, name);
     104      411451 :         if (fname == NULL) {
     105           0 :                 return dname;
     106             :         }
     107      411451 :         talloc_free(dname);
     108             : 
     109      411451 :         return fname;
     110             : }
     111             : 
     112             : 
     113             : /**
     114             :  A useful function for returning a path in the Samba lock directory.
     115             : **/
     116      406136 : char *lpcfg_lock_path(TALLOC_CTX* mem_ctx, struct loadparm_context *lp_ctx,
     117             :                          const char *name)
     118             : {
     119      406136 :         return lpcfg_common_path(mem_ctx, lpcfg_lock_directory(lp_ctx), name);
     120             : }
     121             : 
     122             : /**
     123             :  A useful function for returning a path in the Samba state directory.
     124             : **/
     125         288 : char *lpcfg_state_path(TALLOC_CTX* mem_ctx, struct loadparm_context *lp_ctx,
     126             :                        const char *name)
     127             : {
     128         288 :         return lpcfg_common_path(mem_ctx, lpcfg_state_directory(lp_ctx), name);
     129             : }
     130             : 
     131             : /**
     132             :  A useful function for returning a path in the Samba cache directory.
     133             : **/
     134        5027 : char *lpcfg_cache_path(TALLOC_CTX* mem_ctx, struct loadparm_context *lp_ctx,
     135             :                        const char *name)
     136             : {
     137        5027 :         return lpcfg_common_path(mem_ctx, lpcfg_cache_directory(lp_ctx), name);
     138             : }
     139             : 
     140             : /**
     141             :  * @brief Returns an absolute path to a file in the directory containing the current config file
     142             :  *
     143             :  * @param name File to find, relative to the config file directory.
     144             :  *
     145             :  * @retval Pointer to a talloc'ed string containing the full path.
     146             :  **/
     147             : 
     148      400153 : char *lpcfg_config_path(TALLOC_CTX* mem_ctx, struct loadparm_context *lp_ctx,
     149             :                            const char *name)
     150             : {
     151       13153 :         char *fname, *config_dir, *p;
     152      400153 :         config_dir = talloc_strdup(mem_ctx, lpcfg_configfile(lp_ctx));
     153      400153 :         if (config_dir == NULL) {
     154      110825 :                 config_dir = talloc_strdup(mem_ctx, lp_default_path());
     155             :         }
     156      400153 :         p = strrchr(config_dir, '/');
     157      400153 :         if (p == NULL) {
     158           0 :                 talloc_free(config_dir);
     159           0 :                 config_dir = talloc_strdup(mem_ctx, ".");
     160           0 :                 if (config_dir == NULL) {
     161           0 :                         return NULL;
     162             :                 }
     163             :         } else {
     164      400153 :                 p[0] = '\0';
     165             :         }
     166      400153 :         fname = talloc_asprintf(mem_ctx, "%s/%s", config_dir, name);
     167      400153 :         talloc_free(config_dir);
     168      400153 :         return fname;
     169             : }
     170             : 
     171             : /**
     172             :  * @brief Returns an absolute path to a file in the Samba private directory.
     173             :  *
     174             :  * @param name File to find, relative to PRIVATEDIR.
     175             :  * if name is not relative, then use it as-is
     176             :  *
     177             :  * @retval Pointer to a talloc'ed string containing the full path.
     178             :  **/
     179      850064 : char *lpcfg_private_path(TALLOC_CTX* mem_ctx,
     180             :                             struct loadparm_context *lp_ctx,
     181             :                             const char *name)
     182             : {
     183       32967 :         char *fname;
     184      850064 :         if (name == NULL) {
     185           0 :                 return NULL;
     186             :         }
     187      850064 :         if (name[0] == 0 || name[0] == '/' || strstr(name, ":/")) {
     188       35411 :                 return talloc_strdup(mem_ctx, name);
     189             :         }
     190      814653 :         fname = talloc_asprintf(mem_ctx, "%s/%s", lpcfg_private_dir(lp_ctx), name);
     191      814653 :         return fname;
     192             : }
     193             : 
     194             : /**
     195             :  * @brief Returns an absolute path to a NTDB or TDB file in the Samba
     196             :  * private directory.
     197             :  *
     198             :  * @param name File to find, relative to PRIVATEDIR, without .tdb extension.
     199             :  *
     200             :  * @retval Pointer to a talloc'ed string containing the full path, for
     201             :  * use with dbwrap_local_open().
     202             :  **/
     203      100157 : char *lpcfg_private_db_path(TALLOC_CTX *mem_ctx,
     204             :                             struct loadparm_context *lp_ctx,
     205             :                             const char *name)
     206             : {
     207      100157 :         return talloc_asprintf(mem_ctx, "%s/%s.tdb",
     208             :                                lpcfg_private_dir(lp_ctx), name);
     209             : }
     210             : 
     211             : /**
     212             :   return a path in the smbd.tmp directory, where all temporary file
     213             :   for smbd go. If NULL is passed for name then return the directory 
     214             :   path itself
     215             : */
     216      110253 : char *smbd_tmp_path(TALLOC_CTX *mem_ctx, 
     217             :                              struct loadparm_context *lp_ctx,
     218             :                              const char *name)
     219             : {
     220        5151 :         char *fname, *dname;
     221        5151 :         bool ok;
     222             : 
     223      110253 :         dname = lpcfg_private_path(mem_ctx, lp_ctx, "smbd.tmp");
     224      110253 :         if (dname == NULL) {
     225           0 :                 return NULL;
     226             :         }
     227             : 
     228      110253 :         ok = directory_create_or_exist(dname, 0755);
     229      110253 :         if (!ok) {
     230           0 :                 return NULL;
     231             :         }
     232             : 
     233      110253 :         if (name == NULL) {
     234          63 :                 return dname;
     235             :         }
     236             : 
     237      110187 :         fname = talloc_asprintf(mem_ctx, "%s/%s", dname, name);
     238      110187 :         if (fname == NULL) {
     239           0 :                 return dname;
     240             :         }
     241      110187 :         talloc_free(dname);
     242             : 
     243      110187 :         return fname;
     244             : }
     245             : 
     246           8 : const char *lpcfg_imessaging_path(TALLOC_CTX *mem_ctx,
     247             :                                        struct loadparm_context *lp_ctx)
     248             : {
     249           8 :         return smbd_tmp_path(mem_ctx, lp_ctx, "msg");
     250             : }
     251             : 
     252      226755 : const char *lpcfg_sam_name(struct loadparm_context *lp_ctx)
     253             : {
     254      226755 :         switch (lpcfg_server_role(lp_ctx)) {
     255      224130 :         case ROLE_DOMAIN_BDC:
     256             :         case ROLE_DOMAIN_PDC:
     257             :         case ROLE_ACTIVE_DIRECTORY_DC:
     258             :         case ROLE_IPA_DC:
     259      224130 :                 return lpcfg_workgroup(lp_ctx);
     260        2625 :         default:
     261        2625 :                 return lpcfg_netbios_name(lp_ctx);
     262             :         }
     263             : }
     264             : 
     265       78960 : const char *lpcfg_sam_dnsname(struct loadparm_context *lp_ctx)
     266             : {
     267       78960 :         switch (lpcfg_server_role(lp_ctx)) {
     268       78862 :         case ROLE_ACTIVE_DIRECTORY_DC:
     269             :         case ROLE_IPA_DC:
     270       78862 :                 return lpcfg_dnsdomain(lp_ctx);
     271          98 :         default:
     272          98 :                 return NULL;
     273             :         }
     274             : }
     275             : 
     276             : static int
     277           0 : tdb_fetch_lifetime_fn(TDB_DATA key, TDB_DATA data, void *private_data)
     278             : {
     279           0 :         if (data.dsize < 256) {
     280           0 :                 long *result = private_data;
     281           0 :                 char tmp[data.dsize + 1];
     282           0 :                 memcpy(tmp, data.dptr, data.dsize);
     283           0 :                 tmp[data.dsize] = '\0';
     284           0 :                 *result = atol(tmp);
     285           0 :                 return 0;
     286             :         }
     287           0 :         return -1;
     288             : }
     289             : 
     290         222 : static long tdb_fetch_lifetime(struct tdb_context *tdb,
     291             :                                const char *keystr)
     292             : {
     293         222 :         long result = -1;
     294           0 :         int ret;
     295             : 
     296         222 :         ret = tdb_parse_record(
     297             :                 tdb,
     298         222 :                 (TDB_DATA){
     299             :                         .dptr = discard_const_p(uint8_t, keystr),
     300         222 :                         .dsize = strlen(keystr),
     301             :                 },
     302             :                 tdb_fetch_lifetime_fn,
     303             :                 &result);
     304         222 :         if (ret == -1) {
     305         222 :                 return -1;
     306             :         }
     307           0 :         return result;
     308             : }
     309             : 
     310         227 : void lpcfg_default_kdc_policy(TALLOC_CTX *mem_ctx,
     311             :                                 struct loadparm_context *lp_ctx,
     312             :                                 time_t *svc_tkt_lifetime,
     313             :                                 time_t *usr_tkt_lifetime,
     314             :                                 time_t *renewal_lifetime)
     315             : {
     316           8 :         long val;
     317         227 :         TDB_CONTEXT *ctx = NULL;
     318         227 :         const char *kdc_tdb = NULL;
     319             : 
     320         227 :         kdc_tdb = lpcfg_cache_path(mem_ctx, lp_ctx, "gpo.tdb");
     321         227 :         if (kdc_tdb)
     322         227 :                 ctx = tdb_open(kdc_tdb, 0, TDB_DEFAULT, O_RDWR, 0600);
     323             : 
     324         227 :         if (!ctx || ( val = tdb_fetch_lifetime(ctx, "kdc:service_ticket_lifetime") ) == -1 )
     325         227 :                 val = lpcfg_parm_long(lp_ctx, NULL, "kdc", "service ticket lifetime", 10);
     326         227 :         *svc_tkt_lifetime = val * 60 * 60;
     327             : 
     328         227 :         if (!ctx || ( val = tdb_fetch_lifetime(ctx, "kdc:user_ticket_lifetime") ) == -1 )
     329         227 :                 val = lpcfg_parm_long(lp_ctx, NULL, "kdc", "user ticket lifetime", 10);
     330         227 :         *usr_tkt_lifetime = val * 60 * 60;
     331             : 
     332         227 :         if (!ctx || ( val = tdb_fetch_lifetime(ctx, "kdc:renewal_lifetime") ) == -1 )
     333         227 :                 val = lpcfg_parm_long(lp_ctx, NULL, "kdc", "renewal lifetime", 24 * 7);
     334         227 :         *renewal_lifetime = val * 60 * 60;
     335             : 
     336         227 :         if (ctx != NULL) {
     337          74 :                 tdb_close(ctx);
     338          74 :                 ctx = NULL;
     339             :         }
     340         227 : }

Generated by: LCOV version 1.14