first pass at updating head branch to be to be the same as the SAMBA_2_0 branch
[gd/samba/.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    /* 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          all_string_sub(service,"_","/",0);
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         int ret;
196
197         strlower(service);
198
199         snum = find_service(service);
200         if (snum < 0) {
201                 extern int Client;
202                 if (strequal(service,"IPC$")) {
203                         DEBUG(3,("refusing IPC connection\n"));
204                         *ecode = ERRnoipc;
205                         return NULL;
206                 }
207
208                 DEBUG(0,("%s (%s) couldn't find service %s\n",
209                          remote_machine, client_addr(Client), service));
210                 *ecode = ERRinvnetname;
211                 return NULL;
212         }
213
214         if (strequal(service,HOMES_NAME)) {
215                 if (*user && Get_Pwnam(user,True)) {
216                         fstring dos_username;
217                         fstrcpy(dos_username, user);
218                         unix_to_dos(dos_username, True);
219                         return(make_connection(dos_username,user,password,
220                                                pwlen,dev,vuid,ecode));
221                 }
222
223                 if(lp_security() != SEC_SHARE) {
224                         if (validated_username(vuid)) {
225                                 fstring dos_username;
226                                 fstrcpy(user,validated_username(vuid));
227                                 fstrcpy(dos_username, user);
228                                 unix_to_dos(dos_username, True);
229                                 return(make_connection(dos_username,user,password,pwlen,dev,vuid,ecode));
230                         }
231                 } else {
232                         /* Security = share. Try with sesssetup_user
233                          * as the username.  */
234                         if(*sesssetup_user) {
235                                 fstring dos_username;
236                                 fstrcpy(user,sesssetup_user);
237                                 fstrcpy(dos_username, user);
238                                 unix_to_dos(dos_username, True);
239                                 return(make_connection(dos_username,user,password,pwlen,dev,vuid,ecode));
240                         }
241                 }
242         }
243
244         if (!lp_snum_ok(snum) || 
245             !check_access(Client, 
246                           lp_hostsallow(snum), lp_hostsdeny(snum))) {    
247                 *ecode = ERRaccess;
248                 return NULL;
249         }
250
251         /* you can only connect to the IPC$ service as an ipc device */
252         if (strequal(service,"IPC$"))
253                 pstrcpy(dev,"IPC");
254         
255         if (*dev == '?' || !*dev) {
256                 if (lp_print_ok(snum)) {
257                         pstrcpy(dev,"LPT1:");
258                 } else {
259                         pstrcpy(dev,"A:");
260                 }
261         }
262
263         /* if the request is as a printer and you can't print then refuse */
264         strupper(dev);
265         if (!lp_print_ok(snum) && (strncmp(dev,"LPT",3) == 0)) {
266                 DEBUG(1,("Attempt to connect to non-printer as a printer\n"));
267                 *ecode = ERRinvdevice;
268                 return NULL;
269         }
270
271         /* lowercase the user name */
272         strlower(user);
273
274         /* add it as a possible user name */
275         add_session_user(service);
276
277         /* shall we let them in? */
278         if (!authorise_login(snum,user,password,pwlen,&guest,&force,vuid)) {
279                 DEBUG( 2, ( "Invalid username/password for %s\n", service ) );
280                 *ecode = ERRbadpw;
281                 return NULL;
282         }
283   
284         conn = conn_new();
285         if (!conn) {
286                 DEBUG(0,("Couldn't find free connection.\n"));
287                 *ecode = ERRnoresource;
288                 conn_free(conn);
289                 return NULL;
290         }
291
292         /* find out some info about the user */
293         pass = Get_Pwnam(user,True);
294
295         if (pass == NULL) {
296                 DEBUG(0,( "Couldn't find account %s\n",user));
297                 *ecode = ERRbaduid;
298                 conn_free(conn);
299                 return NULL;
300         }
301
302         conn->read_only = lp_readonly(snum);
303
304         {
305                 pstring list;
306                 StrnCpy(list,lp_readlist(snum),sizeof(pstring)-1);
307                 pstring_sub(list,"%S",service);
308
309                 if (user_in_list(user,list))
310                         conn->read_only = True;
311                 
312                 StrnCpy(list,lp_writelist(snum),sizeof(pstring)-1);
313                 pstring_sub(list,"%S",service);
314                 
315                 if (user_in_list(user,list))
316                         conn->read_only = False;    
317         }
318
319         /* admin user check */
320         
321         /* JRA - original code denied admin user if the share was
322            marked read_only. Changed as I don't think this is needed,
323            but old code left in case there is a problem here.
324         */
325         if (user_in_list(user,lp_admin_users(snum)) 
326 #if 0
327             && !conn->read_only
328 #endif
329             ) {
330                 conn->admin_user = True;
331                 DEBUG(0,("%s logged in as admin user (root privileges)\n",user));
332         } else {
333                 conn->admin_user = False;
334         }
335     
336         conn->force_user = force;
337         conn->vuid = vuid;
338         conn->uid = pass->pw_uid;
339         conn->gid = pass->pw_gid;
340         safe_strcpy(conn->client_address, client_addr(Client), sizeof(conn->client_address)-1);
341         conn->num_files_open = 0;
342         conn->lastused = time(NULL);
343         conn->service = snum;
344         conn->used = True;
345         conn->printer = (strncmp(dev,"LPT",3) == 0);
346         conn->ipc = (strncmp(dev,"IPC",3) == 0);
347         conn->dirptr = NULL;
348         conn->veto_list = NULL;
349         conn->hide_list = NULL;
350         conn->veto_oplock_list = NULL;
351         string_set(&conn->dirpath,"");
352         string_set(&conn->user,user);
353         
354         /*
355          * If force user is true, then store the
356          * given userid and also the primary groupid
357          * of the user we're forcing.
358          */
359         
360         if (*lp_force_user(snum)) {
361                 struct passwd *pass2;
362                 pstring fuser;
363                 pstrcpy(fuser,lp_force_user(snum));
364
365                 /* Allow %S to be used by force user. */
366                 pstring_sub(fuser,"%S",service);
367
368                 pass2 = (struct passwd *)Get_Pwnam(fuser,True);
369                 if (pass2) {
370                         conn->uid = pass2->pw_uid;
371                         conn->gid = pass2->pw_gid;
372                         string_set(&conn->user,fuser);
373                         fstrcpy(user,fuser);
374                         conn->force_user = True;
375                         DEBUG(3,("Forced user %s\n",fuser));      
376                 } else {
377                         DEBUG(1,("Couldn't find user %s\n",fuser));
378                 }
379         }
380
381 #ifdef HAVE_GETGRNAM 
382         /*
383          * If force group is true, then override
384          * any groupid stored for the connecting user.
385          */
386         
387         if (*lp_force_group(snum)) {
388                 struct group *gptr;
389                 pstring gname;
390                 pstring tmp_gname;
391                 BOOL user_must_be_member = False;
392                 
393                 StrnCpy(tmp_gname,lp_force_group(snum),sizeof(pstring)-1);
394
395                 if (tmp_gname[0] == '+') {
396                         user_must_be_member = True;
397                         StrnCpy(gname,&tmp_gname[1],sizeof(pstring)-2);
398                 } else {
399                         StrnCpy(gname,tmp_gname,sizeof(pstring)-1);
400                 }
401                 /* default service may be a group name          */
402                 pstring_sub(gname,"%S",service);
403                 gptr = (struct group *)getgrnam(gname);
404                 
405                 if (gptr) {
406                         /*
407                          * If the user has been forced and the forced group starts
408                          * with a '+', then we only set the group to be the forced
409                          * group if the forced user is a member of that group.
410                          * Otherwise, the meaning of the '+' would be ignored.
411                          */
412                         if (conn->force_user && user_must_be_member) {
413                                 int i;
414                                 for (i = 0; gptr->gr_mem[i] != NULL; i++) {
415                                         if (strcmp(user,gptr->gr_mem[i]) == 0) {
416                                                 conn->gid = gptr->gr_gid;
417                                                 DEBUG(3,("Forced group %s for member %s\n",gname,user));
418                                                 break;
419                                         }
420                                 }
421                         } else {
422                                 conn->gid = gptr->gr_gid;
423                                 DEBUG(3,("Forced group %s\n",gname));
424                         }
425                 } else {
426                         DEBUG(1,("Couldn't find group %s\n",gname));
427                 }
428         }
429 #endif /* HAVE_GETGRNAM */
430
431         {
432                 pstring s;
433                 pstrcpy(s,lp_pathname(snum));
434                 standard_sub(conn,s);
435                 string_set(&conn->connectpath,s);
436                 DEBUG(3,("Connect path is %s\n",s));
437         }
438
439         /* groups stuff added by ih */
440         conn->ngroups = 0;
441         conn->groups = NULL;
442         
443         if (!IS_IPC(conn)) {
444                 /* Find all the groups this uid is in and
445                    store them. Used by become_user() */
446                 setup_groups(conn->user,conn->uid,conn->gid,
447                              &conn->ngroups,&conn->groups);
448                 
449                 /* check number of connections */
450                 if (!claim_connection(conn,
451                                       lp_servicename(SNUM(conn)),
452                                       lp_max_connections(SNUM(conn)),
453                                       False)) {
454                         DEBUG(1,("too many connections - rejected\n"));
455                         *ecode = ERRnoresource;
456                         conn_free(conn);
457                         return NULL;
458                 }  
459                 
460                 if (lp_status(SNUM(conn)))
461                         claim_connection(conn,"STATUS.",
462                                          MAXSTATUS,False);
463         } /* IS_IPC */
464         
465         /* execute any "root preexec = " line */
466         if (*lp_rootpreexec(SNUM(conn))) {
467                 pstring cmd;
468                 pstrcpy(cmd,lp_rootpreexec(SNUM(conn)));
469                 standard_sub(conn,cmd);
470                 DEBUG(5,("cmd=%s\n",cmd));
471                 ret = smbrun(cmd,NULL,False);
472                 if (ret != 0 && lp_rootpreexec_close(SNUM(conn))) {
473                         DEBUG(1,("preexec gave %d - failing connection\n", ret));
474                         conn_free(conn);
475                         *ecode = ERRsrverror;
476                         return NULL;
477                 }
478         }
479         
480         if (!become_user(conn, conn->vuid)) {
481                 DEBUG(0,("Can't become connected user!\n"));
482                 if (!IS_IPC(conn)) {
483                         yield_connection(conn,
484                                          lp_servicename(SNUM(conn)),
485                                          lp_max_connections(SNUM(conn)));
486                         if (lp_status(SNUM(conn))) {
487                                 yield_connection(conn,"STATUS.",MAXSTATUS);
488                         }
489                 }
490                 conn_free(conn);
491                 *ecode = ERRbadpw;
492                 return NULL;
493         }
494         
495         if (dos_ChDir(conn->connectpath) != 0) {
496                 DEBUG(0,("Can't change directory to %s (%s)\n",
497                          conn->connectpath,strerror(errno)));
498                 unbecome_user();
499                 if (!IS_IPC(conn)) {
500                         yield_connection(conn,
501                                          lp_servicename(SNUM(conn)),
502                                          lp_max_connections(SNUM(conn)));
503                         if (lp_status(SNUM(conn))) 
504                                 yield_connection(conn,"STATUS.",MAXSTATUS);
505                 }
506                 conn_free(conn);
507                 *ecode = ERRinvnetname;
508                 return NULL;
509         }
510         
511         string_set(&conn->origpath,conn->connectpath);
512         
513 #if SOFTLINK_OPTIMISATION
514         /* resolve any soft links early */
515         {
516                 pstring s;
517                 pstrcpy(s,conn->connectpath);
518                 dos_GetWd(s);
519                 string_set(&conn->connectpath,s);
520                 dos_ChDir(conn->connectpath);
521         }
522 #endif
523         
524         add_session_user(user);
525                 
526         /* execute any "preexec = " line */
527         if (*lp_preexec(SNUM(conn))) {
528                 pstring cmd;
529                 pstrcpy(cmd,lp_preexec(SNUM(conn)));
530                 standard_sub(conn,cmd);
531                 ret = smbrun(cmd,NULL,False);
532                 if (ret != 0 && lp_preexec_close(SNUM(conn))) {
533                         DEBUG(1,("preexec gave %d - failing connection\n", ret));
534                         conn_free(conn);
535                         *ecode = ERRsrverror;
536                         return NULL;
537                 }
538         }
539
540         /*
541          * Print out the 'connected as' stuff here as we need
542          * to know the effective uid and gid we will be using.
543          */
544
545         if( DEBUGLVL( IS_IPC(conn) ? 3 : 1 ) ) {
546                 dbgtext( "%s (%s) ", remote_machine, conn->client_address );
547                 dbgtext( "connect to service %s ", lp_servicename(SNUM(conn)) );
548                 dbgtext( "as user %s ", user );
549                 dbgtext( "(uid=%d, gid=%d) ", (int)geteuid(), (int)getegid() );
550                 dbgtext( "(pid %d)\n", (int)getpid() );
551         }
552         
553         /* we've finished with the sensitive stuff */
554         unbecome_user();
555         
556         /* Add veto/hide lists */
557         if (!IS_IPC(conn) && !IS_PRINT(conn)) {
558                 set_namearray( &conn->veto_list, lp_veto_files(SNUM(conn)));
559                 set_namearray( &conn->hide_list, lp_hide_files(SNUM(conn)));
560                 set_namearray( &conn->veto_oplock_list, lp_veto_oplocks(SNUM(conn)));
561         }
562         
563         return(conn);
564 }
565
566
567 /****************************************************************************
568 close a cnum
569 ****************************************************************************/
570 void close_cnum(connection_struct *conn, uint16 vuid)
571 {
572         DirCacheFlush(SNUM(conn));
573
574         unbecome_user();
575
576         DEBUG(IS_IPC(conn)?3:1, ("%s (%s) closed connection to service %s\n",
577                                  remote_machine,conn->client_address,
578                                  lp_servicename(SNUM(conn))));
579
580         yield_connection(conn,
581                          lp_servicename(SNUM(conn)),
582                          lp_max_connections(SNUM(conn)));
583
584         if (lp_status(SNUM(conn)))
585                 yield_connection(conn,"STATUS.",MAXSTATUS);
586
587         file_close_conn(conn);
588         dptr_closecnum(conn);
589
590         /* execute any "postexec = " line */
591         if (*lp_postexec(SNUM(conn)) && 
592             become_user(conn, vuid))  {
593                 pstring cmd;
594                 pstrcpy(cmd,lp_postexec(SNUM(conn)));
595                 standard_sub(conn,cmd);
596                 smbrun(cmd,NULL,False);
597                 unbecome_user();
598         }
599
600         unbecome_user();
601         /* execute any "root postexec = " line */
602         if (*lp_rootpostexec(SNUM(conn)))  {
603                 pstring cmd;
604                 pstrcpy(cmd,lp_rootpostexec(SNUM(conn)));
605                 standard_sub(conn,cmd);
606                 smbrun(cmd,NULL,False);
607         }
608         
609         conn_free(conn);
610 }