Changeset 988 for vendor/current/source3/lib/substitute.c
- Timestamp:
- Nov 24, 2016, 1:14:11 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
vendor/current/source3/lib/substitute.c
r746 r988 25 25 #include "auth.h" 26 26 27 static char *alloc_sub_basic(const char *smb_name, const char *domain_name,28 const char *str);29 30 27 userdom_struct current_user_info; 31 28 fstring remote_proto="UNKNOWN"; … … 41 38 void free_local_machine_name(void) 42 39 { 43 SAFE_FREE(local_machine);40 TALLOC_FREE(local_machine); 44 41 } 45 42 … … 54 51 } 55 52 56 tmp_local_machine = SMB_STRDUP(local_name);53 tmp_local_machine = talloc_strdup(NULL, local_name); 57 54 if (!tmp_local_machine) { 58 55 return false; … … 60 57 trim_char(tmp_local_machine,' ',' '); 61 58 62 SAFE_FREE(local_machine);59 TALLOC_FREE(local_machine); 63 60 len = strlen(tmp_local_machine); 64 local_machine = SMB_CALLOC_ARRAY(char, len+1);61 local_machine = (char *)TALLOC_ZERO(NULL, len+1); 65 62 if (!local_machine) { 66 SAFE_FREE(tmp_local_machine);63 TALLOC_FREE(tmp_local_machine); 67 64 return false; 68 65 } … … 70 67 alpha_strcpy(local_machine,tmp_local_machine, 71 68 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); 74 74 75 75 already_perm = perm; … … 81 81 { 82 82 if (!local_machine || !*local_machine) { 83 return global_myname();83 return lp_netbios_name(); 84 84 } 85 85 … … 105 105 } 106 106 107 tmp_remote_machine = SMB_STRDUP(remote_name);107 tmp_remote_machine = talloc_strdup(NULL, remote_name); 108 108 if (!tmp_remote_machine) { 109 109 return false; … … 111 111 trim_char(tmp_remote_machine,' ',' '); 112 112 113 SAFE_FREE(remote_machine);113 TALLOC_FREE(remote_machine); 114 114 len = strlen(tmp_remote_machine); 115 remote_machine = SMB_CALLOC_ARRAY(char, len+1);115 remote_machine = (char *)TALLOC_ZERO(NULL, len+1); 116 116 if (!remote_machine) { 117 SAFE_FREE(tmp_remote_machine);117 TALLOC_FREE(tmp_remote_machine); 118 118 return false; 119 119 } … … 122 122 alpha_strcpy(remote_machine,tmp_remote_machine, 123 123 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); 126 129 127 130 already_perm = perm; … … 152 155 } 153 156 154 tmp = SMB_STRDUP(name);157 tmp = talloc_strdup(NULL, name); 155 158 if (!tmp) { 156 159 return; 157 160 } 158 161 trim_char(tmp, ' ', ' '); 159 strlower_m(tmp); 162 if (!strlower_m(tmp)) { 163 TALLOC_FREE(tmp); 164 return; 165 } 160 166 161 167 len = strlen(tmp); 162 168 163 169 if (len == 0) { 164 SAFE_FREE(tmp);170 TALLOC_FREE(tmp); 165 171 return; 166 172 } … … 175 181 } 176 182 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); 179 185 if (!smb_user_name) { 180 SAFE_FREE(tmp);186 TALLOC_FREE(tmp); 181 187 return; 182 188 } … … 187 193 len+1); 188 194 189 SAFE_FREE(tmp);195 TALLOC_FREE(tmp); 190 196 191 197 if (is_machine_account) { … … 211 217 if (sub_peername != NULL && 212 218 sub_peername != sub_peeraddr) { 213 free(discard_const_p(char,sub_peername));219 talloc_free(discard_const_p(char,sub_peername)); 214 220 sub_peername = NULL; 215 221 } 216 sub_peername = SMB_STRDUP(peername);222 sub_peername = talloc_strdup(NULL, peername); 217 223 if (sub_peername == NULL) { 218 224 sub_peername = sub_peeraddr; … … 221 227 /* 222 228 * Shouldn't we do the ::ffff: cancellation here as well? The 223 * original code in alloc_sub_basic() did not do it, so I'm229 * original code in talloc_sub_basic() did not do it, so I'm 224 230 * leaving it out here as well for compatibility. 225 231 */ … … 268 274 Given a pointer to a %$(NAME) in p and the whole string in str 269 275 expand it as an environment variable. 276 str must be a talloced string. 270 277 Return a new allocated and expanded string. 271 278 Based on code by Branko Cibej <branko.cibej@hermes.si> … … 274 281 ********************************************************************/ 275 282 276 static char * 283 static char *realloc_expand_env_var(char *str, char *p) 277 284 { 278 285 char *envname; … … 302 309 303 310 /* 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 ) { 305 312 return NULL; 306 313 } … … 311 318 if ((envval = getenv(envname)) == NULL) { 312 319 DEBUG(0,("expand_env_var: Environment variable [%s] not set\n", envname)); 313 SAFE_FREE(envname);320 TALLOC_FREE(envname); 314 321 return str; 315 322 } … … 324 331 envname[copylen] = '\0'; 325 332 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); 426 334 427 335 return r; … … 448 356 #if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT)) 449 357 450 if (lp_nis_home _map()) {358 if (lp_nis_homedir()) { 451 359 const char *home_path_start; 452 360 char *automount_value = automount_lookup(ctx, user_name); … … 495 403 server_name = talloc_strdup(ctx, local_machine_name); 496 404 } else { 497 server_name = talloc_strdup(ctx, global_myname());405 server_name = talloc_strdup(ctx, lp_netbios_name()); 498 406 } 499 407 … … 503 411 504 412 #if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT)) 505 if (lp_nis_home _map()) {413 if (lp_nis_homedir()) { 506 414 char *p; 507 415 char *srv; … … 540 448 char *s; 541 449 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 ) { 543 451 strncpy( str, s, len ); 544 452 } 545 453 546 SAFE_FREE( s );454 TALLOC_FREE( s ); 547 455 } 548 456 549 457 /**************************************************************************** 550 458 Do some standard substitutions in a string. 551 This function will return an allocated string that haveto be freed.459 This function will return an talloced string that has to be freed. 552 460 ****************************************************************************/ 553 461 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) 462 char *talloc_sub_basic(TALLOC_CTX *mem_ctx, 463 const char *smb_name, 464 const char *domain_name, 465 const char *str) 572 466 { 573 467 char *b, *p, *s, *r, *a_string; … … 579 473 580 474 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")); 582 476 return NULL; 583 477 } 584 478 585 a_string = SMB_STRDUP(str);479 a_string = talloc_strdup(mem_ctx, str); 586 480 if (a_string == NULL) { 587 DEBUG(0, (" alloc_sub_basic: Out of memory!\n"));481 DEBUG(0, ("talloc_sub_basic: Out of memory!\n")); 588 482 return NULL; 589 483 } … … 606 500 case 'G' : { 607 501 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 } 609 514 if (r == NULL) { 610 515 goto error; 611 516 } 517 612 518 pass = Get_Pwnam_alloc(tmp_ctx, r); 613 519 if (pass != NULL) { … … 638 544 break; 639 545 case 'L' : 640 if ( StrnCaseCmp(p, "%LOGONSERVER%", strlen("%LOGONSERVER%")) == 0 ) {546 if ( strncasecmp_m(p, "%LOGONSERVER%", strlen("%LOGONSERVER%")) == 0 ) { 641 547 break; 642 548 } … … 644 550 a_string = realloc_string_sub(a_string, "%L", local_machine_name); 645 551 } else { 646 a_string = realloc_string_sub(a_string, "%L", global_myname());552 a_string = realloc_string_sub(a_string, "%L", lp_netbios_name()); 647 553 } 648 554 break; … … 665 571 break; 666 572 case 'd' : 667 slprintf(pidstr,sizeof(pidstr)-1, "%d",(int) sys_getpid());573 slprintf(pidstr,sizeof(pidstr)-1, "%d",(int)getpid()); 668 574 a_string = realloc_string_sub(a_string, "%d", pidstr); 669 575 break; … … 686 592 a_string = realloc_expand_env_var(a_string, p); /* Expand environment variables */ 687 593 break; 688 case '(':689 a_string = realloc_expand_longvar( a_string, p );690 break;691 594 case 'V' : 692 595 slprintf(vnnstr,sizeof(vnnstr)-1, "%u", get_my_vnn()); … … 708 611 709 612 error: 710 SAFE_FREE(a_string);613 TALLOC_FREE(a_string); 711 614 712 615 done: … … 723 626 const char *input_string, 724 627 const char *username, 628 const char *grpname, 725 629 const char *domain, 726 630 uid_t uid, … … 758 662 case 'G' : 759 663 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); 763 676 } else { 764 677 a_string = talloc_string_sub( … … 769 682 case 'g' : 770 683 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); 774 696 } else { 775 697 a_string = talloc_string_sub( … … 809 731 ****************************************************************************/ 810 732 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) 733 char *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) 815 741 { 816 742 char *a_string, *ret_string; 817 743 char *b, *p, *s; 818 744 819 a_string = SMB_STRDUP(str);745 a_string = talloc_strdup(talloc_tos(), str); 820 746 if (a_string == NULL) { 821 DEBUG(0, (" alloc_sub_advanced: Out of memory!\n"));747 DEBUG(0, ("talloc_sub_advanced: Out of memory!\n")); 822 748 return NULL; 823 749 } … … 873 799 } 874 800 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); 877 803 return ret_string; 878 804 } 879 880 /*881 * This obviously is inefficient and needs to be merged into882 * 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 902 805 903 806 void standard_sub_advanced(const char *servicename, const char *user, … … 906 809 char *str, size_t len) 907 810 { 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 827 bool 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.