large collection of minor fixes. Mostly typos
[Samba.git] / source / lib / substitute.c
blob25be4b030f714d0790e0712d56628e396be054f8
1 /*
2 Unix SMB/Netbios implementation.
3 Version 3.0
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.
23 #include "includes.h"
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)
44 fstring envname;
45 char *envval;
46 char *q, *r;
47 int copylen;
49 if (p[1] != '$')
50 return 1;
52 if (p[2] != '(')
53 return 2;
56 * Look for the terminating ')'.
59 if ((q = strchr(p,')')) == NULL) {
60 DEBUG(0,("expand_env_var: Unterminated environment variable [%s]\n", p));
61 return 2;
65 * Extract the name from within the %$(NAME) string.
68 r = p+3;
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));
75 return 2;
79 * Copy the full %$(NAME) into envname so it
80 * can be replaced.
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);
116 } else {
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",
120 server_path ));
123 #endif
125 DEBUG(4,("Home server path: %s\n", server_path));
127 return 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())
148 int home_server_len;
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';
159 #endif
161 DEBUG(4,("Home server: %s\n", server_name));
163 return server_name;
166 /****************************************************************************
167 Do some standard substitutions in a string.
168 ****************************************************************************/
169 void standard_sub_basic(char *str)
171 char *p, *s;
172 fstring pidstr;
174 for (s=str; (p=strchr(s, '%'));s=p) {
175 fstring tmp_str;
177 int l = sizeof(pstring) - (int)(p-str);
179 switch (*(p+1)) {
180 case 'U' :
181 fstrcpy(tmp_str, sam_logon_in_ssb?samlogon_user:current_user_info.smb_name);
182 strlower(tmp_str);
183 string_sub(p,"%U",tmp_str,l);
184 break;
185 case 'D' :
186 fstrcpy(tmp_str, current_user_info.domain);
187 strupper(tmp_str);
188 string_sub(p,"%D", tmp_str,l);
189 break;
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;
196 case 'd' :
197 slprintf(pidstr,sizeof(pidstr)-1, "%d",(int)sys_getpid());
198 string_sub(p,"%d", pidstr,l);
199 break;
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 */
204 case '\0':
205 p++;
206 break; /* don't run off the end of the string */
208 default: p+=2;
209 break;
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)
220 char *p, *s, *home;
221 struct passwd *pass;
223 for (s=str; (p=strchr(s, '%'));s=p) {
224 int l = sizeof(pstring) - (int)(p-str);
226 switch (*(p+1)) {
227 case 'G' :
228 if ((pass = Get_Pwnam(user,False))!=NULL) {
229 string_sub(p,"%G",gidtoname(pass->pw_gid),l);
230 } else {
231 p += 2;
233 break;
234 case 'N' : string_sub(p,"%N", automount_server(user),l); break;
235 case 'H':
236 if ((home = get_user_home_dir(user))) {
237 string_sub(p,"%H",home, l);
238 } else {
239 p += 2;
241 break;
242 case 'P':
243 string_sub(p,"%P", connectpath, l);
244 break;
246 case 'S':
247 string_sub(p,"%S", lp_servicename(snum), l);
248 break;
250 case 'g':
251 string_sub(p,"%g", gidtoname(gid), l);
252 break;
253 case 'u':
254 string_sub(p,"%u", user, l);
255 break;
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. */
264 case 'p':
265 string_sub(p,"%p", automount_path(lp_servicename(snum)), l);
266 break;
267 case '\0':
268 p++;
269 break; /* don't run off the end of the string */
271 default: p+=2;
272 break;
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);