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