finally got sick of the "extern int Client" code and the stupid
[bbaumbach/samba-autobuild/.git] / source / 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 struct timeval 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.tv_sec;
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    all_string_sub(service,"\\","/",0);
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_user_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_user_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    /* Check for default vfs service?  Unsure whether to implement this */
146    if (iService < 0)
147    {
148    }
149
150    /* just possibly it's a default service? */
151    if (iService < 0) 
152    {
153      char *pdefservice = lp_defaultservice();
154      if (pdefservice && *pdefservice && 
155          !strequal(pdefservice,service) &&
156          !strstr(service,".."))
157      {
158        /*
159         * We need to do a local copy here as lp_defaultservice() 
160         * returns one of the rotating lp_string buffers that
161         * could get overwritten by the recursive find_service() call
162         * below. Fix from Josef Hinteregger <joehtg@joehtg.co.at>.
163         */
164        pstring defservice;
165        pstrcpy(defservice, pdefservice);
166        iService = find_service(defservice);
167        if (iService >= 0)
168        {
169          all_string_sub(service,"_","/",0);
170          iService = lp_add_service(service,iService);
171        }
172      }
173    }
174
175    if (iService >= 0)
176      if (!VALID_SNUM(iService))
177      {
178        DEBUG(0,("Invalid snum %d for %s\n",iService,service));
179        iService = -1;
180      }
181
182    if (iService < 0)
183      DEBUG(3,("find_service() failed to find service %s\n", service));
184
185    return (iService);
186 }
187
188
189 /****************************************************************************
190   make a connection to a service
191 ****************************************************************************/
192 connection_struct *make_connection(char *service,char *user,char *password, int pwlen, char *dev,uint16 vuid, int *ecode)
193 {
194         int snum;
195         struct passwd *pass = NULL;
196         BOOL guest = False;
197         BOOL force = False;
198         connection_struct *conn;
199         int ret;
200
201         strlower(service);
202
203         snum = find_service(service);
204         if (snum < 0) {
205                 if (strequal(service,"IPC$")) {
206                         DEBUG(3,("refusing IPC connection\n"));
207                         *ecode = ERRnoipc;
208                         return NULL;
209                 }
210
211                 DEBUG(0,("%s (%s) couldn't find service %s\n",
212                          remote_machine, client_addr(), service));
213                 *ecode = ERRinvnetname;
214                 return NULL;
215         }
216
217         if (strequal(service,HOMES_NAME)) {
218                 if (*user && Get_Pwnam(user,True)) {
219                         fstring dos_username;
220                         fstrcpy(dos_username, user);
221                         unix_to_dos(dos_username, True);
222                         return(make_connection(dos_username,user,password,
223                                                pwlen,dev,vuid,ecode));
224                 }
225
226                 if(lp_security() != SEC_SHARE) {
227                         if (validated_username(vuid)) {
228                                 fstring dos_username;
229                                 fstrcpy(user,validated_username(vuid));
230                                 fstrcpy(dos_username, user);
231                                 unix_to_dos(dos_username, True);
232                                 return(make_connection(dos_username,user,password,pwlen,dev,vuid,ecode));
233                         }
234                 } else {
235                         /* Security = share. Try with sesssetup_user
236                          * as the username.  */
237                         if(*sesssetup_user) {
238                                 fstring dos_username;
239                                 fstrcpy(user,sesssetup_user);
240                                 fstrcpy(dos_username, user);
241                                 unix_to_dos(dos_username, True);
242                                 return(make_connection(dos_username,user,password,pwlen,dev,vuid,ecode));
243                         }
244                 }
245         }
246
247         if (!lp_snum_ok(snum) || 
248             !check_access(smbd_server_fd(), 
249                           lp_hostsallow(snum), lp_hostsdeny(snum))) {    
250                 *ecode = ERRaccess;
251                 return NULL;
252         }
253
254         /* you can only connect to the IPC$ service as an ipc device */
255         if (strequal(service,"IPC$"))
256                 pstrcpy(dev,"IPC");
257         
258         if (*dev == '?' || !*dev) {
259                 if (lp_print_ok(snum)) {
260                         pstrcpy(dev,"LPT1:");
261                 } else {
262                         pstrcpy(dev,"A:");
263                 }
264         }
265
266         /* if the request is as a printer and you can't print then refuse */
267         strupper(dev);
268         if (!lp_print_ok(snum) && (strncmp(dev,"LPT",3) == 0)) {
269                 DEBUG(1,("Attempt to connect to non-printer as a printer\n"));
270                 *ecode = ERRinvdevice;
271                 return NULL;
272         }
273
274         /* lowercase the user name */
275         strlower(user);
276
277         /* add it as a possible user name */
278         add_session_user(service);
279
280         /* shall we let them in? */
281         if (!authorise_login(snum,user,password,pwlen,&guest,&force,vuid)) {
282                 DEBUG( 2, ( "Invalid username/password for %s\n", service ) );
283                 *ecode = ERRbadpw;
284                 return NULL;
285         }
286   
287         conn = conn_new();
288         if (!conn) {
289                 DEBUG(0,("Couldn't find free connection.\n"));
290                 *ecode = ERRnoresource;
291                 conn_free(conn);
292                 return NULL;
293         }
294
295         /* find out some info about the user */
296         pass = Get_Pwnam(user,True);
297
298         if (pass == NULL) {
299                 DEBUG(0,( "Couldn't find account %s\n",user));
300                 *ecode = ERRbaduid;
301                 conn_free(conn);
302                 return NULL;
303         }
304
305         conn->read_only = lp_readonly(snum);
306
307         {
308                 pstring list;
309                 StrnCpy(list,lp_readlist(snum),sizeof(pstring)-1);
310                 pstring_sub(list,"%S",service);
311
312                 if (user_in_list(user,list))
313                         conn->read_only = True;
314                 
315                 StrnCpy(list,lp_writelist(snum),sizeof(pstring)-1);
316                 pstring_sub(list,"%S",service);
317                 
318                 if (user_in_list(user,list))
319                         conn->read_only = False;    
320         }
321
322         /* admin user check */
323         
324         /* JRA - original code denied admin user if the share was
325            marked read_only. Changed as I don't think this is needed,
326            but old code left in case there is a problem here.
327         */
328         if (user_in_list(user,lp_admin_users(snum)) 
329 #if 0
330             && !conn->read_only
331 #endif
332             ) {
333                 conn->admin_user = True;
334                 DEBUG(0,("%s logged in as admin user (root privileges)\n",user));
335         } else {
336                 conn->admin_user = False;
337         }
338     
339         conn->force_user = force;
340         conn->vuid = vuid;
341         conn->uid = pass->pw_uid;
342         conn->gid = pass->pw_gid;
343         safe_strcpy(conn->client_address, client_addr(), sizeof(conn->client_address)-1);
344         conn->num_files_open = 0;
345         conn->lastused = time(NULL);
346         conn->service = snum;
347         conn->used = True;
348         conn->printer = (strncmp(dev,"LPT",3) == 0);
349         conn->ipc = (strncmp(dev,"IPC",3) == 0);
350         conn->dirptr = NULL;
351         conn->veto_list = NULL;
352         conn->hide_list = NULL;
353         conn->veto_oplock_list = NULL;
354         string_set(&conn->dirpath,"");
355         string_set(&conn->user,user);
356         
357         conn->vfs_conn = (struct vfs_connection_struct *)
358             malloc(sizeof(struct vfs_connection_struct));
359
360         if (conn->vfs_conn == NULL) {
361             DEBUG(0, ("No memory to create vfs_connection_struct"));
362             return NULL;
363         }
364         
365         ZERO_STRUCTP(conn->vfs_conn);
366
367         /* Copy across relevant data from connection struct */
368         
369         conn->vfs_conn->printer = conn->printer;
370         conn->vfs_conn->ipc = conn->ipc;
371         conn->vfs_conn->read_only = conn->read_only;
372         conn->vfs_conn->admin_user = conn->admin_user;
373
374         pstrcpy(conn->vfs_conn->dirpath, conn->dirpath);
375         pstrcpy(conn->vfs_conn->connectpath, conn->connectpath);
376         pstrcpy(conn->vfs_conn->origpath, conn->origpath);
377         
378         pstrcpy(conn->vfs_conn->service, service);
379         pstrcpy(conn->vfs_conn->user, conn->user);
380         
381         conn->vfs_conn->uid = conn->uid;
382         conn->vfs_conn->gid = conn->gid;
383         conn->vfs_conn->ngroups = conn->ngroups;
384         conn->vfs_conn->groups = (gid_t *)memdup(conn->groups, 
385                                                  conn->ngroups * sizeof(gid_t));
386
387         /* Initialise VFS function pointers */
388
389         if (*lp_vfsobj(SNUM(conn))) {
390
391 #ifdef HAVE_LIBDL
392
393             /* Loadable object file */
394
395             if (!vfs_init_custom(conn)) {
396                 return NULL;
397             }
398 #else
399             DEBUG(0, ("No libdl present - cannot use VFS objects\n"));
400             conn_free(conn);
401             return NULL;
402 #endif
403
404         } else {
405
406             /* Normal share - initialise with disk access functions */
407
408             vfs_init_default(conn);
409         }
410
411         /*
412          * If force user is true, then store the
413          * given userid and also the primary groupid
414          * of the user we're forcing.
415          */
416         
417         if (*lp_force_user(snum)) {
418                 struct passwd *pass2;
419                 pstring fuser;
420                 pstrcpy(fuser,lp_force_user(snum));
421
422                 /* Allow %S to be used by force user. */
423                 pstring_sub(fuser,"%S",service);
424
425                 pass2 = (struct passwd *)Get_Pwnam(fuser,True);
426                 if (pass2) {
427                         conn->uid = pass2->pw_uid;
428                         conn->gid = pass2->pw_gid;
429                         string_set(&conn->user,fuser);
430                         fstrcpy(user,fuser);
431                         conn->force_user = True;
432                         DEBUG(3,("Forced user %s\n",fuser));      
433                 } else {
434                         DEBUG(1,("Couldn't find user %s\n",fuser));
435                 }
436         }
437
438 #ifdef HAVE_GETGRNAM 
439         /*
440          * If force group is true, then override
441          * any groupid stored for the connecting user.
442          */
443         
444         if (*lp_force_group(snum)) {
445                 struct group *gptr;
446                 pstring gname;
447                 pstring tmp_gname;
448                 BOOL user_must_be_member = False;
449                 
450                 StrnCpy(tmp_gname,lp_force_group(snum),sizeof(pstring)-1);
451
452                 if (tmp_gname[0] == '+') {
453                         user_must_be_member = True;
454                         StrnCpy(gname,&tmp_gname[1],sizeof(pstring)-2);
455                 } else {
456                         StrnCpy(gname,tmp_gname,sizeof(pstring)-1);
457                 }
458                 /* default service may be a group name          */
459                 pstring_sub(gname,"%S",service);
460                 gptr = (struct group *)getgrnam(gname);
461                 
462                 if (gptr) {
463                         /*
464                          * If the user has been forced and the forced group starts
465                          * with a '+', then we only set the group to be the forced
466                          * group if the forced user is a member of that group.
467                          * Otherwise, the meaning of the '+' would be ignored.
468                          */
469                         if (conn->force_user && user_must_be_member) {
470                                 int i;
471                                 for (i = 0; gptr->gr_mem[i] != NULL; i++) {
472                                         if (strcmp(user,gptr->gr_mem[i]) == 0) {
473                                                 conn->gid = gptr->gr_gid;
474                                                 DEBUG(3,("Forced group %s for member %s\n",gname,user));
475                                                 break;
476                                         }
477                                 }
478                         } else {
479                                 conn->gid = gptr->gr_gid;
480                                 DEBUG(3,("Forced group %s\n",gname));
481                         }
482                 } else {
483                         DEBUG(1,("Couldn't find group %s\n",gname));
484                 }
485         }
486 #endif /* HAVE_GETGRNAM */
487
488         {
489                 pstring s;
490                 pstrcpy(s,lp_pathname(snum));
491                 standard_sub(conn,s);
492                 string_set(&conn->connectpath,s);
493                 DEBUG(3,("Connect path is %s\n",s));
494         }
495
496         /* groups stuff added by ih */
497         conn->ngroups = 0;
498         conn->groups = NULL;
499         
500         if (!IS_IPC(conn)) {
501                 /* Find all the groups this uid is in and
502                    store them. Used by become_user() */
503                 setup_groups(conn->user,conn->uid,conn->gid,
504                              &conn->ngroups,&conn->groups);
505                 
506                 /* check number of connections */
507                 if (!claim_connection(conn,
508                                       lp_servicename(SNUM(conn)),
509                                       lp_max_connections(SNUM(conn)),
510                                       False)) {
511                         DEBUG(1,("too many connections - rejected\n"));
512                         *ecode = ERRnoresource;
513                         conn_free(conn);
514                         return NULL;
515                 }  
516                 
517                 if (lp_status(SNUM(conn)))
518                         claim_connection(conn,"",
519                                          MAXSTATUS,False);
520         } /* IS_IPC */
521         
522         /* execute any "root preexec = " line */
523         if (*lp_rootpreexec(SNUM(conn))) {
524                 pstring cmd;
525                 pstrcpy(cmd,lp_rootpreexec(SNUM(conn)));
526                 standard_sub(conn,cmd);
527                 DEBUG(5,("cmd=%s\n",cmd));
528                 ret = smbrun(cmd,NULL,False);
529                 if (ret != 0 && lp_rootpreexec_close(SNUM(conn))) {
530                         DEBUG(1,("preexec gave %d - failing connection\n", ret));
531                         conn_free(conn);
532                         *ecode = ERRsrverror;
533                         return NULL;
534                 }
535         }
536         
537         if (!become_user(conn, conn->vuid)) {
538                 DEBUG(0,("Can't become connected user!\n"));
539                 if (!IS_IPC(conn)) {
540                         yield_connection(conn,
541                                          lp_servicename(SNUM(conn)),
542                                          lp_max_connections(SNUM(conn)));
543                         if (lp_status(SNUM(conn))) {
544                                 yield_connection(conn,"",MAXSTATUS);
545                         }
546                 }
547                 conn_free(conn);
548                 *ecode = ERRbadpw;
549                 return NULL;
550         }
551         
552         if (dos_ChDir(conn->connectpath) != 0) {
553                 DEBUG(0,("Can't change directory to %s (%s)\n",
554                          conn->connectpath,strerror(errno)));
555                 unbecome_user();
556                 if (!IS_IPC(conn)) {
557                         yield_connection(conn,
558                                          lp_servicename(SNUM(conn)),
559                                          lp_max_connections(SNUM(conn)));
560                         if (lp_status(SNUM(conn))) 
561                                 yield_connection(conn,"",MAXSTATUS);
562                 }
563                 conn_free(conn);
564                 *ecode = ERRinvnetname;
565                 return NULL;
566         }
567         
568         string_set(&conn->origpath,conn->connectpath);
569         
570 #if SOFTLINK_OPTIMISATION
571         /* resolve any soft links early */
572         {
573                 pstring s;
574                 pstrcpy(s,conn->connectpath);
575                 dos_GetWd(s);
576                 string_set(&conn->connectpath,s);
577                 dos_ChDir(conn->connectpath);
578         }
579 #endif
580         
581         add_session_user(user);
582                 
583         /* execute any "preexec = " line */
584         if (*lp_preexec(SNUM(conn))) {
585                 pstring cmd;
586                 pstrcpy(cmd,lp_preexec(SNUM(conn)));
587                 standard_sub(conn,cmd);
588                 ret = smbrun(cmd,NULL,False);
589                 if (ret != 0 && lp_preexec_close(SNUM(conn))) {
590                         DEBUG(1,("preexec gave %d - failing connection\n", ret));
591                         conn_free(conn);
592                         *ecode = ERRsrverror;
593                         return NULL;
594                 }
595         }
596
597         /*
598          * Print out the 'connected as' stuff here as we need
599          * to know the effective uid and gid we will be using.
600          */
601
602         if( DEBUGLVL( IS_IPC(conn) ? 3 : 1 ) ) {
603                 dbgtext( "%s (%s) ", remote_machine, conn->client_address );
604                 dbgtext( "connect to service %s ", lp_servicename(SNUM(conn)) );
605                 dbgtext( "as user %s ", user );
606                 dbgtext( "(uid=%d, gid=%d) ", (int)geteuid(), (int)getegid() );
607                 dbgtext( "(pid %d)\n", (int)getpid() );
608         }
609         
610         /* we've finished with the sensitive stuff */
611         unbecome_user();
612         
613         /* Add veto/hide lists */
614         if (!IS_IPC(conn) && !IS_PRINT(conn)) {
615                 set_namearray( &conn->veto_list, lp_veto_files(SNUM(conn)));
616                 set_namearray( &conn->hide_list, lp_hide_files(SNUM(conn)));
617                 set_namearray( &conn->veto_oplock_list, lp_veto_oplocks(SNUM(conn)));
618         }
619         
620         /* Invoke VFS make connection hook */
621
622         if (conn->vfs_ops.connect) {
623             if (conn->vfs_ops.connect(conn->vfs_conn, service, user) < 0) {
624                 return NULL;
625             }
626         }
627             
628         return(conn);
629 }
630
631
632 /****************************************************************************
633 close a cnum
634 ****************************************************************************/
635 void close_cnum(connection_struct *conn, uint16 vuid)
636 {
637         DirCacheFlush(SNUM(conn));
638
639         unbecome_user();
640
641         DEBUG(IS_IPC(conn)?3:1, ("%s (%s) closed connection to service %s\n",
642                                  remote_machine,conn->client_address,
643                                  lp_servicename(SNUM(conn))));
644
645         if (conn->vfs_ops.disconnect != NULL) {
646
647             /* Call VFS disconnect hook */
648             
649             conn->vfs_ops.disconnect();
650             
651         }
652
653         /* Close dlopen() handle */
654
655         if (conn->vfs_conn->dl_handle != NULL) {
656             dlclose(conn->vfs_conn->dl_handle);  /* should we check return val? */
657         }
658
659         /* Free vfs_connection_struct */
660             
661         if (conn->vfs_conn != NULL) {
662             if (conn->vfs_conn->groups != NULL) {
663                 free(conn->vfs_conn->groups);
664             }
665             free(conn->vfs_conn);
666         }
667
668         yield_connection(conn,
669                          lp_servicename(SNUM(conn)),
670                          lp_max_connections(SNUM(conn)));
671
672         if (lp_status(SNUM(conn)))
673                 yield_connection(conn,"",MAXSTATUS);
674
675         file_close_conn(conn);
676         dptr_closecnum(conn);
677
678         /* execute any "postexec = " line */
679         if (*lp_postexec(SNUM(conn)) && 
680             become_user(conn, vuid))  {
681                 pstring cmd;
682                 pstrcpy(cmd,lp_postexec(SNUM(conn)));
683                 standard_sub(conn,cmd);
684                 smbrun(cmd,NULL,False);
685                 unbecome_user();
686         }
687
688         unbecome_user();
689         /* execute any "root postexec = " line */
690         if (*lp_rootpostexec(SNUM(conn)))  {
691                 pstring cmd;
692                 pstrcpy(cmd,lp_rootpostexec(SNUM(conn)));
693                 standard_sub(conn,cmd);
694                 smbrun(cmd,NULL,False);
695         }
696         
697         conn_free(conn);
698 }