2 Unix SMB/CIFS implementation.
3 string substitution functions
4 Copyright (C) Andrew Tridgell 1992-2000
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 extern struct current_user current_user
;
26 fstring local_machine
="";
27 fstring remote_arch
="UNKNOWN";
28 userdom_struct current_user_info
;
29 fstring remote_proto
="UNKNOWN";
31 static fstring remote_machine
;
32 static fstring smb_user_name
;
35 * Set the 'local' machine name
36 * @param local_name the name we are being called
37 * @param if this is the 'final' name for us, not be be changed again
40 void set_local_machine_name(const char* local_name
, BOOL perm
)
42 static BOOL already_perm
= False
;
43 fstring tmp_local_machine
;
45 fstrcpy(tmp_local_machine
,local_name
);
46 trim_char(tmp_local_machine
,' ',' ');
49 * Windows NT/2k uses "*SMBSERVER" and XP uses "*SMBSERV"
53 if ( strequal(tmp_local_machine
, "*SMBSERVER") || strequal(tmp_local_machine
, "*SMBSERV") ) {
54 fstrcpy( local_machine
, client_socket_addr() );
63 alpha_strcpy(local_machine
,tmp_local_machine
,SAFE_NETBIOS_CHARS
,sizeof(local_machine
)-1);
64 strlower_m(local_machine
);
68 * Set the 'remote' machine name
69 * @param remote_name the name our client wants to be called by
70 * @param if this is the 'final' name for them, not be be changed again
73 void set_remote_machine_name(const char* remote_name
, BOOL perm
)
75 static BOOL already_perm
= False
;
76 fstring tmp_remote_machine
;
83 fstrcpy(tmp_remote_machine
,remote_name
);
84 trim_char(tmp_remote_machine
,' ',' ');
85 alpha_strcpy(remote_machine
,tmp_remote_machine
,SAFE_NETBIOS_CHARS
,sizeof(remote_machine
)-1);
86 strlower_m(remote_machine
);
89 const char* get_remote_machine_name(void)
91 return remote_machine
;
94 const char* get_local_machine_name(void)
96 if (!*local_machine
) {
97 return global_myname();
100 return local_machine
;
103 /*******************************************************************
104 Setup the string used by %U substitution.
105 ********************************************************************/
107 void sub_set_smb_name(const char *name
)
111 /* don't let anonymous logins override the name */
116 trim_char(tmp
,' ',' ');
118 alpha_strcpy(smb_user_name
,tmp
,SAFE_NETBIOS_CHARS
,sizeof(smb_user_name
)-1);
121 char* sub_get_smb_name( void )
123 return smb_user_name
;
126 /*******************************************************************
127 Setup the strings used by substitutions. Called per packet. Ensure
128 %U name is set correctly also.
129 ********************************************************************/
131 void set_current_user_info(const userdom_struct
*pcui
)
133 current_user_info
= *pcui
;
134 /* The following is safe as current_user_info.smb_name
135 * has already been sanitised in register_vuid. */
136 fstrcpy(smb_user_name
, current_user_info
.smb_name
);
139 /*******************************************************************
140 return the current active user name
141 *******************************************************************/
143 const char* get_current_username( void )
145 if ( current_user_info
.smb_name
[0] == '\0' )
146 return smb_user_name
;
148 return current_user_info
.smb_name
;
151 /*******************************************************************
152 Given a pointer to a %$(NAME) expand it as an environment variable.
153 Return the number of characters by which the pointer should be advanced.
154 Based on code by Branko Cibej <branko.cibej@hermes.si>
155 When this is called p points at the '%' character.
156 ********************************************************************/
158 static size_t expand_env_var(char *p
, int len
)
172 * Look for the terminating ')'.
175 if ((q
= strchr_m(p
,')')) == NULL
) {
176 DEBUG(0,("expand_env_var: Unterminated environment variable [%s]\n", p
));
181 * Extract the name from within the %$(NAME) string.
185 copylen
= MIN((q
-r
),(sizeof(envname
)-1));
186 strncpy(envname
,r
,copylen
);
187 envname
[copylen
] = '\0';
189 if ((envval
= getenv(envname
)) == NULL
) {
190 DEBUG(0,("expand_env_var: Environment variable [%s] not set\n", envname
));
195 * Copy the full %$(NAME) into envname so it
199 copylen
= MIN((q
+1-p
),(sizeof(envname
)-1));
200 strncpy(envname
,p
,copylen
);
201 envname
[copylen
] = '\0';
202 string_sub(p
,envname
,envval
,len
);
203 return 0; /* Allow the environment contents to be parsed. */
206 /*******************************************************************
207 Given a pointer to a %$(NAME) in p and the whole string in str
208 expand it as an environment variable.
209 Return a new allocated and expanded string.
210 Based on code by Branko Cibej <branko.cibej@hermes.si>
211 When this is called p points at the '%' character.
212 May substitute multiple occurrencies of the same env var.
213 ********************************************************************/
216 static char * realloc_expand_env_var(char *str
, char *p
)
223 if (p
[0] != '%' || p
[1] != '$' || p
[2] != '(')
227 * Look for the terminating ')'.
230 if ((q
= strchr_m(p
,')')) == NULL
) {
231 DEBUG(0,("expand_env_var: Unterminated environment variable [%s]\n", p
));
236 * Extract the name from within the %$(NAME) string.
241 envname
= (char *)SMB_MALLOC(copylen
+ 1 + 4); /* reserve space for use later add %$() chars */
242 if (envname
== NULL
) return NULL
;
243 strncpy(envname
,r
,copylen
);
244 envname
[copylen
] = '\0';
246 if ((envval
= getenv(envname
)) == NULL
) {
247 DEBUG(0,("expand_env_var: Environment variable [%s] not set\n", envname
));
253 * Copy the full %$(NAME) into envname so it
258 strncpy(envname
,p
,copylen
);
259 envname
[copylen
] = '\0';
260 r
= realloc_string_sub(str
, envname
, envval
);
262 if (r
== NULL
) return NULL
;
266 /*******************************************************************
267 Patch from jkf@soton.ac.uk
268 Added this to implement %p (NIS auto-map version of %H)
269 *******************************************************************/
271 static char *automount_path(const char *user_name
)
273 static pstring server_path
;
275 /* use the passwd entry as the default */
276 /* this will be the default if WITH_AUTOMOUNT is not used or fails */
278 pstrcpy(server_path
, get_user_home_dir(user_name
));
280 #if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT))
282 if (lp_nis_home_map()) {
283 char *home_path_start
;
284 char *automount_value
= automount_lookup(user_name
);
286 if(strlen(automount_value
) > 0) {
287 home_path_start
= strchr_m(automount_value
,':');
288 if (home_path_start
!= NULL
) {
289 DEBUG(5, ("NIS lookup succeeded. Home path is: %s\n",
290 home_path_start
?(home_path_start
+1):""));
291 pstrcpy(server_path
, home_path_start
+1);
294 /* NIS key lookup failed: default to user home directory from password file */
295 DEBUG(5, ("NIS lookup failed. Using Home path from passwd file. Home path is: %s\n", server_path
));
300 DEBUG(4,("Home server path: %s\n", server_path
));
305 /*******************************************************************
306 Patch from jkf@soton.ac.uk
307 This is Luke's original function with the NIS lookup code
308 moved out to a separate function.
309 *******************************************************************/
311 static const char *automount_server(const char *user_name
)
313 static pstring server_name
;
314 const char *local_machine_name
= get_local_machine_name();
316 /* use the local machine name as the default */
317 /* this will be the default if WITH_AUTOMOUNT is not used or fails */
318 if (local_machine_name
&& *local_machine_name
)
319 pstrcpy(server_name
, local_machine_name
);
321 pstrcpy(server_name
, global_myname());
323 #if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT))
325 if (lp_nis_home_map()) {
327 char *automount_value
= automount_lookup(user_name
);
328 home_server_len
= strcspn(automount_value
,":");
329 DEBUG(5, ("NIS lookup succeeded. Home server length: %d\n",home_server_len
));
330 if (home_server_len
> sizeof(pstring
))
331 home_server_len
= sizeof(pstring
);
332 strncpy(server_name
, automount_value
, home_server_len
);
333 server_name
[home_server_len
] = '\0';
337 DEBUG(4,("Home server: %s\n", server_name
));
342 /****************************************************************************
343 Do some standard substitutions in a string.
344 len is the length in bytes of the space allowed in string str. If zero means
345 don't allow expansions.
346 ****************************************************************************/
348 void standard_sub_basic(const char *smb_name
, char *str
,size_t len
)
353 const char *local_machine_name
= get_local_machine_name();
355 for (s
=str
; (p
=strchr_m(s
, '%'));s
=p
) {
358 int l
= (int)len
- (int)(p
-str
);
365 fstrcpy(tmp_str
, smb_name
);
367 string_sub(p
,"%U",tmp_str
,l
);
370 fstrcpy(tmp_str
, smb_name
);
371 if ((pass
= Get_Pwnam(tmp_str
))!=NULL
) {
372 string_sub(p
,"%G",gidtoname(pass
->pw_gid
),l
);
378 fstrcpy(tmp_str
, current_user_info
.domain
);
380 string_sub(p
,"%D", tmp_str
,l
);
383 string_sub(p
,"%I", client_addr(),l
);
386 string_sub(p
,"%i", client_socket_addr(),l
);
389 if (local_machine_name
&& *local_machine_name
)
390 string_sub(p
,"%L", local_machine_name
,l
);
394 pstrcpy(temp_name
, global_myname());
395 strlower_m(temp_name
);
396 string_sub(p
,"%L", temp_name
,l
);
400 string_sub(p
,"%M", client_name(),l
);
403 string_sub(p
,"%R", remote_proto
,l
);
406 string_sub(p
,"%T", timestring(False
),l
);
409 string_sub(p
,"%a", remote_arch
,l
);
412 slprintf(pidstr
,sizeof(pidstr
)-1, "%d",(int)sys_getpid());
413 string_sub(p
,"%d", pidstr
,l
);
416 string_sub(p
,"%h", myhostname(),l
);
419 string_sub(p
,"%m", get_remote_machine_name(),l
);
422 string_sub(p
,"%v", SAMBA_VERSION_STRING
,l
);
425 p
+= expand_env_var(p
,l
);
426 break; /* Expand environment variables */
429 break; /* don't run off the end of the string */
437 static void standard_sub_advanced(int snum
, const char *user
,
438 const char *connectpath
, gid_t gid
,
439 const char *smb_name
, char *str
, size_t len
)
443 for (s
=str
; (p
=strchr_m(s
, '%'));s
=p
) {
444 int l
= (int)len
- (int)(p
-str
);
451 string_sub(p
,"%N", automount_server(user
),l
);
454 if ((home
= get_user_home_dir(user
)))
455 string_sub(p
,"%H",home
, l
);
460 string_sub(p
,"%P", connectpath
, l
);
464 string_sub(p
,"%S", lp_servicename(snum
), l
);
467 string_sub(p
,"%g", gidtoname(gid
), l
);
470 string_sub(p
,"%u", user
, l
);
473 /* Patch from jkf@soton.ac.uk Left the %N (NIS
474 * server name) in standard_sub_basic as it is
475 * a feature for logon servers, hence uses the
476 * username. The %p (NIS server path) code is
477 * here as it is used instead of the default
478 * "path =" string in [homes] and so needs the
479 * service name, not the username. */
482 string_sub(p
,"%p", automount_path(lp_servicename(snum
)), l
);
486 break; /* don't run off the end of the string */
493 standard_sub_basic(smb_name
, str
, len
);
496 /****************************************************************************
497 Do some standard substitutions in a string.
498 This function will return an allocated string that have to be freed.
499 ****************************************************************************/
501 char *talloc_sub_basic(TALLOC_CTX
*mem_ctx
, const char *smb_name
, const char *str
)
504 a
= alloc_sub_basic(smb_name
, str
);
506 t
= talloc_strdup(mem_ctx
, a
);
511 char *alloc_sub_basic(const char *smb_name
, const char *str
)
513 char *b
, *p
, *s
, *t
, *r
, *a_string
;
516 const char *local_machine_name
= get_local_machine_name();
518 /* workaround to prevent a crash while lookinf at bug #687 */
521 DEBUG(0,("alloc_sub_basic: NULL source string! This should not happen\n"));
525 a_string
= SMB_STRDUP(str
);
526 if (a_string
== NULL
) {
527 DEBUG(0, ("alloc_sub_specified: Out of memory!\n"));
531 for (b
= s
= a_string
; (p
= strchr_m(s
, '%')); s
= a_string
+ (p
- b
)) {
538 r
= strdup_lower(smb_name
);
539 if (r
== NULL
) goto error
;
540 t
= realloc_string_sub(t
, "%U", r
);
543 r
= SMB_STRDUP(smb_name
);
544 if (r
== NULL
) goto error
;
545 if ((pass
= Get_Pwnam(r
))!=NULL
) {
546 t
= realloc_string_sub(t
, "%G", gidtoname(pass
->pw_gid
));
550 r
= strdup_upper(current_user_info
.domain
);
551 if (r
== NULL
) goto error
;
552 t
= realloc_string_sub(t
, "%D", r
);
555 t
= realloc_string_sub(t
, "%I", client_addr());
558 if (local_machine_name
&& *local_machine_name
)
559 t
= realloc_string_sub(t
, "%L", local_machine_name
);
561 t
= realloc_string_sub(t
, "%L", global_myname());
564 t
= realloc_string_sub(t
, "%N", automount_server(smb_name
));
567 t
= realloc_string_sub(t
, "%M", client_name());
570 t
= realloc_string_sub(t
, "%R", remote_proto
);
573 t
= realloc_string_sub(t
, "%T", timestring(False
));
576 t
= realloc_string_sub(t
, "%a", remote_arch
);
579 slprintf(pidstr
,sizeof(pidstr
)-1, "%d",(int)sys_getpid());
580 t
= realloc_string_sub(t
, "%d", pidstr
);
583 t
= realloc_string_sub(t
, "%h", myhostname());
586 t
= realloc_string_sub(t
, "%m", remote_machine
);
589 t
= realloc_string_sub(t
, "%v", SAMBA_VERSION_STRING
);
592 t
= realloc_expand_env_var(t
, p
); /* Expand environment variables */
601 if (t
== NULL
) goto error
;
611 /****************************************************************************
612 Do some specific substitutions in a string.
613 This function will return an allocated string that have to be freed.
614 ****************************************************************************/
616 char *talloc_sub_specified(TALLOC_CTX
*mem_ctx
,
617 const char *input_string
,
618 const char *username
,
624 a
= alloc_sub_specified(input_string
, username
, domain
, uid
, gid
);
626 t
= talloc_strdup(mem_ctx
, a
);
631 char *alloc_sub_specified(const char *input_string
,
632 const char *username
,
637 char *a_string
, *ret_string
;
640 a_string
= SMB_STRDUP(input_string
);
641 if (a_string
== NULL
) {
642 DEBUG(0, ("alloc_sub_specified: Out of memory!\n"));
646 for (b
= s
= a_string
; (p
= strchr_m(s
, '%')); s
= a_string
+ (p
- b
)) {
652 t
= realloc_string_sub(t
, "%U", username
);
655 t
= realloc_string_sub(t
, "%u", username
);
659 t
= realloc_string_sub(t
, "%G", gidtoname(gid
));
661 t
= realloc_string_sub(t
, "%G", "NO_GROUP");
666 t
= realloc_string_sub(t
, "%g", gidtoname(gid
));
668 t
= realloc_string_sub(t
, "%g", "NO_GROUP");
672 t
= realloc_string_sub(t
, "%D", domain
);
675 t
= realloc_string_sub(t
, "%N", automount_server(username
));
689 ret_string
= alloc_sub_basic(username
, a_string
);
694 char *talloc_sub_advanced(TALLOC_CTX
*mem_ctx
,
697 const char *connectpath
,
699 const char *smb_name
,
703 a
= alloc_sub_advanced(snum
, user
, connectpath
, gid
, smb_name
, str
);
705 t
= talloc_strdup(mem_ctx
, a
);
710 char *alloc_sub_advanced(int snum
, const char *user
,
711 const char *connectpath
, gid_t gid
,
712 const char *smb_name
, const char *str
)
714 char *a_string
, *ret_string
;
715 char *b
, *p
, *s
, *t
, *h
;
717 a_string
= SMB_STRDUP(str
);
718 if (a_string
== NULL
) {
719 DEBUG(0, ("alloc_sub_specified: Out of memory!\n"));
723 for (b
= s
= a_string
; (p
= strchr_m(s
, '%')); s
= a_string
+ (p
- b
)) {
729 t
= realloc_string_sub(t
, "%N", automount_server(user
));
732 if ((h
= get_user_home_dir(user
)))
733 t
= realloc_string_sub(t
, "%H", h
);
736 t
= realloc_string_sub(t
, "%P", connectpath
);
739 t
= realloc_string_sub(t
, "%S", lp_servicename(snum
));
742 t
= realloc_string_sub(t
, "%g", gidtoname(gid
));
745 t
= realloc_string_sub(t
, "%u", user
);
748 /* Patch from jkf@soton.ac.uk Left the %N (NIS
749 * server name) in standard_sub_basic as it is
750 * a feature for logon servers, hence uses the
751 * username. The %p (NIS server path) code is
752 * here as it is used instead of the default
753 * "path =" string in [homes] and so needs the
754 * service name, not the username. */
756 t
= realloc_string_sub(t
, "%p", automount_path(lp_servicename(snum
)));
771 ret_string
= alloc_sub_basic(smb_name
, a_string
);
776 /****************************************************************************
777 Do some standard substitutions in a string.
778 ****************************************************************************/
780 void standard_sub_conn(connection_struct
*conn
, char *str
, size_t len
)
782 standard_sub_advanced(SNUM(conn
), conn
->user
, conn
->connectpath
,
783 conn
->gid
, smb_user_name
, str
, len
);
786 char *talloc_sub_conn(TALLOC_CTX
*mem_ctx
, connection_struct
*conn
, const char *str
)
788 return talloc_sub_advanced(mem_ctx
, SNUM(conn
), conn
->user
,
789 conn
->connectpath
, conn
->gid
,
793 char *alloc_sub_conn(connection_struct
*conn
, const char *str
)
795 return alloc_sub_advanced(SNUM(conn
), conn
->user
, conn
->connectpath
,
796 conn
->gid
, smb_user_name
, str
);
799 /****************************************************************************
800 Like standard_sub but by snum.
801 ****************************************************************************/
803 void standard_sub_snum(int snum
, char *str
, size_t len
)
805 static uid_t cached_uid
= -1;
806 static fstring cached_user
;
807 /* calling uidtoname() on every substitute would be too expensive, so
808 we cache the result here as nearly every call is for the same uid */
810 if (cached_uid
!= current_user
.uid
) {
811 fstrcpy(cached_user
, uidtoname(current_user
.uid
));
812 cached_uid
= current_user
.uid
;
815 standard_sub_advanced(snum
, cached_user
, "", current_user
.gid
,
816 smb_user_name
, str
, len
);