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 struct timeval smb_last_time;
25 extern int case_default;
26 extern BOOL case_preserve;
27 extern BOOL short_case_preserve;
28 extern BOOL case_mangle;
29 extern BOOL case_sensitive;
30 extern BOOL use_mangled_map;
31 extern fstring remote_machine;
32 extern userdom_struct current_user_info;
33 extern fstring remote_machine;
36 /****************************************************************************
37 load parameters specific to a connection/service
38 ****************************************************************************/
39 BOOL become_service(connection_struct *conn,BOOL do_chdir)
41 extern char magic_char;
42 static connection_struct *last_conn;
50 conn->lastused = smb_last_time.tv_sec;
55 vfs_ChDir(conn,conn->connectpath) != 0 &&
56 vfs_ChDir(conn,conn->origpath) != 0) {
57 DEBUG(0,("chdir (%s) failed\n",
62 if (conn == last_conn)
67 case_default = lp_defaultcase(snum);
68 case_preserve = lp_preservecase(snum);
69 short_case_preserve = lp_shortpreservecase(snum);
70 case_mangle = lp_casemangle(snum);
71 case_sensitive = lp_casesensitive(snum);
72 magic_char = lp_magicchar(snum);
73 use_mangled_map = (*lp_mangled_map(snum) ? True:False);
77 /****************************************************************************
78 Add a home service. Returns the new service number or -1 if fail.
79 ****************************************************************************/
81 int add_home_service(char *service, char *homedir)
88 if (!service || !homedir)
91 if ((iHomeService = lp_servicenumber(HOMES_NAME)) < 0)
95 * If this is a winbindd provided username, remove
96 * the domain component before adding the service.
97 * Log a warning if the "path=" parameter does not
101 fstrcpy(new_service, service);
103 if ((usr_p = strchr_m(service,*lp_winbind_separator())) != NULL)
104 fstrcpy(new_service, usr_p+1);
106 lp_add_home(new_service,iHomeService,homedir);
107 iService = lp_servicenumber(new_service);
112 /****************************************************************************
113 Find a service entry. service is always in dos codepage.
114 ****************************************************************************/
116 int find_service(char *service)
120 all_string_sub(service,"\\","/",0);
122 iService = lp_servicenumber(service);
124 /* now handle the special case of a home directory */
127 char *phome_dir = get_user_home_dir(service);
132 * Try mapping the servicename, it may
133 * be a Windows to unix mapped user name.
135 if(map_username(service))
136 phome_dir = get_user_home_dir(service);
139 DEBUG(3,("checking for home directory %s gave %s\n",service,
140 phome_dir?phome_dir:"(NULL)"));
142 iService = add_home_service(service,phome_dir);
145 /* If we still don't have a service, attempt to add it as a printer. */
150 if ((iPrinterService = lp_servicenumber(PRINTERS_NAME)) >= 0)
154 DEBUG(3,("checking whether %s is a valid printer name...\n", service));
156 if ((pszTemp != NULL) && pcap_printername_ok(service, pszTemp))
158 DEBUG(3,("%s is a valid printer name\n", service));
159 DEBUG(3,("adding %s as a printer service\n", service));
160 lp_add_printer(service,iPrinterService);
161 iService = lp_servicenumber(service);
163 DEBUG(0,("failed to add %s as a printer service!\n", service));
166 DEBUG(3,("%s is not a valid printer name\n", service));
170 /* Check for default vfs service? Unsure whether to implement this */
175 /* just possibly it's a default service? */
178 char *pdefservice = lp_defaultservice();
179 if (pdefservice && *pdefservice &&
180 !strequal(pdefservice,service) &&
181 !strstr(service,".."))
184 * We need to do a local copy here as lp_defaultservice()
185 * returns one of the rotating lp_string buffers that
186 * could get overwritten by the recursive find_service() call
187 * below. Fix from Josef Hinteregger <joehtg@joehtg.co.at>.
190 pstrcpy(defservice, pdefservice);
191 iService = find_service(defservice);
194 all_string_sub(service,"_","/",0);
195 iService = lp_add_service(service,iService);
201 if (!VALID_SNUM(iService))
203 DEBUG(0,("Invalid snum %d for %s\n",iService,service));
208 DEBUG(3,("find_service() failed to find service %s\n", service));
214 /****************************************************************************
215 do some basic sainity checks on the share.
216 This function modifies dev, ecode.
217 ****************************************************************************/
218 static NTSTATUS share_sanity_checks(int snum, char* service, char *dev)
221 if (!lp_snum_ok(snum) ||
222 !check_access(smbd_server_fd(),
223 lp_hostsallow(snum), lp_hostsdeny(snum))) {
224 return NT_STATUS_ACCESS_DENIED;
227 /* you can only connect to the IPC$ service as an ipc device */
228 if (strequal(service,"IPC$") || strequal(service,"ADMIN$"))
231 if (*dev == '?' || !*dev) {
232 if (lp_print_ok(snum)) {
233 pstrcpy(dev,"LPT1:");
239 /* if the request is as a printer and you can't print then refuse */
241 if (!lp_print_ok(snum) && (strncmp(dev,"LPT",3) == 0)) {
242 DEBUG(1,("Attempt to connect to non-printer as a printer\n"));
243 return NT_STATUS_BAD_DEVICE_TYPE;
246 /* Behave as a printer if we are supposed to */
247 if (lp_print_ok(snum) && (strcmp(dev, "A:") == 0)) {
248 pstrcpy(dev, "LPT1:");
255 /****************************************************************************
257 ****************************************************************************/
258 static void set_read_only(connection_struct *conn)
261 char *service = lp_servicename(conn->service);
262 conn->read_only = lp_readonly(conn->service);
264 if (!service) return;
266 lp_list_copy(&list, lp_readlist(conn->service));
268 if (!lp_list_substitute(list, "%S", service)) {
269 DEBUG(0, ("ERROR: read list substitution failed\n"));
271 if (user_in_list(conn->user, list))
272 conn->read_only = True;
276 lp_list_copy(&list, lp_writelist(conn->service));
278 if (!lp_list_substitute(list, "%S", service)) {
279 DEBUG(0, ("ERROR: write list substitution failed\n"));
281 if (user_in_list(conn->user, list))
282 conn->read_only = False;
288 /****************************************************************************
290 ****************************************************************************/
291 static void set_admin_user(connection_struct *conn)
293 /* admin user check */
295 /* JRA - original code denied admin user if the share was
296 marked read_only. Changed as I don't think this is needed,
297 but old code left in case there is a problem here.
299 if (user_in_list(conn->user,lp_admin_users(conn->service))
304 conn->admin_user = True;
305 DEBUG(0,("%s logged in as admin user (root privileges)\n",conn->user));
307 conn->admin_user = False;
310 #if 0 /* This done later, for now */
311 /* admin users always run as uid=0 */
312 if (conn->admin_user) {
319 /****************************************************************************
320 make a connection to a service
321 ****************************************************************************/
322 connection_struct *make_connection(char *service,char *password,
323 int pwlen, char *dev,uint16 vuid, NTSTATUS *status)
326 struct passwd *pass = NULL;
329 connection_struct *conn;
336 snum = find_service(service);
339 if (strequal(service,"IPC$") || strequal(service,"ADMIN$")) {
340 DEBUG(3,("refusing IPC connection\n"));
341 *status = NT_STATUS_ACCESS_DENIED;
345 DEBUG(0,("%s (%s) couldn't find service %s\n",
346 remote_machine, client_addr(), service));
347 *status = NT_STATUS_BAD_NETWORK_PATH;
351 if (strequal(service,HOMES_NAME)) {
352 if(lp_security() != SEC_SHARE) {
353 if (validated_username(vuid)) {
354 fstring unix_username;
355 fstrcpy(unix_username,validated_username(vuid));
356 return(make_connection(unix_username,password,pwlen,dev,vuid,status));
359 /* Security = share. Try with current_user_info.smb_name
360 * as the username. */
361 if(*current_user_info.smb_name) {
362 fstring unix_username;
363 fstrcpy(unix_username,current_user_info.smb_name);
364 map_username(unix_username);
365 return(make_connection(unix_username,password,pwlen,dev,vuid,status));
370 if (NT_STATUS_IS_ERR(*status = share_sanity_checks(snum, service, dev))) {
374 /* add it as a possible user name if we
375 are in share mode security */
376 if (lp_security() == SEC_SHARE) {
377 add_session_user(service);
381 /* shall we let them in? */
382 if (!authorise_login(snum,user,password,pwlen,&guest,&force,vuid)) {
383 DEBUG( 2, ( "Invalid username/password for %s [%s]\n", service, user ) );
384 *status = NT_STATUS_WRONG_PASSWORD;
388 add_session_user(user);
392 DEBUG(0,("Couldn't find free connection.\n"));
393 *status = NT_STATUS_INSUFFICIENT_RESOURCES;
397 /* find out some info about the user */
398 pass = smb_getpwnam(user,True);
401 DEBUG(0,( "Couldn't find account %s\n",user));
402 *status = NT_STATUS_NO_SUCH_USER;
407 conn->force_user = force;
409 conn->uid = pass->pw_uid;
410 conn->gid = pass->pw_gid;
411 safe_strcpy(conn->client_address, client_addr(),
412 sizeof(conn->client_address)-1);
413 conn->num_files_open = 0;
414 conn->lastused = time(NULL);
415 conn->service = snum;
417 conn->printer = (strncmp(dev,"LPT",3) == 0);
418 conn->ipc = ((strncmp(dev,"IPC",3) == 0) || strequal(dev,"ADMIN$"));
420 conn->veto_list = NULL;
421 conn->hide_list = NULL;
422 conn->veto_oplock_list = NULL;
423 string_set(&conn->dirpath,"");
424 string_set(&conn->user,user);
425 conn->nt_user_token = NULL;
429 set_admin_user(conn);
432 * If force user is true, then store the
433 * given userid and also the primary groupid
434 * of the user we're forcing.
437 if (*lp_force_user(snum)) {
438 struct passwd *pass2;
440 pstrcpy(fuser,lp_force_user(snum));
442 /* Allow %S to be used by force user. */
443 pstring_sub(fuser,"%S",service);
445 pass2 = (struct passwd *)Get_Pwnam(fuser,True);
447 conn->uid = pass2->pw_uid;
448 conn->gid = pass2->pw_gid;
449 string_set(&conn->user,fuser);
451 conn->force_user = True;
452 DEBUG(3,("Forced user %s\n",fuser));
454 DEBUG(1,("Couldn't find user %s\n",fuser));
458 /* admin users always run as uid=0 */
459 if (conn->admin_user) {
465 * If force group is true, then override
466 * any groupid stored for the connecting user.
469 if (*lp_force_group(snum)) {
473 BOOL user_must_be_member = False;
475 StrnCpy(tmp_gname,lp_force_group(snum),sizeof(pstring)-1);
477 if (tmp_gname[0] == '+') {
478 user_must_be_member = True;
479 StrnCpy(gname,&tmp_gname[1],sizeof(pstring)-2);
481 StrnCpy(gname,tmp_gname,sizeof(pstring)-1);
483 /* default service may be a group name */
484 pstring_sub(gname,"%S",service);
485 gid = nametogid(gname);
487 if (gid != (gid_t)-1) {
489 * If the user has been forced and the forced group starts
490 * with a '+', then we only set the group to be the forced
491 * group if the forced user is a member of that group.
492 * Otherwise, the meaning of the '+' would be ignored.
494 if (conn->force_user && user_must_be_member) {
495 if (user_in_group_list( user, gname )) {
497 DEBUG(3,("Forced group %s for member %s\n",gname,user));
501 DEBUG(3,("Forced group %s\n",gname));
504 DEBUG(1,("Couldn't find group %s\n",gname));
507 #endif /* HAVE_GETGRNAM */
511 pstrcpy(s,lp_pathname(snum));
512 standard_sub_conn(conn,s);
513 string_set(&conn->connectpath,s);
514 DEBUG(3,("Connect path is %s\n",s));
517 /* groups stuff added by ih */
521 /* Find all the groups this uid is in and
522 store them. Used by become_user() */
523 initialise_groups(conn->user, conn->uid, conn->gid);
524 get_current_groups(&conn->ngroups,&conn->groups);
526 conn->nt_user_token = create_nt_token(conn->uid, conn->gid,
527 conn->ngroups, conn->groups,
531 * New code to check if there's a share security descripter
532 * added from NT server manager. This is done after the
533 * smb.conf checks are done as we need a uid and token. JRA.
537 BOOL can_write = share_access_check(conn, snum, vuid, FILE_WRITE_DATA);
540 if (!share_access_check(conn, snum, vuid, FILE_READ_DATA)) {
541 /* No access, read or write. */
542 *status = NT_STATUS_ACCESS_DENIED;
543 DEBUG(0,( "make_connection: connection to %s denied due to security descriptor.\n",
548 conn->read_only = True;
552 /* Initialise VFS function pointers */
554 if (!smbd_vfs_init(conn)) {
555 DEBUG(0, ("vfs_init failed for service %s\n", lp_servicename(SNUM(conn))));
560 if (!become_user(conn, conn->vuid)) {
561 /* No point continuing if they fail the basic checks */
562 DEBUG(0,("Can't become connected user!\n"));
564 *status = NT_STATUS_LOGON_FAILURE;
568 /* ROOT Activities: */
570 /* check number of connections */
571 if (!claim_connection(conn,
572 lp_servicename(SNUM(conn)),
573 lp_max_connections(SNUM(conn)),
575 DEBUG(1,("too many connections - rejected\n"));
576 *status = NT_STATUS_INSUFFICIENT_RESOURCES;
583 /* Preexecs are done here as they might make the dir we are to ChDir to below */
584 /* execute any "root preexec = " line */
585 if (*lp_rootpreexec(SNUM(conn))) {
588 pstrcpy(cmd,lp_rootpreexec(SNUM(conn)));
589 standard_sub_conn(conn,cmd);
590 DEBUG(5,("cmd=%s\n",cmd));
591 ret = smbrun(cmd,NULL);
592 if (ret != 0 && lp_rootpreexec_close(SNUM(conn))) {
593 DEBUG(1,("root preexec gave %d - failing connection\n", ret));
594 yield_connection(conn,
595 lp_servicename(SNUM(conn)),
596 lp_max_connections(SNUM(conn)));
598 *status = NT_STATUS_UNSUCCESSFUL;
606 /* USER Activites: */
607 /* Remember that a different vuid can connect later without these checks... */
609 /* Preexecs are done here as they might make the dir we are to ChDir to below */
610 /* execute any "preexec = " line */
611 if (*lp_preexec(SNUM(conn))) {
614 pstrcpy(cmd,lp_preexec(SNUM(conn)));
615 standard_sub_conn(conn,cmd);
616 ret = smbrun(cmd,NULL);
617 if (ret != 0 && lp_preexec_close(SNUM(conn))) {
618 DEBUG(1,("preexec gave %d - failing connection\n", ret));
620 yield_connection(conn, lp_servicename(SNUM(conn)), lp_max_connections(SNUM(conn)));
622 *status = NT_STATUS_UNSUCCESSFUL;
627 if (vfs_ChDir(conn,conn->connectpath) != 0) {
628 DEBUG(0,("%s (%s) Can't change directory to %s (%s)\n",
629 remote_machine, conn->client_address,
630 conn->connectpath,strerror(errno)));
632 yield_connection(conn,
633 lp_servicename(SNUM(conn)),
634 lp_max_connections(SNUM(conn)));
636 *status = NT_STATUS_BAD_NETWORK_NAME;
640 string_set(&conn->origpath,conn->connectpath);
642 #if SOFTLINK_OPTIMISATION
643 /* resolve any soft links early */
646 pstrcpy(s,conn->connectpath);
648 string_set(&conn->connectpath,s);
649 vfs_ChDir(conn,conn->connectpath);
654 * Print out the 'connected as' stuff here as we need
655 * to know the effective uid and gid we will be using
656 * (at least initially).
659 if( DEBUGLVL( IS_IPC(conn) ? 3 : 1 ) ) {
660 dbgtext( "%s (%s) ", remote_machine, conn->client_address );
661 dbgtext( "connect to service %s ", lp_servicename(SNUM(conn)) );
662 dbgtext( "initially as user %s ", user );
663 dbgtext( "(uid=%d, gid=%d) ", (int)geteuid(), (int)getegid() );
664 dbgtext( "(pid %d)\n", (int)sys_getpid() );
667 /* Add veto/hide lists */
668 if (!IS_IPC(conn) && !IS_PRINT(conn)) {
669 set_namearray( &conn->veto_list, lp_veto_files(SNUM(conn)));
670 set_namearray( &conn->hide_list, lp_hide_files(SNUM(conn)));
671 set_namearray( &conn->veto_oplock_list, lp_veto_oplocks(SNUM(conn)));
674 /* Invoke VFS make connection hook */
676 if (conn->vfs_ops.connect) {
677 if (conn->vfs_ops.connect(conn, service, user) < 0) {
678 DEBUG(0,("make_connection: VFS make connection failed!\n"));
679 *status = NT_STATUS_UNSUCCESSFUL;
686 /* we've finished with the sensitive stuff */
693 /****************************************************************************
695 ****************************************************************************/
696 void close_cnum(connection_struct *conn, uint16 vuid)
698 DirCacheFlush(SNUM(conn));
702 DEBUG(IS_IPC(conn)?3:1, ("%s (%s) closed connection to service %s\n",
703 remote_machine,conn->client_address,
704 lp_servicename(SNUM(conn))));
706 if (conn->vfs_ops.disconnect != NULL) {
708 /* Call VFS disconnect hook */
710 conn->vfs_ops.disconnect(conn);
714 yield_connection(conn,
715 lp_servicename(SNUM(conn)),
716 lp_max_connections(SNUM(conn)));
718 file_close_conn(conn);
719 dptr_closecnum(conn);
721 /* execute any "postexec = " line */
722 if (*lp_postexec(SNUM(conn)) &&
723 become_user(conn, vuid)) {
725 pstrcpy(cmd,lp_postexec(SNUM(conn)));
726 standard_sub_conn(conn,cmd);
732 /* execute any "root postexec = " line */
733 if (*lp_rootpostexec(SNUM(conn))) {
735 pstrcpy(cmd,lp_rootpostexec(SNUM(conn)));
736 standard_sub_conn(conn,cmd);