2 Unix SMB/Netbios implementation.
4 string substitution functions
5 Copyright (C) Andrew Tridgell 1992-2000
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 extern int DEBUGLEVEL
;
27 fstring local_machine
="";
28 fstring remote_arch
="UNKNOWN";
29 userdom_struct current_user_info
;
30 pstring samlogon_user
="";
31 BOOL sam_logon_in_ssb
= False
;
32 fstring remote_proto
="UNKNOWN";
33 fstring remote_machine
="";
36 /*******************************************************************
37 Given a pointer to a %$(NAME) expand it as an environment variable.
38 Return the number of characters by which the pointer should be advanced.
39 Based on code by Branko Cibej <branko.cibej@hermes.si>
40 When this is called p points at the '%' character.
41 ********************************************************************/
42 static size_t expand_env_var(char *p
, int len
)
56 * Look for the terminating ')'.
59 if ((q
= strchr(p
,')')) == NULL
) {
60 DEBUG(0,("expand_env_var: Unterminated environment variable [%s]\n", p
));
65 * Extract the name from within the %$(NAME) string.
69 copylen
= MIN((q
-r
),(sizeof(envname
)-1));
70 strncpy(envname
,r
,copylen
);
71 envname
[copylen
] = '\0';
73 if ((envval
= getenv(envname
)) == NULL
) {
74 DEBUG(0,("expand_env_var: Environment variable [%s] not set\n", envname
));
79 * Copy the full %$(NAME) into envname so it
83 copylen
= MIN((q
+1-p
),(sizeof(envname
)-1));
84 strncpy(envname
,p
,copylen
);
85 envname
[copylen
] = '\0';
86 string_sub(p
,envname
,envval
,len
);
87 return 0; /* Allow the environment contents to be parsed. */
90 /*******************************************************************
91 Patch from jkf@soton.ac.uk
92 Added this to implement %p (NIS auto-map version of %H)
93 *******************************************************************/
94 static char *automount_path(char *user_name
)
96 static pstring server_path
;
98 /* use the passwd entry as the default */
99 /* this will be the default if WITH_AUTOMOUNT is not used or fails */
100 /* pstrcpy() copes with get_user_home_dir() returning NULL */
101 pstrcpy(server_path
, get_user_home_dir(user_name
));
103 #if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT))
105 if (lp_nis_home_map()) {
106 char *home_path_start
;
107 char *automount_value
= automount_lookup(user_name
);
109 if(strlen(automount_value
) > 0) {
110 home_path_start
= strchr(automount_value
,':');
111 if (home_path_start
!= NULL
) {
112 DEBUG(5, ("NIS lookup succeeded. Home path is: %s\n",
113 home_path_start
?(home_path_start
+1):""));
114 pstrcpy(server_path
, home_path_start
+1);
117 /* NIS key lookup failed: default to user home directory from password file */
118 pstrcpy(server_path
, get_user_home_dir(user_name
));
119 DEBUG(5, ("NIS lookup failed. Using Home path from passwd file. Home path is: %s\n",
125 DEBUG(4,("Home server path: %s\n", server_path
));
131 /*******************************************************************
132 Patch from jkf@soton.ac.uk
133 This is Luke's original function with the NIS lookup code
134 moved out to a separate function.
135 *******************************************************************/
136 static char *automount_server(char *user_name
)
138 static pstring server_name
;
140 /* use the local machine name as the default */
141 /* this will be the default if WITH_AUTOMOUNT is not used or fails */
142 pstrcpy(server_name
, local_machine
);
144 #if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT))
146 if (lp_nis_home_map())
149 char *automount_value
= automount_lookup(user_name
);
150 home_server_len
= strcspn(automount_value
,":");
151 DEBUG(5, ("NIS lookup succeeded. Home server length: %d\n",home_server_len
));
152 if (home_server_len
> sizeof(pstring
))
154 home_server_len
= sizeof(pstring
);
156 strncpy(server_name
, automount_value
, home_server_len
);
157 server_name
[home_server_len
] = '\0';
161 DEBUG(4,("Home server: %s\n", server_name
));
166 /****************************************************************************
167 Do some standard substitutions in a string.
168 ****************************************************************************/
169 void standard_sub_basic(char *str
)
174 for (s
=str
; (p
=strchr(s
, '%'));s
=p
) {
177 int l
= sizeof(pstring
) - (int)(p
-str
);
181 fstrcpy(tmp_str
, sam_logon_in_ssb
?samlogon_user
:current_user_info
.smb_name
);
183 string_sub(p
,"%U",tmp_str
,l
);
186 fstrcpy(tmp_str
, current_user_info
.domain
);
188 string_sub(p
,"%D", tmp_str
,l
);
190 case 'I' : string_sub(p
,"%I", client_addr(),l
); break;
191 case 'L' : string_sub(p
,"%L", local_machine
,l
); break;
192 case 'M' : string_sub(p
,"%M", client_name(),l
); break;
193 case 'R' : string_sub(p
,"%R", remote_proto
,l
); break;
194 case 'T' : string_sub(p
,"%T", timestring(False
),l
); break;
195 case 'a' : string_sub(p
,"%a", remote_arch
,l
); break;
197 slprintf(pidstr
,sizeof(pidstr
)-1, "%d",(int)sys_getpid());
198 string_sub(p
,"%d", pidstr
,l
);
200 case 'h' : string_sub(p
,"%h", myhostname(),l
); break;
201 case 'm' : string_sub(p
,"%m", remote_machine
,l
); break;
202 case 'v' : string_sub(p
,"%v", VERSION
,l
); break;
203 case '$' : p
+= expand_env_var(p
,l
); break; /* Expand environment variables */
206 break; /* don't run off the end of the string */
215 /****************************************************************************
216 Do some standard substitutions in a string.
217 ****************************************************************************/
218 void standard_sub_advanced(int snum
, char *user
, char *connectpath
, gid_t gid
, char *str
)
223 for (s
=str
; (p
=strchr(s
, '%'));s
=p
) {
224 int l
= sizeof(pstring
) - (int)(p
-str
);
228 if ((pass
= Get_Pwnam(user
,False
))!=NULL
) {
229 string_sub(p
,"%G",gidtoname(pass
->pw_gid
),l
);
234 case 'N' : string_sub(p
,"%N", automount_server(user
),l
); break;
236 if ((home
= get_user_home_dir(user
))) {
237 string_sub(p
,"%H",home
, l
);
243 string_sub(p
,"%P", connectpath
, l
);
247 string_sub(p
,"%S", lp_servicename(snum
), l
);
251 string_sub(p
,"%g", gidtoname(gid
), l
);
254 string_sub(p
,"%u", user
, l
);
257 /* Patch from jkf@soton.ac.uk Left the %N (NIS
258 * server name) in standard_sub_basic as it is
259 * a feature for logon servers, hence uses the
260 * username. The %p (NIS server path) code is
261 * here as it is used instead of the default
262 * "path =" string in [homes] and so needs the
263 * service name, not the username. */
265 string_sub(p
,"%p", automount_path(lp_servicename(snum
)), l
);
269 break; /* don't run off the end of the string */
276 standard_sub_basic(str
);
279 /****************************************************************************
280 Do some standard substitutions in a string.
281 ****************************************************************************/
282 void standard_sub_conn(connection_struct
*conn
, char *str
)
284 standard_sub_advanced(SNUM(conn
), conn
->user
, conn
->connectpath
, conn
->gid
, str
);
287 /****************************************************************************
288 like standard_sub but by snum
289 ****************************************************************************/
290 void standard_sub_snum(int snum
, char *str
)
292 extern struct current_user current_user
;
293 static uid_t cached_uid
= -1;
294 static fstring cached_user
;
295 /* calling uidtoname() on every substitute would be too expensive, so
296 we cache the result here as nearly every call is for the same uid */
298 if (cached_uid
!= current_user
.uid
) {
299 fstrcpy(cached_user
, uidtoname(current_user
.uid
));
300 cached_uid
= current_user
.uid
;
303 standard_sub_advanced(snum
, cached_user
, "", -1, str
);
306 /*******************************************************************
307 Substitute strings with useful parameters.
308 ********************************************************************/
309 void standard_sub_vuser(char *str
, user_struct
*vuser
)
311 standard_sub_advanced(-1, vuser
->user
.unix_name
, "", -1, str
);
314 /*******************************************************************
315 Substitute strings with useful parameters.
316 ********************************************************************/
317 void standard_sub_vsnum(char *str
, user_struct
*vuser
, int snum
)
319 standard_sub_advanced(snum
, vuser
->user
.unix_name
, "", -1, str
);