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 const char *home_path_start;
284 const 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 (!StrnCaseCmp(p, "%LOGONSERVER%", strlen("%LOGONSERVER%"))) {
394 if (local_machine_name && *local_machine_name) {
395 string_sub_once(p, "%L", local_machine_name, l);
399 pstrcpy(temp_name, global_myname());
400 strlower_m(temp_name);
401 string_sub_once(p, "%L", temp_name, l);
405 string_sub(p,"%M", client_name(),l);
408 string_sub(p,"%R", remote_proto,l);
411 string_sub(p,"%T", timestring(False),l);
414 string_sub(p,"%a", remote_arch,l);
417 slprintf(pidstr,sizeof(pidstr)-1, "%d",(int)sys_getpid());
418 string_sub(p,"%d", pidstr,l);
421 string_sub(p,"%h", myhostname(),l);
424 string_sub(p,"%m", get_remote_machine_name(),l);
427 string_sub(p,"%v", SAMBA_VERSION_STRING,l);
430 p += expand_env_var(p,l);
431 break; /* Expand environment variables */
434 break; /* don't run off the end of the string */
442 static void standard_sub_advanced(int snum, const char *user,
443 const char *connectpath, gid_t gid,
444 const char *smb_name, char *str, size_t len)
448 for (s=str; (p=strchr_m(s, '%'));s=p) {
449 int l = (int)len - (int)(p-str);
456 string_sub(p,"%N", automount_server(user),l);
459 if ((home = get_user_home_dir(user)))
460 string_sub(p,"%H",home, l);
465 string_sub(p,"%P", connectpath, l);
469 string_sub(p,"%S", lp_servicename(snum), l);
472 string_sub(p,"%g", gidtoname(gid), l);
475 string_sub(p,"%u", user, l);
478 /* Patch from jkf@soton.ac.uk Left the %N (NIS
479 * server name) in standard_sub_basic as it is
480 * a feature for logon servers, hence uses the
481 * username. The %p (NIS server path) code is
482 * here as it is used instead of the default
483 * "path =" string in [homes] and so needs the
484 * service name, not the username. */
487 string_sub(p,"%p", automount_path(lp_servicename(snum)), l);
491 break; /* don't run off the end of the string */
498 standard_sub_basic(smb_name, str, len);
501 /****************************************************************************
502 Do some standard substitutions in a string.
503 This function will return an allocated string that have to be freed.
504 ****************************************************************************/
506 char *talloc_sub_basic(TALLOC_CTX *mem_ctx, const char *smb_name, const char *str)
509 a = alloc_sub_basic(smb_name, str);
511 t = talloc_strdup(mem_ctx, a);
516 char *alloc_sub_basic(const char *smb_name, const char *str)
518 char *b, *p, *s, *t, *r, *a_string;
521 const char *local_machine_name = get_local_machine_name();
523 /* workaround to prevent a crash while lookinf at bug #687 */
526 DEBUG(0,("alloc_sub_basic: NULL source string! This should not happen\n"));
530 a_string = SMB_STRDUP(str);
531 if (a_string == NULL) {
532 DEBUG(0, ("alloc_sub_specified: Out of memory!\n"));
536 for (b = s = a_string; (p = strchr_m(s, '%')); s = a_string + (p - b)) {
543 r = strdup_lower(smb_name);
544 if (r == NULL) goto error;
545 t = realloc_string_sub(t, "%U", r);
548 r = SMB_STRDUP(smb_name);
549 if (r == NULL) goto error;
550 if ((pass = Get_Pwnam(r))!=NULL) {
551 t = realloc_string_sub(t, "%G", gidtoname(pass->pw_gid));
555 r = strdup_upper(current_user_info.domain);
556 if (r == NULL) goto error;
557 t = realloc_string_sub(t, "%D", r);
560 t = realloc_string_sub(t, "%I", client_addr());
563 if (local_machine_name && *local_machine_name)
564 t = realloc_string_sub(t, "%L", local_machine_name);
566 t = realloc_string_sub(t, "%L", global_myname());
569 t = realloc_string_sub(t, "%N", automount_server(smb_name));
572 t = realloc_string_sub(t, "%M", client_name());
575 t = realloc_string_sub(t, "%R", remote_proto);
578 t = realloc_string_sub(t, "%T", timestring(False));
581 t = realloc_string_sub(t, "%a", remote_arch);
584 slprintf(pidstr,sizeof(pidstr)-1, "%d",(int)sys_getpid());
585 t = realloc_string_sub(t, "%d", pidstr);
588 t = realloc_string_sub(t, "%h", myhostname());
591 t = realloc_string_sub(t, "%m", remote_machine);
594 t = realloc_string_sub(t, "%v", SAMBA_VERSION_STRING);
597 t = realloc_expand_env_var(t, p); /* Expand environment variables */
606 if (t == NULL) goto error;
616 /****************************************************************************
617 Do some specific substitutions in a string.
618 This function will return an allocated string that have to be freed.
619 ****************************************************************************/
621 char *talloc_sub_specified(TALLOC_CTX *mem_ctx,
622 const char *input_string,
623 const char *username,
629 a = alloc_sub_specified(input_string, username, domain, uid, gid);
631 t = talloc_strdup(mem_ctx, a);
636 char *alloc_sub_specified(const char *input_string,
637 const char *username,
642 char *a_string, *ret_string;
645 a_string = SMB_STRDUP(input_string);
646 if (a_string == NULL) {
647 DEBUG(0, ("alloc_sub_specified: Out of memory!\n"));
651 for (b = s = a_string; (p = strchr_m(s, '%')); s = a_string + (p - b)) {
657 t = realloc_string_sub(t, "%U", username);
660 t = realloc_string_sub(t, "%u", username);
664 t = realloc_string_sub(t, "%G", gidtoname(gid));
666 t = realloc_string_sub(t, "%G", "NO_GROUP");
671 t = realloc_string_sub(t, "%g", gidtoname(gid));
673 t = realloc_string_sub(t, "%g", "NO_GROUP");
677 t = realloc_string_sub(t, "%D", domain);
680 t = realloc_string_sub(t, "%N", automount_server(username));
694 ret_string = alloc_sub_basic(username, a_string);
699 char *talloc_sub_advanced(TALLOC_CTX *mem_ctx,
702 const char *connectpath,
704 const char *smb_name,
708 a = alloc_sub_advanced(snum, user, connectpath, gid, smb_name, str);
710 t = talloc_strdup(mem_ctx, a);
715 char *alloc_sub_advanced(int snum, const char *user,
716 const char *connectpath, gid_t gid,
717 const char *smb_name, const char *str)
719 char *a_string, *ret_string;
720 char *b, *p, *s, *t, *h;
722 a_string = SMB_STRDUP(str);
723 if (a_string == NULL) {
724 DEBUG(0, ("alloc_sub_specified: Out of memory!\n"));
728 for (b = s = a_string; (p = strchr_m(s, '%')); s = a_string + (p - b)) {
734 t = realloc_string_sub(t, "%N", automount_server(user));
737 if ((h = get_user_home_dir(user)))
738 t = realloc_string_sub(t, "%H", h);
741 t = realloc_string_sub(t, "%P", connectpath);
744 t = realloc_string_sub(t, "%S", lp_servicename(snum));
747 t = realloc_string_sub(t, "%g", gidtoname(gid));
750 t = realloc_string_sub(t, "%u", user);
753 /* Patch from jkf@soton.ac.uk Left the %N (NIS
754 * server name) in standard_sub_basic as it is
755 * a feature for logon servers, hence uses the
756 * username. The %p (NIS server path) code is
757 * here as it is used instead of the default
758 * "path =" string in [homes] and so needs the
759 * service name, not the username. */
761 t = realloc_string_sub(t, "%p", automount_path(lp_servicename(snum)));
776 ret_string = alloc_sub_basic(smb_name, a_string);
781 /****************************************************************************
782 Do some standard substitutions in a string.
783 ****************************************************************************/
785 void standard_sub_conn(connection_struct *conn, char *str, size_t len)
787 standard_sub_advanced(SNUM(conn), conn->user, conn->connectpath,
788 conn->gid, smb_user_name, str, len);
791 char *talloc_sub_conn(TALLOC_CTX *mem_ctx, connection_struct *conn, const char *str)
793 return talloc_sub_advanced(mem_ctx, SNUM(conn), conn->user,
794 conn->connectpath, conn->gid,
798 char *alloc_sub_conn(connection_struct *conn, const char *str)
800 return alloc_sub_advanced(SNUM(conn), conn->user, conn->connectpath,
801 conn->gid, smb_user_name, str);
804 /****************************************************************************
805 Like standard_sub but by snum.
806 ****************************************************************************/
808 void standard_sub_snum(int snum, char *str, size_t len)
810 static uid_t cached_uid = -1;
811 static fstring cached_user;
812 /* calling uidtoname() on every substitute would be too expensive, so
813 we cache the result here as nearly every call is for the same uid */
815 if (cached_uid != current_user.uid) {
816 fstrcpy(cached_user, uidtoname(current_user.uid));
817 cached_uid = current_user.uid;
820 standard_sub_advanced(snum, cached_user, "", current_user.gid,
821 smb_user_name, str, len);