don't allow ".." in service name when doing "default service"
[samba.git] / source3 / smbd / service.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    service (connection) opening and closing
5    Copyright (C) Andrew Tridgell 1992-1998
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 #include "includes.h"
23
24 extern int DEBUGLEVEL;
25
26 extern time_t smb_last_time;
27 extern int case_default;
28 extern BOOL case_preserve;
29 extern BOOL short_case_preserve;
30 extern BOOL case_mangle;
31 extern BOOL case_sensitive;
32 extern BOOL use_mangled_map;
33 extern fstring remote_machine;
34 extern pstring sesssetup_user;
35 extern fstring remote_machine;
36
37
38 /****************************************************************************
39 load parameters specific to a connection/service
40 ****************************************************************************/
41 BOOL become_service(connection_struct *conn,BOOL do_chdir)
42 {
43         extern char magic_char;
44         static connection_struct *last_conn;
45         int snum;
46
47         if (!conn)  {
48                 last_conn = NULL;
49                 return(False);
50         }
51
52         conn->lastused = smb_last_time;
53
54         snum = SNUM(conn);
55   
56         if (do_chdir &&
57             dos_ChDir(conn->connectpath) != 0 &&
58             dos_ChDir(conn->origpath) != 0) {
59                 DEBUG(0,("chdir (%s) failed\n",
60                          conn->connectpath));
61                 return(False);
62         }
63
64         if (conn == last_conn)
65                 return(True);
66
67         last_conn = conn;
68
69         case_default = lp_defaultcase(snum);
70         case_preserve = lp_preservecase(snum);
71         short_case_preserve = lp_shortpreservecase(snum);
72         case_mangle = lp_casemangle(snum);
73         case_sensitive = lp_casesensitive(snum);
74         magic_char = lp_magicchar(snum);
75         use_mangled_map = (*lp_mangled_map(snum) ? True:False);
76         return(True);
77 }
78
79
80 /****************************************************************************
81   find a service entry
82 ****************************************************************************/
83 int find_service(char *service)
84 {
85    int iService;
86
87    string_sub(service,"\\","/");
88
89    iService = lp_servicenumber(service);
90
91    /* now handle the special case of a home directory */
92    if (iService < 0)
93    {
94       char *phome_dir = get_home_dir(service);
95
96       if(!phome_dir)
97       {
98         /*
99          * Try mapping the servicename, it may
100          * be a Windows to unix mapped user name.
101          */
102         if(map_username(service))
103           phome_dir = get_home_dir(service);
104       }
105
106       DEBUG(3,("checking for home directory %s gave %s\n",service,
107             phome_dir?phome_dir:"(NULL)"));
108
109       if (phome_dir)
110       {   
111         int iHomeService;
112         if ((iHomeService = lp_servicenumber(HOMES_NAME)) >= 0)
113         {
114           lp_add_home(service,iHomeService,phome_dir);
115           iService = lp_servicenumber(service);
116         }
117       }
118    }
119
120    /* If we still don't have a service, attempt to add it as a printer. */
121    if (iService < 0)
122    {
123       int iPrinterService;
124
125       if ((iPrinterService = lp_servicenumber(PRINTERS_NAME)) >= 0)
126       {
127          char *pszTemp;
128
129          DEBUG(3,("checking whether %s is a valid printer name...\n", service));
130          pszTemp = PRINTCAP;
131          if ((pszTemp != NULL) && pcap_printername_ok(service, pszTemp))
132          {
133             DEBUG(3,("%s is a valid printer name\n", service));
134             DEBUG(3,("adding %s as a printer service\n", service));
135             lp_add_printer(service,iPrinterService);
136             iService = lp_servicenumber(service);
137             if (iService < 0)
138                DEBUG(0,("failed to add %s as a printer service!\n", service));
139          }
140          else
141             DEBUG(3,("%s is not a valid printer name\n", service));
142       }
143    }
144
145    /* just possibly it's a default service? */
146    if (iService < 0) 
147    {
148      char *pdefservice = lp_defaultservice();
149      if (pdefservice && *pdefservice && 
150          !strequal(pdefservice,service) &&
151          !strstr(service,".."))
152      {
153        /*
154         * We need to do a local copy here as lp_defaultservice() 
155         * returns one of the rotating lp_string buffers that
156         * could get overwritten by the recursive find_service() call
157         * below. Fix from Josef Hinteregger <joehtg@joehtg.co.at>.
158         */
159        pstring defservice;
160        pstrcpy(defservice, pdefservice);
161        iService = find_service(defservice);
162        if (iService >= 0)
163        {
164          string_sub(service,"_","/");
165          iService = lp_add_service(service,iService);
166        }
167      }
168    }
169
170    if (iService >= 0)
171      if (!VALID_SNUM(iService))
172      {
173        DEBUG(0,("Invalid snum %d for %s\n",iService,service));
174        iService = -1;
175      }
176
177    if (iService < 0)
178      DEBUG(3,("find_service() failed to find service %s\n", service));
179
180    return (iService);
181 }
182
183
184 /****************************************************************************
185   make a connection to a service
186 ****************************************************************************/
187 connection_struct *make_connection(char *service,char *user,char *password, int pwlen, char *dev,uint16 vuid, int *ecode)
188 {
189         int snum;
190         struct passwd *pass = NULL;
191         BOOL guest = False;
192         BOOL force = False;
193         extern int Client;
194         connection_struct *conn;
195
196         strlower(service);
197
198         snum = find_service(service);
199         if (snum < 0) {
200                 extern int Client;
201                 if (strequal(service,"IPC$")) {
202                         DEBUG(3,("refusing IPC connection\n"));
203                         *ecode = ERRnoipc;
204                         return NULL;
205                 }
206
207                 DEBUG(0,("%s (%s) couldn't find service %s\n",
208                          remote_machine, client_addr(Client), service));
209                 *ecode = ERRinvnetname;
210                 return NULL;
211         }
212
213         if (strequal(service,HOMES_NAME)) {
214                 if (*user && Get_Pwnam(user,True))
215                         return(make_connection(user,user,password,
216                                                pwlen,dev,vuid,ecode));
217
218                 if(lp_security() != SEC_SHARE) {
219                         if (validated_username(vuid)) {
220                                 pstrcpy(user,validated_username(vuid));
221                                 return(make_connection(user,user,password,pwlen,dev,vuid,ecode));
222                         }
223                 } else {
224                         /* Security = share. Try with sesssetup_user
225                          * as the username.  */
226                         if(*sesssetup_user) {
227                                 pstrcpy(user,sesssetup_user);
228                                 return(make_connection(user,user,password,pwlen,dev,vuid,ecode));
229                         }
230                 }
231         }
232
233         if (!lp_snum_ok(snum) || 
234             !check_access(Client, 
235                           lp_hostsallow(snum), lp_hostsdeny(snum))) {    
236                 *ecode = ERRaccess;
237                 return NULL;
238         }
239
240         /* you can only connect to the IPC$ service as an ipc device */
241         if (strequal(service,"IPC$"))
242                 pstrcpy(dev,"IPC");
243         
244         if (*dev == '?' || !*dev) {
245                 if (lp_print_ok(snum)) {
246                         pstrcpy(dev,"LPT1:");
247                 } else {
248                         pstrcpy(dev,"A:");
249                 }
250         }
251
252         /* if the request is as a printer and you can't print then refuse */
253         strupper(dev);
254         if (!lp_print_ok(snum) && (strncmp(dev,"LPT",3) == 0)) {
255                 DEBUG(1,("Attempt to connect to non-printer as a printer\n"));
256                 *ecode = ERRinvdevice;
257                 return NULL;
258         }
259
260         /* lowercase the user name */
261         strlower(user);
262
263         /* add it as a possible user name */
264         add_session_user(service);
265
266         /* shall we let them in? */
267         if (!authorise_login(snum,user,password,pwlen,&guest,&force,vuid)) {
268                 DEBUG( 2, ( "Invalid username/password for %s\n", service ) );
269                 *ecode = ERRbadpw;
270                 return NULL;
271         }
272   
273         conn = conn_new();
274         if (!conn) {
275                 DEBUG(0,("Couldn't find free connection.\n"));
276                 *ecode = ERRnoresource;
277                 conn_free(conn);
278                 return NULL;
279         }
280
281         /* find out some info about the user */
282         pass = Get_Pwnam(user,True);
283
284         if (pass == NULL) {
285                 DEBUG(0,( "Couldn't find account %s\n",user));
286                 *ecode = ERRbaduid;
287                 conn_free(conn);
288                 return NULL;
289         }
290
291         conn->read_only = lp_readonly(snum);
292
293         {
294                 pstring list;
295                 StrnCpy(list,lp_readlist(snum),sizeof(pstring)-1);
296                 string_sub(list,"%S",service);
297
298                 if (user_in_list(user,list))
299                         conn->read_only = True;
300                 
301                 StrnCpy(list,lp_writelist(snum),sizeof(pstring)-1);
302                 string_sub(list,"%S",service);
303                 
304                 if (user_in_list(user,list))
305                         conn->read_only = False;    
306         }
307
308         /* admin user check */
309         
310         /* JRA - original code denied admin user if the share was
311            marked read_only. Changed as I don't think this is needed,
312            but old code left in case there is a problem here.
313         */
314         if (user_in_list(user,lp_admin_users(snum)) 
315 #if 0
316             && !conn->read_only
317 #endif
318             ) {
319                 conn->admin_user = True;
320                 DEBUG(0,("%s logged in as admin user (root privileges)\n",user));
321         } else {
322                 conn->admin_user = False;
323         }
324     
325         conn->force_user = force;
326         conn->vuid = vuid;
327         conn->uid = pass->pw_uid;
328         conn->gid = pass->pw_gid;
329         conn->num_files_open = 0;
330         conn->lastused = time(NULL);
331         conn->service = snum;
332         conn->used = True;
333         conn->printer = (strncmp(dev,"LPT",3) == 0);
334         conn->ipc = (strncmp(dev,"IPC",3) == 0);
335         conn->dirptr = NULL;
336         conn->veto_list = NULL;
337         conn->hide_list = NULL;
338         conn->veto_oplock_list = NULL;
339         string_set(&conn->dirpath,"");
340         string_set(&conn->user,user);
341         
342 #ifdef HAVE_GETGRNAM 
343         if (*lp_force_group(snum)) {
344                 struct group *gptr;
345                 pstring gname;
346                 
347                 StrnCpy(gname,lp_force_group(snum),sizeof(pstring)-1);
348                 /* default service may be a group name          */
349                 string_sub(gname,"%S",service);
350                 gptr = (struct group *)getgrnam(gname);
351                 
352                 if (gptr) {
353                         conn->gid = gptr->gr_gid;
354                         DEBUG(3,("Forced group %s\n",gname));
355                 } else {
356                         DEBUG(1,("Couldn't find group %s\n",gname));
357                 }
358         }
359 #endif
360         
361         if (*lp_force_user(snum)) {
362                 struct passwd *pass2;
363                 fstring fuser;
364                 fstrcpy(fuser,lp_force_user(snum));
365                 pass2 = (struct passwd *)Get_Pwnam(fuser,True);
366                 if (pass2) {
367                         conn->uid = pass2->pw_uid;
368                         string_set(&conn->user,fuser);
369                         fstrcpy(user,fuser);
370                         conn->force_user = True;
371                         DEBUG(3,("Forced user %s\n",fuser));      
372                 } else {
373                         DEBUG(1,("Couldn't find user %s\n",fuser));
374                 }
375         }
376
377         {
378                 pstring s;
379                 pstrcpy(s,lp_pathname(snum));
380                 standard_sub(conn,s);
381                 string_set(&conn->connectpath,s);
382                 DEBUG(3,("Connect path is %s\n",s));
383         }
384
385         /* groups stuff added by ih */
386         conn->ngroups = 0;
387         conn->groups = NULL;
388         
389         if (!IS_IPC(conn)) {
390                 /* Find all the groups this uid is in and
391                    store them. Used by become_user() */
392                 get_unixgroups(conn->user,conn->uid,conn->gid,
393                              &conn->ngroups,&conn->groups);
394                 
395                 /* check number of connections */
396                 if (!claim_connection(conn,
397                                       lp_servicename(SNUM(conn)),
398                                       lp_max_connections(SNUM(conn)),
399                                       False)) {
400                         DEBUG(1,("too many connections - rejected\n"));
401                         *ecode = ERRnoresource;
402                         conn_free(conn);
403                         return NULL;
404                 }  
405                 
406                 if (lp_status(SNUM(conn)))
407                         claim_connection(conn,"STATUS.",
408                                          MAXSTATUS,False);
409         } /* IS_IPC */
410         
411         /* execute any "root preexec = " line */
412         if (*lp_rootpreexec(SNUM(conn))) {
413                 pstring cmd;
414                 pstrcpy(cmd,lp_rootpreexec(SNUM(conn)));
415                 standard_sub(conn,cmd);
416                 DEBUG(5,("cmd=%s\n",cmd));
417                 smbrun(cmd,NULL,False);
418         }
419         
420         if (!become_user(conn, conn->vuid)) {
421                 DEBUG(0,("Can't become connected user!\n"));
422                 if (!IS_IPC(conn)) {
423                         yield_connection(conn,
424                                          lp_servicename(SNUM(conn)),
425                                          lp_max_connections(SNUM(conn)));
426                         if (lp_status(SNUM(conn))) {
427                                 yield_connection(conn,"STATUS.",MAXSTATUS);
428                         }
429                 }
430                 conn_free(conn);
431                 *ecode = ERRbadpw;
432                 return NULL;
433         }
434         
435         if (dos_ChDir(conn->connectpath) != 0) {
436                 DEBUG(0,("Can't change directory to %s (%s)\n",
437                          conn->connectpath,strerror(errno)));
438                 unbecome_user();
439                 if (!IS_IPC(conn)) {
440                         yield_connection(conn,
441                                          lp_servicename(SNUM(conn)),
442                                          lp_max_connections(SNUM(conn)));
443                         if (lp_status(SNUM(conn))) 
444                                 yield_connection(conn,"STATUS.",MAXSTATUS);
445                 }
446                 conn_free(conn);
447                 *ecode = ERRinvnetname;
448                 return NULL;
449         }
450         
451         string_set(&conn->origpath,conn->connectpath);
452         
453 #if SOFTLINK_OPTIMISATION
454         /* resolve any soft links early */
455         {
456                 pstring s;
457                 pstrcpy(s,conn->connectpath);
458                 dos_GetWd(s);
459                 string_set(&conn->connectpath,s);
460                 dos_ChDir(conn->connectpath);
461         }
462 #endif
463         
464         add_session_user(user);
465                 
466         /* execute any "preexec = " line */
467         if (*lp_preexec(SNUM(conn))) {
468                 pstring cmd;
469                 pstrcpy(cmd,lp_preexec(SNUM(conn)));
470                 standard_sub(conn,cmd);
471                 smbrun(cmd,NULL,False);
472         }
473         
474         /* we've finished with the sensitive stuff */
475         unbecome_user();
476         
477         /* Add veto/hide lists */
478         if (!IS_IPC(conn) && !IS_PRINT(conn)) {
479                 set_namearray( &conn->veto_list, lp_veto_files(SNUM(conn)));
480                 set_namearray( &conn->hide_list, lp_hide_files(SNUM(conn)));
481                 set_namearray( &conn->veto_oplock_list, lp_veto_oplocks(SNUM(conn)));
482         }
483         
484         if( DEBUGLVL( IS_IPC(conn) ? 3 : 1 ) ) {
485                 extern int Client;
486                 
487                 dbgtext( "%s (%s) ", remote_machine, client_addr(Client) );
488                 dbgtext( "connect to service %s ", lp_servicename(SNUM(conn)) );
489                 dbgtext( "as user %s ", user );
490                 dbgtext( "(uid=%d, gid=%d) ", (int)conn->uid, (int)conn->gid );
491                 dbgtext( "(pid %d)\n", (int)getpid() );
492         }
493         
494         return(conn);
495 }
496
497
498 /****************************************************************************
499 close a cnum
500 ****************************************************************************/
501 void close_cnum(connection_struct *conn, uint16 vuid)
502 {
503         extern int Client;
504         DirCacheFlush(SNUM(conn));
505
506         unbecome_user();
507
508         DEBUG(IS_IPC(conn)?3:1, ("%s (%s) closed connection to service %s\n",
509                                  remote_machine,client_addr(Client),
510                                  lp_servicename(SNUM(conn))));
511
512         yield_connection(conn,
513                          lp_servicename(SNUM(conn)),
514                          lp_max_connections(SNUM(conn)));
515
516         if (lp_status(SNUM(conn)))
517                 yield_connection(conn,"STATUS.",MAXSTATUS);
518
519         file_close_conn(conn);
520         dptr_closecnum(conn);
521
522         /* execute any "postexec = " line */
523         if (*lp_postexec(SNUM(conn)) && 
524             become_user(conn, vuid))  {
525                 pstring cmd;
526                 pstrcpy(cmd,lp_postexec(SNUM(conn)));
527                 standard_sub(conn,cmd);
528                 smbrun(cmd,NULL,False);
529                 unbecome_user();
530         }
531
532         unbecome_user();
533         /* execute any "root postexec = " line */
534         if (*lp_rootpostexec(SNUM(conn)))  {
535                 pstring cmd;
536                 pstrcpy(cmd,lp_rootpostexec(SNUM(conn)));
537                 standard_sub(conn,cmd);
538                 smbrun(cmd,NULL,False);
539         }
540         
541         conn_free(conn);
542 }
543
544