71d718154d1a1a9e95251acbfbbb6c44a589ac5d
[kai/samba.git] / source / smbd / service.c
1 #define OLD_NTDOMAIN 1
2
3 /* 
4    Unix SMB/Netbios implementation.
5    Version 1.9.
6    service (connection) opening and closing
7    Copyright (C) Andrew Tridgell 1992-1998
8    
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #include "includes.h"
25
26 extern int DEBUGLEVEL;
27
28 extern struct timeval smb_last_time;
29 extern int case_default;
30 extern BOOL case_preserve;
31 extern BOOL short_case_preserve;
32 extern BOOL case_mangle;
33 extern BOOL case_sensitive;
34 extern BOOL use_mangled_map;
35 extern fstring remote_machine;
36 extern pstring sesssetup_user;
37 extern fstring remote_machine;
38
39
40 /****************************************************************************
41 load parameters specific to a connection/service
42 ****************************************************************************/
43 BOOL become_service(connection_struct *conn,BOOL do_chdir)
44 {
45         extern char magic_char;
46         static connection_struct *last_conn;
47         int snum;
48
49         if (!conn)  {
50                 last_conn = NULL;
51                 return(False);
52         }
53
54         conn->lastused = smb_last_time.tv_sec;
55
56         snum = SNUM(conn);
57   
58         if (do_chdir &&
59             dos_ChDir(conn->connectpath) != 0 &&
60             dos_ChDir(conn->origpath) != 0) {
61                 DEBUG(0,("chdir (%s) failed\n",
62                          conn->connectpath));
63                 return(False);
64         }
65
66         if (conn == last_conn)
67                 return(True);
68
69         last_conn = conn;
70
71         case_default = lp_defaultcase(snum);
72         case_preserve = lp_preservecase(snum);
73         short_case_preserve = lp_shortpreservecase(snum);
74         case_mangle = lp_casemangle(snum);
75         case_sensitive = lp_casesensitive(snum);
76         magic_char = lp_magicchar(snum);
77         use_mangled_map = (*lp_mangled_map(snum) ? True:False);
78         return(True);
79 }
80
81
82 /****************************************************************************
83   find a service entry
84 ****************************************************************************/
85 int find_service(char *service)
86 {
87    int iService;
88
89    all_string_sub(service,"\\","/",0);
90
91    iService = lp_servicenumber(service);
92
93    /* now handle the special case of a home directory */
94    if (iService < 0)
95    {
96       char *phome_dir = get_user_home_dir(service);
97
98       if(!phome_dir)
99       {
100         /*
101          * Try mapping the servicename, it may
102          * be a Windows to unix mapped user name.
103          */
104         if(map_username(service))
105           phome_dir = get_user_home_dir(service);
106       }
107
108       DEBUG(3,("checking for home directory %s gave %s\n",service,
109             phome_dir?phome_dir:"(NULL)"));
110
111       if (phome_dir)
112       {   
113         int iHomeService;
114         if ((iHomeService = lp_servicenumber(HOMES_NAME)) >= 0)
115         {
116           lp_add_home(service,iHomeService,phome_dir);
117           iService = lp_servicenumber(service);
118         }
119       }
120    }
121
122    /* If we still don't have a service, attempt to add it as a printer. */
123    if (iService < 0)
124    {
125       int iPrinterService;
126
127       if ((iPrinterService = lp_servicenumber(PRINTERS_NAME)) >= 0)
128       {
129          char *pszTemp;
130
131          DEBUG(3,("checking whether %s is a valid printer name...\n", service));
132          pszTemp = PRINTCAP;
133          if ((pszTemp != NULL) && pcap_printername_ok(service, pszTemp))
134          {
135             DEBUG(3,("%s is a valid printer name\n", service));
136             DEBUG(3,("adding %s as a printer service\n", service));
137             lp_add_printer(service,iPrinterService);
138             iService = lp_servicenumber(service);
139             if (iService < 0)
140                DEBUG(0,("failed to add %s as a printer service!\n", service));
141          }
142          else
143             DEBUG(3,("%s is not a valid printer name\n", service));
144       }
145    }
146
147    /* Check for default vfs service?  Unsure whether to implement this */
148    if (iService < 0)
149    {
150    }
151
152    /* just possibly it's a default service? */
153    if (iService < 0) 
154    {
155      char *pdefservice = lp_defaultservice();
156      if (pdefservice && *pdefservice && 
157          !strequal(pdefservice,service) &&
158          !strstr(service,".."))
159      {
160        /*
161         * We need to do a local copy here as lp_defaultservice() 
162         * returns one of the rotating lp_string buffers that
163         * could get overwritten by the recursive find_service() call
164         * below. Fix from Josef Hinteregger <joehtg@joehtg.co.at>.
165         */
166        pstring defservice;
167        pstrcpy(defservice, pdefservice);
168        iService = find_service(defservice);
169        if (iService >= 0)
170        {
171          all_string_sub(service,"_","/",0);
172          iService = lp_add_service(service,iService);
173        }
174      }
175    }
176
177    if (iService >= 0)
178      if (!VALID_SNUM(iService))
179      {
180        DEBUG(0,("Invalid snum %d for %s\n",iService,service));
181        iService = -1;
182      }
183
184    if (iService < 0)
185      DEBUG(3,("find_service() failed to find service %s\n", service));
186
187    return (iService);
188 }
189
190
191 /****************************************************************************
192   make a connection to a service
193 ****************************************************************************/
194 connection_struct *make_connection(char *service,char *user,char *password, int pwlen, char *dev,uint16 vuid, int *ecode)
195 {
196         int snum;
197         struct passwd *pass = NULL;
198         BOOL guest = False;
199         BOOL force = False;
200         connection_struct *conn;
201         int ret;
202
203         strlower(service);
204
205         snum = find_service(service);
206         if (snum < 0) {
207                 if (strequal(service,"IPC$")) {
208                         DEBUG(3,("refusing IPC connection\n"));
209                         *ecode = ERRnoipc;
210                         return NULL;
211                 }
212
213                 DEBUG(0,("%s (%s) couldn't find service %s\n",
214                          remote_machine, client_addr(), service));
215                 *ecode = ERRinvnetname;
216                 return NULL;
217         }
218
219         if (strequal(service,HOMES_NAME)) {
220                 if (*user && Get_Pwnam(user,True)) {
221                         fstring dos_username;
222                         fstrcpy(dos_username, user);
223                         unix_to_dos(dos_username, True);
224                         return(make_connection(dos_username,user,password,
225                                                pwlen,dev,vuid,ecode));
226                 }
227
228                 if(lp_security() != SEC_SHARE) {
229                         if (validated_username(vuid)) {
230                                 fstring dos_username;
231                                 fstrcpy(user,validated_username(vuid));
232                                 fstrcpy(dos_username, user);
233                                 unix_to_dos(dos_username, True);
234                                 return(make_connection(dos_username,user,password,pwlen,dev,vuid,ecode));
235                         }
236                 } else {
237                         /* Security = share. Try with sesssetup_user
238                          * as the username.  */
239                         if(*sesssetup_user) {
240                                 fstring dos_username;
241                                 fstrcpy(user,sesssetup_user);
242                                 fstrcpy(dos_username, user);
243                                 unix_to_dos(dos_username, True);
244                                 return(make_connection(dos_username,user,password,pwlen,dev,vuid,ecode));
245                         }
246                 }
247         }
248
249         if (!lp_snum_ok(snum) || 
250             !check_access(smbd_server_fd(), 
251                           lp_hostsallow(snum), lp_hostsdeny(snum))) {    
252                 *ecode = ERRaccess;
253                 return NULL;
254         }
255
256         /* you can only connect to the IPC$ service as an ipc device */
257         if (strequal(service,"IPC$"))
258                 pstrcpy(dev,"IPC");
259         
260         if (*dev == '?' || !*dev) {
261                 if (lp_print_ok(snum)) {
262                         pstrcpy(dev,"LPT1:");
263                 } else {
264                         pstrcpy(dev,"A:");
265                 }
266         }
267
268         /* if the request is as a printer and you can't print then refuse */
269         strupper(dev);
270         if (!lp_print_ok(snum) && (strncmp(dev,"LPT",3) == 0)) {
271                 DEBUG(1,("Attempt to connect to non-printer as a printer\n"));
272                 *ecode = ERRinvdevice;
273                 return NULL;
274         }
275
276         /* lowercase the user name */
277         strlower(user);
278
279         /* add it as a possible user name */
280         add_session_user(service);
281
282         /* shall we let them in? */
283         if (!authorise_login(snum,user,password,pwlen,&guest,&force,vuid)) {
284                 DEBUG( 2, ( "Invalid username/password for %s\n", service ) );
285                 *ecode = ERRbadpw;
286                 return NULL;
287         }
288   
289         conn = conn_new();
290         if (!conn) {
291                 DEBUG(0,("Couldn't find free connection.\n"));
292                 *ecode = ERRnoresource;
293                 conn_free(conn);
294                 return NULL;
295         }
296
297         /* find out some info about the user */
298         pass = smb_getpwnam(user,validated_domain(vuid),True);
299
300         if (pass == NULL) {
301                 DEBUG(0,( "Couldn't find account %s\n",user));
302                 *ecode = ERRbaduid;
303                 conn_free(conn);
304                 return NULL;
305         }
306
307         conn->read_only = lp_readonly(snum);
308
309         {
310                 pstring list;
311                 StrnCpy(list,lp_readlist(snum),sizeof(pstring)-1);
312                 pstring_sub(list,"%S",service);
313
314                 if (user_in_list(user,list))
315                         conn->read_only = True;
316                 
317                 StrnCpy(list,lp_writelist(snum),sizeof(pstring)-1);
318                 pstring_sub(list,"%S",service);
319                 
320                 if (user_in_list(user,list))
321                         conn->read_only = False;    
322         }
323
324         /* admin user check */
325         
326         /* JRA - original code denied admin user if the share was
327            marked read_only. Changed as I don't think this is needed,
328            but old code left in case there is a problem here.
329         */
330         if (user_in_list(user,lp_admin_users(snum)) 
331 #if 0
332             && !conn->read_only
333 #endif
334             ) {
335                 conn->admin_user = True;
336                 DEBUG(0,("%s logged in as admin user (root privileges)\n",user));
337         } else {
338                 conn->admin_user = False;
339         }
340     
341         conn->force_user = force;
342         conn->vuid = vuid;
343         conn->uid = pass->pw_uid;
344         conn->gid = pass->pw_gid;
345         safe_strcpy(conn->client_address, client_addr(), sizeof(conn->client_address)-1);
346         conn->num_files_open = 0;
347         conn->lastused = time(NULL);
348         conn->service = snum;
349         conn->used = True;
350         conn->printer = (strncmp(dev,"LPT",3) == 0);
351         conn->ipc = (strncmp(dev,"IPC",3) == 0);
352         conn->dirptr = NULL;
353         conn->veto_list = NULL;
354         conn->hide_list = NULL;
355         conn->veto_oplock_list = NULL;
356         string_set(&conn->dirpath,"");
357         string_set(&conn->user,user);
358         
359         conn->vfs_conn = (struct vfs_connection_struct *)
360             malloc(sizeof(struct vfs_connection_struct));
361
362         if (conn->vfs_conn == NULL) {
363             DEBUG(0, ("No memory to create vfs_connection_struct"));
364             return NULL;
365         }
366         
367         ZERO_STRUCTP(conn->vfs_conn);
368
369         /* Copy across relevant data from connection struct */
370         
371         conn->vfs_conn->printer = conn->printer;
372         conn->vfs_conn->ipc = conn->ipc;
373         conn->vfs_conn->read_only = conn->read_only;
374         conn->vfs_conn->admin_user = conn->admin_user;
375
376         pstrcpy(conn->vfs_conn->dirpath, conn->dirpath);
377         pstrcpy(conn->vfs_conn->connectpath, conn->connectpath);
378         pstrcpy(conn->vfs_conn->origpath, conn->origpath);
379         
380         pstrcpy(conn->vfs_conn->service, service);
381         pstrcpy(conn->vfs_conn->user, conn->user);
382         
383         conn->vfs_conn->uid = conn->uid;
384         conn->vfs_conn->gid = conn->gid;
385         conn->vfs_conn->ngroups = conn->ngroups;
386         if (conn->vfs_conn->ngroups != 0) {
387                 conn->vfs_conn->groups = (gid_t *)memdup(conn->groups, 
388                                                          conn->ngroups * sizeof(gid_t));
389         } else {
390                 conn->vfs_conn->groups = NULL;
391         }
392
393         /* Initialise VFS function pointers */
394
395         if (*lp_vfsobj(SNUM(conn))) {
396
397 #ifdef HAVE_LIBDL
398
399             /* Loadable object file */
400
401             if (!vfs_init_custom(conn)) {
402                 return NULL;
403             }
404 #else
405             DEBUG(0, ("No libdl present - cannot use VFS objects\n"));
406             conn_free(conn);
407             return NULL;
408 #endif
409
410         } else {
411
412             /* Normal share - initialise with disk access functions */
413
414             vfs_init_default(conn);
415         }
416
417         /*
418          * If force user is true, then store the
419          * given userid and also the primary groupid
420          * of the user we're forcing.
421          */
422         
423         if (*lp_force_user(snum)) {
424                 struct passwd *pass2;
425                 pstring fuser;
426                 pstrcpy(fuser,lp_force_user(snum));
427
428                 /* Allow %S to be used by force user. */
429                 pstring_sub(fuser,"%S",service);
430
431                 pass2 = (struct passwd *)Get_Pwnam(fuser,True);
432                 if (pass2) {
433                         conn->uid = pass2->pw_uid;
434                         conn->gid = pass2->pw_gid;
435                         string_set(&conn->user,fuser);
436                         fstrcpy(user,fuser);
437                         conn->force_user = True;
438                         DEBUG(3,("Forced user %s\n",fuser));      
439                 } else {
440                         DEBUG(1,("Couldn't find user %s\n",fuser));
441                 }
442         }
443
444 #ifdef HAVE_GETGRNAM 
445         /*
446          * If force group is true, then override
447          * any groupid stored for the connecting user.
448          */
449         
450         if (*lp_force_group(snum)) {
451                 struct group *gptr;
452                 pstring gname;
453                 pstring tmp_gname;
454                 BOOL user_must_be_member = False;
455                 
456                 StrnCpy(tmp_gname,lp_force_group(snum),sizeof(pstring)-1);
457
458                 if (tmp_gname[0] == '+') {
459                         user_must_be_member = True;
460                         StrnCpy(gname,&tmp_gname[1],sizeof(pstring)-2);
461                 } else {
462                         StrnCpy(gname,tmp_gname,sizeof(pstring)-1);
463                 }
464                 /* default service may be a group name          */
465                 pstring_sub(gname,"%S",service);
466                 gptr = (struct group *)getgrnam(gname);
467                 
468                 if (gptr) {
469                         /*
470                          * If the user has been forced and the forced group starts
471                          * with a '+', then we only set the group to be the forced
472                          * group if the forced user is a member of that group.
473                          * Otherwise, the meaning of the '+' would be ignored.
474                          */
475                         if (conn->force_user && user_must_be_member) {
476                                 int i;
477                                 for (i = 0; gptr->gr_mem[i] != NULL; i++) {
478                                         if (strcmp(user,gptr->gr_mem[i]) == 0) {
479                                                 conn->gid = gptr->gr_gid;
480                                                 DEBUG(3,("Forced group %s for member %s\n",gname,user));
481                                                 break;
482                                         }
483                                 }
484                         } else {
485                                 conn->gid = gptr->gr_gid;
486                                 DEBUG(3,("Forced group %s\n",gname));
487                         }
488                 } else {
489                         DEBUG(1,("Couldn't find group %s\n",gname));
490                 }
491         }
492 #endif /* HAVE_GETGRNAM */
493
494         {
495                 pstring s;
496                 pstrcpy(s,lp_pathname(snum));
497                 standard_sub_conn(conn,s);
498                 string_set(&conn->connectpath,s);
499                 DEBUG(3,("Connect path is %s\n",s));
500         }
501
502         /* groups stuff added by ih */
503         conn->ngroups = 0;
504         conn->groups = NULL;
505         
506         if (!IS_IPC(conn)) {
507                 /* Find all the groups this uid is in and
508                    store them. Used by become_user() */
509                 setup_groups(conn->user,validated_domain(vuid),conn->uid,conn->gid,
510                              &conn->ngroups,&conn->groups);
511                 
512                 /* check number of connections */
513                 if (!claim_connection(conn,
514                                       lp_servicename(SNUM(conn)),
515                                       lp_max_connections(SNUM(conn)),
516                                       False)) {
517                         DEBUG(1,("too many connections - rejected\n"));
518                         *ecode = ERRnoresource;
519                         conn_free(conn);
520                         return NULL;
521                 }  
522                 
523                 if (lp_status(SNUM(conn)))
524                         claim_connection(conn,"",
525                                          MAXSTATUS,False);
526         } /* IS_IPC */
527         
528         /* execute any "root preexec = " line */
529         if (*lp_rootpreexec(SNUM(conn))) {
530                 pstring cmd;
531                 pstrcpy(cmd,lp_rootpreexec(SNUM(conn)));
532                 standard_sub_conn(conn,cmd);
533                 DEBUG(5,("cmd=%s\n",cmd));
534                 ret = smbrun(cmd,NULL,False);
535                 if (ret != 0 && lp_rootpreexec_close(SNUM(conn))) {
536                         DEBUG(1,("preexec gave %d - failing connection\n", ret));
537                         conn_free(conn);
538                         *ecode = ERRsrverror;
539                         return NULL;
540                 }
541         }
542         
543         if (!become_user(conn, conn->vuid)) {
544                 DEBUG(0,("Can't become connected user!\n"));
545                 if (!IS_IPC(conn)) {
546                         yield_connection(conn,
547                                          lp_servicename(SNUM(conn)),
548                                          lp_max_connections(SNUM(conn)));
549                         if (lp_status(SNUM(conn))) {
550                                 yield_connection(conn,"",MAXSTATUS);
551                         }
552                 }
553                 conn_free(conn);
554                 *ecode = ERRbadpw;
555                 return NULL;
556         }
557         
558         if (dos_ChDir(conn->connectpath) != 0) {
559                 DEBUG(0,("Can't change directory to %s (%s)\n",
560                          conn->connectpath,strerror(errno)));
561                 unbecome_user();
562                 if (!IS_IPC(conn)) {
563                         yield_connection(conn,
564                                          lp_servicename(SNUM(conn)),
565                                          lp_max_connections(SNUM(conn)));
566                         if (lp_status(SNUM(conn))) 
567                                 yield_connection(conn,"",MAXSTATUS);
568                 }
569                 conn_free(conn);
570                 *ecode = ERRinvnetname;
571                 return NULL;
572         }
573         
574         string_set(&conn->origpath,conn->connectpath);
575         
576 #if SOFTLINK_OPTIMISATION
577         /* resolve any soft links early */
578         {
579                 pstring s;
580                 pstrcpy(s,conn->connectpath);
581                 dos_GetWd(s);
582                 string_set(&conn->connectpath,s);
583                 dos_ChDir(conn->connectpath);
584         }
585 #endif
586         
587         add_session_user(user);
588                 
589         /* execute any "preexec = " line */
590         if (*lp_preexec(SNUM(conn))) {
591                 pstring cmd;
592                 pstrcpy(cmd,lp_preexec(SNUM(conn)));
593                 standard_sub_conn(conn,cmd);
594                 ret = smbrun(cmd,NULL,False);
595                 if (ret != 0 && lp_preexec_close(SNUM(conn))) {
596                         DEBUG(1,("preexec gave %d - failing connection\n", ret));
597                         conn_free(conn);
598                         *ecode = ERRsrverror;
599                         return NULL;
600                 }
601         }
602
603         /*
604          * Print out the 'connected as' stuff here as we need
605          * to know the effective uid and gid we will be using.
606          */
607
608         if( DEBUGLVL( IS_IPC(conn) ? 3 : 1 ) ) {
609                 dbgtext( "%s (%s) ", remote_machine, conn->client_address );
610                 dbgtext( "connect to service %s ", lp_servicename(SNUM(conn)) );
611                 dbgtext( "as user %s ", user );
612                 dbgtext( "(uid=%d, gid=%d) ", (int)geteuid(), (int)getegid() );
613                 dbgtext( "(pid %d)\n", (int)sys_getpid() );
614         }
615         
616         /* we've finished with the sensitive stuff */
617         unbecome_user();
618         
619         /* Add veto/hide lists */
620         if (!IS_IPC(conn) && !IS_PRINT(conn)) {
621                 set_namearray( &conn->veto_list, lp_veto_files(SNUM(conn)));
622                 set_namearray( &conn->hide_list, lp_hide_files(SNUM(conn)));
623                 set_namearray( &conn->veto_oplock_list, lp_veto_oplocks(SNUM(conn)));
624         }
625         
626         /* Invoke VFS make connection hook */
627
628         if (conn->vfs_ops.connect) {
629             if (conn->vfs_ops.connect(conn->vfs_conn, service, user) < 0) {
630                 return NULL;
631             }
632         }
633             
634         return(conn);
635 }
636
637
638 /****************************************************************************
639 close a cnum
640 ****************************************************************************/
641 void close_cnum(connection_struct *conn, uint16 vuid)
642 {
643         DirCacheFlush(SNUM(conn));
644
645         unbecome_user();
646
647         DEBUG(IS_IPC(conn)?3:1, ("%s (%s) closed connection to service %s\n",
648                                  remote_machine,conn->client_address,
649                                  lp_servicename(SNUM(conn))));
650
651         if (conn->vfs_ops.disconnect != NULL) {
652
653             /* Call VFS disconnect hook */
654             
655             conn->vfs_ops.disconnect();
656             
657         }
658
659         yield_connection(conn,
660                          lp_servicename(SNUM(conn)),
661                          lp_max_connections(SNUM(conn)));
662
663         if (lp_status(SNUM(conn)))
664                 yield_connection(conn,"",MAXSTATUS);
665
666         file_close_conn(conn);
667         dptr_closecnum(conn);
668
669         /* execute any "postexec = " line */
670         if (*lp_postexec(SNUM(conn)) && 
671             become_user(conn, vuid))  {
672                 pstring cmd;
673                 pstrcpy(cmd,lp_postexec(SNUM(conn)));
674                 standard_sub_conn(conn,cmd);
675                 smbrun(cmd,NULL,False);
676                 unbecome_user();
677         }
678
679         unbecome_user();
680         /* execute any "root postexec = " line */
681         if (*lp_rootpostexec(SNUM(conn)))  {
682                 pstring cmd;
683                 pstrcpy(cmd,lp_rootpostexec(SNUM(conn)));
684                 standard_sub_conn(conn,cmd);
685                 smbrun(cmd,NULL,False);
686         }
687         conn_free(conn);
688 }
689
690 #undef OLD_NTDOMAIN