Changes from APPLIANCE_HEAD:
[ira/wip.git] / source3 / 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             vfs_ChDir(conn,conn->connectpath) != 0 &&
60             vfs_ChDir(conn,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 = ERRnosuchshare;
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         /* Behave as a printer if we are supposed to */
277         if (lp_print_ok(snum) && (strcmp(dev, "A:") == 0)) {
278                 pstrcpy(dev, "LPT1:");
279         }
280
281         /* lowercase the user name */
282         strlower(user);
283
284         /* add it as a possible user name */
285         add_session_user(service);
286
287         /* shall we let them in? */
288         if (!authorise_login(snum,user,password,pwlen,&guest,&force,vuid)) {
289                 DEBUG( 2, ( "Invalid username/password for %s [%s]\n", service, user ) );
290                 *ecode = ERRbadpw;
291                 return NULL;
292         }
293   
294         conn = conn_new();
295         if (!conn) {
296                 DEBUG(0,("Couldn't find free connection.\n"));
297                 *ecode = ERRnoresource;
298                 conn_free(conn);
299                 return NULL;
300         }
301
302         /* find out some info about the user */
303         pass = smb_getpwnam(user,True);
304
305         if (pass == NULL) {
306                 DEBUG(0,( "Couldn't find account %s\n",user));
307                 *ecode = ERRbaduid;
308                 conn_free(conn);
309                 return NULL;
310         }
311
312         conn->read_only = lp_readonly(snum);
313
314         {
315                 pstring list;
316                 StrnCpy(list,lp_readlist(snum),sizeof(pstring)-1);
317                 pstring_sub(list,"%S",service);
318
319                 if (user_in_list(user,list))
320                         conn->read_only = True;
321                 
322                 StrnCpy(list,lp_writelist(snum),sizeof(pstring)-1);
323                 pstring_sub(list,"%S",service);
324                 
325                 if (user_in_list(user,list))
326                         conn->read_only = False;    
327         }
328
329         /* admin user check */
330         
331         /* JRA - original code denied admin user if the share was
332            marked read_only. Changed as I don't think this is needed,
333            but old code left in case there is a problem here.
334         */
335         if (user_in_list(user,lp_admin_users(snum)) 
336 #if 0
337             && !conn->read_only
338 #endif
339             ) {
340                 conn->admin_user = True;
341                 DEBUG(0,("%s logged in as admin user (root privileges)\n",user));
342         } else {
343                 conn->admin_user = False;
344         }
345     
346         conn->force_user = force;
347         conn->vuid = vuid;
348         conn->uid = pass->pw_uid;
349         conn->gid = pass->pw_gid;
350         safe_strcpy(conn->client_address, client_addr(), sizeof(conn->client_address)-1);
351         conn->num_files_open = 0;
352         conn->lastused = time(NULL);
353         conn->service = snum;
354         conn->used = True;
355         conn->printer = (strncmp(dev,"LPT",3) == 0);
356         conn->ipc = (strncmp(dev,"IPC",3) == 0);
357         conn->dirptr = NULL;
358         conn->veto_list = NULL;
359         conn->hide_list = NULL;
360         conn->veto_oplock_list = NULL;
361         string_set(&conn->dirpath,"");
362         string_set(&conn->user,user);
363         conn->nt_user_token = NULL;
364         
365         /*
366          * If force user is true, then store the
367          * given userid and also the primary groupid
368          * of the user we're forcing.
369          */
370         
371         if (*lp_force_user(snum)) {
372                 struct passwd *pass2;
373                 pstring fuser;
374                 pstrcpy(fuser,lp_force_user(snum));
375
376                 /* Allow %S to be used by force user. */
377                 pstring_sub(fuser,"%S",service);
378
379                 pass2 = (struct passwd *)Get_Pwnam(fuser,True);
380                 if (pass2) {
381                         conn->uid = pass2->pw_uid;
382                         conn->gid = pass2->pw_gid;
383                         string_set(&conn->user,fuser);
384                         fstrcpy(user,fuser);
385                         conn->force_user = True;
386                         DEBUG(3,("Forced user %s\n",fuser));      
387                 } else {
388                         DEBUG(1,("Couldn't find user %s\n",fuser));
389                 }
390         }
391
392         /* admin users always run as uid=0 */
393         if (conn->admin_user) {
394                 conn->uid = 0;
395         }
396
397 #ifdef HAVE_GETGRNAM 
398         /*
399          * If force group is true, then override
400          * any groupid stored for the connecting user.
401          */
402         
403         if (*lp_force_group(snum)) {
404                 gid_t gid;
405                 pstring gname;
406                 pstring tmp_gname;
407                 BOOL user_must_be_member = False;
408                 
409                 StrnCpy(tmp_gname,lp_force_group(snum),sizeof(pstring)-1);
410
411                 if (tmp_gname[0] == '+') {
412                         user_must_be_member = True;
413                         StrnCpy(gname,&tmp_gname[1],sizeof(pstring)-2);
414                 } else {
415                         StrnCpy(gname,tmp_gname,sizeof(pstring)-1);
416                 }
417                 /* default service may be a group name          */
418                 pstring_sub(gname,"%S",service);
419                 gid = nametogid(gname);
420                 
421                 if (gid != (gid_t)-1) {
422                         /*
423                          * If the user has been forced and the forced group starts
424                          * with a '+', then we only set the group to be the forced
425                          * group if the forced user is a member of that group.
426                          * Otherwise, the meaning of the '+' would be ignored.
427                          */
428                         if (conn->force_user && user_must_be_member) {
429                                 if (user_in_group_list( user, gname )) {
430                                                 conn->gid = gid;
431                                                 DEBUG(3,("Forced group %s for member %s\n",gname,user));
432                                 }
433                         } else {
434                                 conn->gid = gid;
435                                 DEBUG(3,("Forced group %s\n",gname));
436                         }
437                 } else {
438                         DEBUG(1,("Couldn't find group %s\n",gname));
439                 }
440         }
441 #endif /* HAVE_GETGRNAM */
442
443         {
444                 pstring s;
445                 pstrcpy(s,lp_pathname(snum));
446                 standard_sub_conn(conn,s);
447                 string_set(&conn->connectpath,s);
448                 DEBUG(3,("Connect path is %s\n",s));
449         }
450
451         /* groups stuff added by ih */
452         conn->ngroups = 0;
453         conn->groups = NULL;
454         
455         /* Find all the groups this uid is in and
456            store them. Used by become_user() */
457         initialise_groups(conn->user, conn->uid, conn->gid); 
458         get_current_groups(&conn->ngroups,&conn->groups);
459                 
460         /* check number of connections */
461         if (!claim_connection(conn,
462                               lp_servicename(SNUM(conn)),
463                               lp_max_connections(SNUM(conn)),
464                               False)) {
465                 DEBUG(1,("too many connections - rejected\n"));
466                 *ecode = ERRnoresource;
467                 conn_free(conn);
468                 return NULL;
469         }  
470                 
471         conn->nt_user_token = create_nt_token(conn->uid, conn->gid, 
472                                               conn->ngroups, conn->groups,
473                                               guest);
474
475         /* Initialise VFS function pointers */
476
477         if (*lp_vfsobj(SNUM(conn))) {
478
479 #ifdef HAVE_LIBDL
480
481             /* Loadable object file */
482
483             if (!vfs_init_custom(conn)) {
484                 DEBUG(0, ("vfs_init failed\n"));
485                     conn_free(conn);
486                         return NULL;
487             }
488 #else
489             DEBUG(0, ("No libdl present - cannot use VFS objects\n"));
490             conn_free(conn);
491             return NULL;
492 #endif
493
494         } else {
495
496             /* Normal share - initialise with disk access functions */
497
498             vfs_init_default(conn);
499         }
500
501         /* execute any "root preexec = " line */
502         if (*lp_rootpreexec(SNUM(conn))) {
503                 pstring cmd;
504                 pstrcpy(cmd,lp_rootpreexec(SNUM(conn)));
505                 standard_sub_conn(conn,cmd);
506                 DEBUG(5,("cmd=%s\n",cmd));
507                 ret = smbrun(cmd,NULL,False);
508                 if (ret != 0 && lp_rootpreexec_close(SNUM(conn))) {
509                         DEBUG(1,("preexec gave %d - failing connection\n", ret));
510                         conn_free(conn);
511                         *ecode = ERRsrverror;
512                         return NULL;
513                 }
514         }
515         
516         if (!become_user(conn, conn->vuid)) {
517                 DEBUG(0,("Can't become connected user!\n"));
518                 yield_connection(conn,
519                                  lp_servicename(SNUM(conn)),
520                                  lp_max_connections(SNUM(conn)));
521                 conn_free(conn);
522                 *ecode = ERRbadpw;
523                 return NULL;
524         }
525         
526         if (vfs_ChDir(conn,conn->connectpath) != 0) {
527                 DEBUG(0,("Can't change directory to %s (%s)\n",
528                          conn->connectpath,strerror(errno)));
529                 unbecome_user();
530                 yield_connection(conn,
531                                  lp_servicename(SNUM(conn)),
532                                  lp_max_connections(SNUM(conn)));
533                 conn_free(conn);
534                 *ecode = ERRnosuchshare;
535                 return NULL;
536         }
537         
538         string_set(&conn->origpath,conn->connectpath);
539         
540 #if SOFTLINK_OPTIMISATION
541         /* resolve any soft links early */
542         {
543                 pstring s;
544                 pstrcpy(s,conn->connectpath);
545                 vfs_GetWd(conn,s);
546                 string_set(&conn->connectpath,s);
547                 vfs_ChDir(conn,conn->connectpath);
548         }
549 #endif
550         
551         add_session_user(user);
552                 
553         /* execute any "preexec = " line */
554         if (*lp_preexec(SNUM(conn))) {
555                 pstring cmd;
556                 pstrcpy(cmd,lp_preexec(SNUM(conn)));
557                 standard_sub_conn(conn,cmd);
558                 ret = smbrun(cmd,NULL,False);
559                 if (ret != 0 && lp_preexec_close(SNUM(conn))) {
560                         DEBUG(1,("preexec gave %d - failing connection\n", ret));
561                         conn_free(conn);
562                         *ecode = ERRsrverror;
563                         return NULL;
564                 }
565         }
566
567         /*
568          * Print out the 'connected as' stuff here as we need
569          * to know the effective uid and gid we will be using.
570          */
571
572         if( DEBUGLVL( IS_IPC(conn) ? 3 : 1 ) ) {
573                 dbgtext( "%s (%s) ", remote_machine, conn->client_address );
574                 dbgtext( "connect to service %s ", lp_servicename(SNUM(conn)) );
575                 dbgtext( "as user %s ", user );
576                 dbgtext( "(uid=%d, gid=%d) ", (int)geteuid(), (int)getegid() );
577                 dbgtext( "(pid %d)\n", (int)sys_getpid() );
578         }
579         
580         /* we've finished with the sensitive stuff */
581         unbecome_user();
582         
583         /* Add veto/hide lists */
584         if (!IS_IPC(conn) && !IS_PRINT(conn)) {
585                 set_namearray( &conn->veto_list, lp_veto_files(SNUM(conn)));
586                 set_namearray( &conn->hide_list, lp_hide_files(SNUM(conn)));
587                 set_namearray( &conn->veto_oplock_list, lp_veto_oplocks(SNUM(conn)));
588         }
589         
590         /* Invoke VFS make connection hook */
591
592         if (conn->vfs_ops.connect) {
593                 if (conn->vfs_ops.connect(conn, service, user) < 0)
594                         return NULL;
595         }
596             
597         return(conn);
598 }
599
600
601 /****************************************************************************
602 close a cnum
603 ****************************************************************************/
604 void close_cnum(connection_struct *conn, uint16 vuid)
605 {
606         DirCacheFlush(SNUM(conn));
607
608         unbecome_user();
609
610         DEBUG(IS_IPC(conn)?3:1, ("%s (%s) closed connection to service %s\n",
611                                  remote_machine,conn->client_address,
612                                  lp_servicename(SNUM(conn))));
613
614         if (conn->vfs_ops.disconnect != NULL) {
615
616             /* Call VFS disconnect hook */
617             
618             conn->vfs_ops.disconnect(conn);
619             
620         }
621
622         yield_connection(conn,
623                          lp_servicename(SNUM(conn)),
624                          lp_max_connections(SNUM(conn)));
625
626         file_close_conn(conn);
627         dptr_closecnum(conn);
628
629         /* execute any "postexec = " line */
630         if (*lp_postexec(SNUM(conn)) && 
631             become_user(conn, vuid))  {
632                 pstring cmd;
633                 pstrcpy(cmd,lp_postexec(SNUM(conn)));
634                 standard_sub_conn(conn,cmd);
635                 smbrun(cmd,NULL,False);
636                 unbecome_user();
637         }
638
639         unbecome_user();
640         /* execute any "root postexec = " line */
641         if (*lp_rootpostexec(SNUM(conn)))  {
642                 pstring cmd;
643                 pstrcpy(cmd,lp_rootpostexec(SNUM(conn)));
644                 standard_sub_conn(conn,cmd);
645                 smbrun(cmd,NULL,False);
646         }
647         conn_free(conn);
648 }
649
650 #undef OLD_NTDOMAIN