Patch to make automount lookup fallback to get home directory from getpwnam.
[samba.git] / source / 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 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="";
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                 char *home_path_start;
107                 char *automount_value = automount_lookup(user_name);
108
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);
115                         }
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 ));
121                 }
122         }
123 #endif
124
125         DEBUG(4,("Home server path: %s\n", server_path));
126
127         return server_path;
128 }
129
130
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)
137 {
138         static pstring server_name;
139
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);
143
144 #if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT))
145
146         if (lp_nis_home_map())
147         {
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))
153                 {
154                         home_server_len = sizeof(pstring);
155                 }
156                 strncpy(server_name, automount_value, home_server_len);
157                 server_name[home_server_len] = '\0';
158         }
159 #endif
160
161         DEBUG(4,("Home server: %s\n", server_name));
162
163         return server_name;
164 }
165
166 /****************************************************************************
167  Do some standard substitutions in a string.
168 ****************************************************************************/
169 void standard_sub_basic(char *str)
170 {
171         char *p, *s;
172         fstring pidstr;
173
174         for (s=str; (p=strchr(s, '%'));s=p) {
175                 int l = sizeof(pstring) - (int)(p-str);
176                 
177                 switch (*(p+1)) {
178                 case 'I' : string_sub(p,"%I", client_addr(),l); break;
179                 case 'L' : string_sub(p,"%L", local_machine,l); break;
180                 case 'M' : string_sub(p,"%M", client_name(),l); break;
181                 case 'R' : string_sub(p,"%R", remote_proto,l); break;
182                 case 'T' : string_sub(p,"%T", timestring(False),l); break;
183                 case 'a' : string_sub(p,"%a", remote_arch,l); break;
184                 case 'd' :
185                         slprintf(pidstr,sizeof(pidstr), "%d",(int)sys_getpid());
186                         string_sub(p,"%d", pidstr,l);
187                         break;
188                 case 'h' : string_sub(p,"%h", myhostname(),l); break;
189                 case 'm' : string_sub(p,"%m", remote_machine,l); break;
190                 case 'v' : string_sub(p,"%v", VERSION,l); break;
191                 case '$' : p += expand_env_var(p,l); break; /* Expand environment variables */
192                 case '\0': 
193                         p++; 
194                         break; /* don't run off the end of the string */
195                         
196                 default: p+=2; 
197                         break;
198                 }
199         }
200 }
201
202
203 /****************************************************************************
204  Do some standard substitutions in a string.
205 ****************************************************************************/
206 void standard_sub_advanced(int snum, char *user, char *connectpath, gid_t gid, char *str)
207 {
208         char *p, *s, *home;
209         struct passwd *pass;
210
211         for (s=str; (p=strchr(s, '%'));s=p) {
212                 int l = sizeof(pstring) - (int)(p-str);
213                 
214                 switch (*(p+1)) {
215                 case 'U' : string_sub(p,"%U",sam_logon_in_ssb?samlogon_user:current_user_info.smb_name,l); break;
216                 case 'G' :
217                         if ((pass = Get_Pwnam(user,False))!=NULL) {
218                                 string_sub(p,"%G",gidtoname(pass->pw_gid),l);
219                         } else {
220                                 p += 2;
221                         }
222                         break;
223                 case 'D' : string_sub(p,"%D", current_user_info.domain,l); break;
224                 case 'N' : string_sub(p,"%N", automount_server(user),l); break;
225                 case 'H':
226                         if ((home = get_user_home_dir(user))) {
227                                 string_sub(p,"%H",home, l);
228                         } else {
229                                 p += 2;
230                         }
231                         break;
232                 case 'P': 
233                         string_sub(p,"%P", connectpath, l); 
234                         break;
235                         
236                 case 'S': 
237                         string_sub(p,"%S", lp_servicename(snum), l); 
238                         break;
239                         
240                 case 'g': 
241                         string_sub(p,"%g", gidtoname(gid), l); 
242                         break;
243                 case 'u': 
244                         string_sub(p,"%u", user, l); 
245                         break;
246                         
247                         /* Patch from jkf@soton.ac.uk Left the %N (NIS
248                          * server name) in standard_sub_basic as it is
249                          * a feature for logon servers, hence uses the
250                          * username.  The %p (NIS server path) code is
251                          * here as it is used instead of the default
252                          * "path =" string in [homes] and so needs the
253                          * service name, not the username.  */
254                 case 'p': 
255                         string_sub(p,"%p", automount_path(lp_servicename(snum)), l); 
256                         break;
257                 case '\0': 
258                         p++; 
259                         break; /* don't run off the end of the string */
260                         
261                 default: p+=2; 
262                         break;
263                 }
264         }
265
266         standard_sub_basic(str);
267 }
268
269 /****************************************************************************
270  Do some standard substitutions in a string.
271 ****************************************************************************/
272 void standard_sub_conn(connection_struct *conn, char *str)
273 {
274         standard_sub_advanced(SNUM(conn), conn->user, conn->connectpath, conn->gid, str);
275 }
276
277 /****************************************************************************
278 like standard_sub but by snum
279 ****************************************************************************/
280 void standard_sub_snum(int snum, char *str)
281 {
282         extern struct current_user current_user;
283         static uid_t cached_uid = -1;
284         static fstring cached_user;
285         /* calling uidtoname() on every substitute would be too expensive, so
286            we cache the result here as nearly every call is for the same uid */
287
288         if (cached_uid != current_user.uid) {
289                 fstrcpy(cached_user, uidtoname(current_user.uid));
290                 cached_uid = current_user.uid;
291         }
292
293         standard_sub_advanced(snum, cached_user, "", -1, str);
294 }
295
296 /*******************************************************************
297  Substitute strings with useful parameters.
298 ********************************************************************/
299 void standard_sub_vuser(char *str, user_struct *vuser)
300 {
301         standard_sub_advanced(-1, vuser->user.unix_name, "", -1, str);
302 }
303
304 /*******************************************************************
305  Substitute strings with useful parameters.
306 ********************************************************************/
307 void standard_sub_vsnum(char *str, user_struct *vuser, int snum)
308 {
309         standard_sub_advanced(snum, vuser->user.unix_name, "", -1, str);
310 }
311