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 fstring local_machine="";
25 fstring remote_arch="UNKNOWN";
26 userdom_struct current_user_info;
27 fstring remote_proto="UNKNOWN";
29 static fstring remote_machine;
30 static fstring smb_user_name;
33 * Set the 'local' machine name
34 * @param local_name the name we are being called
35 * @param if this is the 'final' name for us, not be be changed again
38 void set_local_machine_name(const char* local_name, BOOL perm)
40 static BOOL already_perm = False;
41 fstring tmp_local_machine;
43 fstrcpy(tmp_local_machine,local_name);
44 trim_char(tmp_local_machine,' ',' ');
47 * Windows NT/2k uses "*SMBSERVER" and XP uses "*SMBSERV"
51 if ( strequal(tmp_local_machine, "*SMBSERVER") || strequal(tmp_local_machine, "*SMBSERV") ) {
52 fstrcpy( local_machine, client_socket_addr() );
61 alpha_strcpy(local_machine,tmp_local_machine,SAFE_NETBIOS_CHARS,sizeof(local_machine)-1);
62 strlower_m(local_machine);
66 * Set the 'remote' machine name
67 * @param remote_name the name our client wants to be called by
68 * @param if this is the 'final' name for them, not be be changed again
71 void set_remote_machine_name(const char* remote_name, BOOL perm)
73 static BOOL already_perm = False;
74 fstring tmp_remote_machine;
81 fstrcpy(tmp_remote_machine,remote_name);
82 trim_char(tmp_remote_machine,' ',' ');
83 alpha_strcpy(remote_machine,tmp_remote_machine,SAFE_NETBIOS_CHARS,sizeof(remote_machine)-1);
84 strlower_m(remote_machine);
87 const char* get_remote_machine_name(void)
89 return remote_machine;
92 const char* get_local_machine_name(void)
94 if (!*local_machine) {
95 return global_myname();
101 /*******************************************************************
102 Setup the string used by %U substitution.
103 ********************************************************************/
105 void sub_set_smb_name(const char *name)
109 /* don't let anonymous logins override the name */
114 trim_char(tmp,' ',' ');
116 alpha_strcpy(smb_user_name,tmp,SAFE_NETBIOS_CHARS,sizeof(smb_user_name)-1);
119 char* sub_get_smb_name( void )
121 return smb_user_name;
124 /*******************************************************************
125 Setup the strings used by substitutions. Called per packet. Ensure
126 %U name is set correctly also.
127 ********************************************************************/
129 void set_current_user_info(const userdom_struct *pcui)
131 current_user_info = *pcui;
132 /* The following is safe as current_user_info.smb_name
133 * has already been sanitised in register_vuid. */
134 fstrcpy(smb_user_name, current_user_info.smb_name);
137 /*******************************************************************
138 return the current active user name
139 *******************************************************************/
141 const char* get_current_username( void )
143 if ( current_user_info.smb_name[0] == '\0' )
144 return smb_user_name;
146 return current_user_info.smb_name;
149 /*******************************************************************
150 Given a pointer to a %$(NAME) expand it as an environment variable.
151 Return the number of characters by which the pointer should be advanced.
152 Based on code by Branko Cibej <branko.cibej@hermes.si>
153 When this is called p points at the '%' character.
154 ********************************************************************/
156 static size_t expand_env_var(char *p, int len)
170 * Look for the terminating ')'.
173 if ((q = strchr_m(p,')')) == NULL) {
174 DEBUG(0,("expand_env_var: Unterminated environment variable [%s]\n", p));
179 * Extract the name from within the %$(NAME) string.
183 copylen = MIN((q-r),(sizeof(envname)-1));
184 strncpy(envname,r,copylen);
185 envname[copylen] = '\0';
187 if ((envval = getenv(envname)) == NULL) {
188 DEBUG(0,("expand_env_var: Environment variable [%s] not set\n", envname));
193 * Copy the full %$(NAME) into envname so it
197 copylen = MIN((q+1-p),(sizeof(envname)-1));
198 strncpy(envname,p,copylen);
199 envname[copylen] = '\0';
200 string_sub(p,envname,envval,len);
201 return 0; /* Allow the environment contents to be parsed. */
204 /*******************************************************************
205 Given a pointer to a %$(NAME) in p and the whole string in str
206 expand it as an environment variable.
207 Return a new allocated and expanded string.
208 Based on code by Branko Cibej <branko.cibej@hermes.si>
209 When this is called p points at the '%' character.
210 May substitute multiple occurrencies of the same env var.
211 ********************************************************************/
214 static char * realloc_expand_env_var(char *str, char *p)
221 if (p[0] != '%' || p[1] != '$' || p[2] != '(')
225 * Look for the terminating ')'.
228 if ((q = strchr_m(p,')')) == NULL) {
229 DEBUG(0,("expand_env_var: Unterminated environment variable [%s]\n", p));
234 * Extract the name from within the %$(NAME) string.
239 envname = (char *)SMB_MALLOC(copylen + 1 + 4); /* reserve space for use later add %$() chars */
240 if (envname == NULL) return NULL;
241 strncpy(envname,r,copylen);
242 envname[copylen] = '\0';
244 if ((envval = getenv(envname)) == NULL) {
245 DEBUG(0,("expand_env_var: Environment variable [%s] not set\n", envname));
251 * Copy the full %$(NAME) into envname so it
256 strncpy(envname,p,copylen);
257 envname[copylen] = '\0';
258 r = realloc_string_sub(str, envname, envval);
260 if (r == NULL) return NULL;
264 /*******************************************************************
265 Patch from jkf@soton.ac.uk
266 Added this to implement %p (NIS auto-map version of %H)
267 *******************************************************************/
269 static char *automount_path(const char *user_name)
271 static pstring server_path;
273 /* use the passwd entry as the default */
274 /* this will be the default if WITH_AUTOMOUNT is not used or fails */
276 pstrcpy(server_path, get_user_home_dir(user_name));
278 #if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT))
280 if (lp_nis_home_map()) {
281 char *home_path_start;
282 char *automount_value = automount_lookup(user_name);
284 if(strlen(automount_value) > 0) {
285 home_path_start = strchr_m(automount_value,':');
286 if (home_path_start != NULL) {
287 DEBUG(5, ("NIS lookup succeeded. Home path is: %s\n",
288 home_path_start?(home_path_start+1):""));
289 pstrcpy(server_path, home_path_start+1);
292 /* NIS key lookup failed: default to user home directory from password file */
293 DEBUG(5, ("NIS lookup failed. Using Home path from passwd file. Home path is: %s\n", server_path ));
298 DEBUG(4,("Home server path: %s\n", server_path));
303 /*******************************************************************
304 Patch from jkf@soton.ac.uk
305 This is Luke's original function with the NIS lookup code
306 moved out to a separate function.
307 *******************************************************************/
309 static const char *automount_server(const char *user_name)
311 static pstring server_name;
312 const char *local_machine_name = get_local_machine_name();
314 /* use the local machine name as the default */
315 /* this will be the default if WITH_AUTOMOUNT is not used or fails */
316 if (local_machine_name && *local_machine_name)
317 pstrcpy(server_name, local_machine_name);
319 pstrcpy(server_name, global_myname());
321 #if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT))
323 if (lp_nis_home_map()) {
325 char *automount_value = automount_lookup(user_name);
326 home_server_len = strcspn(automount_value,":");
327 DEBUG(5, ("NIS lookup succeeded. Home server length: %d\n",home_server_len));
328 if (home_server_len > sizeof(pstring))
329 home_server_len = sizeof(pstring);
330 strncpy(server_name, automount_value, home_server_len);
331 server_name[home_server_len] = '\0';
335 DEBUG(4,("Home server: %s\n", server_name));
340 /****************************************************************************
341 Do some standard substitutions in a string.
342 len is the length in bytes of the space allowed in string str. If zero means
343 don't allow expansions.
344 ****************************************************************************/
346 void standard_sub_basic(const char *smb_name, char *str,size_t len)
351 const char *local_machine_name = get_local_machine_name();
353 for (s=str; (p=strchr_m(s, '%'));s=p) {
356 int l = (int)len - (int)(p-str);
363 fstrcpy(tmp_str, smb_name);
365 string_sub(p,"%U",tmp_str,l);
368 fstrcpy(tmp_str, smb_name);
369 if ((pass = Get_Pwnam(tmp_str))!=NULL) {
370 string_sub(p,"%G",gidtoname(pass->pw_gid),l);
376 fstrcpy(tmp_str, current_user_info.domain);
378 string_sub(p,"%D", tmp_str,l);
381 string_sub(p,"%I", client_addr(),l);
384 string_sub(p,"%i", client_socket_addr(),l);
387 if (local_machine_name && *local_machine_name)
388 string_sub(p,"%L", local_machine_name,l);
392 pstrcpy(temp_name, global_myname());
393 strlower_m(temp_name);
394 string_sub(p,"%L", temp_name,l);
398 string_sub(p,"%M", client_name(),l);
401 string_sub(p,"%R", remote_proto,l);
404 string_sub(p,"%T", timestring(False),l);
407 string_sub(p,"%a", remote_arch,l);
410 slprintf(pidstr,sizeof(pidstr)-1, "%d",(int)sys_getpid());
411 string_sub(p,"%d", pidstr,l);
414 string_sub(p,"%h", myhostname(),l);
417 string_sub(p,"%m", get_remote_machine_name(),l);
420 string_sub(p,"%v", SAMBA_VERSION_STRING,l);
423 p += expand_env_var(p,l);
424 break; /* Expand environment variables */
427 break; /* don't run off the end of the string */
435 static void standard_sub_advanced(int snum, const char *user,
436 const char *connectpath, gid_t gid,
437 const char *smb_name, char *str, size_t len)
441 for (s=str; (p=strchr_m(s, '%'));s=p) {
442 int l = (int)len - (int)(p-str);
449 string_sub(p,"%N", automount_server(user),l);
452 if ((home = get_user_home_dir(user)))
453 string_sub(p,"%H",home, l);
458 string_sub(p,"%P", connectpath, l);
462 string_sub(p,"%S", lp_servicename(snum), l);
465 string_sub(p,"%g", gidtoname(gid), l);
468 string_sub(p,"%u", user, l);
471 /* Patch from jkf@soton.ac.uk Left the %N (NIS
472 * server name) in standard_sub_basic as it is
473 * a feature for logon servers, hence uses the
474 * username. The %p (NIS server path) code is
475 * here as it is used instead of the default
476 * "path =" string in [homes] and so needs the
477 * service name, not the username. */
480 string_sub(p,"%p", automount_path(lp_servicename(snum)), l);
484 break; /* don't run off the end of the string */
491 standard_sub_basic(smb_name, str, len);
494 /****************************************************************************
495 Do some standard substitutions in a string.
496 This function will return an allocated string that have to be freed.
497 ****************************************************************************/
499 char *talloc_sub_basic(TALLOC_CTX *mem_ctx, const char *smb_name, const char *str)
502 a = alloc_sub_basic(smb_name, str);
504 t = talloc_strdup(mem_ctx, a);
509 char *alloc_sub_basic(const char *smb_name, const char *str)
511 char *b, *p, *s, *t, *r, *a_string;
514 const char *local_machine_name = get_local_machine_name();
516 /* workaround to prevent a crash while lookinf at bug #687 */
519 DEBUG(0,("alloc_sub_basic: NULL source string! This should not happen\n"));
523 a_string = SMB_STRDUP(str);
524 if (a_string == NULL) {
525 DEBUG(0, ("alloc_sub_specified: Out of memory!\n"));
529 for (b = s = a_string; (p = strchr_m(s, '%')); s = a_string + (p - b)) {
536 r = strdup_lower(smb_name);
537 if (r == NULL) goto error;
538 t = realloc_string_sub(t, "%U", r);
541 r = SMB_STRDUP(smb_name);
542 if (r == NULL) goto error;
543 if ((pass = Get_Pwnam(r))!=NULL) {
544 t = realloc_string_sub(t, "%G", gidtoname(pass->pw_gid));
548 r = strdup_upper(current_user_info.domain);
549 if (r == NULL) goto error;
550 t = realloc_string_sub(t, "%D", r);
553 t = realloc_string_sub(t, "%I", client_addr());
556 if (local_machine_name && *local_machine_name)
557 t = realloc_string_sub(t, "%L", local_machine_name);
559 t = realloc_string_sub(t, "%L", global_myname());
562 t = realloc_string_sub(t, "%N", automount_server(smb_name));
565 t = realloc_string_sub(t, "%M", client_name());
568 t = realloc_string_sub(t, "%R", remote_proto);
571 t = realloc_string_sub(t, "%T", timestring(False));
574 t = realloc_string_sub(t, "%a", remote_arch);
577 slprintf(pidstr,sizeof(pidstr)-1, "%d",(int)sys_getpid());
578 t = realloc_string_sub(t, "%d", pidstr);
581 t = realloc_string_sub(t, "%h", myhostname());
584 t = realloc_string_sub(t, "%m", remote_machine);
587 t = realloc_string_sub(t, "%v", SAMBA_VERSION_STRING);
590 t = realloc_expand_env_var(t, p); /* Expand environment variables */
599 if (t == NULL) goto error;
609 /****************************************************************************
610 Do some specific substitutions in a string.
611 This function will return an allocated string that have to be freed.
612 ****************************************************************************/
614 char *talloc_sub_specified(TALLOC_CTX *mem_ctx,
615 const char *input_string,
616 const char *username,
622 a = alloc_sub_specified(input_string, username, domain, uid, gid);
624 t = talloc_strdup(mem_ctx, a);
629 char *alloc_sub_specified(const char *input_string,
630 const char *username,
635 char *a_string, *ret_string;
638 a_string = SMB_STRDUP(input_string);
639 if (a_string == NULL) {
640 DEBUG(0, ("alloc_sub_specified: Out of memory!\n"));
644 for (b = s = a_string; (p = strchr_m(s, '%')); s = a_string + (p - b)) {
650 t = realloc_string_sub(t, "%U", username);
653 t = realloc_string_sub(t, "%u", username);
657 t = realloc_string_sub(t, "%G", gidtoname(gid));
659 t = realloc_string_sub(t, "%G", "NO_GROUP");
664 t = realloc_string_sub(t, "%g", gidtoname(gid));
666 t = realloc_string_sub(t, "%g", "NO_GROUP");
670 t = realloc_string_sub(t, "%D", domain);
673 t = realloc_string_sub(t, "%N", automount_server(username));
687 ret_string = alloc_sub_basic(username, a_string);
692 char *talloc_sub_advanced(TALLOC_CTX *mem_ctx,
695 const char *connectpath,
697 const char *smb_name,
701 a = alloc_sub_advanced(snum, user, connectpath, gid, smb_name, str);
703 t = talloc_strdup(mem_ctx, a);
708 char *alloc_sub_advanced(int snum, const char *user,
709 const char *connectpath, gid_t gid,
710 const char *smb_name, const char *str)
712 char *a_string, *ret_string;
713 char *b, *p, *s, *t, *h;
715 a_string = SMB_STRDUP(str);
716 if (a_string == NULL) {
717 DEBUG(0, ("alloc_sub_specified: Out of memory!\n"));
721 for (b = s = a_string; (p = strchr_m(s, '%')); s = a_string + (p - b)) {
727 t = realloc_string_sub(t, "%N", automount_server(user));
730 if ((h = get_user_home_dir(user)))
731 t = realloc_string_sub(t, "%H", h);
734 t = realloc_string_sub(t, "%P", connectpath);
737 t = realloc_string_sub(t, "%S", lp_servicename(snum));
740 t = realloc_string_sub(t, "%g", gidtoname(gid));
743 t = realloc_string_sub(t, "%u", user);
746 /* Patch from jkf@soton.ac.uk Left the %N (NIS
747 * server name) in standard_sub_basic as it is
748 * a feature for logon servers, hence uses the
749 * username. The %p (NIS server path) code is
750 * here as it is used instead of the default
751 * "path =" string in [homes] and so needs the
752 * service name, not the username. */
754 t = realloc_string_sub(t, "%p", automount_path(lp_servicename(snum)));
769 ret_string = alloc_sub_basic(smb_name, a_string);
774 /****************************************************************************
775 Do some standard substitutions in a string.
776 ****************************************************************************/
778 void standard_sub_conn(connection_struct *conn, char *str, size_t len)
780 standard_sub_advanced(SNUM(conn), conn->user, conn->connectpath,
781 conn->gid, smb_user_name, str, len);
784 char *talloc_sub_conn(TALLOC_CTX *mem_ctx, connection_struct *conn, const char *str)
786 return talloc_sub_advanced(mem_ctx, SNUM(conn), conn->user,
787 conn->connectpath, conn->gid,
791 char *alloc_sub_conn(connection_struct *conn, const char *str)
793 return alloc_sub_advanced(SNUM(conn), conn->user, conn->connectpath,
794 conn->gid, smb_user_name, str);
797 /****************************************************************************
798 Like standard_sub but by snum.
799 ****************************************************************************/
801 void standard_sub_snum(int snum, char *str, size_t len)
803 extern struct current_user current_user;
804 static uid_t cached_uid = -1;
805 static fstring cached_user;
806 /* calling uidtoname() on every substitute would be too expensive, so
807 we cache the result here as nearly every call is for the same uid */
809 if (cached_uid != current_user.uid) {
810 fstrcpy(cached_user, uidtoname(current_user.uid));
811 cached_uid = current_user.uid;
814 standard_sub_advanced(snum, cached_user, "", current_user.gid,
815 smb_user_name, str, len);