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