Herb's warning fixes. Also the POSIX locking fix.
[samba.git] / source3 / lib / substitute.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 3.0
4    string substitution functions
5    Copyright (C) Andrew Tridgell 1992-2000
6    
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.
11    
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.
16    
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.
20 */
21
22
23 #include "includes.h"
24
25 extern int DEBUGLEVEL;
26
27 fstring local_machine="";
28 fstring remote_arch="UNKNOWN";
29 pstring sesssetup_user="";
30 pstring samlogon_user="";
31 BOOL sam_logon_in_ssb = False;
32 fstring remote_proto="UNKNOWN";
33 fstring remote_machine="";
34
35
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)
43 {
44         fstring envname;
45         char *envval;
46         char *q, *r;
47         int copylen;
48
49         if (p[1] != '$')
50                 return 1;
51
52         if (p[2] != '(')
53                 return 2;
54
55         /*
56          * Look for the terminating ')'.
57          */
58
59         if ((q = strchr(p,')')) == NULL) {
60                 DEBUG(0,("expand_env_var: Unterminated environment variable [%s]\n", p));
61                 return 2;
62         }
63
64         /*
65          * Extract the name from within the %$(NAME) string.
66          */
67
68         r = p+3;
69         copylen = MIN((q-r),(sizeof(envname)-1));
70         strncpy(envname,r,copylen);
71         envname[copylen] = '\0';
72
73         if ((envval = getenv(envname)) == NULL) {
74                 DEBUG(0,("expand_env_var: Environment variable [%s] not set\n", envname));
75                 return 2;
76         }
77
78         /*
79          * Copy the full %$(NAME) into envname so it
80          * can be replaced.
81          */
82
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. */
88 }
89
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)
95 {
96         static pstring server_path;
97
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));
102
103 #if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT))
104
105         if (lp_nis_home_map())
106         {
107                 char *home_path_start;
108                 char *automount_value = automount_lookup(user_name);
109                 home_path_start = strchr(automount_value,':');
110                 if (home_path_start != NULL)
111                 {
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);
115                 }
116         }
117 #endif
118
119         DEBUG(4,("Home server path: %s\n", server_path));
120
121         return server_path;
122 }
123
124
125 /*******************************************************************
126  Patch from jkf@soton.ac.uk
127  This is Luke's original function with the NIS lookup code
128  moved out to a separate function.
129 *******************************************************************/
130 static char *automount_server(char *user_name)
131 {
132         static pstring server_name;
133
134         /* use the local machine name as the default */
135         /* this will be the default if WITH_AUTOMOUNT is not used or fails */
136         pstrcpy(server_name, local_machine);
137
138 #if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT))
139
140         if (lp_nis_home_map())
141         {
142                 int home_server_len;
143                 char *automount_value = automount_lookup(user_name);
144                 home_server_len = strcspn(automount_value,":");
145                 DEBUG(5, ("NIS lookup succeeded.  Home server length: %d\n",home_server_len));
146                 if (home_server_len > sizeof(pstring))
147                 {
148                         home_server_len = sizeof(pstring);
149                 }
150                 strncpy(server_name, automount_value, home_server_len);
151                 server_name[home_server_len] = '\0';
152         }
153 #endif
154
155         DEBUG(4,("Home server: %s\n", server_name));
156
157         return server_name;
158 }
159
160 /****************************************************************************
161  Do some standard substitutions in a string.
162 ****************************************************************************/
163 void standard_sub_basic(char *str)
164 {
165         char *p, *s;
166         fstring pidstr;
167
168         for (s=str; (p=strchr(s, '%'));s=p) {
169                 int l = sizeof(pstring) - (int)(p-str);
170                 
171                 switch (*(p+1)) {
172                 case 'I' : string_sub(p,"%I", client_addr(),l); break;
173                 case 'L' : string_sub(p,"%L", local_machine,l); break;
174                 case 'M' : string_sub(p,"%M", client_name(),l); break;
175                 case 'R' : string_sub(p,"%R", remote_proto,l); break;
176                 case 'T' : string_sub(p,"%T", timestring(False),l); break;
177                 case 'a' : string_sub(p,"%a", remote_arch,l); break;
178                 case 'd' :
179                         slprintf(pidstr,sizeof(pidstr), "%d",(int)sys_getpid());
180                         string_sub(p,"%d", pidstr,l);
181                         break;
182                 case 'h' : string_sub(p,"%h", myhostname(),l); break;
183                 case 'm' : string_sub(p,"%m", remote_machine,l); break;
184                 case 'v' : string_sub(p,"%v", VERSION,l); break;
185                 case '$' : p += expand_env_var(p,l); break; /* Expand environment variables */
186                 case '\0': 
187                         p++; 
188                         break; /* don't run off the end of the string */
189                         
190                 default: p+=2; 
191                         break;
192                 }
193         }
194 }
195
196
197 /****************************************************************************
198  Do some standard substitutions in a string.
199 ****************************************************************************/
200 void standard_sub_advanced(int snum, char *user, char *connectpath, gid_t gid, char *str)
201 {
202         char *p, *s, *home;
203         struct passwd *pass;
204
205         for (s=str; (p=strchr(s, '%'));s=p) {
206                 int l = sizeof(pstring) - (int)(p-str);
207                 
208                 switch (*(p+1)) {
209                 case 'U' : string_sub(p,"%U",sam_logon_in_ssb?samlogon_user:sesssetup_user,l); break;
210                 case 'G' :
211                         if ((pass = Get_Pwnam(user,False))!=NULL) {
212                                 string_sub(p,"%G",gidtoname(pass->pw_gid),l);
213                         } else {
214                                 p += 2;
215                         }
216                         break;
217                 case 'N' : string_sub(p,"%N", automount_server(user),l); break;
218                 case 'H':
219                         if ((home = get_user_home_dir(user))) {
220                                 string_sub(p,"%H",home, l);
221                         } else {
222                                 p += 2;
223                         }
224                         break;
225                 case 'P': 
226                         string_sub(p,"%P", connectpath, l); 
227                         break;
228                         
229                 case 'S': 
230                         string_sub(p,"%S", lp_servicename(snum), l); 
231                         break;
232                         
233                 case 'g': 
234                         string_sub(p,"%g", gidtoname(gid), l); 
235                         break;
236                 case 'u': 
237                         string_sub(p,"%u", user, l); 
238                         break;
239                         
240                         /* Patch from jkf@soton.ac.uk Left the %N (NIS
241                          * server name) in standard_sub_basic as it is
242                          * a feature for logon servers, hence uses the
243                          * username.  The %p (NIS server path) code is
244                          * here as it is used instead of the default
245                          * "path =" string in [homes] and so needs the
246                          * service name, not the username.  */
247                 case 'p': 
248                         string_sub(p,"%p", automount_path(lp_servicename(snum)), l); 
249                         break;
250                 case '\0': 
251                         p++; 
252                         break; /* don't run off the end of the string */
253                         
254                 default: p+=2; 
255                         break;
256                 }
257         }
258
259         standard_sub_basic(str);
260 }
261
262 /****************************************************************************
263  Do some standard substitutions in a string.
264 ****************************************************************************/
265 void standard_sub_conn(connection_struct *conn, char *str)
266 {
267         standard_sub_advanced(SNUM(conn), conn->user, conn->connectpath, conn->gid, str);
268 }
269
270 /****************************************************************************
271 like standard_sub but by snum
272 ****************************************************************************/
273 void standard_sub_snum(int snum, char *str)
274 {
275         extern struct current_user current_user;
276         static uid_t cached_uid = -1;
277         static fstring cached_user;
278         /* calling uidtoname() on every substitute would be too expensive, so
279            we cache the result here as nearly every call is for the same uid */
280
281         if (cached_uid != current_user.uid) {
282                 fstrcpy(cached_user, uidtoname(current_user.uid));
283                 cached_uid = current_user.uid;
284         }
285
286         standard_sub_advanced(snum, cached_user, "", -1, str);
287 }
288
289 /*******************************************************************
290  Substitute strings with useful parameters.
291 ********************************************************************/
292 void standard_sub_vuser(char *str, user_struct *vuser)
293 {
294         standard_sub_advanced(-1, vuser->user.unix_name, "", -1, str);
295 }
296
297 /*******************************************************************
298  Substitute strings with useful parameters.
299 ********************************************************************/
300 void standard_sub_vsnum(char *str, user_struct *vuser, int snum)
301 {
302         standard_sub_advanced(snum, vuser->user.unix_name, "", -1, str);
303 }
304