Ignore:
Timestamp:
Nov 24, 2016, 1:14:11 PM (9 years ago)
Author:
Silvan Scherrer
Message:

Samba Server: update vendor to version 4.4.3

File:
1 edited

Legend:

Unmodified
Added
Removed
  • vendor/current/source3/lib/substitute.c

    r746 r988  
    2525#include "auth.h"
    2626
    27 static char *alloc_sub_basic(const char *smb_name, const char *domain_name,
    28                              const char *str);
    29 
    3027userdom_struct current_user_info;
    3128fstring remote_proto="UNKNOWN";
     
    4138void free_local_machine_name(void)
    4239{
    43         SAFE_FREE(local_machine);
     40        TALLOC_FREE(local_machine);
    4441}
    4542
     
    5451        }
    5552
    56         tmp_local_machine = SMB_STRDUP(local_name);
     53        tmp_local_machine = talloc_strdup(NULL, local_name);
    5754        if (!tmp_local_machine) {
    5855                return false;
     
    6057        trim_char(tmp_local_machine,' ',' ');
    6158
    62         SAFE_FREE(local_machine);
     59        TALLOC_FREE(local_machine);
    6360        len = strlen(tmp_local_machine);
    64         local_machine = SMB_CALLOC_ARRAY(char, len+1);
     61        local_machine = (char *)TALLOC_ZERO(NULL, len+1);
    6562        if (!local_machine) {
    66                 SAFE_FREE(tmp_local_machine);
     63                TALLOC_FREE(tmp_local_machine);
    6764                return false;
    6865        }
     
    7067        alpha_strcpy(local_machine,tmp_local_machine,
    7168                        SAFE_NETBIOS_CHARS,len+1);
    72         strlower_m(local_machine);
    73         SAFE_FREE(tmp_local_machine);
     69        if (!strlower_m(local_machine)) {
     70                TALLOC_FREE(tmp_local_machine);
     71                return false;
     72        }
     73        TALLOC_FREE(tmp_local_machine);
    7474
    7575        already_perm = perm;
     
    8181{
    8282        if (!local_machine || !*local_machine) {
    83                 return global_myname();
     83                return lp_netbios_name();
    8484        }
    8585
     
    105105        }
    106106
    107         tmp_remote_machine = SMB_STRDUP(remote_name);
     107        tmp_remote_machine = talloc_strdup(NULL, remote_name);
    108108        if (!tmp_remote_machine) {
    109109                return false;
     
    111111        trim_char(tmp_remote_machine,' ',' ');
    112112
    113         SAFE_FREE(remote_machine);
     113        TALLOC_FREE(remote_machine);
    114114        len = strlen(tmp_remote_machine);
    115         remote_machine = SMB_CALLOC_ARRAY(char, len+1);
     115        remote_machine = (char *)TALLOC_ZERO(NULL, len+1);
    116116        if (!remote_machine) {
    117                 SAFE_FREE(tmp_remote_machine);
     117                TALLOC_FREE(tmp_remote_machine);
    118118                return false;
    119119        }
     
    122122        alpha_strcpy(remote_machine,tmp_remote_machine,
    123123                        SAFE_NETBIOS_CHARS,len+1);
    124         strlower_m(remote_machine);
    125         SAFE_FREE(tmp_remote_machine);
     124        if (!strlower_m(remote_machine)) {
     125                TALLOC_FREE(tmp_remote_machine);
     126                return false;
     127        }
     128        TALLOC_FREE(tmp_remote_machine);
    126129
    127130        already_perm = perm;
     
    152155        }
    153156
    154         tmp = SMB_STRDUP(name);
     157        tmp = talloc_strdup(NULL, name);
    155158        if (!tmp) {
    156159                return;
    157160        }
    158161        trim_char(tmp, ' ', ' ');
    159         strlower_m(tmp);
     162        if (!strlower_m(tmp)) {
     163                TALLOC_FREE(tmp);
     164                return;
     165        }
    160166
    161167        len = strlen(tmp);
    162168
    163169        if (len == 0) {
    164                 SAFE_FREE(tmp);
     170                TALLOC_FREE(tmp);
    165171                return;
    166172        }
     
    175181        }
    176182
    177         SAFE_FREE(smb_user_name);
    178         smb_user_name = SMB_CALLOC_ARRAY(char, len+1);
     183        TALLOC_FREE(smb_user_name);
     184        smb_user_name = (char *)TALLOC_ZERO(NULL, len+1);
    179185        if (!smb_user_name) {
    180                 SAFE_FREE(tmp);
     186                TALLOC_FREE(tmp);
    181187                return;
    182188        }
     
    187193                        len+1);
    188194
    189         SAFE_FREE(tmp);
     195        TALLOC_FREE(tmp);
    190196
    191197        if (is_machine_account) {
     
    211217        if (sub_peername != NULL &&
    212218                        sub_peername != sub_peeraddr) {
    213                 free(discard_const_p(char,sub_peername));
     219                talloc_free(discard_const_p(char,sub_peername));
    214220                sub_peername = NULL;
    215221        }
    216         sub_peername = SMB_STRDUP(peername);
     222        sub_peername = talloc_strdup(NULL, peername);
    217223        if (sub_peername == NULL) {
    218224                sub_peername = sub_peeraddr;
     
    221227        /*
    222228         * Shouldn't we do the ::ffff: cancellation here as well? The
    223          * original code in alloc_sub_basic() did not do it, so I'm
     229         * original code in talloc_sub_basic() did not do it, so I'm
    224230         * leaving it out here as well for compatibility.
    225231         */
     
    268274 Given a pointer to a %$(NAME) in p and the whole string in str
    269275 expand it as an environment variable.
     276 str must be a talloced string.
    270277 Return a new allocated and expanded string.
    271278 Based on code by Branko Cibej <branko.cibej@hermes.si>
     
    274281********************************************************************/
    275282
    276 static char * realloc_expand_env_var(char *str, char *p)
     283static char *realloc_expand_env_var(char *str, char *p)
    277284{
    278285        char *envname;
     
    302309
    303310        /* reserve space for use later add %$() chars */
    304         if ( (envname = (char *)SMB_MALLOC(copylen + 1 + 4)) == NULL ) {
     311        if ( (envname = talloc_array(talloc_tos(), char, copylen + 1 + 4)) == NULL ) {
    305312                return NULL;
    306313        }
     
    311318        if ((envval = getenv(envname)) == NULL) {
    312319                DEBUG(0,("expand_env_var: Environment variable [%s] not set\n", envname));
    313                 SAFE_FREE(envname);
     320                TALLOC_FREE(envname);
    314321                return str;
    315322        }
     
    324331        envname[copylen] = '\0';
    325332        r = realloc_string_sub(str, envname, envval);
    326         SAFE_FREE(envname);
    327 
    328         return r;
    329 }
    330 
    331 /*******************************************************************
    332 *******************************************************************/
    333 
    334 static char *longvar_domainsid( void )
    335 {
    336         struct dom_sid sid;
    337         fstring tmp;
    338         char *sid_string;
    339 
    340         if ( !secrets_fetch_domain_sid( lp_workgroup(), &sid ) ) {
    341                 return NULL;
    342         }
    343 
    344         sid_string = SMB_STRDUP( sid_to_fstring( tmp, &sid ) );
    345 
    346         if ( !sid_string ) {
    347                 DEBUG(0,("longvar_domainsid: failed to dup SID string!\n"));
    348         }
    349 
    350         return sid_string;
    351 }
    352 
    353 /*******************************************************************
    354 *******************************************************************/
    355 
    356 struct api_longvar {
    357         const char *name;
    358         char* (*fn)( void );
    359 };
    360 
    361 static struct api_longvar longvar_table[] = {
    362         { "DomainSID",          longvar_domainsid },
    363         { NULL,                 NULL }
    364 };
    365 
    366 static char *get_longvar_val( const char *varname )
    367 {
    368         int i;
    369 
    370         DEBUG(7,("get_longvar_val: expanding variable [%s]\n", varname));
    371 
    372         for ( i=0; longvar_table[i].name; i++ ) {
    373                 if ( strequal( longvar_table[i].name, varname ) ) {
    374                         return longvar_table[i].fn();
    375                 }
    376         }
    377 
    378         return NULL;
    379 }
    380 
    381 /*******************************************************************
    382  Expand the long smb.conf variable names given a pointer to a %(NAME).
    383  Return the number of characters by which the pointer should be advanced.
    384  When this is called p points at the '%' character.
    385 ********************************************************************/
    386 
    387 static char *realloc_expand_longvar(char *str, char *p)
    388 {
    389         fstring varname;
    390         char *value;
    391         char *q, *r;
    392         int copylen;
    393 
    394         if ( p[0] != '%' || p[1] != '(' ) {
    395                 return str;
    396         }
    397 
    398         /* Look for the terminating ')'.*/
    399 
    400         if ((q = strchr_m(p,')')) == NULL) {
    401                 DEBUG(0,("realloc_expand_longvar: Unterminated environment variable [%s]\n", p));
    402                 return str;
    403         }
    404 
    405         /* Extract the name from within the %(NAME) string.*/
    406 
    407         r = p+2;
    408         copylen = MIN( (q-r), (sizeof(varname)-1) );
    409         strncpy(varname, r, copylen);
    410         varname[copylen] = '\0';
    411 
    412         if ((value = get_longvar_val(varname)) == NULL) {
    413                 DEBUG(0,("realloc_expand_longvar: Variable [%s] not set.  Skipping\n", varname));
    414                 return str;
    415         }
    416 
    417         /* Copy the full %(NAME) into envname so it can be replaced.*/
    418 
    419         copylen = MIN( (q+1-p),(sizeof(varname)-1) );
    420         strncpy( varname, p, copylen );
    421         varname[copylen] = '\0';
    422         r = realloc_string_sub(str, varname, value);
    423         SAFE_FREE( value );
    424 
    425         /* skip over the %(varname) */
     333        TALLOC_FREE(envname);
    426334
    427335        return r;
     
    448356#if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT))
    449357
    450         if (lp_nis_home_map()) {
     358        if (lp_nis_homedir()) {
    451359                const char *home_path_start;
    452360                char *automount_value = automount_lookup(ctx, user_name);
     
    495403                server_name = talloc_strdup(ctx, local_machine_name);
    496404        } else {
    497                 server_name = talloc_strdup(ctx, global_myname());
     405                server_name = talloc_strdup(ctx, lp_netbios_name());
    498406        }
    499407
     
    503411
    504412#if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT))
    505         if (lp_nis_home_map()) {
     413        if (lp_nis_homedir()) {
    506414                char *p;
    507415                char *srv;
     
    540448        char *s;
    541449
    542         if ( (s = alloc_sub_basic( smb_name, domain_name, str )) != NULL ) {
     450        if ( (s = talloc_sub_basic(talloc_tos(), smb_name, domain_name, str )) != NULL ) {
    543451                strncpy( str, s, len );
    544452        }
    545453
    546         SAFE_FREE( s );
     454        TALLOC_FREE( s );
    547455}
    548456
    549457/****************************************************************************
    550458 Do some standard substitutions in a string.
    551  This function will return an allocated string that have to be freed.
     459 This function will return an talloced string that has to be freed.
    552460****************************************************************************/
    553461
    554 char *talloc_sub_basic(TALLOC_CTX *mem_ctx, const char *smb_name,
    555                        const char *domain_name, const char *str)
    556 {
    557         char *a, *t;
    558 
    559         if ( (a = alloc_sub_basic(smb_name, domain_name, str)) == NULL ) {
    560                 return NULL;
    561         }
    562         t = talloc_strdup(mem_ctx, a);
    563         SAFE_FREE(a);
    564         return t;
    565 }
    566 
    567 /****************************************************************************
    568 ****************************************************************************/
    569 
    570 static char *alloc_sub_basic(const char *smb_name, const char *domain_name,
    571                              const char *str)
     462char *talloc_sub_basic(TALLOC_CTX *mem_ctx,
     463                        const char *smb_name,
     464                        const char *domain_name,
     465                        const char *str)
    572466{
    573467        char *b, *p, *s, *r, *a_string;
     
    579473
    580474        if (!str) {
    581                 DEBUG(0,("alloc_sub_basic: NULL source string!  This should not happen\n"));
     475                DEBUG(0,("talloc_sub_basic: NULL source string!  This should not happen\n"));
    582476                return NULL;
    583477        }
    584478
    585         a_string = SMB_STRDUP(str);
     479        a_string = talloc_strdup(mem_ctx, str);
    586480        if (a_string == NULL) {
    587                 DEBUG(0, ("alloc_sub_basic: Out of memory!\n"));
     481                DEBUG(0, ("talloc_sub_basic: Out of memory!\n"));
    588482                return NULL;
    589483        }
     
    606500                case 'G' : {
    607501                        struct passwd *pass;
    608                         r = talloc_strdup(tmp_ctx, smb_name);
     502
     503                        if (domain_name != NULL && domain_name[0] != '\0' &&
     504                            !strequal(domain_name, my_sam_name()))
     505                        {
     506                                r = talloc_asprintf(tmp_ctx,
     507                                                    "%s%c%s",
     508                                                    domain_name,
     509                                                    *lp_winbind_separator(),
     510                                                    smb_name);
     511                        } else {
     512                                r = talloc_strdup(tmp_ctx, smb_name);
     513                        }
    609514                        if (r == NULL) {
    610515                                goto error;
    611516                        }
     517
    612518                        pass = Get_Pwnam_alloc(tmp_ctx, r);
    613519                        if (pass != NULL) {
     
    638544                        break;
    639545                case 'L' :
    640                         if ( StrnCaseCmp(p, "%LOGONSERVER%", strlen("%LOGONSERVER%")) == 0 ) {
     546                        if ( strncasecmp_m(p, "%LOGONSERVER%", strlen("%LOGONSERVER%")) == 0 ) {
    641547                                break;
    642548                        }
     
    644550                                a_string = realloc_string_sub(a_string, "%L", local_machine_name);
    645551                        } else {
    646                                 a_string = realloc_string_sub(a_string, "%L", global_myname());
     552                                a_string = realloc_string_sub(a_string, "%L", lp_netbios_name());
    647553                        }
    648554                        break;
     
    665571                        break;
    666572                case 'd' :
    667                         slprintf(pidstr,sizeof(pidstr)-1, "%d",(int)sys_getpid());
     573                        slprintf(pidstr,sizeof(pidstr)-1, "%d",(int)getpid());
    668574                        a_string = realloc_string_sub(a_string, "%d", pidstr);
    669575                        break;
     
    686592                        a_string = realloc_expand_env_var(a_string, p); /* Expand environment variables */
    687593                        break;
    688                 case '(':
    689                         a_string = realloc_expand_longvar( a_string, p );
    690                         break;
    691594                case 'V' :
    692595                        slprintf(vnnstr,sizeof(vnnstr)-1, "%u", get_my_vnn());
     
    708611
    709612error:
    710         SAFE_FREE(a_string);
     613        TALLOC_FREE(a_string);
    711614
    712615done:
     
    723626                        const char *input_string,
    724627                        const char *username,
     628                        const char *grpname,
    725629                        const char *domain,
    726630                        uid_t uid,
     
    758662                case 'G' :
    759663                        if (gid != -1) {
    760                                 a_string = talloc_string_sub(
    761                                         tmp_ctx, a_string, "%G",
    762                                         gidtoname(gid));
     664                                const char *name;
     665
     666                                if (grpname != NULL) {
     667                                        name = grpname;
     668                                } else {
     669                                        name = gidtoname(gid);
     670                                }
     671
     672                                a_string = talloc_string_sub(tmp_ctx,
     673                                                             a_string,
     674                                                             "%G",
     675                                                             name);
    763676                        } else {
    764677                                a_string = talloc_string_sub(
     
    769682                case 'g' :
    770683                        if (gid != -1) {
    771                                 a_string = talloc_string_sub(
    772                                         tmp_ctx, a_string, "%g",
    773                                         gidtoname(gid));
     684                                const char *name;
     685
     686                                if (grpname != NULL) {
     687                                        name = grpname;
     688                                } else {
     689                                        name = gidtoname(gid);
     690                                }
     691
     692                                a_string = talloc_string_sub(tmp_ctx,
     693                                                             a_string,
     694                                                             "%g",
     695                                                             name);
    774696                        } else {
    775697                                a_string = talloc_string_sub(
     
    809731****************************************************************************/
    810732
    811 static char *alloc_sub_advanced(const char *servicename, const char *user,
    812                          const char *connectpath, gid_t gid,
    813                          const char *smb_name, const char *domain_name,
    814                          const char *str)
     733char *talloc_sub_advanced(TALLOC_CTX *ctx,
     734                        const char *servicename,
     735                        const char *user,
     736                        const char *connectpath,
     737                        gid_t gid,
     738                        const char *smb_name,
     739                        const char *domain_name,
     740                        const char *str)
    815741{
    816742        char *a_string, *ret_string;
    817743        char *b, *p, *s;
    818744
    819         a_string = SMB_STRDUP(str);
     745        a_string = talloc_strdup(talloc_tos(), str);
    820746        if (a_string == NULL) {
    821                 DEBUG(0, ("alloc_sub_advanced: Out of memory!\n"));
     747                DEBUG(0, ("talloc_sub_advanced: Out of memory!\n"));
    822748                return NULL;
    823749        }
     
    873799        }
    874800
    875         ret_string = alloc_sub_basic(smb_name, domain_name, a_string);
    876         SAFE_FREE(a_string);
     801        ret_string = talloc_sub_basic(ctx, smb_name, domain_name, a_string);
     802        TALLOC_FREE(a_string);
    877803        return ret_string;
    878804}
    879 
    880 /*
    881  * This obviously is inefficient and needs to be merged into
    882  * alloc_sub_advanced...
    883  */
    884 
    885 char *talloc_sub_advanced(TALLOC_CTX *mem_ctx,
    886                           const char *servicename, const char *user,
    887                           const char *connectpath, gid_t gid,
    888                           const char *smb_name, const char *domain_name,
    889                           const char *str)
    890 {
    891         char *a, *t;
    892 
    893         if (!(a = alloc_sub_advanced(servicename, user, connectpath, gid,
    894                                      smb_name, domain_name, str))) {
    895                 return NULL;
    896         }
    897         t = talloc_strdup(mem_ctx, a);
    898         SAFE_FREE(a);
    899         return t;
    900 }
    901 
    902805
    903806void standard_sub_advanced(const char *servicename, const char *user,
     
    906809                           char *str, size_t len)
    907810{
    908         char *s;
    909 
    910         s = alloc_sub_advanced(servicename, user, connectpath,
    911                                gid, smb_name, domain_name, str);
    912 
    913         if ( s ) {
    914                 strncpy( str, s, len );
    915                 SAFE_FREE( s );
    916         }
    917 }
    918 
    919 /****************************************************************************
    920  Do some standard substitutions in a string.
    921 ****************************************************************************/
    922 
    923 char *standard_sub_conn(TALLOC_CTX *ctx, connection_struct *conn, const char *str)
    924 {
    925         return talloc_sub_advanced(ctx,
    926                                 lp_servicename(SNUM(conn)),
    927                                 conn->session_info->unix_name,
    928                                 conn->connectpath,
    929                                 conn->session_info->utok.gid,
    930                                 get_smb_user_name(),
    931                                 "",
    932                                 str);
    933 }
     811        char *s = talloc_sub_advanced(talloc_tos(),
     812                                servicename, user, connectpath,
     813                                gid, smb_name, domain_name, str);
     814
     815        if (!s) {
     816                return;
     817        }
     818        strlcpy( str, s, len );
     819        TALLOC_FREE( s );
     820}
     821
     822/******************************************************************************
     823 version of standard_sub_basic() for string lists; uses talloc_sub_basic()
     824 for the work
     825 *****************************************************************************/
     826
     827bool str_list_sub_basic( char **list, const char *smb_name,
     828                         const char *domain_name )
     829{
     830        TALLOC_CTX *ctx = list;
     831        char *s, *tmpstr;
     832
     833        while ( *list ) {
     834                s = *list;
     835                tmpstr = talloc_sub_basic(ctx, smb_name, domain_name, s);
     836                if ( !tmpstr ) {
     837                        DEBUG(0,("str_list_sub_basic: "
     838                                "talloc_sub_basic() return NULL!\n"));
     839                        return false;
     840                }
     841
     842                TALLOC_FREE(*list);
     843                *list = tmpstr;
     844
     845                list++;
     846        }
     847
     848        return true;
     849}
Note: See TracChangeset for help on using the changeset viewer.