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 | #include "system/passwd.h"
|
---|
24 | #include "secrets.h"
|
---|
25 | #include "auth.h"
|
---|
26 |
|
---|
27 | static char *alloc_sub_basic(const char *smb_name, const char *domain_name,
|
---|
28 | const char *str);
|
---|
29 |
|
---|
30 | userdom_struct current_user_info;
|
---|
31 | fstring remote_proto="UNKNOWN";
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * Set the 'local' machine name
|
---|
35 | * @param local_name the name we are being called
|
---|
36 | * @param if this is the 'final' name for us, not be be changed again
|
---|
37 | */
|
---|
38 |
|
---|
39 | static char *local_machine;
|
---|
40 |
|
---|
41 | void free_local_machine_name(void)
|
---|
42 | {
|
---|
43 | SAFE_FREE(local_machine);
|
---|
44 | }
|
---|
45 |
|
---|
46 | bool set_local_machine_name(const char *local_name, bool perm)
|
---|
47 | {
|
---|
48 | static bool already_perm = false;
|
---|
49 | char *tmp_local_machine = NULL;
|
---|
50 | size_t len;
|
---|
51 |
|
---|
52 | if (already_perm) {
|
---|
53 | return true;
|
---|
54 | }
|
---|
55 |
|
---|
56 | tmp_local_machine = SMB_STRDUP(local_name);
|
---|
57 | if (!tmp_local_machine) {
|
---|
58 | return false;
|
---|
59 | }
|
---|
60 | trim_char(tmp_local_machine,' ',' ');
|
---|
61 |
|
---|
62 | SAFE_FREE(local_machine);
|
---|
63 | len = strlen(tmp_local_machine);
|
---|
64 | local_machine = SMB_CALLOC_ARRAY(char, len+1);
|
---|
65 | if (!local_machine) {
|
---|
66 | SAFE_FREE(tmp_local_machine);
|
---|
67 | return false;
|
---|
68 | }
|
---|
69 | /* alpha_strcpy includes the space for the terminating nul. */
|
---|
70 | alpha_strcpy(local_machine,tmp_local_machine,
|
---|
71 | SAFE_NETBIOS_CHARS,len+1);
|
---|
72 | strlower_m(local_machine);
|
---|
73 | SAFE_FREE(tmp_local_machine);
|
---|
74 |
|
---|
75 | already_perm = perm;
|
---|
76 |
|
---|
77 | return true;
|
---|
78 | }
|
---|
79 |
|
---|
80 | const char *get_local_machine_name(void)
|
---|
81 | {
|
---|
82 | if (!local_machine || !*local_machine) {
|
---|
83 | return global_myname();
|
---|
84 | }
|
---|
85 |
|
---|
86 | return local_machine;
|
---|
87 | }
|
---|
88 |
|
---|
89 | /**
|
---|
90 | * Set the 'remote' machine name
|
---|
91 | * @param remote_name the name our client wants to be called by
|
---|
92 | * @param if this is the 'final' name for them, not be be changed again
|
---|
93 | */
|
---|
94 |
|
---|
95 | static char *remote_machine;
|
---|
96 |
|
---|
97 | bool set_remote_machine_name(const char *remote_name, bool perm)
|
---|
98 | {
|
---|
99 | static bool already_perm = False;
|
---|
100 | char *tmp_remote_machine;
|
---|
101 | size_t len;
|
---|
102 |
|
---|
103 | if (already_perm) {
|
---|
104 | return true;
|
---|
105 | }
|
---|
106 |
|
---|
107 | tmp_remote_machine = SMB_STRDUP(remote_name);
|
---|
108 | if (!tmp_remote_machine) {
|
---|
109 | return false;
|
---|
110 | }
|
---|
111 | trim_char(tmp_remote_machine,' ',' ');
|
---|
112 |
|
---|
113 | SAFE_FREE(remote_machine);
|
---|
114 | len = strlen(tmp_remote_machine);
|
---|
115 | remote_machine = SMB_CALLOC_ARRAY(char, len+1);
|
---|
116 | if (!remote_machine) {
|
---|
117 | SAFE_FREE(tmp_remote_machine);
|
---|
118 | return false;
|
---|
119 | }
|
---|
120 |
|
---|
121 | /* alpha_strcpy includes the space for the terminating nul. */
|
---|
122 | alpha_strcpy(remote_machine,tmp_remote_machine,
|
---|
123 | SAFE_NETBIOS_CHARS,len+1);
|
---|
124 | strlower_m(remote_machine);
|
---|
125 | SAFE_FREE(tmp_remote_machine);
|
---|
126 |
|
---|
127 | already_perm = perm;
|
---|
128 |
|
---|
129 | return true;
|
---|
130 | }
|
---|
131 |
|
---|
132 | const char *get_remote_machine_name(void)
|
---|
133 | {
|
---|
134 | return remote_machine ? remote_machine : "";
|
---|
135 | }
|
---|
136 |
|
---|
137 | /*******************************************************************
|
---|
138 | Setup the string used by %U substitution.
|
---|
139 | ********************************************************************/
|
---|
140 |
|
---|
141 | static char *smb_user_name;
|
---|
142 |
|
---|
143 | void sub_set_smb_name(const char *name)
|
---|
144 | {
|
---|
145 | char *tmp;
|
---|
146 | size_t len;
|
---|
147 | bool is_machine_account = false;
|
---|
148 |
|
---|
149 | /* don't let anonymous logins override the name */
|
---|
150 | if (!name || !*name) {
|
---|
151 | return;
|
---|
152 | }
|
---|
153 |
|
---|
154 | tmp = SMB_STRDUP(name);
|
---|
155 | if (!tmp) {
|
---|
156 | return;
|
---|
157 | }
|
---|
158 | trim_char(tmp, ' ', ' ');
|
---|
159 | strlower_m(tmp);
|
---|
160 |
|
---|
161 | len = strlen(tmp);
|
---|
162 |
|
---|
163 | if (len == 0) {
|
---|
164 | SAFE_FREE(tmp);
|
---|
165 | return;
|
---|
166 | }
|
---|
167 |
|
---|
168 | /* long story but here goes....we have to allow usernames
|
---|
169 | ending in '$' as they are valid machine account names.
|
---|
170 | So check for a machine account and re-add the '$'
|
---|
171 | at the end after the call to alpha_strcpy(). --jerry */
|
---|
172 |
|
---|
173 | if (tmp[len-1] == '$') {
|
---|
174 | is_machine_account = True;
|
---|
175 | }
|
---|
176 |
|
---|
177 | SAFE_FREE(smb_user_name);
|
---|
178 | smb_user_name = SMB_CALLOC_ARRAY(char, len+1);
|
---|
179 | if (!smb_user_name) {
|
---|
180 | SAFE_FREE(tmp);
|
---|
181 | return;
|
---|
182 | }
|
---|
183 |
|
---|
184 | /* alpha_strcpy includes the space for the terminating nul. */
|
---|
185 | alpha_strcpy(smb_user_name, tmp,
|
---|
186 | SAFE_NETBIOS_CHARS,
|
---|
187 | len+1);
|
---|
188 |
|
---|
189 | SAFE_FREE(tmp);
|
---|
190 |
|
---|
191 | if (is_machine_account) {
|
---|
192 | len = strlen(smb_user_name);
|
---|
193 | smb_user_name[len-1] = '$';
|
---|
194 | }
|
---|
195 | }
|
---|
196 |
|
---|
197 | static char sub_peeraddr[INET6_ADDRSTRLEN];
|
---|
198 | static const char *sub_peername = NULL;
|
---|
199 | static char sub_sockaddr[INET6_ADDRSTRLEN];
|
---|
200 |
|
---|
201 | void sub_set_socket_ids(const char *peeraddr, const char *peername,
|
---|
202 | const char *sockaddr)
|
---|
203 | {
|
---|
204 | const char *addr = peeraddr;
|
---|
205 |
|
---|
206 | if (strnequal(addr, "::ffff:", 7)) {
|
---|
207 | addr += 7;
|
---|
208 | }
|
---|
209 | strlcpy(sub_peeraddr, addr, sizeof(sub_peeraddr));
|
---|
210 |
|
---|
211 | if (sub_peername != NULL &&
|
---|
212 | sub_peername != sub_peeraddr) {
|
---|
213 | free(discard_const_p(char,sub_peername));
|
---|
214 | sub_peername = NULL;
|
---|
215 | }
|
---|
216 | sub_peername = SMB_STRDUP(peername);
|
---|
217 | if (sub_peername == NULL) {
|
---|
218 | sub_peername = sub_peeraddr;
|
---|
219 | }
|
---|
220 |
|
---|
221 | /*
|
---|
222 | * 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
|
---|
224 | * leaving it out here as well for compatibility.
|
---|
225 | */
|
---|
226 | strlcpy(sub_sockaddr, sockaddr, sizeof(sub_sockaddr));
|
---|
227 | }
|
---|
228 |
|
---|
229 | static const char *get_smb_user_name(void)
|
---|
230 | {
|
---|
231 | return smb_user_name ? smb_user_name : "";
|
---|
232 | }
|
---|
233 |
|
---|
234 | /*******************************************************************
|
---|
235 | Setup the strings used by substitutions. Called per packet. Ensure
|
---|
236 | %U name is set correctly also.
|
---|
237 |
|
---|
238 | smb_name must be sanitized by alpha_strcpy
|
---|
239 | ********************************************************************/
|
---|
240 |
|
---|
241 | void set_current_user_info(const char *smb_name, const char *unix_name,
|
---|
242 | const char *domain)
|
---|
243 | {
|
---|
244 | fstrcpy(current_user_info.smb_name, smb_name);
|
---|
245 | fstrcpy(current_user_info.unix_name, unix_name);
|
---|
246 | fstrcpy(current_user_info.domain, domain);
|
---|
247 |
|
---|
248 | /* The following is safe as current_user_info.smb_name
|
---|
249 | * has already been sanitised in register_existing_vuid. */
|
---|
250 |
|
---|
251 | sub_set_smb_name(current_user_info.smb_name);
|
---|
252 | }
|
---|
253 |
|
---|
254 | /*******************************************************************
|
---|
255 | Return the current active user name.
|
---|
256 | *******************************************************************/
|
---|
257 |
|
---|
258 | const char *get_current_username(void)
|
---|
259 | {
|
---|
260 | if (current_user_info.smb_name[0] == '\0' ) {
|
---|
261 | return get_smb_user_name();
|
---|
262 | }
|
---|
263 |
|
---|
264 | return current_user_info.smb_name;
|
---|
265 | }
|
---|
266 |
|
---|
267 | /*******************************************************************
|
---|
268 | Given a pointer to a %$(NAME) in p and the whole string in str
|
---|
269 | expand it as an environment variable.
|
---|
270 | Return a new allocated and expanded string.
|
---|
271 | Based on code by Branko Cibej <branko.cibej@hermes.si>
|
---|
272 | When this is called p points at the '%' character.
|
---|
273 | May substitute multiple occurrencies of the same env var.
|
---|
274 | ********************************************************************/
|
---|
275 |
|
---|
276 | static char * realloc_expand_env_var(char *str, char *p)
|
---|
277 | {
|
---|
278 | char *envname;
|
---|
279 | char *envval;
|
---|
280 | char *q, *r;
|
---|
281 | int copylen;
|
---|
282 |
|
---|
283 | if (p[0] != '%' || p[1] != '$' || p[2] != '(') {
|
---|
284 | return str;
|
---|
285 | }
|
---|
286 |
|
---|
287 | /*
|
---|
288 | * Look for the terminating ')'.
|
---|
289 | */
|
---|
290 |
|
---|
291 | if ((q = strchr_m(p,')')) == NULL) {
|
---|
292 | DEBUG(0,("expand_env_var: Unterminated environment variable [%s]\n", p));
|
---|
293 | return str;
|
---|
294 | }
|
---|
295 |
|
---|
296 | /*
|
---|
297 | * Extract the name from within the %$(NAME) string.
|
---|
298 | */
|
---|
299 |
|
---|
300 | r = p + 3;
|
---|
301 | copylen = q - r;
|
---|
302 |
|
---|
303 | /* reserve space for use later add %$() chars */
|
---|
304 | if ( (envname = (char *)SMB_MALLOC(copylen + 1 + 4)) == NULL ) {
|
---|
305 | return NULL;
|
---|
306 | }
|
---|
307 |
|
---|
308 | strncpy(envname,r,copylen);
|
---|
309 | envname[copylen] = '\0';
|
---|
310 |
|
---|
311 | if ((envval = getenv(envname)) == NULL) {
|
---|
312 | DEBUG(0,("expand_env_var: Environment variable [%s] not set\n", envname));
|
---|
313 | SAFE_FREE(envname);
|
---|
314 | return str;
|
---|
315 | }
|
---|
316 |
|
---|
317 | /*
|
---|
318 | * Copy the full %$(NAME) into envname so it
|
---|
319 | * can be replaced.
|
---|
320 | */
|
---|
321 |
|
---|
322 | copylen = q + 1 - p;
|
---|
323 | strncpy(envname,p,copylen);
|
---|
324 | envname[copylen] = '\0';
|
---|
325 | 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) */
|
---|
426 |
|
---|
427 | return r;
|
---|
428 | }
|
---|
429 |
|
---|
430 | /*******************************************************************
|
---|
431 | Patch from jkf@soton.ac.uk
|
---|
432 | Added this to implement %p (NIS auto-map version of %H)
|
---|
433 | *******************************************************************/
|
---|
434 |
|
---|
435 | static const char *automount_path(const char *user_name)
|
---|
436 | {
|
---|
437 | TALLOC_CTX *ctx = talloc_tos();
|
---|
438 | const char *server_path;
|
---|
439 |
|
---|
440 | /* use the passwd entry as the default */
|
---|
441 | /* this will be the default if WITH_AUTOMOUNT is not used or fails */
|
---|
442 |
|
---|
443 | server_path = talloc_strdup(ctx, get_user_home_dir(ctx, user_name));
|
---|
444 | if (!server_path) {
|
---|
445 | return "";
|
---|
446 | }
|
---|
447 |
|
---|
448 | #if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT))
|
---|
449 |
|
---|
450 | if (lp_nis_home_map()) {
|
---|
451 | const char *home_path_start;
|
---|
452 | char *automount_value = automount_lookup(ctx, user_name);
|
---|
453 |
|
---|
454 | if(automount_value && strlen(automount_value) > 0) {
|
---|
455 | home_path_start = strchr_m(automount_value,':');
|
---|
456 | if (home_path_start != NULL) {
|
---|
457 | DEBUG(5, ("NIS lookup succeeded. "
|
---|
458 | "Home path is: %s\n",
|
---|
459 | home_path_start ?
|
---|
460 | (home_path_start+1):""));
|
---|
461 | server_path = talloc_strdup(ctx,
|
---|
462 | home_path_start+1);
|
---|
463 | if (!server_path) {
|
---|
464 | server_path = "";
|
---|
465 | }
|
---|
466 | }
|
---|
467 | } else {
|
---|
468 | /* NIS key lookup failed: default to
|
---|
469 | * user home directory from password file */
|
---|
470 | DEBUG(5, ("NIS lookup failed. Using Home path from "
|
---|
471 | "passwd file. Home path is: %s\n", server_path ));
|
---|
472 | }
|
---|
473 | }
|
---|
474 | #endif
|
---|
475 |
|
---|
476 | DEBUG(4,("Home server path: %s\n", server_path));
|
---|
477 | return server_path;
|
---|
478 | }
|
---|
479 |
|
---|
480 | /*******************************************************************
|
---|
481 | Patch from jkf@soton.ac.uk
|
---|
482 | This is Luke's original function with the NIS lookup code
|
---|
483 | moved out to a separate function.
|
---|
484 | *******************************************************************/
|
---|
485 |
|
---|
486 | static const char *automount_server(const char *user_name)
|
---|
487 | {
|
---|
488 | TALLOC_CTX *ctx = talloc_tos();
|
---|
489 | const char *server_name;
|
---|
490 | const char *local_machine_name = get_local_machine_name();
|
---|
491 |
|
---|
492 | /* use the local machine name as the default */
|
---|
493 | /* this will be the default if WITH_AUTOMOUNT is not used or fails */
|
---|
494 | if (local_machine_name && *local_machine_name) {
|
---|
495 | server_name = talloc_strdup(ctx, local_machine_name);
|
---|
496 | } else {
|
---|
497 | server_name = talloc_strdup(ctx, global_myname());
|
---|
498 | }
|
---|
499 |
|
---|
500 | if (!server_name) {
|
---|
501 | return "";
|
---|
502 | }
|
---|
503 |
|
---|
504 | #if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT))
|
---|
505 | if (lp_nis_home_map()) {
|
---|
506 | char *p;
|
---|
507 | char *srv;
|
---|
508 | char *automount_value = automount_lookup(ctx, user_name);
|
---|
509 | if (!automount_value) {
|
---|
510 | return "";
|
---|
511 | }
|
---|
512 | srv = talloc_strdup(ctx, automount_value);
|
---|
513 | if (!srv) {
|
---|
514 | return "";
|
---|
515 | }
|
---|
516 | p = strchr_m(srv, ':');
|
---|
517 | if (!p) {
|
---|
518 | return "";
|
---|
519 | }
|
---|
520 | *p = '\0';
|
---|
521 | server_name = srv;
|
---|
522 | DEBUG(5, ("NIS lookup succeeded. Home server %s\n",
|
---|
523 | server_name));
|
---|
524 | }
|
---|
525 | #endif
|
---|
526 |
|
---|
527 | DEBUG(4,("Home server: %s\n", server_name));
|
---|
528 | return server_name;
|
---|
529 | }
|
---|
530 |
|
---|
531 | /****************************************************************************
|
---|
532 | Do some standard substitutions in a string.
|
---|
533 | len is the length in bytes of the space allowed in string str. If zero means
|
---|
534 | don't allow expansions.
|
---|
535 | ****************************************************************************/
|
---|
536 |
|
---|
537 | void standard_sub_basic(const char *smb_name, const char *domain_name,
|
---|
538 | char *str, size_t len)
|
---|
539 | {
|
---|
540 | char *s;
|
---|
541 |
|
---|
542 | if ( (s = alloc_sub_basic( smb_name, domain_name, str )) != NULL ) {
|
---|
543 | strncpy( str, s, len );
|
---|
544 | }
|
---|
545 |
|
---|
546 | SAFE_FREE( s );
|
---|
547 | }
|
---|
548 |
|
---|
549 | /****************************************************************************
|
---|
550 | Do some standard substitutions in a string.
|
---|
551 | This function will return an allocated string that have to be freed.
|
---|
552 | ****************************************************************************/
|
---|
553 |
|
---|
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)
|
---|
572 | {
|
---|
573 | char *b, *p, *s, *r, *a_string;
|
---|
574 | fstring pidstr, vnnstr;
|
---|
575 | const char *local_machine_name = get_local_machine_name();
|
---|
576 | TALLOC_CTX *tmp_ctx = NULL;
|
---|
577 |
|
---|
578 | /* workaround to prevent a crash while looking at bug #687 */
|
---|
579 |
|
---|
580 | if (!str) {
|
---|
581 | DEBUG(0,("alloc_sub_basic: NULL source string! This should not happen\n"));
|
---|
582 | return NULL;
|
---|
583 | }
|
---|
584 |
|
---|
585 | a_string = SMB_STRDUP(str);
|
---|
586 | if (a_string == NULL) {
|
---|
587 | DEBUG(0, ("alloc_sub_basic: Out of memory!\n"));
|
---|
588 | return NULL;
|
---|
589 | }
|
---|
590 |
|
---|
591 | tmp_ctx = talloc_stackframe();
|
---|
592 |
|
---|
593 | for (b = s = a_string; (p = strchr_m(s, '%')); s = a_string + (p - b)) {
|
---|
594 |
|
---|
595 | r = NULL;
|
---|
596 | b = a_string;
|
---|
597 |
|
---|
598 | switch (*(p+1)) {
|
---|
599 | case 'U' :
|
---|
600 | r = strlower_talloc(tmp_ctx, smb_name);
|
---|
601 | if (r == NULL) {
|
---|
602 | goto error;
|
---|
603 | }
|
---|
604 | a_string = realloc_string_sub(a_string, "%U", r);
|
---|
605 | break;
|
---|
606 | case 'G' : {
|
---|
607 | struct passwd *pass;
|
---|
608 | r = talloc_strdup(tmp_ctx, smb_name);
|
---|
609 | if (r == NULL) {
|
---|
610 | goto error;
|
---|
611 | }
|
---|
612 | pass = Get_Pwnam_alloc(tmp_ctx, r);
|
---|
613 | if (pass != NULL) {
|
---|
614 | a_string = realloc_string_sub(
|
---|
615 | a_string, "%G",
|
---|
616 | gidtoname(pass->pw_gid));
|
---|
617 | }
|
---|
618 | TALLOC_FREE(pass);
|
---|
619 | break;
|
---|
620 | }
|
---|
621 | case 'D' :
|
---|
622 | r = strupper_talloc(tmp_ctx, domain_name);
|
---|
623 | if (r == NULL) {
|
---|
624 | goto error;
|
---|
625 | }
|
---|
626 | a_string = realloc_string_sub(a_string, "%D", r);
|
---|
627 | break;
|
---|
628 | case 'I' : {
|
---|
629 | a_string = realloc_string_sub(
|
---|
630 | a_string, "%I",
|
---|
631 | sub_peeraddr[0] ? sub_peeraddr : "0.0.0.0");
|
---|
632 | break;
|
---|
633 | }
|
---|
634 | case 'i':
|
---|
635 | a_string = realloc_string_sub(
|
---|
636 | a_string, "%i",
|
---|
637 | sub_sockaddr[0] ? sub_sockaddr : "0.0.0.0");
|
---|
638 | break;
|
---|
639 | case 'L' :
|
---|
640 | if ( StrnCaseCmp(p, "%LOGONSERVER%", strlen("%LOGONSERVER%")) == 0 ) {
|
---|
641 | break;
|
---|
642 | }
|
---|
643 | if (local_machine_name && *local_machine_name) {
|
---|
644 | a_string = realloc_string_sub(a_string, "%L", local_machine_name);
|
---|
645 | } else {
|
---|
646 | a_string = realloc_string_sub(a_string, "%L", global_myname());
|
---|
647 | }
|
---|
648 | break;
|
---|
649 | case 'N':
|
---|
650 | a_string = realloc_string_sub(a_string, "%N", automount_server(smb_name));
|
---|
651 | break;
|
---|
652 | case 'M' :
|
---|
653 | a_string = realloc_string_sub(a_string, "%M",
|
---|
654 | sub_peername ? sub_peername : "");
|
---|
655 | break;
|
---|
656 | case 'R' :
|
---|
657 | a_string = realloc_string_sub(a_string, "%R", remote_proto);
|
---|
658 | break;
|
---|
659 | case 'T' :
|
---|
660 | a_string = realloc_string_sub(a_string, "%T", current_timestring(tmp_ctx, False));
|
---|
661 | break;
|
---|
662 | case 'a' :
|
---|
663 | a_string = realloc_string_sub(a_string, "%a",
|
---|
664 | get_remote_arch_str());
|
---|
665 | break;
|
---|
666 | case 'd' :
|
---|
667 | slprintf(pidstr,sizeof(pidstr)-1, "%d",(int)sys_getpid());
|
---|
668 | a_string = realloc_string_sub(a_string, "%d", pidstr);
|
---|
669 | break;
|
---|
670 | case 'h' :
|
---|
671 | a_string = realloc_string_sub(a_string, "%h", myhostname());
|
---|
672 | break;
|
---|
673 | case 'm' :
|
---|
674 | a_string = realloc_string_sub(a_string, "%m",
|
---|
675 | remote_machine
|
---|
676 | ? remote_machine
|
---|
677 | : "");
|
---|
678 | break;
|
---|
679 | case 'v' :
|
---|
680 | a_string = realloc_string_sub(a_string, "%v", samba_version_string());
|
---|
681 | break;
|
---|
682 | case 'w' :
|
---|
683 | a_string = realloc_string_sub(a_string, "%w", lp_winbind_separator());
|
---|
684 | break;
|
---|
685 | case '$' :
|
---|
686 | a_string = realloc_expand_env_var(a_string, p); /* Expand environment variables */
|
---|
687 | break;
|
---|
688 | case '(':
|
---|
689 | a_string = realloc_expand_longvar( a_string, p );
|
---|
690 | break;
|
---|
691 | case 'V' :
|
---|
692 | slprintf(vnnstr,sizeof(vnnstr)-1, "%u", get_my_vnn());
|
---|
693 | a_string = realloc_string_sub(a_string, "%V", vnnstr);
|
---|
694 | break;
|
---|
695 | default:
|
---|
696 | break;
|
---|
697 | }
|
---|
698 |
|
---|
699 | p++;
|
---|
700 | TALLOC_FREE(r);
|
---|
701 |
|
---|
702 | if (a_string == NULL) {
|
---|
703 | goto done;
|
---|
704 | }
|
---|
705 | }
|
---|
706 |
|
---|
707 | goto done;
|
---|
708 |
|
---|
709 | error:
|
---|
710 | SAFE_FREE(a_string);
|
---|
711 |
|
---|
712 | done:
|
---|
713 | TALLOC_FREE(tmp_ctx);
|
---|
714 | return a_string;
|
---|
715 | }
|
---|
716 |
|
---|
717 | /****************************************************************************
|
---|
718 | Do some specific substitutions in a string.
|
---|
719 | This function will return an allocated string that have to be freed.
|
---|
720 | ****************************************************************************/
|
---|
721 |
|
---|
722 | char *talloc_sub_specified(TALLOC_CTX *mem_ctx,
|
---|
723 | const char *input_string,
|
---|
724 | const char *username,
|
---|
725 | const char *domain,
|
---|
726 | uid_t uid,
|
---|
727 | gid_t gid)
|
---|
728 | {
|
---|
729 | char *a_string;
|
---|
730 | char *ret_string = NULL;
|
---|
731 | char *b, *p, *s;
|
---|
732 | TALLOC_CTX *tmp_ctx;
|
---|
733 |
|
---|
734 | if (!(tmp_ctx = talloc_new(mem_ctx))) {
|
---|
735 | DEBUG(0, ("talloc_new failed\n"));
|
---|
736 | return NULL;
|
---|
737 | }
|
---|
738 |
|
---|
739 | a_string = talloc_strdup(tmp_ctx, input_string);
|
---|
740 | if (a_string == NULL) {
|
---|
741 | DEBUG(0, ("talloc_sub_specified: Out of memory!\n"));
|
---|
742 | goto done;
|
---|
743 | }
|
---|
744 |
|
---|
745 | for (b = s = a_string; (p = strchr_m(s, '%')); s = a_string + (p - b)) {
|
---|
746 |
|
---|
747 | b = a_string;
|
---|
748 |
|
---|
749 | switch (*(p+1)) {
|
---|
750 | case 'U' :
|
---|
751 | a_string = talloc_string_sub(
|
---|
752 | tmp_ctx, a_string, "%U", username);
|
---|
753 | break;
|
---|
754 | case 'u' :
|
---|
755 | a_string = talloc_string_sub(
|
---|
756 | tmp_ctx, a_string, "%u", username);
|
---|
757 | break;
|
---|
758 | case 'G' :
|
---|
759 | if (gid != -1) {
|
---|
760 | a_string = talloc_string_sub(
|
---|
761 | tmp_ctx, a_string, "%G",
|
---|
762 | gidtoname(gid));
|
---|
763 | } else {
|
---|
764 | a_string = talloc_string_sub(
|
---|
765 | tmp_ctx, a_string,
|
---|
766 | "%G", "NO_GROUP");
|
---|
767 | }
|
---|
768 | break;
|
---|
769 | case 'g' :
|
---|
770 | if (gid != -1) {
|
---|
771 | a_string = talloc_string_sub(
|
---|
772 | tmp_ctx, a_string, "%g",
|
---|
773 | gidtoname(gid));
|
---|
774 | } else {
|
---|
775 | a_string = talloc_string_sub(
|
---|
776 | tmp_ctx, a_string, "%g", "NO_GROUP");
|
---|
777 | }
|
---|
778 | break;
|
---|
779 | case 'D' :
|
---|
780 | a_string = talloc_string_sub(tmp_ctx, a_string,
|
---|
781 | "%D", domain);
|
---|
782 | break;
|
---|
783 | case 'N' :
|
---|
784 | a_string = talloc_string_sub(
|
---|
785 | tmp_ctx, a_string, "%N",
|
---|
786 | automount_server(username));
|
---|
787 | break;
|
---|
788 | default:
|
---|
789 | break;
|
---|
790 | }
|
---|
791 |
|
---|
792 | p++;
|
---|
793 | if (a_string == NULL) {
|
---|
794 | goto done;
|
---|
795 | }
|
---|
796 | }
|
---|
797 |
|
---|
798 | /* Watch out, using "mem_ctx" here, so all intermediate stuff goes
|
---|
799 | * away with the TALLOC_FREE(tmp_ctx) further down. */
|
---|
800 |
|
---|
801 | ret_string = talloc_sub_basic(mem_ctx, username, domain, a_string);
|
---|
802 |
|
---|
803 | done:
|
---|
804 | TALLOC_FREE(tmp_ctx);
|
---|
805 | return ret_string;
|
---|
806 | }
|
---|
807 |
|
---|
808 | /****************************************************************************
|
---|
809 | ****************************************************************************/
|
---|
810 |
|
---|
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)
|
---|
815 | {
|
---|
816 | char *a_string, *ret_string;
|
---|
817 | char *b, *p, *s;
|
---|
818 |
|
---|
819 | a_string = SMB_STRDUP(str);
|
---|
820 | if (a_string == NULL) {
|
---|
821 | DEBUG(0, ("alloc_sub_advanced: Out of memory!\n"));
|
---|
822 | return NULL;
|
---|
823 | }
|
---|
824 |
|
---|
825 | for (b = s = a_string; (p = strchr_m(s, '%')); s = a_string + (p - b)) {
|
---|
826 |
|
---|
827 | b = a_string;
|
---|
828 |
|
---|
829 | switch (*(p+1)) {
|
---|
830 | case 'N' :
|
---|
831 | a_string = realloc_string_sub(a_string, "%N", automount_server(user));
|
---|
832 | break;
|
---|
833 | case 'H': {
|
---|
834 | char *h;
|
---|
835 | if ((h = get_user_home_dir(talloc_tos(), user)))
|
---|
836 | a_string = realloc_string_sub(a_string, "%H", h);
|
---|
837 | TALLOC_FREE(h);
|
---|
838 | break;
|
---|
839 | }
|
---|
840 | case 'P':
|
---|
841 | a_string = realloc_string_sub(a_string, "%P", connectpath);
|
---|
842 | break;
|
---|
843 | case 'S':
|
---|
844 | a_string = realloc_string_sub(a_string, "%S", servicename);
|
---|
845 | break;
|
---|
846 | case 'g':
|
---|
847 | a_string = realloc_string_sub(a_string, "%g", gidtoname(gid));
|
---|
848 | break;
|
---|
849 | case 'u':
|
---|
850 | a_string = realloc_string_sub(a_string, "%u", user);
|
---|
851 | break;
|
---|
852 |
|
---|
853 | /* Patch from jkf@soton.ac.uk Left the %N (NIS
|
---|
854 | * server name) in standard_sub_basic as it is
|
---|
855 | * a feature for logon servers, hence uses the
|
---|
856 | * username. The %p (NIS server path) code is
|
---|
857 | * here as it is used instead of the default
|
---|
858 | * "path =" string in [homes] and so needs the
|
---|
859 | * service name, not the username. */
|
---|
860 | case 'p':
|
---|
861 | a_string = realloc_string_sub(a_string, "%p",
|
---|
862 | automount_path(servicename));
|
---|
863 | break;
|
---|
864 |
|
---|
865 | default:
|
---|
866 | break;
|
---|
867 | }
|
---|
868 |
|
---|
869 | p++;
|
---|
870 | if (a_string == NULL) {
|
---|
871 | return NULL;
|
---|
872 | }
|
---|
873 | }
|
---|
874 |
|
---|
875 | ret_string = alloc_sub_basic(smb_name, domain_name, a_string);
|
---|
876 | SAFE_FREE(a_string);
|
---|
877 | return ret_string;
|
---|
878 | }
|
---|
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 |
|
---|
902 |
|
---|
903 | void standard_sub_advanced(const char *servicename, const char *user,
|
---|
904 | const char *connectpath, gid_t gid,
|
---|
905 | const char *smb_name, const char *domain_name,
|
---|
906 | char *str, size_t len)
|
---|
907 | {
|
---|
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 | }
|
---|