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 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;
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.tv_sec;
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 all_string_sub(service,"\\","/",0);
89 iService = lp_servicenumber(service);
91 /* now handle the special case of a home directory */
94 char *phome_dir = get_user_home_dir(service);
99 * Try mapping the servicename, it may
100 * be a Windows to unix mapped user name.
102 if(map_username(service))
103 phome_dir = get_user_home_dir(service);
106 DEBUG(3,("checking for home directory %s gave %s\n",service,
107 phome_dir?phome_dir:"(NULL)"));
112 if ((iHomeService = lp_servicenumber(HOMES_NAME)) >= 0)
114 lp_add_home(service,iHomeService,phome_dir);
115 iService = lp_servicenumber(service);
120 /* If we still don't have a service, attempt to add it as a printer. */
125 if ((iPrinterService = lp_servicenumber(PRINTERS_NAME)) >= 0)
129 DEBUG(3,("checking whether %s is a valid printer name...\n", service));
131 if ((pszTemp != NULL) && pcap_printername_ok(service, pszTemp))
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);
138 DEBUG(0,("failed to add %s as a printer service!\n", service));
141 DEBUG(3,("%s is not a valid printer name\n", service));
145 /* Check for default vfs service? Unsure whether to implement this */
150 /* just possibly it's a default service? */
153 char *pdefservice = lp_defaultservice();
154 if (pdefservice && *pdefservice &&
155 !strequal(pdefservice,service) &&
156 !strstr(service,".."))
159 * We need to do a local copy here as lp_defaultservice()
160 * returns one of the rotating lp_string buffers that
161 * could get overwritten by the recursive find_service() call
162 * below. Fix from Josef Hinteregger <joehtg@joehtg.co.at>.
165 pstrcpy(defservice, pdefservice);
166 iService = find_service(defservice);
169 all_string_sub(service,"_","/",0);
170 iService = lp_add_service(service,iService);
176 if (!VALID_SNUM(iService))
178 DEBUG(0,("Invalid snum %d for %s\n",iService,service));
183 DEBUG(3,("find_service() failed to find service %s\n", service));
189 /****************************************************************************
190 make a connection to a service
191 ****************************************************************************/
192 connection_struct *make_connection(char *service,char *user,char *password, int pwlen, char *dev,uint16 vuid, int *ecode)
195 struct passwd *pass = NULL;
198 connection_struct *conn;
203 snum = find_service(service);
205 if (strequal(service,"IPC$")) {
206 DEBUG(3,("refusing IPC connection\n"));
211 DEBUG(0,("%s (%s) couldn't find service %s\n",
212 remote_machine, client_addr(), service));
213 *ecode = ERRinvnetname;
217 if (strequal(service,HOMES_NAME)) {
218 if (*user && Get_Pwnam(user,True)) {
219 fstring dos_username;
220 fstrcpy(dos_username, user);
221 unix_to_dos(dos_username, True);
222 return(make_connection(dos_username,user,password,
223 pwlen,dev,vuid,ecode));
226 if(lp_security() != SEC_SHARE) {
227 if (validated_username(vuid)) {
228 fstring dos_username;
229 fstrcpy(user,validated_username(vuid));
230 fstrcpy(dos_username, user);
231 unix_to_dos(dos_username, True);
232 return(make_connection(dos_username,user,password,pwlen,dev,vuid,ecode));
235 /* Security = share. Try with sesssetup_user
236 * as the username. */
237 if(*sesssetup_user) {
238 fstring dos_username;
239 fstrcpy(user,sesssetup_user);
240 fstrcpy(dos_username, user);
241 unix_to_dos(dos_username, True);
242 return(make_connection(dos_username,user,password,pwlen,dev,vuid,ecode));
247 if (!lp_snum_ok(snum) ||
248 !check_access(smbd_server_fd(),
249 lp_hostsallow(snum), lp_hostsdeny(snum))) {
254 /* you can only connect to the IPC$ service as an ipc device */
255 if (strequal(service,"IPC$"))
258 if (*dev == '?' || !*dev) {
259 if (lp_print_ok(snum)) {
260 pstrcpy(dev,"LPT1:");
266 /* if the request is as a printer and you can't print then refuse */
268 if (!lp_print_ok(snum) && (strncmp(dev,"LPT",3) == 0)) {
269 DEBUG(1,("Attempt to connect to non-printer as a printer\n"));
270 *ecode = ERRinvdevice;
274 /* lowercase the user name */
277 /* add it as a possible user name */
278 add_session_user(service);
280 /* shall we let them in? */
281 if (!authorise_login(snum,user,password,pwlen,&guest,&force,vuid)) {
282 DEBUG( 2, ( "Invalid username/password for %s\n", service ) );
289 DEBUG(0,("Couldn't find free connection.\n"));
290 *ecode = ERRnoresource;
295 /* find out some info about the user */
296 pass = Get_Pwnam(user,True);
299 DEBUG(0,( "Couldn't find account %s\n",user));
305 conn->read_only = lp_readonly(snum);
309 StrnCpy(list,lp_readlist(snum),sizeof(pstring)-1);
310 pstring_sub(list,"%S",service);
312 if (user_in_list(user,list))
313 conn->read_only = True;
315 StrnCpy(list,lp_writelist(snum),sizeof(pstring)-1);
316 pstring_sub(list,"%S",service);
318 if (user_in_list(user,list))
319 conn->read_only = False;
322 /* admin user check */
324 /* JRA - original code denied admin user if the share was
325 marked read_only. Changed as I don't think this is needed,
326 but old code left in case there is a problem here.
328 if (user_in_list(user,lp_admin_users(snum))
333 conn->admin_user = True;
334 DEBUG(0,("%s logged in as admin user (root privileges)\n",user));
336 conn->admin_user = False;
339 conn->force_user = force;
341 conn->uid = pass->pw_uid;
342 conn->gid = pass->pw_gid;
343 safe_strcpy(conn->client_address, client_addr(), sizeof(conn->client_address)-1);
344 conn->num_files_open = 0;
345 conn->lastused = time(NULL);
346 conn->service = snum;
348 conn->printer = (strncmp(dev,"LPT",3) == 0);
349 conn->ipc = (strncmp(dev,"IPC",3) == 0);
351 conn->veto_list = NULL;
352 conn->hide_list = NULL;
353 conn->veto_oplock_list = NULL;
354 string_set(&conn->dirpath,"");
355 string_set(&conn->user,user);
357 conn->vfs_conn = (struct vfs_connection_struct *)
358 malloc(sizeof(struct vfs_connection_struct));
360 if (conn->vfs_conn == NULL) {
361 DEBUG(0, ("No memory to create vfs_connection_struct"));
365 ZERO_STRUCTP(conn->vfs_conn);
367 /* Copy across relevant data from connection struct */
369 conn->vfs_conn->printer = conn->printer;
370 conn->vfs_conn->ipc = conn->ipc;
371 conn->vfs_conn->read_only = conn->read_only;
372 conn->vfs_conn->admin_user = conn->admin_user;
374 pstrcpy(conn->vfs_conn->dirpath, conn->dirpath);
375 pstrcpy(conn->vfs_conn->connectpath, conn->connectpath);
376 pstrcpy(conn->vfs_conn->origpath, conn->origpath);
378 pstrcpy(conn->vfs_conn->service, service);
379 pstrcpy(conn->vfs_conn->user, conn->user);
381 conn->vfs_conn->uid = conn->uid;
382 conn->vfs_conn->gid = conn->gid;
383 conn->vfs_conn->ngroups = conn->ngroups;
384 if (conn->vfs_conn->ngroups != 0) {
385 conn->vfs_conn->groups = (gid_t *)memdup(conn->groups,
386 conn->ngroups * sizeof(gid_t));
388 conn->vfs_conn->groups = NULL;
391 /* Initialise VFS function pointers */
393 if (*lp_vfsobj(SNUM(conn))) {
397 /* Loadable object file */
399 if (!vfs_init_custom(conn)) {
403 DEBUG(0, ("No libdl present - cannot use VFS objects\n"));
410 /* Normal share - initialise with disk access functions */
412 vfs_init_default(conn);
416 * If force user is true, then store the
417 * given userid and also the primary groupid
418 * of the user we're forcing.
421 if (*lp_force_user(snum)) {
422 struct passwd *pass2;
424 pstrcpy(fuser,lp_force_user(snum));
426 /* Allow %S to be used by force user. */
427 pstring_sub(fuser,"%S",service);
429 pass2 = (struct passwd *)Get_Pwnam(fuser,True);
431 conn->uid = pass2->pw_uid;
432 conn->gid = pass2->pw_gid;
433 string_set(&conn->user,fuser);
435 conn->force_user = True;
436 DEBUG(3,("Forced user %s\n",fuser));
438 DEBUG(1,("Couldn't find user %s\n",fuser));
444 * If force group is true, then override
445 * any groupid stored for the connecting user.
448 if (*lp_force_group(snum)) {
452 BOOL user_must_be_member = False;
454 StrnCpy(tmp_gname,lp_force_group(snum),sizeof(pstring)-1);
456 if (tmp_gname[0] == '+') {
457 user_must_be_member = True;
458 StrnCpy(gname,&tmp_gname[1],sizeof(pstring)-2);
460 StrnCpy(gname,tmp_gname,sizeof(pstring)-1);
462 /* default service may be a group name */
463 pstring_sub(gname,"%S",service);
464 gptr = (struct group *)getgrnam(gname);
468 * If the user has been forced and the forced group starts
469 * with a '+', then we only set the group to be the forced
470 * group if the forced user is a member of that group.
471 * Otherwise, the meaning of the '+' would be ignored.
473 if (conn->force_user && user_must_be_member) {
475 for (i = 0; gptr->gr_mem[i] != NULL; i++) {
476 if (strcmp(user,gptr->gr_mem[i]) == 0) {
477 conn->gid = gptr->gr_gid;
478 DEBUG(3,("Forced group %s for member %s\n",gname,user));
483 conn->gid = gptr->gr_gid;
484 DEBUG(3,("Forced group %s\n",gname));
487 DEBUG(1,("Couldn't find group %s\n",gname));
490 #endif /* HAVE_GETGRNAM */
494 pstrcpy(s,lp_pathname(snum));
495 standard_sub_conn(conn,s);
496 string_set(&conn->connectpath,s);
497 DEBUG(3,("Connect path is %s\n",s));
500 /* groups stuff added by ih */
505 /* Find all the groups this uid is in and
506 store them. Used by become_user() */
507 setup_groups(conn->user,conn->uid,conn->gid,
508 &conn->ngroups,&conn->groups);
510 /* check number of connections */
511 if (!claim_connection(conn,
512 lp_servicename(SNUM(conn)),
513 lp_max_connections(SNUM(conn)),
515 DEBUG(1,("too many connections - rejected\n"));
516 *ecode = ERRnoresource;
521 if (lp_status(SNUM(conn)))
522 claim_connection(conn,"",
526 /* execute any "root preexec = " line */
527 if (*lp_rootpreexec(SNUM(conn))) {
529 pstrcpy(cmd,lp_rootpreexec(SNUM(conn)));
530 standard_sub_conn(conn,cmd);
531 DEBUG(5,("cmd=%s\n",cmd));
532 ret = smbrun(cmd,NULL,False);
533 if (ret != 0 && lp_rootpreexec_close(SNUM(conn))) {
534 DEBUG(1,("preexec gave %d - failing connection\n", ret));
536 *ecode = ERRsrverror;
541 if (!become_user(conn, conn->vuid)) {
542 DEBUG(0,("Can't become connected user!\n"));
544 yield_connection(conn,
545 lp_servicename(SNUM(conn)),
546 lp_max_connections(SNUM(conn)));
547 if (lp_status(SNUM(conn))) {
548 yield_connection(conn,"",MAXSTATUS);
556 if (dos_ChDir(conn->connectpath) != 0) {
557 DEBUG(0,("Can't change directory to %s (%s)\n",
558 conn->connectpath,strerror(errno)));
561 yield_connection(conn,
562 lp_servicename(SNUM(conn)),
563 lp_max_connections(SNUM(conn)));
564 if (lp_status(SNUM(conn)))
565 yield_connection(conn,"",MAXSTATUS);
568 *ecode = ERRinvnetname;
572 string_set(&conn->origpath,conn->connectpath);
574 #if SOFTLINK_OPTIMISATION
575 /* resolve any soft links early */
578 pstrcpy(s,conn->connectpath);
580 string_set(&conn->connectpath,s);
581 dos_ChDir(conn->connectpath);
585 add_session_user(user);
587 /* execute any "preexec = " line */
588 if (*lp_preexec(SNUM(conn))) {
590 pstrcpy(cmd,lp_preexec(SNUM(conn)));
591 standard_sub_conn(conn,cmd);
592 ret = smbrun(cmd,NULL,False);
593 if (ret != 0 && lp_preexec_close(SNUM(conn))) {
594 DEBUG(1,("preexec gave %d - failing connection\n", ret));
596 *ecode = ERRsrverror;
602 * Print out the 'connected as' stuff here as we need
603 * to know the effective uid and gid we will be using.
606 if( DEBUGLVL( IS_IPC(conn) ? 3 : 1 ) ) {
607 dbgtext( "%s (%s) ", remote_machine, conn->client_address );
608 dbgtext( "connect to service %s ", lp_servicename(SNUM(conn)) );
609 dbgtext( "as user %s ", user );
610 dbgtext( "(uid=%d, gid=%d) ", (int)geteuid(), (int)getegid() );
611 dbgtext( "(pid %d)\n", (int)sys_getpid() );
614 /* we've finished with the sensitive stuff */
617 /* Add veto/hide lists */
618 if (!IS_IPC(conn) && !IS_PRINT(conn)) {
619 set_namearray( &conn->veto_list, lp_veto_files(SNUM(conn)));
620 set_namearray( &conn->hide_list, lp_hide_files(SNUM(conn)));
621 set_namearray( &conn->veto_oplock_list, lp_veto_oplocks(SNUM(conn)));
624 /* Invoke VFS make connection hook */
626 if (conn->vfs_ops.connect) {
627 if (conn->vfs_ops.connect(conn->vfs_conn, service, user) < 0) {
636 /****************************************************************************
638 ****************************************************************************/
639 void close_cnum(connection_struct *conn, uint16 vuid)
641 DirCacheFlush(SNUM(conn));
645 DEBUG(IS_IPC(conn)?3:1, ("%s (%s) closed connection to service %s\n",
646 remote_machine,conn->client_address,
647 lp_servicename(SNUM(conn))));
649 if (conn->vfs_ops.disconnect != NULL) {
651 /* Call VFS disconnect hook */
653 conn->vfs_ops.disconnect();
657 yield_connection(conn,
658 lp_servicename(SNUM(conn)),
659 lp_max_connections(SNUM(conn)));
661 if (lp_status(SNUM(conn)))
662 yield_connection(conn,"",MAXSTATUS);
664 file_close_conn(conn);
665 dptr_closecnum(conn);
667 /* execute any "postexec = " line */
668 if (*lp_postexec(SNUM(conn)) &&
669 become_user(conn, vuid)) {
671 pstrcpy(cmd,lp_postexec(SNUM(conn)));
672 standard_sub_conn(conn,cmd);
673 smbrun(cmd,NULL,False);
678 /* execute any "root postexec = " line */
679 if (*lp_rootpostexec(SNUM(conn))) {
681 pstrcpy(cmd,lp_rootpostexec(SNUM(conn)));
682 standard_sub_conn(conn,cmd);
683 smbrun(cmd,NULL,False);