[206] | 1 | /*
|
---|
| 2 | Unix SMB/CIFS implementation.
|
---|
| 3 | string substitution functions
|
---|
| 4 | Copyright (C) Andrew Tridgell 1992-2000
|
---|
| 5 | Copyright (C) Gerald Carter 2006
|
---|
| 6 |
|
---|
| 7 | This program is free software; you can redistribute it and/or modify
|
---|
| 8 | it under the terms of the GNU General Public License as published by
|
---|
| 9 | the Free Software Foundation; either version 3 of the License, or
|
---|
| 10 | (at your option) any later version.
|
---|
| 11 |
|
---|
| 12 | This program is distributed in the hope that it will be useful,
|
---|
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | GNU General Public License for more details.
|
---|
| 16 |
|
---|
| 17 | You should have received a copy of the GNU General Public License
|
---|
| 18 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 |
|
---|
| 21 |
|
---|
| 22 | #include "includes.h"
|
---|
| 23 |
|
---|
| 24 | userdom_struct current_user_info;
|
---|
| 25 | fstring remote_proto="UNKNOWN";
|
---|
| 26 |
|
---|
| 27 | /**
|
---|
| 28 | * Set the 'local' machine name
|
---|
| 29 | * @param local_name the name we are being called
|
---|
| 30 | * @param if this is the 'final' name for us, not be be changed again
|
---|
| 31 | */
|
---|
| 32 |
|
---|
| 33 | static char *local_machine;
|
---|
| 34 |
|
---|
| 35 | void free_local_machine_name(void)
|
---|
| 36 | {
|
---|
| 37 | SAFE_FREE(local_machine);
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | bool set_local_machine_name(const char *local_name, bool perm)
|
---|
| 41 | {
|
---|
| 42 | static bool already_perm = false;
|
---|
| 43 | char *tmp_local_machine = NULL;
|
---|
| 44 | char addr[INET6_ADDRSTRLEN];
|
---|
| 45 | size_t len;
|
---|
| 46 |
|
---|
| 47 | tmp_local_machine = SMB_STRDUP(local_name);
|
---|
| 48 | if (!tmp_local_machine) {
|
---|
| 49 | return false;
|
---|
| 50 | }
|
---|
| 51 | trim_char(tmp_local_machine,' ',' ');
|
---|
| 52 |
|
---|
| 53 | /*
|
---|
| 54 | * Windows NT/2k uses "*SMBSERVER" and XP uses "*SMBSERV"
|
---|
| 55 | * arrggg!!!
|
---|
| 56 | */
|
---|
| 57 |
|
---|
| 58 | if (strequal(tmp_local_machine, "*SMBSERVER") ||
|
---|
| 59 | strequal(tmp_local_machine, "*SMBSERV") ) {
|
---|
| 60 | SAFE_FREE(local_machine);
|
---|
| 61 | local_machine = SMB_STRDUP(client_socket_addr(get_client_fd(),
|
---|
| 62 | addr, sizeof(addr)) );
|
---|
| 63 | SAFE_FREE(tmp_local_machine);
|
---|
| 64 | return local_machine ? true : false;
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | if (already_perm) {
|
---|
| 68 | return true;
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | SAFE_FREE(local_machine);
|
---|
| 72 | len = strlen(tmp_local_machine);
|
---|
| 73 | local_machine = SMB_CALLOC_ARRAY(char, len+1);
|
---|
| 74 | if (!local_machine) {
|
---|
| 75 | SAFE_FREE(tmp_local_machine);
|
---|
| 76 | return false;
|
---|
| 77 | }
|
---|
| 78 | /* alpha_strcpy includes the space for the terminating nul. */
|
---|
| 79 | alpha_strcpy(local_machine,tmp_local_machine,
|
---|
| 80 | SAFE_NETBIOS_CHARS,len+1);
|
---|
| 81 | strlower_m(local_machine);
|
---|
| 82 | SAFE_FREE(tmp_local_machine);
|
---|
| 83 |
|
---|
| 84 | already_perm = perm;
|
---|
| 85 |
|
---|
| 86 | return true;
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | const char *get_local_machine_name(void)
|
---|
| 90 | {
|
---|
| 91 | if (!local_machine || !*local_machine) {
|
---|
| 92 | return global_myname();
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | return local_machine;
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | /**
|
---|
| 99 | * Set the 'remote' machine name
|
---|
| 100 | * @param remote_name the name our client wants to be called by
|
---|
| 101 | * @param if this is the 'final' name for them, not be be changed again
|
---|
| 102 | */
|
---|
| 103 |
|
---|
| 104 | static char *remote_machine;
|
---|
| 105 |
|
---|
| 106 | bool set_remote_machine_name(const char *remote_name, bool perm)
|
---|
| 107 | {
|
---|
| 108 | static bool already_perm = False;
|
---|
| 109 | char *tmp_remote_machine;
|
---|
| 110 | size_t len;
|
---|
| 111 |
|
---|
| 112 | if (already_perm) {
|
---|
| 113 | return true;
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | tmp_remote_machine = SMB_STRDUP(remote_name);
|
---|
| 117 | if (!tmp_remote_machine) {
|
---|
| 118 | return false;
|
---|
| 119 | }
|
---|
| 120 | trim_char(tmp_remote_machine,' ',' ');
|
---|
| 121 |
|
---|
| 122 | SAFE_FREE(remote_machine);
|
---|
| 123 | len = strlen(tmp_remote_machine);
|
---|
| 124 | remote_machine = SMB_CALLOC_ARRAY(char, len+1);
|
---|
| 125 | if (!remote_machine) {
|
---|
| 126 | SAFE_FREE(tmp_remote_machine);
|
---|
| 127 | return false;
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | /* alpha_strcpy includes the space for the terminating nul. */
|
---|
| 131 | alpha_strcpy(remote_machine,tmp_remote_machine,
|
---|
| 132 | SAFE_NETBIOS_CHARS,len+1);
|
---|
| 133 | strlower_m(remote_machine);
|
---|
| 134 | SAFE_FREE(tmp_remote_machine);
|
---|
| 135 |
|
---|
| 136 | already_perm = perm;
|
---|
| 137 |
|
---|
| 138 | return true;
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | const char *get_remote_machine_name(void)
|
---|
| 142 | {
|
---|
| 143 | return remote_machine ? remote_machine : "";
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | /*******************************************************************
|
---|
| 147 | Setup the string used by %U substitution.
|
---|
| 148 | ********************************************************************/
|
---|
| 149 |
|
---|
| 150 | static char *smb_user_name;
|
---|
| 151 |
|
---|
| 152 | void sub_set_smb_name(const char *name)
|
---|
| 153 | {
|
---|
| 154 | char *tmp;
|
---|
| 155 | size_t len;
|
---|
| 156 | bool is_machine_account = false;
|
---|
| 157 |
|
---|
| 158 | /* don't let anonymous logins override the name */
|
---|
| 159 | if (!name || !*name) {
|
---|
| 160 | return;
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | tmp = SMB_STRDUP(name);
|
---|
| 164 | if (!tmp) {
|
---|
| 165 | return;
|
---|
| 166 | }
|
---|
| 167 | trim_char(tmp, ' ', ' ');
|
---|
| 168 | strlower_m(tmp);
|
---|
| 169 |
|
---|
| 170 | len = strlen(tmp);
|
---|
| 171 |
|
---|
| 172 | if (len == 0) {
|
---|
| 173 | SAFE_FREE(tmp);
|
---|
| 174 | return;
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 | /* long story but here goes....we have to allow usernames
|
---|
| 178 | ending in '$' as they are valid machine account names.
|
---|
| 179 | So check for a machine account and re-add the '$'
|
---|
| 180 | at the end after the call to alpha_strcpy(). --jerry */
|
---|
| 181 |
|
---|
| 182 | if (tmp[len-1] == '$') {
|
---|
| 183 | is_machine_account = True;
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | SAFE_FREE(smb_user_name);
|
---|
| 187 | smb_user_name = SMB_CALLOC_ARRAY(char, len+1);
|
---|
| 188 | if (!smb_user_name) {
|
---|
| 189 | SAFE_FREE(tmp);
|
---|
| 190 | return;
|
---|
| 191 | }
|
---|
| 192 |
|
---|
| 193 | /* alpha_strcpy includes the space for the terminating nul. */
|
---|
| 194 | alpha_strcpy(smb_user_name, tmp,
|
---|
| 195 | SAFE_NETBIOS_CHARS,
|
---|
| 196 | len+1);
|
---|
| 197 |
|
---|
| 198 | SAFE_FREE(tmp);
|
---|
| 199 |
|
---|
| 200 | if (is_machine_account) {
|
---|
| 201 | len = strlen(smb_user_name);
|
---|
| 202 | smb_user_name[len-1] = '$';
|
---|
| 203 | }
|
---|
| 204 | }
|
---|
| 205 |
|
---|
| 206 | static const char *get_smb_user_name(void)
|
---|
| 207 | {
|
---|
| 208 | return smb_user_name ? smb_user_name : "";
|
---|
| 209 | }
|
---|
| 210 |
|
---|
| 211 | /*******************************************************************
|
---|
| 212 | Setup the strings used by substitutions. Called per packet. Ensure
|
---|
| 213 | %U name is set correctly also.
|
---|
| 214 |
|
---|
| 215 | smb_name must be sanitized by alpha_strcpy
|
---|
| 216 | ********************************************************************/
|
---|
| 217 |
|
---|
| 218 | void set_current_user_info(const char *smb_name, const char *unix_name,
|
---|
| 219 | const char *full_name, const char *domain)
|
---|
| 220 | {
|
---|
| 221 | fstrcpy(current_user_info.smb_name, smb_name);
|
---|
| 222 | fstrcpy(current_user_info.unix_name, unix_name);
|
---|
| 223 | fstrcpy(current_user_info.full_name, full_name);
|
---|
| 224 | fstrcpy(current_user_info.domain, domain);
|
---|
| 225 |
|
---|
| 226 | /* The following is safe as current_user_info.smb_name
|
---|
| 227 | * has already been sanitised in register_existing_vuid. */
|
---|
| 228 |
|
---|
| 229 | sub_set_smb_name(current_user_info.smb_name);
|
---|
| 230 | }
|
---|
| 231 |
|
---|
| 232 | /*******************************************************************
|
---|
| 233 | Return the current active user name.
|
---|
| 234 | *******************************************************************/
|
---|
| 235 |
|
---|
| 236 | const char *get_current_username(void)
|
---|
| 237 | {
|
---|
| 238 | if (current_user_info.smb_name[0] == '\0' ) {
|
---|
| 239 | return get_smb_user_name();
|
---|
| 240 | }
|
---|
| 241 |
|
---|
| 242 | return current_user_info.smb_name;
|
---|
| 243 | }
|
---|
| 244 |
|
---|
| 245 | /*******************************************************************
|
---|
| 246 | Given a pointer to a %$(NAME) in p and the whole string in str
|
---|
| 247 | expand it as an environment variable.
|
---|
| 248 | Return a new allocated and expanded string.
|
---|
| 249 | Based on code by Branko Cibej <branko.cibej@hermes.si>
|
---|
| 250 | When this is called p points at the '%' character.
|
---|
| 251 | May substitute multiple occurrencies of the same env var.
|
---|
| 252 | ********************************************************************/
|
---|
| 253 |
|
---|
| 254 | static char * realloc_expand_env_var(char *str, char *p)
|
---|
| 255 | {
|
---|
| 256 | char *envname;
|
---|
| 257 | char *envval;
|
---|
| 258 | char *q, *r;
|
---|
| 259 | int copylen;
|
---|
| 260 |
|
---|
| 261 | if (p[0] != '%' || p[1] != '$' || p[2] != '(') {
|
---|
| 262 | return str;
|
---|
| 263 | }
|
---|
| 264 |
|
---|
| 265 | /*
|
---|
| 266 | * Look for the terminating ')'.
|
---|
| 267 | */
|
---|
| 268 |
|
---|
| 269 | if ((q = strchr_m(p,')')) == NULL) {
|
---|
| 270 | DEBUG(0,("expand_env_var: Unterminated environment variable [%s]\n", p));
|
---|
| 271 | return str;
|
---|
| 272 | }
|
---|
| 273 |
|
---|
| 274 | /*
|
---|
| 275 | * Extract the name from within the %$(NAME) string.
|
---|
| 276 | */
|
---|
| 277 |
|
---|
| 278 | r = p + 3;
|
---|
| 279 | copylen = q - r;
|
---|
| 280 |
|
---|
| 281 | /* reserve space for use later add %$() chars */
|
---|
| 282 | if ( (envname = (char *)SMB_MALLOC(copylen + 1 + 4)) == NULL ) {
|
---|
| 283 | return NULL;
|
---|
| 284 | }
|
---|
| 285 |
|
---|
| 286 | strncpy(envname,r,copylen);
|
---|
| 287 | envname[copylen] = '\0';
|
---|
| 288 |
|
---|
| 289 | if ((envval = getenv(envname)) == NULL) {
|
---|
| 290 | DEBUG(0,("expand_env_var: Environment variable [%s] not set\n", envname));
|
---|
| 291 | SAFE_FREE(envname);
|
---|
| 292 | return str;
|
---|
| 293 | }
|
---|
| 294 |
|
---|
| 295 | /*
|
---|
| 296 | * Copy the full %$(NAME) into envname so it
|
---|
| 297 | * can be replaced.
|
---|
| 298 | */
|
---|
| 299 |
|
---|
| 300 | copylen = q + 1 - p;
|
---|
| 301 | strncpy(envname,p,copylen);
|
---|
| 302 | envname[copylen] = '\0';
|
---|
| 303 | r = realloc_string_sub(str, envname, envval);
|
---|
| 304 | SAFE_FREE(envname);
|
---|
| 305 |
|
---|
| 306 | return r;
|
---|
| 307 | }
|
---|
| 308 |
|
---|
| 309 | /*******************************************************************
|
---|
| 310 | *******************************************************************/
|
---|
| 311 |
|
---|
| 312 | static char *longvar_domainsid( void )
|
---|
| 313 | {
|
---|
| 314 | DOM_SID sid;
|
---|
| 315 | fstring tmp;
|
---|
| 316 | char *sid_string;
|
---|
| 317 |
|
---|
| 318 | if ( !secrets_fetch_domain_sid( lp_workgroup(), &sid ) ) {
|
---|
| 319 | return NULL;
|
---|
| 320 | }
|
---|
| 321 |
|
---|
| 322 | sid_string = SMB_STRDUP( sid_to_fstring( tmp, &sid ) );
|
---|
| 323 |
|
---|
| 324 | if ( !sid_string ) {
|
---|
| 325 | DEBUG(0,("longvar_domainsid: failed to dup SID string!\n"));
|
---|
| 326 | }
|
---|
| 327 |
|
---|
| 328 | return sid_string;
|
---|
| 329 | }
|
---|
| 330 |
|
---|
| 331 | /*******************************************************************
|
---|
| 332 | *******************************************************************/
|
---|
| 333 |
|
---|
| 334 | struct api_longvar {
|
---|
| 335 | const char *name;
|
---|
| 336 | char* (*fn)( void );
|
---|
| 337 | };
|
---|
| 338 |
|
---|
| 339 | static struct api_longvar longvar_table[] = {
|
---|
| 340 | { "DomainSID", longvar_domainsid },
|
---|
| 341 | { NULL, NULL }
|
---|
| 342 | };
|
---|
| 343 |
|
---|
| 344 | static char *get_longvar_val( const char *varname )
|
---|
| 345 | {
|
---|
| 346 | int i;
|
---|
| 347 |
|
---|
| 348 | DEBUG(7,("get_longvar_val: expanding variable [%s]\n", varname));
|
---|
| 349 |
|
---|
| 350 | for ( i=0; longvar_table[i].name; i++ ) {
|
---|
| 351 | if ( strequal( longvar_table[i].name, varname ) ) {
|
---|
| 352 | return longvar_table[i].fn();
|
---|
| 353 | }
|
---|
| 354 | }
|
---|
| 355 |
|
---|
| 356 | return NULL;
|
---|
| 357 | }
|
---|
| 358 |
|
---|
| 359 | /*******************************************************************
|
---|
| 360 | Expand the long smb.conf variable names given a pointer to a %(NAME).
|
---|
| 361 | Return the number of characters by which the pointer should be advanced.
|
---|
| 362 | When this is called p points at the '%' character.
|
---|
| 363 | ********************************************************************/
|
---|
| 364 |
|
---|
| 365 | static char *realloc_expand_longvar(char *str, char *p)
|
---|
| 366 | {
|
---|
| 367 | fstring varname;
|
---|
| 368 | char *value;
|
---|
| 369 | char *q, *r;
|
---|
| 370 | int copylen;
|
---|
| 371 |
|
---|
| 372 | if ( p[0] != '%' || p[1] != '(' ) {
|
---|
| 373 | return str;
|
---|
| 374 | }
|
---|
| 375 |
|
---|
| 376 | /* Look for the terminating ')'.*/
|
---|
| 377 |
|
---|
| 378 | if ((q = strchr_m(p,')')) == NULL) {
|
---|
| 379 | DEBUG(0,("realloc_expand_longvar: Unterminated environment variable [%s]\n", p));
|
---|
| 380 | return str;
|
---|
| 381 | }
|
---|
| 382 |
|
---|
| 383 | /* Extract the name from within the %(NAME) string.*/
|
---|
| 384 |
|
---|
| 385 | r = p+2;
|
---|
| 386 | copylen = MIN( (q-r), (sizeof(varname)-1) );
|
---|
| 387 | strncpy(varname, r, copylen);
|
---|
| 388 | varname[copylen] = '\0';
|
---|
| 389 |
|
---|
| 390 | if ((value = get_longvar_val(varname)) == NULL) {
|
---|
| 391 | DEBUG(0,("realloc_expand_longvar: Variable [%s] not set. Skipping\n", varname));
|
---|
| 392 | return str;
|
---|
| 393 | }
|
---|
| 394 |
|
---|
| 395 | /* Copy the full %(NAME) into envname so it can be replaced.*/
|
---|
| 396 |
|
---|
| 397 | copylen = MIN( (q+1-p),(sizeof(varname)-1) );
|
---|
| 398 | strncpy( varname, p, copylen );
|
---|
| 399 | varname[copylen] = '\0';
|
---|
| 400 | r = realloc_string_sub(str, varname, value);
|
---|
| 401 | SAFE_FREE( value );
|
---|
| 402 |
|
---|
| 403 | /* skip over the %(varname) */
|
---|
| 404 |
|
---|
| 405 | return r;
|
---|
| 406 | }
|
---|
| 407 |
|
---|
| 408 | /*******************************************************************
|
---|
| 409 | Patch from jkf@soton.ac.uk
|
---|
| 410 | Added this to implement %p (NIS auto-map version of %H)
|
---|
| 411 | *******************************************************************/
|
---|
| 412 |
|
---|
| 413 | static const char *automount_path(const char *user_name)
|
---|
| 414 | {
|
---|
| 415 | TALLOC_CTX *ctx = talloc_tos();
|
---|
| 416 | const char *server_path;
|
---|
| 417 |
|
---|
| 418 | /* use the passwd entry as the default */
|
---|
| 419 | /* this will be the default if WITH_AUTOMOUNT is not used or fails */
|
---|
| 420 |
|
---|
| 421 | server_path = talloc_strdup(ctx, get_user_home_dir(ctx, user_name));
|
---|
| 422 | if (!server_path) {
|
---|
| 423 | return "";
|
---|
| 424 | }
|
---|
| 425 |
|
---|
| 426 | #if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT))
|
---|
| 427 |
|
---|
| 428 | if (lp_nis_home_map()) {
|
---|
| 429 | const char *home_path_start;
|
---|
| 430 | char *automount_value = automount_lookup(ctx, user_name);
|
---|
| 431 |
|
---|
| 432 | if(automount_value && strlen(automount_value) > 0) {
|
---|
| 433 | home_path_start = strchr_m(automount_value,':');
|
---|
| 434 | if (home_path_start != NULL) {
|
---|
| 435 | DEBUG(5, ("NIS lookup succeeded. "
|
---|
| 436 | "Home path is: %s\n",
|
---|
| 437 | home_path_start ?
|
---|
| 438 | (home_path_start+1):""));
|
---|
| 439 | server_path = talloc_strdup(ctx,
|
---|
| 440 | home_path_start+1);
|
---|
| 441 | if (!server_path) {
|
---|
| 442 | server_path = "";
|
---|
| 443 | }
|
---|
| 444 | }
|
---|
| 445 | } else {
|
---|
| 446 | /* NIS key lookup failed: default to
|
---|
| 447 | * user home directory from password file */
|
---|
| 448 | DEBUG(5, ("NIS lookup failed. Using Home path from "
|
---|
| 449 | "passwd file. Home path is: %s\n", server_path ));
|
---|
| 450 | }
|
---|
| 451 | }
|
---|
| 452 | #endif
|
---|
| 453 |
|
---|
| 454 | DEBUG(4,("Home server path: %s\n", server_path));
|
---|
| 455 | return server_path;
|
---|
| 456 | }
|
---|
| 457 |
|
---|
| 458 | /*******************************************************************
|
---|
| 459 | Patch from jkf@soton.ac.uk
|
---|
| 460 | This is Luke's original function with the NIS lookup code
|
---|
| 461 | moved out to a separate function.
|
---|
| 462 | *******************************************************************/
|
---|
| 463 |
|
---|
| 464 | static const char *automount_server(const char *user_name)
|
---|
| 465 | {
|
---|
| 466 | TALLOC_CTX *ctx = talloc_tos();
|
---|
| 467 | const char *server_name;
|
---|
| 468 | const char *local_machine_name = get_local_machine_name();
|
---|
| 469 |
|
---|
| 470 | /* use the local machine name as the default */
|
---|
| 471 | /* this will be the default if WITH_AUTOMOUNT is not used or fails */
|
---|
| 472 | if (local_machine_name && *local_machine_name) {
|
---|
| 473 | server_name = talloc_strdup(ctx, local_machine_name);
|
---|
| 474 | } else {
|
---|
| 475 | server_name = talloc_strdup(ctx, global_myname());
|
---|
| 476 | }
|
---|
| 477 |
|
---|
| 478 | if (!server_name) {
|
---|
| 479 | return "";
|
---|
| 480 | }
|
---|
| 481 |
|
---|
| 482 | #if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT))
|
---|
| 483 | if (lp_nis_home_map()) {
|
---|
| 484 | char *p;
|
---|
| 485 | char *srv;
|
---|
| 486 | char *automount_value = automount_lookup(ctx, user_name);
|
---|
| 487 | if (!automount_value) {
|
---|
| 488 | return "";
|
---|
| 489 | }
|
---|
| 490 | srv = talloc_strdup(ctx, automount_value);
|
---|
| 491 | if (!srv) {
|
---|
| 492 | return "";
|
---|
| 493 | }
|
---|
| 494 | p = strchr_m(srv, ':');
|
---|
| 495 | if (!p) {
|
---|
| 496 | return "";
|
---|
| 497 | }
|
---|
| 498 | *p = '\0';
|
---|
| 499 | server_name = srv;
|
---|
| 500 | DEBUG(5, ("NIS lookup succeeded. Home server %s\n",
|
---|
| 501 | server_name));
|
---|
| 502 | }
|
---|
| 503 | #endif
|
---|
| 504 |
|
---|
| 505 | DEBUG(4,("Home server: %s\n", server_name));
|
---|
| 506 | return server_name;
|
---|
| 507 | }
|
---|
| 508 |
|
---|
| 509 | /****************************************************************************
|
---|
| 510 | Do some standard substitutions in a string.
|
---|
| 511 | len is the length in bytes of the space allowed in string str. If zero means
|
---|
| 512 | don't allow expansions.
|
---|
| 513 | ****************************************************************************/
|
---|
| 514 |
|
---|
| 515 | void standard_sub_basic(const char *smb_name, const char *domain_name,
|
---|
| 516 | char *str, size_t len)
|
---|
| 517 | {
|
---|
| 518 | char *s;
|
---|
| 519 |
|
---|
| 520 | if ( (s = alloc_sub_basic( smb_name, domain_name, str )) != NULL ) {
|
---|
| 521 | strncpy( str, s, len );
|
---|
| 522 | }
|
---|
| 523 |
|
---|
| 524 | SAFE_FREE( s );
|
---|
| 525 |
|
---|
| 526 | }
|
---|
| 527 |
|
---|
| 528 | /****************************************************************************
|
---|
| 529 | Do some standard substitutions in a string.
|
---|
| 530 | This function will return an allocated string that have to be freed.
|
---|
| 531 | ****************************************************************************/
|
---|
| 532 |
|
---|
| 533 | char *talloc_sub_basic(TALLOC_CTX *mem_ctx, const char *smb_name,
|
---|
| 534 | const char *domain_name, const char *str)
|
---|
| 535 | {
|
---|
| 536 | char *a, *t;
|
---|
| 537 |
|
---|
| 538 | if ( (a = alloc_sub_basic(smb_name, domain_name, str)) == NULL ) {
|
---|
| 539 | return NULL;
|
---|
| 540 | }
|
---|
| 541 | t = talloc_strdup(mem_ctx, a);
|
---|
| 542 | SAFE_FREE(a);
|
---|
| 543 | return t;
|
---|
| 544 | }
|
---|
| 545 |
|
---|
| 546 | /****************************************************************************
|
---|
| 547 | ****************************************************************************/
|
---|
| 548 |
|
---|
| 549 | char *alloc_sub_basic(const char *smb_name, const char *domain_name,
|
---|
| 550 | const char *str)
|
---|
| 551 | {
|
---|
| 552 | char *b, *p, *s, *r, *a_string;
|
---|
| 553 | fstring pidstr, vnnstr;
|
---|
| 554 | char addr[INET6_ADDRSTRLEN];
|
---|
| 555 | const char *local_machine_name = get_local_machine_name();
|
---|
| 556 | TALLOC_CTX *tmp_ctx = NULL;
|
---|
| 557 |
|
---|
| 558 | /* workaround to prevent a crash while looking at bug #687 */
|
---|
| 559 |
|
---|
| 560 | if (!str) {
|
---|
| 561 | DEBUG(0,("alloc_sub_basic: NULL source string! This should not happen\n"));
|
---|
| 562 | return NULL;
|
---|
| 563 | }
|
---|
| 564 |
|
---|
| 565 | a_string = SMB_STRDUP(str);
|
---|
| 566 | if (a_string == NULL) {
|
---|
| 567 | DEBUG(0, ("alloc_sub_basic: Out of memory!\n"));
|
---|
| 568 | return NULL;
|
---|
| 569 | }
|
---|
| 570 |
|
---|
| 571 | tmp_ctx = talloc_stackframe();
|
---|
| 572 |
|
---|
| 573 | for (b = s = a_string; (p = strchr_m(s, '%')); s = a_string + (p - b)) {
|
---|
| 574 |
|
---|
| 575 | r = NULL;
|
---|
| 576 | b = a_string;
|
---|
| 577 |
|
---|
| 578 | switch (*(p+1)) {
|
---|
| 579 | case 'U' :
|
---|
| 580 | r = strdup_lower(smb_name);
|
---|
| 581 | if (r == NULL) {
|
---|
| 582 | goto error;
|
---|
| 583 | }
|
---|
| 584 | a_string = realloc_string_sub(a_string, "%U", r);
|
---|
| 585 | break;
|
---|
| 586 | case 'G' : {
|
---|
| 587 | struct passwd *pass;
|
---|
| 588 | r = SMB_STRDUP(smb_name);
|
---|
| 589 | if (r == NULL) {
|
---|
| 590 | goto error;
|
---|
| 591 | }
|
---|
| 592 | pass = Get_Pwnam_alloc(tmp_ctx, r);
|
---|
| 593 | if (pass != NULL) {
|
---|
| 594 | a_string = realloc_string_sub(
|
---|
| 595 | a_string, "%G",
|
---|
| 596 | gidtoname(pass->pw_gid));
|
---|
| 597 | }
|
---|
| 598 | TALLOC_FREE(pass);
|
---|
| 599 | break;
|
---|
| 600 | }
|
---|
| 601 | case 'D' :
|
---|
| 602 | r = strdup_upper(domain_name);
|
---|
| 603 | if (r == NULL) {
|
---|
| 604 | goto error;
|
---|
| 605 | }
|
---|
| 606 | a_string = realloc_string_sub(a_string, "%D", r);
|
---|
| 607 | break;
|
---|
| 608 | case 'I' : {
|
---|
| 609 | int offset = 0;
|
---|
| 610 | client_addr(get_client_fd(), addr, sizeof(addr));
|
---|
| 611 | if (strnequal(addr,"::ffff:",7)) {
|
---|
| 612 | offset = 7;
|
---|
| 613 | }
|
---|
| 614 | a_string = realloc_string_sub(a_string, "%I",
|
---|
| 615 | addr + offset);
|
---|
| 616 | break;
|
---|
| 617 | }
|
---|
| 618 | case 'i':
|
---|
| 619 | a_string = realloc_string_sub( a_string, "%i",
|
---|
| 620 | client_socket_addr(get_client_fd(), addr, sizeof(addr)) );
|
---|
| 621 | break;
|
---|
| 622 | case 'L' :
|
---|
| 623 | if ( StrnCaseCmp(p, "%LOGONSERVER%", strlen("%LOGONSERVER%")) == 0 ) {
|
---|
| 624 | break;
|
---|
| 625 | }
|
---|
| 626 | if (local_machine_name && *local_machine_name) {
|
---|
| 627 | a_string = realloc_string_sub(a_string, "%L", local_machine_name);
|
---|
| 628 | } else {
|
---|
| 629 | a_string = realloc_string_sub(a_string, "%L", global_myname());
|
---|
| 630 | }
|
---|
| 631 | break;
|
---|
| 632 | case 'N':
|
---|
| 633 | a_string = realloc_string_sub(a_string, "%N", automount_server(smb_name));
|
---|
| 634 | break;
|
---|
| 635 | case 'M' :
|
---|
| 636 | a_string = realloc_string_sub(a_string, "%M", client_name(get_client_fd()));
|
---|
| 637 | break;
|
---|
| 638 | case 'R' :
|
---|
| 639 | a_string = realloc_string_sub(a_string, "%R", remote_proto);
|
---|
| 640 | break;
|
---|
| 641 | case 'T' :
|
---|
| 642 | a_string = realloc_string_sub(a_string, "%T", current_timestring(tmp_ctx, False));
|
---|
| 643 | break;
|
---|
| 644 | case 'a' :
|
---|
| 645 | a_string = realloc_string_sub(a_string, "%a",
|
---|
| 646 | get_remote_arch_str());
|
---|
| 647 | break;
|
---|
| 648 | case 'd' :
|
---|
| 649 | slprintf(pidstr,sizeof(pidstr)-1, "%d",(int)sys_getpid());
|
---|
| 650 | a_string = realloc_string_sub(a_string, "%d", pidstr);
|
---|
| 651 | break;
|
---|
| 652 | case 'h' :
|
---|
| 653 | a_string = realloc_string_sub(a_string, "%h", myhostname());
|
---|
| 654 | break;
|
---|
| 655 | case 'm' :
|
---|
| 656 | a_string = realloc_string_sub(a_string, "%m",
|
---|
| 657 | remote_machine
|
---|
| 658 | ? remote_machine
|
---|
| 659 | : "");
|
---|
| 660 | break;
|
---|
| 661 | case 'v' :
|
---|
| 662 | a_string = realloc_string_sub(a_string, "%v", SAMBA_VERSION_STRING);
|
---|
| 663 | break;
|
---|
| 664 | case 'w' :
|
---|
| 665 | a_string = realloc_string_sub(a_string, "%w", lp_winbind_separator());
|
---|
| 666 | break;
|
---|
| 667 | case '$' :
|
---|
| 668 | a_string = realloc_expand_env_var(a_string, p); /* Expand environment variables */
|
---|
| 669 | break;
|
---|
| 670 | case '(':
|
---|
| 671 | a_string = realloc_expand_longvar( a_string, p );
|
---|
| 672 | break;
|
---|
| 673 | case 'V' :
|
---|
| 674 | slprintf(vnnstr,sizeof(vnnstr)-1, "%u", get_my_vnn());
|
---|
| 675 | a_string = realloc_string_sub(a_string, "%V", vnnstr);
|
---|
| 676 | break;
|
---|
| 677 | default:
|
---|
| 678 | break;
|
---|
| 679 | }
|
---|
| 680 |
|
---|
| 681 | p++;
|
---|
| 682 | SAFE_FREE(r);
|
---|
| 683 |
|
---|
| 684 | if (a_string == NULL) {
|
---|
| 685 | goto done;
|
---|
| 686 | }
|
---|
| 687 | }
|
---|
| 688 |
|
---|
| 689 | goto done;
|
---|
| 690 |
|
---|
| 691 | error:
|
---|
| 692 | SAFE_FREE(a_string);
|
---|
| 693 |
|
---|
| 694 | done:
|
---|
| 695 | TALLOC_FREE(tmp_ctx);
|
---|
| 696 | return a_string;
|
---|
| 697 | }
|
---|
| 698 |
|
---|
| 699 | /****************************************************************************
|
---|
| 700 | Do some specific substitutions in a string.
|
---|
| 701 | This function will return an allocated string that have to be freed.
|
---|
| 702 | ****************************************************************************/
|
---|
| 703 |
|
---|
| 704 | char *talloc_sub_specified(TALLOC_CTX *mem_ctx,
|
---|
| 705 | const char *input_string,
|
---|
| 706 | const char *username,
|
---|
| 707 | const char *domain,
|
---|
| 708 | uid_t uid,
|
---|
| 709 | gid_t gid)
|
---|
| 710 | {
|
---|
| 711 | char *a_string;
|
---|
| 712 | char *ret_string = NULL;
|
---|
| 713 | char *b, *p, *s;
|
---|
| 714 | TALLOC_CTX *tmp_ctx;
|
---|
| 715 |
|
---|
| 716 | if (!(tmp_ctx = talloc_new(mem_ctx))) {
|
---|
| 717 | DEBUG(0, ("talloc_new failed\n"));
|
---|
| 718 | return NULL;
|
---|
| 719 | }
|
---|
| 720 |
|
---|
| 721 | a_string = talloc_strdup(tmp_ctx, input_string);
|
---|
| 722 | if (a_string == NULL) {
|
---|
| 723 | DEBUG(0, ("talloc_sub_specified: Out of memory!\n"));
|
---|
| 724 | goto done;
|
---|
| 725 | }
|
---|
| 726 |
|
---|
| 727 | for (b = s = a_string; (p = strchr_m(s, '%')); s = a_string + (p - b)) {
|
---|
| 728 |
|
---|
| 729 | b = a_string;
|
---|
| 730 |
|
---|
| 731 | switch (*(p+1)) {
|
---|
| 732 | case 'U' :
|
---|
| 733 | a_string = talloc_string_sub(
|
---|
| 734 | tmp_ctx, a_string, "%U", username);
|
---|
| 735 | break;
|
---|
| 736 | case 'u' :
|
---|
| 737 | a_string = talloc_string_sub(
|
---|
| 738 | tmp_ctx, a_string, "%u", username);
|
---|
| 739 | break;
|
---|
| 740 | case 'G' :
|
---|
| 741 | if (gid != -1) {
|
---|
| 742 | a_string = talloc_string_sub(
|
---|
| 743 | tmp_ctx, a_string, "%G",
|
---|
| 744 | gidtoname(gid));
|
---|
| 745 | } else {
|
---|
| 746 | a_string = talloc_string_sub(
|
---|
| 747 | tmp_ctx, a_string,
|
---|
| 748 | "%G", "NO_GROUP");
|
---|
| 749 | }
|
---|
| 750 | break;
|
---|
| 751 | case 'g' :
|
---|
| 752 | if (gid != -1) {
|
---|
| 753 | a_string = talloc_string_sub(
|
---|
| 754 | tmp_ctx, a_string, "%g",
|
---|
| 755 | gidtoname(gid));
|
---|
| 756 | } else {
|
---|
| 757 | a_string = talloc_string_sub(
|
---|
| 758 | tmp_ctx, a_string, "%g", "NO_GROUP");
|
---|
| 759 | }
|
---|
| 760 | break;
|
---|
| 761 | case 'D' :
|
---|
| 762 | a_string = talloc_string_sub(tmp_ctx, a_string,
|
---|
| 763 | "%D", domain);
|
---|
| 764 | break;
|
---|
| 765 | case 'N' :
|
---|
| 766 | a_string = talloc_string_sub(
|
---|
| 767 | tmp_ctx, a_string, "%N",
|
---|
| 768 | automount_server(username));
|
---|
| 769 | break;
|
---|
| 770 | default:
|
---|
| 771 | break;
|
---|
| 772 | }
|
---|
| 773 |
|
---|
| 774 | p++;
|
---|
| 775 | if (a_string == NULL) {
|
---|
| 776 | goto done;
|
---|
| 777 | }
|
---|
| 778 | }
|
---|
| 779 |
|
---|
| 780 | /* Watch out, using "mem_ctx" here, so all intermediate stuff goes
|
---|
| 781 | * away with the TALLOC_FREE(tmp_ctx) further down. */
|
---|
| 782 |
|
---|
| 783 | ret_string = talloc_sub_basic(mem_ctx, username, domain, a_string);
|
---|
| 784 |
|
---|
| 785 | done:
|
---|
| 786 | TALLOC_FREE(tmp_ctx);
|
---|
| 787 | return ret_string;
|
---|
| 788 | }
|
---|
| 789 |
|
---|
| 790 | /****************************************************************************
|
---|
| 791 | ****************************************************************************/
|
---|
| 792 |
|
---|
| 793 | static char *alloc_sub_advanced(const char *servicename, const char *user,
|
---|
| 794 | const char *connectpath, gid_t gid,
|
---|
| 795 | const char *smb_name, const char *domain_name,
|
---|
| 796 | const char *str)
|
---|
| 797 | {
|
---|
| 798 | char *a_string, *ret_string;
|
---|
| 799 | char *b, *p, *s;
|
---|
| 800 |
|
---|
| 801 | a_string = SMB_STRDUP(str);
|
---|
| 802 | if (a_string == NULL) {
|
---|
| 803 | DEBUG(0, ("alloc_sub_advanced: Out of memory!\n"));
|
---|
| 804 | return NULL;
|
---|
| 805 | }
|
---|
| 806 |
|
---|
| 807 | for (b = s = a_string; (p = strchr_m(s, '%')); s = a_string + (p - b)) {
|
---|
| 808 |
|
---|
| 809 | b = a_string;
|
---|
| 810 |
|
---|
| 811 | switch (*(p+1)) {
|
---|
| 812 | case 'N' :
|
---|
| 813 | a_string = realloc_string_sub(a_string, "%N", automount_server(user));
|
---|
| 814 | break;
|
---|
| 815 | case 'H': {
|
---|
| 816 | char *h;
|
---|
| 817 | if ((h = get_user_home_dir(talloc_tos(), user)))
|
---|
| 818 | a_string = realloc_string_sub(a_string, "%H", h);
|
---|
| 819 | TALLOC_FREE(h);
|
---|
| 820 | break;
|
---|
| 821 | }
|
---|
| 822 | case 'P':
|
---|
| 823 | a_string = realloc_string_sub(a_string, "%P", connectpath);
|
---|
| 824 | break;
|
---|
| 825 | case 'S':
|
---|
| 826 | a_string = realloc_string_sub(a_string, "%S", servicename);
|
---|
| 827 | break;
|
---|
| 828 | case 'g':
|
---|
| 829 | a_string = realloc_string_sub(a_string, "%g", gidtoname(gid));
|
---|
| 830 | break;
|
---|
| 831 | case 'u':
|
---|
| 832 | a_string = realloc_string_sub(a_string, "%u", user);
|
---|
| 833 | break;
|
---|
| 834 |
|
---|
| 835 | /* Patch from jkf@soton.ac.uk Left the %N (NIS
|
---|
| 836 | * server name) in standard_sub_basic as it is
|
---|
| 837 | * a feature for logon servers, hence uses the
|
---|
| 838 | * username. The %p (NIS server path) code is
|
---|
| 839 | * here as it is used instead of the default
|
---|
| 840 | * "path =" string in [homes] and so needs the
|
---|
| 841 | * service name, not the username. */
|
---|
| 842 | case 'p':
|
---|
| 843 | a_string = realloc_string_sub(a_string, "%p",
|
---|
| 844 | automount_path(servicename));
|
---|
| 845 | break;
|
---|
| 846 |
|
---|
| 847 | default:
|
---|
| 848 | break;
|
---|
| 849 | }
|
---|
| 850 |
|
---|
| 851 | p++;
|
---|
| 852 | if (a_string == NULL) {
|
---|
| 853 | return NULL;
|
---|
| 854 | }
|
---|
| 855 | }
|
---|
| 856 |
|
---|
| 857 | ret_string = alloc_sub_basic(smb_name, domain_name, a_string);
|
---|
| 858 | SAFE_FREE(a_string);
|
---|
| 859 | return ret_string;
|
---|
| 860 | }
|
---|
| 861 |
|
---|
| 862 | /*
|
---|
| 863 | * This obviously is inefficient and needs to be merged into
|
---|
| 864 | * alloc_sub_advanced...
|
---|
| 865 | */
|
---|
| 866 |
|
---|
| 867 | char *talloc_sub_advanced(TALLOC_CTX *mem_ctx,
|
---|
| 868 | const char *servicename, const char *user,
|
---|
| 869 | const char *connectpath, gid_t gid,
|
---|
| 870 | const char *smb_name, const char *domain_name,
|
---|
| 871 | const char *str)
|
---|
| 872 | {
|
---|
| 873 | char *a, *t;
|
---|
| 874 |
|
---|
| 875 | if (!(a = alloc_sub_advanced(servicename, user, connectpath, gid,
|
---|
| 876 | smb_name, domain_name, str))) {
|
---|
| 877 | return NULL;
|
---|
| 878 | }
|
---|
| 879 | t = talloc_strdup(mem_ctx, a);
|
---|
| 880 | SAFE_FREE(a);
|
---|
| 881 | return t;
|
---|
| 882 | }
|
---|
| 883 |
|
---|
| 884 |
|
---|
| 885 | void standard_sub_advanced(const char *servicename, const char *user,
|
---|
| 886 | const char *connectpath, gid_t gid,
|
---|
| 887 | const char *smb_name, const char *domain_name,
|
---|
| 888 | char *str, size_t len)
|
---|
| 889 | {
|
---|
| 890 | char *s;
|
---|
| 891 |
|
---|
| 892 | s = alloc_sub_advanced(servicename, user, connectpath,
|
---|
| 893 | gid, smb_name, domain_name, str);
|
---|
| 894 |
|
---|
| 895 | if ( s ) {
|
---|
| 896 | strncpy( str, s, len );
|
---|
| 897 | SAFE_FREE( s );
|
---|
| 898 | }
|
---|
| 899 | }
|
---|
| 900 |
|
---|
| 901 | /****************************************************************************
|
---|
| 902 | Do some standard substitutions in a string.
|
---|
| 903 | ****************************************************************************/
|
---|
| 904 |
|
---|
| 905 | char *standard_sub_conn(TALLOC_CTX *ctx, connection_struct *conn, const char *str)
|
---|
| 906 | {
|
---|
| 907 | return talloc_sub_advanced(ctx,
|
---|
| 908 | lp_servicename(SNUM(conn)),
|
---|
| 909 | conn->server_info->unix_name,
|
---|
| 910 | conn->connectpath,
|
---|
| 911 | conn->server_info->utok.gid,
|
---|
| 912 | get_smb_user_name(),
|
---|
| 913 | "",
|
---|
| 914 | str);
|
---|
| 915 | }
|
---|