2 Unix SMB/Netbios implementation.
4 service (connection) opening and closing
5 Copyright (C) Andrew Tridgell 1992-1998
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.
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.
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.
24 extern int DEBUGLEVEL;
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;
38 /****************************************************************************
39 load parameters specific to a connection/service
40 ****************************************************************************/
41 BOOL become_service(connection_struct *conn,BOOL do_chdir)
43 extern char magic_char;
44 static connection_struct *last_conn;
52 conn->lastused = smb_last_time;
57 dos_ChDir(conn->connectpath) != 0 &&
58 dos_ChDir(conn->origpath) != 0) {
59 DEBUG(0,("chdir (%s) failed\n",
64 if (conn == last_conn)
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);
80 /****************************************************************************
82 ****************************************************************************/
83 int find_service(char *service)
87 string_sub(service,"\\","/");
89 iService = lp_servicenumber(service);
91 /* now handle the special case of a home directory */
94 char *phome_dir = get_home_dir(service);
100 * Try mapping the servicename, it may
101 * be a Windows to unix mapped user name.
103 if(map_username(service))
104 phome_dir = get_home_dir(service);
107 DEBUG(3,("checking for home directory %s gave %s\n",service,
108 phome_dir?phome_dir:"(NULL)"));
113 pstrcpy(home_dir, phome_dir);
114 if ((iHomeService = lp_servicenumber(HOMES_NAME)) >= 0)
116 lp_add_home(service,iHomeService,home_dir);
117 iService = lp_servicenumber(service);
122 /* If we still don't have a service, attempt to add it as a printer. */
127 if ((iPrinterService = lp_servicenumber(PRINTERS_NAME)) >= 0)
131 DEBUG(3,("checking whether %s is a valid printer name...\n", service));
133 if ((pszTemp != NULL) && pcap_printername_ok(service, pszTemp))
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);
140 DEBUG(0,("failed to add %s as a printer service!\n", service));
143 DEBUG(3,("%s is not a valid printer name\n", service));
147 /* Check for default vfs service? Unsure whether to implement this */
152 /* just possibly it's a default service? */
155 char *pdefservice = lp_defaultservice();
156 if (pdefservice && *pdefservice &&
157 !strequal(pdefservice,service) &&
158 !strstr(service,".."))
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>.
167 pstrcpy(defservice, pdefservice);
168 iService = find_service(defservice);
171 string_sub(service,"_","/");
172 iService = lp_add_service(service,iService);
178 if (!VALID_SNUM(iService))
180 DEBUG(0,("Invalid snum %d for %s\n",iService,service));
185 DEBUG(3,("find_service() failed to find service %s\n", service));
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)
197 struct passwd *pass = NULL;
201 connection_struct *conn;
205 snum = find_service(service);
208 if (strequal(service,"IPC$")) {
209 DEBUG(3,("refusing IPC connection\n"));
214 DEBUG(0,("%s (%s) couldn't find service %s\n",
215 remote_machine, client_addr(Client), service));
216 *ecode = ERRinvnetname;
220 if (strequal(service,HOMES_NAME)) {
221 if (*user && Get_Pwnam(user,True))
222 return(make_connection(user,user,password,
223 pwlen,dev,vuid,ecode));
225 if(lp_security() != SEC_SHARE) {
226 if (validated_username(vuid)) {
227 pstrcpy(user,validated_username(vuid));
228 return(make_connection(user,user,password,pwlen,dev,vuid,ecode));
231 /* Security = share. Try with sesssetup_user
232 * as the username. */
233 if(*sesssetup_user) {
234 pstrcpy(user,sesssetup_user);
235 return(make_connection(user,user,password,pwlen,dev,vuid,ecode));
240 if (!lp_snum_ok(snum) ||
241 !check_access(Client,
242 lp_hostsallow(snum), lp_hostsdeny(snum))) {
247 /* you can only connect to the IPC$ service as an ipc device */
248 if (strequal(service,"IPC$"))
251 if (*dev == '?' || !*dev) {
252 if (lp_print_ok(snum)) {
253 pstrcpy(dev,"LPT1:");
259 /* if the request is as a printer and you can't print then refuse */
261 if (!lp_print_ok(snum) && (strncmp(dev,"LPT",3) == 0)) {
262 DEBUG(1,("Attempt to connect to non-printer as a printer\n"));
263 *ecode = ERRinvdevice;
267 /* lowercase the user name */
270 /* add it as a possible user name */
271 add_session_user(service);
273 /* shall we let them in? */
274 if (!authorise_login(snum,user,password,pwlen,&guest,&force,vuid)) {
275 DEBUG( 2, ( "Invalid username/password for %s\n", service ) );
282 DEBUG(0,("Couldn't find free connection.\n"));
283 *ecode = ERRnoresource;
288 /* find out some info about the user */
289 pass = Get_Pwnam(user,True);
292 DEBUG(0,( "Couldn't find account %s\n",user));
298 conn->read_only = lp_readonly(snum);
302 StrnCpy(list,lp_readlist(snum),sizeof(pstring)-1);
303 string_sub(list,"%S",service);
305 if (user_in_list(user,list))
306 conn->read_only = True;
308 StrnCpy(list,lp_writelist(snum),sizeof(pstring)-1);
309 string_sub(list,"%S",service);
311 if (user_in_list(user,list))
312 conn->read_only = False;
315 /* admin user check */
317 /* JRA - original code denied admin user if the share was
318 marked read_only. Changed as I don't think this is needed,
319 but old code left in case there is a problem here.
321 if (user_in_list(user,lp_admin_users(snum))
326 conn->admin_user = True;
327 DEBUG(0,("%s logged in as admin user (root privileges)\n",user));
329 conn->admin_user = False;
332 conn->force_user = force;
334 conn->uid = pass->pw_uid;
335 conn->gid = pass->pw_gid;
336 conn->num_files_open = 0;
337 conn->lastused = time(NULL);
338 conn->service = snum;
340 conn->printer = (strncmp(dev,"LPT",3) == 0);
341 conn->ipc = (strncmp(dev,"IPC",3) == 0);
343 conn->veto_list = NULL;
344 conn->hide_list = NULL;
345 conn->veto_oplock_list = NULL;
346 string_set(&conn->dirpath,"");
347 string_set(&conn->user,user);
349 /* Initialise VFS function pointers */
351 if (*lp_vfsobj(SNUM(conn))) {
353 /* Loadable object file */
355 if (vfs_init_custom(conn) < 0) {
361 /* Normal share - initialise with disk access functions */
363 vfs_init_default(conn);
367 if (*lp_force_group(snum)) {
371 StrnCpy(gname,lp_force_group(snum),sizeof(pstring)-1);
372 /* default service may be a group name */
373 string_sub(gname,"%S",service);
374 gptr = (struct group *)getgrnam(gname);
377 conn->gid = gptr->gr_gid;
378 DEBUG(3,("Forced group %s\n",gname));
380 DEBUG(1,("Couldn't find group %s\n",gname));
385 if (*lp_force_user(snum)) {
386 struct passwd *pass2;
388 fstrcpy(fuser,lp_force_user(snum));
389 pass2 = (struct passwd *)Get_Pwnam(fuser,True);
391 conn->uid = pass2->pw_uid;
392 string_set(&conn->user,fuser);
394 conn->force_user = True;
395 DEBUG(3,("Forced user %s\n",fuser));
397 DEBUG(1,("Couldn't find user %s\n",fuser));
403 pstrcpy(s,lp_pathname(snum));
404 standard_sub(conn,s);
405 string_set(&conn->connectpath,s);
406 DEBUG(3,("Connect path is %s\n",s));
409 /* groups stuff added by ih */
414 /* Find all the groups this uid is in and
415 store them. Used by become_user() */
416 get_unixgroups(conn->user,conn->uid,conn->gid,
417 &conn->ngroups,&conn->groups);
419 /* check number of connections */
420 if (!claim_connection(conn,
421 lp_servicename(SNUM(conn)),
422 lp_max_connections(SNUM(conn)),
424 DEBUG(1,("too many connections - rejected\n"));
425 *ecode = ERRnoresource;
430 if (lp_status(SNUM(conn)))
431 claim_connection(conn,"STATUS.",
435 /* execute any "root preexec = " line */
436 if (*lp_rootpreexec(SNUM(conn))) {
438 pstrcpy(cmd,lp_rootpreexec(SNUM(conn)));
439 standard_sub(conn,cmd);
440 DEBUG(5,("cmd=%s\n",cmd));
441 smbrun(cmd,NULL,False);
444 if (!become_user(conn, conn->vuid)) {
445 DEBUG(0,("Can't become connected user!\n"));
447 yield_connection(conn,
448 lp_servicename(SNUM(conn)),
449 lp_max_connections(SNUM(conn)));
450 if (lp_status(SNUM(conn))) {
451 yield_connection(conn,"STATUS.",MAXSTATUS);
459 if (dos_ChDir(conn->connectpath) != 0) {
460 DEBUG(0,("Can't change directory to %s (%s)\n",
461 conn->connectpath,strerror(errno)));
464 yield_connection(conn,
465 lp_servicename(SNUM(conn)),
466 lp_max_connections(SNUM(conn)));
467 if (lp_status(SNUM(conn)))
468 yield_connection(conn,"STATUS.",MAXSTATUS);
471 *ecode = ERRinvnetname;
475 string_set(&conn->origpath,conn->connectpath);
477 #if SOFTLINK_OPTIMISATION
478 /* resolve any soft links early */
481 pstrcpy(s,conn->connectpath);
483 string_set(&conn->connectpath,s);
484 dos_ChDir(conn->connectpath);
488 add_session_user(user);
490 /* execute any "preexec = " line */
491 if (*lp_preexec(SNUM(conn))) {
493 pstrcpy(cmd,lp_preexec(SNUM(conn)));
494 standard_sub(conn,cmd);
495 smbrun(cmd,NULL,False);
498 /* we've finished with the sensitive stuff */
501 /* Add veto/hide lists */
502 if (!IS_IPC(conn) && !IS_PRINT(conn)) {
503 set_namearray( &conn->veto_list, lp_veto_files(SNUM(conn)));
504 set_namearray( &conn->hide_list, lp_hide_files(SNUM(conn)));
505 set_namearray( &conn->veto_oplock_list, lp_veto_oplocks(SNUM(conn)));
508 if( DEBUGLVL( IS_IPC(conn) ? 3 : 1 ) ) {
511 dbgtext( "%s (%s) ", remote_machine, client_addr(Client) );
512 dbgtext( "connect to service %s ", lp_servicename(SNUM(conn)) );
513 dbgtext( "as user %s ", user );
514 dbgtext( "(uid=%d, gid=%d) ", (int)conn->uid, (int)conn->gid );
515 dbgtext( "(pid %d)\n", (int)getpid() );
518 /* Invoke make connection hook */
520 if (conn->vfs_ops.connect) {
521 if (conn->vfs_ops.connect(conn, service, user) < 0) {
530 /****************************************************************************
532 ****************************************************************************/
533 void close_cnum(connection_struct *conn, uint16 vuid)
536 DirCacheFlush(SNUM(conn));
540 DEBUG(IS_IPC(conn)?3:1, ("%s (%s) closed connection to service %s\n",
541 remote_machine,client_addr(Client),
542 lp_servicename(SNUM(conn))));
544 if (conn->vfs_ops.disconnect != NULL) {
545 conn->vfs_ops.disconnect(conn, lp_servicename(SNUM(conn)));
548 yield_connection(conn,
549 lp_servicename(SNUM(conn)),
550 lp_max_connections(SNUM(conn)));
552 if (lp_status(SNUM(conn)))
553 yield_connection(conn,"STATUS.",MAXSTATUS);
555 file_close_conn(conn);
556 dptr_closecnum(conn);
558 /* execute any "postexec = " line */
559 if (*lp_postexec(SNUM(conn)) &&
560 become_user(conn, vuid)) {
562 pstrcpy(cmd,lp_postexec(SNUM(conn)));
563 standard_sub(conn,cmd);
564 smbrun(cmd,NULL,False);
569 /* execute any "root postexec = " line */
570 if (*lp_rootpostexec(SNUM(conn))) {
572 pstrcpy(cmd,lp_rootpostexec(SNUM(conn)));
573 standard_sub(conn,cmd);
574 smbrun(cmd,NULL,False);