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="";
32 void set_local_machine_name(const char* local_name)
34 fstring tmp_local_machine;
36 fstrcpy(tmp_local_machine,local_name);
37 trim_string(tmp_local_machine," "," ");
38 strlower(tmp_local_machine);
39 alpha_strcpy(local_machine,tmp_local_machine,SAFE_NETBIOS_CHARS,sizeof(local_machine)-1);
42 void set_remote_machine_name(const char* remote_name)
44 fstring tmp_remote_machine;
46 fstrcpy(tmp_remote_machine,remote_name);
47 trim_string(tmp_remote_machine," "," ");
48 strlower(tmp_remote_machine);
49 alpha_strcpy(remote_machine,tmp_remote_machine,SAFE_NETBIOS_CHARS,sizeof(remote_machine)-1);
52 const char* get_remote_machine_name(void)
54 return remote_machine;
57 const char* get_local_machine_name(void)
62 /*******************************************************************
63 Given a pointer to a %$(NAME) expand it as an environment variable.
64 Return the number of characters by which the pointer should be advanced.
65 Based on code by Branko Cibej <branko.cibej@hermes.si>
66 When this is called p points at the '%' character.
67 ********************************************************************/
69 static size_t expand_env_var(char *p, int len)
83 * Look for the terminating ')'.
86 if ((q = strchr_m(p,')')) == NULL) {
87 DEBUG(0,("expand_env_var: Unterminated environment variable [%s]\n", p));
92 * Extract the name from within the %$(NAME) string.
96 copylen = MIN((q-r),(sizeof(envname)-1));
97 strncpy(envname,r,copylen);
98 envname[copylen] = '\0';
100 if ((envval = getenv(envname)) == NULL) {
101 DEBUG(0,("expand_env_var: Environment variable [%s] not set\n", envname));
106 * Copy the full %$(NAME) into envname so it
110 copylen = MIN((q+1-p),(sizeof(envname)-1));
111 strncpy(envname,p,copylen);
112 envname[copylen] = '\0';
113 string_sub(p,envname,envval,len);
114 return 0; /* Allow the environment contents to be parsed. */
117 /*******************************************************************
118 Given a pointer to a %$(NAME) in p and the whole string in str
119 expand it as an environment variable.
120 Return a new allocated and expanded string.
121 Based on code by Branko Cibej <branko.cibej@hermes.si>
122 When this is called p points at the '%' character.
123 May substitute multiple occurrencies of the same env var.
124 ********************************************************************/
127 static char * realloc_expand_env_var(char *str, char *p)
134 if (p[0] != '%' || p[1] != '$' || p[2] != '(')
138 * Look for the terminating ')'.
141 if ((q = strchr_m(p,')')) == NULL) {
142 DEBUG(0,("expand_env_var: Unterminated environment variable [%s]\n", p));
147 * Extract the name from within the %$(NAME) string.
152 envname = (char *)malloc(copylen + 1 + 4); /* reserve space for use later add %$() chars */
153 if (envname == NULL) return NULL;
154 strncpy(envname,r,copylen);
155 envname[copylen] = '\0';
157 if ((envval = getenv(envname)) == NULL) {
158 DEBUG(0,("expand_env_var: Environment variable [%s] not set\n", envname));
164 * Copy the full %$(NAME) into envname so it
169 strncpy(envname,p,copylen);
170 envname[copylen] = '\0';
171 r = realloc_string_sub(str, envname, envval);
173 if (r == NULL) return NULL;
177 /*******************************************************************
178 Patch from jkf@soton.ac.uk
179 Added this to implement %p (NIS auto-map version of %H)
180 *******************************************************************/
182 static char *automount_path(const char *user_name)
184 static pstring server_path;
186 /* use the passwd entry as the default */
187 /* this will be the default if WITH_AUTOMOUNT is not used or fails */
189 pstrcpy(server_path, get_user_home_dir(user_name));
191 #if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT))
193 if (lp_nis_home_map()) {
194 char *home_path_start;
195 char *automount_value = automount_lookup(user_name);
197 if(strlen(automount_value) > 0) {
198 home_path_start = strchr_m(automount_value,':');
199 if (home_path_start != NULL) {
200 DEBUG(5, ("NIS lookup succeeded. Home path is: %s\n",
201 home_path_start?(home_path_start+1):""));
202 pstrcpy(server_path, home_path_start+1);
205 /* NIS key lookup failed: default to user home directory from password file */
206 DEBUG(5, ("NIS lookup failed. Using Home path from passwd file. Home path is: %s\n", server_path ));
211 DEBUG(4,("Home server path: %s\n", server_path));
216 /*******************************************************************
217 Patch from jkf@soton.ac.uk
218 This is Luke's original function with the NIS lookup code
219 moved out to a separate function.
220 *******************************************************************/
222 static const char *automount_server(const char *user_name)
224 static pstring server_name;
225 const char *local_machine_name = get_local_machine_name();
227 /* use the local machine name as the default */
228 /* this will be the default if WITH_AUTOMOUNT is not used or fails */
229 if (local_machine_name && *local_machine_name)
230 pstrcpy(server_name, local_machine_name);
232 pstrcpy(server_name, global_myname());
234 #if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT))
236 if (lp_nis_home_map()) {
238 char *automount_value = automount_lookup(user_name);
239 home_server_len = strcspn(automount_value,":");
240 DEBUG(5, ("NIS lookup succeeded. Home server length: %d\n",home_server_len));
241 if (home_server_len > sizeof(pstring))
242 home_server_len = sizeof(pstring);
243 strncpy(server_name, automount_value, home_server_len);
244 server_name[home_server_len] = '\0';
248 DEBUG(4,("Home server: %s\n", server_name));
253 /****************************************************************************
254 Do some standard substitutions in a string.
255 len is the length in bytes of the space allowed in string str. If zero means
256 don't allow expansions.
257 ****************************************************************************/
259 void standard_sub_basic(const char *smb_name, char *str,size_t len)
264 const char *local_machine_name = get_local_machine_name();
266 for (s=str; (p=strchr_m(s, '%'));s=p) {
269 int l = (int)len - (int)(p-str);
276 fstrcpy(tmp_str, smb_name);
278 string_sub(p,"%U",tmp_str,l);
281 fstrcpy(tmp_str, smb_name);
282 if ((pass = Get_Pwnam(tmp_str))!=NULL) {
283 string_sub(p,"%G",gidtoname(pass->pw_gid),l);
289 fstrcpy(tmp_str, current_user_info.domain);
291 string_sub(p,"%D", tmp_str,l);
294 string_sub(p,"%I", client_addr(),l);
297 if (local_machine_name && *local_machine_name)
298 string_sub(p,"%L", local_machine_name,l);
302 pstrcpy(temp_name, global_myname());
304 string_sub(p,"%L", temp_name,l);
308 string_sub(p,"%M", client_name(),l);
311 string_sub(p,"%R", remote_proto,l);
314 string_sub(p,"%T", timestring(False),l);
317 string_sub(p,"%a", remote_arch,l);
320 slprintf(pidstr,sizeof(pidstr)-1, "%d",(int)sys_getpid());
321 string_sub(p,"%d", pidstr,l);
324 string_sub(p,"%h", myhostname(),l);
327 string_sub(p,"%m", get_remote_machine_name(),l);
330 string_sub(p,"%v", VERSION,l);
333 p += expand_env_var(p,l);
334 break; /* Expand environment variables */
337 break; /* don't run off the end of the string */
345 static void standard_sub_advanced(int snum, const char *user,
346 const char *connectpath, gid_t gid,
347 const char *smb_name, char *str, size_t len)
351 for (s=str; (p=strchr_m(s, '%'));s=p) {
352 int l = (int)len - (int)(p-str);
359 string_sub(p,"%N", automount_server(user),l);
362 if ((home = get_user_home_dir(user)))
363 string_sub(p,"%H",home, l);
368 string_sub(p,"%P", connectpath, l);
371 string_sub(p,"%S", lp_servicename(snum), l);
374 string_sub(p,"%g", gidtoname(gid), l);
377 string_sub(p,"%u", user, l);
380 /* Patch from jkf@soton.ac.uk Left the %N (NIS
381 * server name) in standard_sub_basic as it is
382 * a feature for logon servers, hence uses the
383 * username. The %p (NIS server path) code is
384 * here as it is used instead of the default
385 * "path =" string in [homes] and so needs the
386 * service name, not the username. */
388 string_sub(p,"%p", automount_path(lp_servicename(snum)), l);
392 break; /* don't run off the end of the string */
399 standard_sub_basic(smb_name, str, len);
402 /****************************************************************************
403 Do some standard substitutions in a string.
404 This function will return an allocated string that have to be freed.
405 ****************************************************************************/
407 char *talloc_sub_basic(TALLOC_CTX *mem_ctx, const char *smb_name, const char *str)
410 a = alloc_sub_basic(smb_name, str);
412 t = talloc_strdup(mem_ctx, a);
417 char *alloc_sub_basic(const char *smb_name, const char *str)
419 char *b, *p, *s, *t, *r, *a_string;
422 const char *local_machine_name = get_local_machine_name();
424 a_string = strdup(str);
425 if (a_string == NULL) {
426 DEBUG(0, ("alloc_sub_specified: Out of memory!\n"));
430 for (b = s = a_string; (p = strchr_m(s, '%')); s = a_string + (p - b)) {
437 r = strdup_lower(smb_name);
438 if (r == NULL) goto error;
439 t = realloc_string_sub(t, "%U", r);
442 r = strdup(smb_name);
443 if (r == NULL) goto error;
444 if ((pass = Get_Pwnam(r))!=NULL) {
445 t = realloc_string_sub(t, "%G", gidtoname(pass->pw_gid));
449 r = strdup_upper(current_user_info.domain);
450 if (r == NULL) goto error;
451 t = realloc_string_sub(t, "%D", r);
454 t = realloc_string_sub(t, "%I", client_addr());
457 if (local_machine_name && *local_machine_name)
458 t = realloc_string_sub(t, "%L", local_machine_name);
460 t = realloc_string_sub(t, "%L", global_myname());
463 t = realloc_string_sub(t, "%M", client_name());
466 t = realloc_string_sub(t, "%R", remote_proto);
469 t = realloc_string_sub(t, "%T", timestring(False));
472 t = realloc_string_sub(t, "%a", remote_arch);
475 slprintf(pidstr,sizeof(pidstr)-1, "%d",(int)sys_getpid());
476 t = realloc_string_sub(t, "%d", pidstr);
479 t = realloc_string_sub(t, "%h", myhostname());
482 t = realloc_string_sub(t, "%m", remote_machine);
485 t = realloc_string_sub(t, "%v", VERSION);
488 t = realloc_expand_env_var(t, p); /* Expand environment variables */
497 if (t == NULL) goto error;
507 /****************************************************************************
508 Do some specific substitutions in a string.
509 This function will return an allocated string that have to be freed.
510 ****************************************************************************/
512 char *talloc_sub_specified(TALLOC_CTX *mem_ctx,
513 const char *input_string,
514 const char *username,
520 a = alloc_sub_specified(input_string, username, domain, uid, gid);
522 t = talloc_strdup(mem_ctx, a);
527 char *alloc_sub_specified(const char *input_string,
528 const char *username,
533 char *a_string, *ret_string;
536 a_string = strdup(input_string);
537 if (a_string == NULL) {
538 DEBUG(0, ("alloc_sub_specified: Out of memory!\n"));
542 for (b = s = a_string; (p = strchr_m(s, '%')); s = a_string + (p - b)) {
548 t = realloc_string_sub(t, "%U", username);
551 t = realloc_string_sub(t, "%u", username);
555 t = realloc_string_sub(t, "%G", gidtoname(gid));
557 t = realloc_string_sub(t, "%G", "NO_GROUP");
562 t = realloc_string_sub(t, "%g", gidtoname(gid));
564 t = realloc_string_sub(t, "%g", "NO_GROUP");
568 t = realloc_string_sub(t, "%D", domain);
571 t = realloc_string_sub(t, "%N", automount_server(username));
585 ret_string = alloc_sub_basic(username, a_string);
590 char *talloc_sub_advanced(TALLOC_CTX *mem_ctx,
593 const char *connectpath,
595 const char *smb_name,
599 a = alloc_sub_advanced(snum, user, connectpath, gid, smb_name, str);
601 t = talloc_strdup(mem_ctx, a);
606 char *alloc_sub_advanced(int snum, const char *user,
607 const char *connectpath, gid_t gid,
608 const char *smb_name, char *str)
610 char *a_string, *ret_string;
611 char *b, *p, *s, *t, *h;
613 a_string = strdup(str);
614 if (a_string == NULL) {
615 DEBUG(0, ("alloc_sub_specified: Out of memory!\n"));
619 for (b = s = a_string; (p = strchr_m(s, '%')); s = a_string + (p - b)) {
625 t = realloc_string_sub(t, "%N", automount_server(user));
628 if ((h = get_user_home_dir(user)))
629 t = realloc_string_sub(t, "%H", h);
632 t = realloc_string_sub(t, "%P", connectpath);
635 t = realloc_string_sub(t, "%S", lp_servicename(snum));
638 t = realloc_string_sub(t, "%g", gidtoname(gid));
641 t = realloc_string_sub(t, "%u", user);
644 /* Patch from jkf@soton.ac.uk Left the %N (NIS
645 * server name) in standard_sub_basic as it is
646 * a feature for logon servers, hence uses the
647 * username. The %p (NIS server path) code is
648 * here as it is used instead of the default
649 * "path =" string in [homes] and so needs the
650 * service name, not the username. */
652 t = realloc_string_sub(t, "%p", automount_path(lp_servicename(snum)));
667 ret_string = alloc_sub_basic(smb_name, a_string);
672 /****************************************************************************
673 Do some standard substitutions in a string.
674 ****************************************************************************/
676 void standard_sub_conn(connection_struct *conn, char *str, size_t len)
678 standard_sub_advanced(SNUM(conn), conn->user, conn->connectpath,
679 conn->gid, current_user_info.smb_name, str, len);
682 char *talloc_sub_conn(TALLOC_CTX *mem_ctx, connection_struct *conn, char *str)
684 return talloc_sub_advanced(mem_ctx, SNUM(conn), conn->user,
685 conn->connectpath, conn->gid,
686 current_user_info.smb_name, str);
689 char *alloc_sub_conn(connection_struct *conn, char *str)
691 return alloc_sub_advanced(SNUM(conn), conn->user, conn->connectpath,
692 conn->gid, current_user_info.smb_name, str);
695 /****************************************************************************
696 Like standard_sub but by snum.
697 ****************************************************************************/
699 void standard_sub_snum(int snum, char *str, size_t len)
701 extern struct current_user current_user;
702 static uid_t cached_uid = -1;
703 static fstring cached_user;
704 /* calling uidtoname() on every substitute would be too expensive, so
705 we cache the result here as nearly every call is for the same uid */
707 if (cached_uid != current_user.uid) {
708 fstrcpy(cached_user, uidtoname(current_user.uid));
709 cached_uid = current_user.uid;
712 standard_sub_advanced(snum, cached_user, "", -1,
713 current_user_info.smb_name, str, len);