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;
48 fstrcpy(tmp_local_machine,local_name);
49 trim_string(tmp_local_machine," "," ");
50 strlower(tmp_local_machine);
51 alpha_strcpy(local_machine,tmp_local_machine,SAFE_NETBIOS_CHARS,sizeof(local_machine)-1);
55 * Set the 'remote' machine name
56 * @param remote_name the name our client wants to be called by
57 * @param if this is the 'final' name for them, not be be changed again
60 void set_remote_machine_name(const char* remote_name, BOOL perm)
62 static BOOL already_perm = False;
63 fstring tmp_remote_machine;
70 fstrcpy(tmp_remote_machine,remote_name);
71 trim_string(tmp_remote_machine," "," ");
72 strlower(tmp_remote_machine);
73 alpha_strcpy(remote_machine,tmp_remote_machine,SAFE_NETBIOS_CHARS,sizeof(remote_machine)-1);
76 const char* get_remote_machine_name(void)
78 return remote_machine;
81 const char* get_local_machine_name(void)
83 if (!*local_machine) {
84 return global_myname();
92 setup the string used by %U substitution
94 void sub_set_smb_name(const char *name)
98 /* don't let anonymous logins override the name */
102 trim_string(tmp," "," ");
104 alpha_strcpy(smb_user_name,tmp,SAFE_NETBIOS_CHARS,sizeof(smb_user_name)-1);
108 /*******************************************************************
109 Given a pointer to a %$(NAME) expand it as an environment variable.
110 Return the number of characters by which the pointer should be advanced.
111 Based on code by Branko Cibej <branko.cibej@hermes.si>
112 When this is called p points at the '%' character.
113 ********************************************************************/
115 static size_t expand_env_var(char *p, int len)
129 * Look for the terminating ')'.
132 if ((q = strchr_m(p,')')) == NULL) {
133 DEBUG(0,("expand_env_var: Unterminated environment variable [%s]\n", p));
138 * Extract the name from within the %$(NAME) string.
142 copylen = MIN((q-r),(sizeof(envname)-1));
143 strncpy(envname,r,copylen);
144 envname[copylen] = '\0';
146 if ((envval = getenv(envname)) == NULL) {
147 DEBUG(0,("expand_env_var: Environment variable [%s] not set\n", envname));
152 * Copy the full %$(NAME) into envname so it
156 copylen = MIN((q+1-p),(sizeof(envname)-1));
157 strncpy(envname,p,copylen);
158 envname[copylen] = '\0';
159 string_sub(p,envname,envval,len);
160 return 0; /* Allow the environment contents to be parsed. */
163 /*******************************************************************
164 Given a pointer to a %$(NAME) in p and the whole string in str
165 expand it as an environment variable.
166 Return a new allocated and expanded string.
167 Based on code by Branko Cibej <branko.cibej@hermes.si>
168 When this is called p points at the '%' character.
169 May substitute multiple occurrencies of the same env var.
170 ********************************************************************/
173 static char * realloc_expand_env_var(char *str, char *p)
180 if (p[0] != '%' || p[1] != '$' || p[2] != '(')
184 * Look for the terminating ')'.
187 if ((q = strchr_m(p,')')) == NULL) {
188 DEBUG(0,("expand_env_var: Unterminated environment variable [%s]\n", p));
193 * Extract the name from within the %$(NAME) string.
198 envname = (char *)malloc(copylen + 1 + 4); /* reserve space for use later add %$() chars */
199 if (envname == NULL) return NULL;
200 strncpy(envname,r,copylen);
201 envname[copylen] = '\0';
203 if ((envval = getenv(envname)) == NULL) {
204 DEBUG(0,("expand_env_var: Environment variable [%s] not set\n", envname));
210 * Copy the full %$(NAME) into envname so it
215 strncpy(envname,p,copylen);
216 envname[copylen] = '\0';
217 r = realloc_string_sub(str, envname, envval);
219 if (r == NULL) return NULL;
223 /*******************************************************************
224 Patch from jkf@soton.ac.uk
225 Added this to implement %p (NIS auto-map version of %H)
226 *******************************************************************/
228 static char *automount_path(const char *user_name)
230 static pstring server_path;
232 /* use the passwd entry as the default */
233 /* this will be the default if WITH_AUTOMOUNT is not used or fails */
235 pstrcpy(server_path, get_user_home_dir(user_name));
237 #if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT))
239 if (lp_nis_home_map()) {
240 char *home_path_start;
241 char *automount_value = automount_lookup(user_name);
243 if(strlen(automount_value) > 0) {
244 home_path_start = strchr_m(automount_value,':');
245 if (home_path_start != NULL) {
246 DEBUG(5, ("NIS lookup succeeded. Home path is: %s\n",
247 home_path_start?(home_path_start+1):""));
248 pstrcpy(server_path, home_path_start+1);
251 /* NIS key lookup failed: default to user home directory from password file */
252 DEBUG(5, ("NIS lookup failed. Using Home path from passwd file. Home path is: %s\n", server_path ));
257 DEBUG(4,("Home server path: %s\n", server_path));
262 /*******************************************************************
263 Patch from jkf@soton.ac.uk
264 This is Luke's original function with the NIS lookup code
265 moved out to a separate function.
266 *******************************************************************/
268 static const char *automount_server(const char *user_name)
270 static pstring server_name;
271 const char *local_machine_name = get_local_machine_name();
273 /* use the local machine name as the default */
274 /* this will be the default if WITH_AUTOMOUNT is not used or fails */
275 if (local_machine_name && *local_machine_name)
276 pstrcpy(server_name, local_machine_name);
278 pstrcpy(server_name, global_myname());
280 #if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT))
282 if (lp_nis_home_map()) {
284 char *automount_value = automount_lookup(user_name);
285 home_server_len = strcspn(automount_value,":");
286 DEBUG(5, ("NIS lookup succeeded. Home server length: %d\n",home_server_len));
287 if (home_server_len > sizeof(pstring))
288 home_server_len = sizeof(pstring);
289 strncpy(server_name, automount_value, home_server_len);
290 server_name[home_server_len] = '\0';
294 DEBUG(4,("Home server: %s\n", server_name));
299 /****************************************************************************
300 Do some standard substitutions in a string.
301 len is the length in bytes of the space allowed in string str. If zero means
302 don't allow expansions.
303 ****************************************************************************/
305 void standard_sub_basic(const char *smb_name, char *str,size_t len)
310 const char *local_machine_name = get_local_machine_name();
312 for (s=str; (p=strchr_m(s, '%'));s=p) {
315 int l = (int)len - (int)(p-str);
322 fstrcpy(tmp_str, smb_name);
324 string_sub(p,"%U",tmp_str,l);
327 fstrcpy(tmp_str, smb_name);
328 if ((pass = Get_Pwnam(tmp_str))!=NULL) {
329 string_sub(p,"%G",gidtoname(pass->pw_gid),l);
335 fstrcpy(tmp_str, current_user_info.domain);
337 string_sub(p,"%D", tmp_str,l);
340 string_sub(p,"%I", client_addr(),l);
343 if (local_machine_name && *local_machine_name)
344 string_sub(p,"%L", local_machine_name,l);
348 pstrcpy(temp_name, global_myname());
350 string_sub(p,"%L", temp_name,l);
354 string_sub(p,"%M", client_name(),l);
357 string_sub(p,"%R", remote_proto,l);
360 string_sub(p,"%T", timestring(False),l);
363 string_sub(p,"%a", remote_arch,l);
366 slprintf(pidstr,sizeof(pidstr)-1, "%d",(int)sys_getpid());
367 string_sub(p,"%d", pidstr,l);
370 string_sub(p,"%h", myhostname(),l);
373 string_sub(p,"%m", get_remote_machine_name(),l);
376 string_sub(p,"%v", VERSION,l);
379 p += expand_env_var(p,l);
380 break; /* Expand environment variables */
383 break; /* don't run off the end of the string */
391 static void standard_sub_advanced(int snum, const char *user,
392 const char *connectpath, gid_t gid,
393 const char *smb_name, char *str, size_t len)
397 for (s=str; (p=strchr_m(s, '%'));s=p) {
398 int l = (int)len - (int)(p-str);
405 string_sub(p,"%N", automount_server(user),l);
408 if ((home = get_user_home_dir(user)))
409 string_sub(p,"%H",home, l);
414 string_sub(p,"%P", connectpath, l);
417 string_sub(p,"%S", lp_servicename(snum), l);
420 string_sub(p,"%g", gidtoname(gid), l);
423 string_sub(p,"%u", user, l);
426 /* Patch from jkf@soton.ac.uk Left the %N (NIS
427 * server name) in standard_sub_basic as it is
428 * a feature for logon servers, hence uses the
429 * username. The %p (NIS server path) code is
430 * here as it is used instead of the default
431 * "path =" string in [homes] and so needs the
432 * service name, not the username. */
434 string_sub(p,"%p", automount_path(lp_servicename(snum)), l);
438 break; /* don't run off the end of the string */
445 standard_sub_basic(smb_name, str, len);
448 /****************************************************************************
449 Do some standard substitutions in a string.
450 This function will return an allocated string that have to be freed.
451 ****************************************************************************/
453 char *talloc_sub_basic(TALLOC_CTX *mem_ctx, const char *smb_name, const char *str)
456 a = alloc_sub_basic(smb_name, str);
458 t = talloc_strdup(mem_ctx, a);
463 char *alloc_sub_basic(const char *smb_name, const char *str)
465 char *b, *p, *s, *t, *r, *a_string;
468 const char *local_machine_name = get_local_machine_name();
470 a_string = strdup(str);
471 if (a_string == NULL) {
472 DEBUG(0, ("alloc_sub_specified: Out of memory!\n"));
476 for (b = s = a_string; (p = strchr_m(s, '%')); s = a_string + (p - b)) {
483 r = strdup_lower(smb_name);
484 if (r == NULL) goto error;
485 t = realloc_string_sub(t, "%U", r);
488 r = strdup(smb_name);
489 if (r == NULL) goto error;
490 if ((pass = Get_Pwnam(r))!=NULL) {
491 t = realloc_string_sub(t, "%G", gidtoname(pass->pw_gid));
495 r = strdup_upper(current_user_info.domain);
496 if (r == NULL) goto error;
497 t = realloc_string_sub(t, "%D", r);
500 t = realloc_string_sub(t, "%I", client_addr());
503 if (local_machine_name && *local_machine_name)
504 t = realloc_string_sub(t, "%L", local_machine_name);
506 t = realloc_string_sub(t, "%L", global_myname());
509 t = realloc_string_sub(t, "%M", client_name());
512 t = realloc_string_sub(t, "%R", remote_proto);
515 t = realloc_string_sub(t, "%T", timestring(False));
518 t = realloc_string_sub(t, "%a", remote_arch);
521 slprintf(pidstr,sizeof(pidstr)-1, "%d",(int)sys_getpid());
522 t = realloc_string_sub(t, "%d", pidstr);
525 t = realloc_string_sub(t, "%h", myhostname());
528 t = realloc_string_sub(t, "%m", remote_machine);
531 t = realloc_string_sub(t, "%v", VERSION);
534 t = realloc_expand_env_var(t, p); /* Expand environment variables */
543 if (t == NULL) goto error;
553 /****************************************************************************
554 Do some specific substitutions in a string.
555 This function will return an allocated string that have to be freed.
556 ****************************************************************************/
558 char *talloc_sub_specified(TALLOC_CTX *mem_ctx,
559 const char *input_string,
560 const char *username,
566 a = alloc_sub_specified(input_string, username, domain, uid, gid);
568 t = talloc_strdup(mem_ctx, a);
573 char *alloc_sub_specified(const char *input_string,
574 const char *username,
579 char *a_string, *ret_string;
582 a_string = strdup(input_string);
583 if (a_string == NULL) {
584 DEBUG(0, ("alloc_sub_specified: Out of memory!\n"));
588 for (b = s = a_string; (p = strchr_m(s, '%')); s = a_string + (p - b)) {
594 t = realloc_string_sub(t, "%U", username);
597 t = realloc_string_sub(t, "%u", username);
601 t = realloc_string_sub(t, "%G", gidtoname(gid));
603 t = realloc_string_sub(t, "%G", "NO_GROUP");
608 t = realloc_string_sub(t, "%g", gidtoname(gid));
610 t = realloc_string_sub(t, "%g", "NO_GROUP");
614 t = realloc_string_sub(t, "%D", domain);
617 t = realloc_string_sub(t, "%N", automount_server(username));
631 ret_string = alloc_sub_basic(username, a_string);
636 char *talloc_sub_advanced(TALLOC_CTX *mem_ctx,
639 const char *connectpath,
641 const char *smb_name,
645 a = alloc_sub_advanced(snum, user, connectpath, gid, smb_name, str);
647 t = talloc_strdup(mem_ctx, a);
652 char *alloc_sub_advanced(int snum, const char *user,
653 const char *connectpath, gid_t gid,
654 const char *smb_name, char *str)
656 char *a_string, *ret_string;
657 char *b, *p, *s, *t, *h;
659 a_string = strdup(str);
660 if (a_string == NULL) {
661 DEBUG(0, ("alloc_sub_specified: Out of memory!\n"));
665 for (b = s = a_string; (p = strchr_m(s, '%')); s = a_string + (p - b)) {
671 t = realloc_string_sub(t, "%N", automount_server(user));
674 if ((h = get_user_home_dir(user)))
675 t = realloc_string_sub(t, "%H", h);
678 t = realloc_string_sub(t, "%P", connectpath);
681 t = realloc_string_sub(t, "%S", lp_servicename(snum));
684 t = realloc_string_sub(t, "%g", gidtoname(gid));
687 t = realloc_string_sub(t, "%u", user);
690 /* Patch from jkf@soton.ac.uk Left the %N (NIS
691 * server name) in standard_sub_basic as it is
692 * a feature for logon servers, hence uses the
693 * username. The %p (NIS server path) code is
694 * here as it is used instead of the default
695 * "path =" string in [homes] and so needs the
696 * service name, not the username. */
698 t = realloc_string_sub(t, "%p", automount_path(lp_servicename(snum)));
713 ret_string = alloc_sub_basic(smb_name, a_string);
718 /****************************************************************************
719 Do some standard substitutions in a string.
720 ****************************************************************************/
722 void standard_sub_conn(connection_struct *conn, char *str, size_t len)
724 standard_sub_advanced(SNUM(conn), conn->user, conn->connectpath,
725 conn->gid, smb_user_name, str, len);
728 char *talloc_sub_conn(TALLOC_CTX *mem_ctx, connection_struct *conn, char *str)
730 return talloc_sub_advanced(mem_ctx, SNUM(conn), conn->user,
731 conn->connectpath, conn->gid,
735 char *alloc_sub_conn(connection_struct *conn, char *str)
737 return alloc_sub_advanced(SNUM(conn), conn->user, conn->connectpath,
738 conn->gid, smb_user_name, str);
741 /****************************************************************************
742 Like standard_sub but by snum.
743 ****************************************************************************/
745 void standard_sub_snum(int snum, char *str, size_t len)
747 extern struct current_user current_user;
748 static uid_t cached_uid = -1;
749 static fstring cached_user;
750 /* calling uidtoname() on every substitute would be too expensive, so
751 we cache the result here as nearly every call is for the same uid */
753 if (cached_uid != current_user.uid) {
754 fstrcpy(cached_user, uidtoname(current_user.uid));
755 cached_uid = current_user.uid;
758 standard_sub_advanced(snum, cached_user, "", -1,
759 smb_user_name, str, len);