2 Unix SMB/CIFS implementation.
3 service (connection) opening and closing
4 Copyright (C) Andrew Tridgell 1992-1998
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 extern userdom_struct current_user_info;
24 static BOOL canonicalize_path(connection_struct *conn, pstring path)
26 #ifdef REALPATH_TAKES_NULL
27 char *resolved_name = SMB_VFS_REALPATH(conn,path,NULL);
31 pstrcpy(path, resolved_name);
32 SAFE_FREE(resolved_name);
36 char resolved_name_buf[PATH_MAX+1];
38 pstring resolved_name_buf;
40 char *resolved_name = SMB_VFS_REALPATH(conn,path,resolved_name_buf);
44 pstrcpy(path, resolved_name);
46 #endif /* REALPATH_TAKES_NULL */
49 /****************************************************************************
50 Ensure when setting connectpath it is a canonicalized (no ./ // or ../)
51 absolute path stating in / and not ending in /.
52 Observent people will notice a similarity between this and check_path_syntax :-).
53 ****************************************************************************/
55 void set_conn_connectpath(connection_struct *conn, const pstring connectpath)
59 const char *s = connectpath;
60 BOOL start_of_name_component = True;
62 *d++ = '/'; /* Always start with root. */
66 /* Eat multiple '/' */
70 if ((d > destname + 1) && (*s != '\0')) {
73 start_of_name_component = True;
77 if (start_of_name_component) {
78 if ((s[0] == '.') && (s[1] == '.') && (s[2] == '/' || s[2] == '\0')) {
79 /* Uh oh - "/../" or "/..\0" ! */
81 /* Go past the ../ or .. */
85 s += 2; /* Go past the .. */
88 /* If we just added a '/' - delete it */
89 if ((d > destname) && (*(d-1) == '/')) {
94 /* Are we at the start ? Can't go back further if so. */
96 *d++ = '/'; /* Can't delete root */
99 /* Go back one level... */
100 /* Decrement d first as d points to the *next* char to write into. */
101 for (d--; d > destname; d--) {
106 /* We're still at the start of a name component, just the previous one. */
108 } else if ((s[0] == '.') && ((s[1] == '\0') || s[1] == '/')) {
109 /* Component of pathname can't be "." only - skip the '.' . */
123 /* Get the size of the next MB character. */
124 next_codepoint(s,&siz);
145 start_of_name_component = False;
149 /* And must not end in '/' */
150 if (d > destname + 1 && (*(d-1) == '/')) {
154 DEBUG(10,("set_conn_connectpath: service %s, connectpath = %s\n",
155 lp_servicename(SNUM(conn)), destname ));
157 string_set(&conn->connectpath, destname);
160 /****************************************************************************
161 Load parameters specific to a connection/service.
162 ****************************************************************************/
164 BOOL set_current_service(connection_struct *conn, uint16 flags, BOOL do_chdir)
166 static connection_struct *last_conn;
167 static uint16 last_flags;
175 conn->lastused_count++;
180 vfs_ChDir(conn,conn->connectpath) != 0 &&
181 vfs_ChDir(conn,conn->origpath) != 0) {
182 DEBUG(0,("chdir (%s) failed\n",
187 if ((conn == last_conn) && (last_flags == flags)) {
194 /* Obey the client case sensitivity requests - only for clients that support it. */
195 switch (lp_casesensitive(snum)) {
198 /* We need this uglyness due to DOS/Win9x clients that lie about case insensitivity. */
199 enum remote_arch_types ra_type = get_remote_arch();
200 if ((ra_type != RA_SAMBA) && (ra_type != RA_CIFSFS)) {
201 /* Client can't support per-packet case sensitive pathnames. */
202 conn->case_sensitive = False;
204 conn->case_sensitive = !(flags & FLAG_CASELESS_PATHNAMES);
209 conn->case_sensitive = True;
212 conn->case_sensitive = False;
218 /****************************************************************************
219 Add a home service. Returns the new service number or -1 if fail.
220 ****************************************************************************/
222 int add_home_service(const char *service, const char *username, const char *homedir)
226 if (!service || !homedir)
229 if ((iHomeService = lp_servicenumber(HOMES_NAME)) < 0)
233 * If this is a winbindd provided username, remove
234 * the domain component before adding the service.
235 * Log a warning if the "path=" parameter does not
236 * include any macros.
240 const char *p = strchr(service,*lp_winbind_separator());
242 /* We only want the 'user' part of the string */
248 if (!lp_add_home(service, iHomeService, username, homedir)) {
252 return lp_servicenumber(service);
256 static int load_registry_service(const char *servicename)
258 struct registry_key *key;
264 struct registry_value *value;
268 if (!lp_registry_shares()) {
272 if (asprintf(&path, "%s\\%s", KEY_SMBCONF, servicename) == -1) {
276 err = reg_open_path(NULL, path, REG_KEY_READ, get_root_nt_token(),
280 if (!W_ERROR_IS_OK(err)) {
284 res = lp_add_service(servicename, -1);
290 W_ERROR_IS_OK(reg_enumvalue(key, key, i, &value_name, &value));
292 switch (value->type) {
295 if (asprintf(&tmp, "%d", value->v.dword) == -1) {
298 lp_do_parameter(res, value_name, tmp);
303 lp_do_parameter(res, value_name, value->v.sz.str);
307 /* Ignore all the rest */
311 TALLOC_FREE(value_name);
315 if (!service_ok(res)) {
316 /* this is actually never reached, since
317 * service_ok only returns False if the service
318 * entry does not have a service name, and we _know_
319 * we do have a service name here... */
329 void load_registry_shares(void)
331 struct registry_key *key;
336 if (!lp_registry_shares()) {
340 err = reg_open_path(NULL, KEY_SMBCONF, REG_KEY_READ,
341 get_root_nt_token(), &key);
342 if (!(W_ERROR_IS_OK(err))) {
346 for (i=0; W_ERROR_IS_OK(reg_enumkey(key, key, i, &name, NULL)); i++) {
347 load_registry_service(name);
356 * Find a service entry.
358 * @param service is modified (to canonical form??)
361 int find_service(fstring service)
365 all_string_sub(service,"\\","/",0);
367 iService = lp_servicenumber(service);
369 /* now handle the special case of a home directory */
371 char *phome_dir = get_user_home_dir(service);
375 * Try mapping the servicename, it may
376 * be a Windows to unix mapped user name.
378 if(map_username(service))
379 phome_dir = get_user_home_dir(service);
382 DEBUG(3,("checking for home directory %s gave %s\n",service,
383 phome_dir?phome_dir:"(NULL)"));
385 iService = add_home_service(service,service /* 'username' */, phome_dir);
388 /* If we still don't have a service, attempt to add it as a printer. */
392 if ((iPrinterService = lp_servicenumber(PRINTERS_NAME)) >= 0) {
393 DEBUG(3,("checking whether %s is a valid printer name...\n", service));
394 if (pcap_printername_ok(service)) {
395 DEBUG(3,("%s is a valid printer name\n", service));
396 DEBUG(3,("adding %s as a printer service\n", service));
397 lp_add_printer(service, iPrinterService);
398 iService = lp_servicenumber(service);
400 DEBUG(0,("failed to add %s as a printer service!\n", service));
403 DEBUG(3,("%s is not a valid printer name\n", service));
408 /* Check for default vfs service? Unsure whether to implement this */
412 /* Is it a usershare service ? */
413 if (iService < 0 && *lp_usershare_path()) {
414 /* Ensure the name is canonicalized. */
416 iService = load_usershare_service(service);
419 /* just possibly it's a default service? */
421 char *pdefservice = lp_defaultservice();
422 if (pdefservice && *pdefservice && !strequal(pdefservice,service) && !strstr_m(service,"..")) {
424 * We need to do a local copy here as lp_defaultservice()
425 * returns one of the rotating lp_string buffers that
426 * could get overwritten by the recursive find_service() call
427 * below. Fix from Josef Hinteregger <joehtg@joehtg.co.at>.
430 pstrcpy(defservice, pdefservice);
432 /* Disallow anything except explicit share names. */
433 if (strequal(defservice,HOMES_NAME) ||
434 strequal(defservice, PRINTERS_NAME) ||
435 strequal(defservice, "IPC$")) {
439 iService = find_service(defservice);
441 all_string_sub(service, "_","/",0);
442 iService = lp_add_service(service, iService);
448 iService = load_registry_service(service);
452 if (!VALID_SNUM(iService)) {
453 DEBUG(0,("Invalid snum %d for %s\n",iService, service));
461 DEBUG(3,("find_service() failed to find service %s\n", service));
467 /****************************************************************************
468 do some basic sainity checks on the share.
469 This function modifies dev, ecode.
470 ****************************************************************************/
472 static NTSTATUS share_sanity_checks(int snum, fstring dev)
475 if (!lp_snum_ok(snum) ||
476 !check_access(smbd_server_fd(),
477 lp_hostsallow(snum), lp_hostsdeny(snum))) {
478 return NT_STATUS_ACCESS_DENIED;
481 if (dev[0] == '?' || !dev[0]) {
482 if (lp_print_ok(snum)) {
483 fstrcpy(dev,"LPT1:");
484 } else if (strequal(lp_fstype(snum), "IPC")) {
493 if (lp_print_ok(snum)) {
494 if (!strequal(dev, "LPT1:")) {
495 return NT_STATUS_BAD_DEVICE_TYPE;
497 } else if (strequal(lp_fstype(snum), "IPC")) {
498 if (!strequal(dev, "IPC")) {
499 return NT_STATUS_BAD_DEVICE_TYPE;
501 } else if (!strequal(dev, "A:")) {
502 return NT_STATUS_BAD_DEVICE_TYPE;
505 /* Behave as a printer if we are supposed to */
506 if (lp_print_ok(snum) && (strcmp(dev, "A:") == 0)) {
507 fstrcpy(dev, "LPT1:");
513 static NTSTATUS find_forced_user(connection_struct *conn, BOOL vuser_is_guest, fstring username)
515 int snum = conn->params->service;
516 char *fuser, *found_username;
519 if (!(fuser = talloc_string_sub(conn->mem_ctx, lp_force_user(snum), "%S",
520 lp_servicename(snum)))) {
521 return NT_STATUS_NO_MEMORY;
524 result = create_token_from_username(conn->mem_ctx, fuser, vuser_is_guest,
525 &conn->uid, &conn->gid, &found_username,
526 &conn->nt_user_token);
527 if (!NT_STATUS_IS_OK(result)) {
531 fstrcpy(username, found_username);
534 TALLOC_FREE(found_username);
539 * Go through lookup_name etc to find the force'd group.
541 * Create a new token from src_token, replacing the primary group sid with the
545 static NTSTATUS find_forced_group(BOOL force_user,
546 int snum, const char *username,
550 NTSTATUS result = NT_STATUS_NO_SUCH_GROUP;
553 enum lsa_SidType type;
555 BOOL user_must_be_member = False;
558 ZERO_STRUCTP(pgroup_sid);
561 mem_ctx = talloc_new(NULL);
562 if (mem_ctx == NULL) {
563 DEBUG(0, ("talloc_new failed\n"));
564 return NT_STATUS_NO_MEMORY;
567 groupname = talloc_strdup(mem_ctx, lp_force_group(snum));
568 if (groupname == NULL) {
569 DEBUG(1, ("talloc_strdup failed\n"));
570 result = NT_STATUS_NO_MEMORY;
574 if (groupname[0] == '+') {
575 user_must_be_member = True;
579 groupname = talloc_string_sub(mem_ctx, groupname,
580 "%S", lp_servicename(snum));
582 if (!lookup_name_smbconf(mem_ctx, groupname,
583 LOOKUP_NAME_ALL|LOOKUP_NAME_GROUP,
584 NULL, NULL, &group_sid, &type)) {
585 DEBUG(10, ("lookup_name_smbconf(%s) failed\n",
590 if ((type != SID_NAME_DOM_GRP) && (type != SID_NAME_ALIAS) &&
591 (type != SID_NAME_WKN_GRP)) {
592 DEBUG(10, ("%s is a %s, not a group\n", groupname,
593 sid_type_lookup(type)));
597 if (!sid_to_gid(&group_sid, &gid)) {
598 DEBUG(10, ("sid_to_gid(%s) for %s failed\n",
599 sid_string_static(&group_sid), groupname));
604 * If the user has been forced and the forced group starts with a '+',
605 * then we only set the group to be the forced group if the forced
606 * user is a member of that group. Otherwise, the meaning of the '+'
610 if (force_user && user_must_be_member) {
611 if (user_in_group_sid(username, &group_sid)) {
612 sid_copy(pgroup_sid, &group_sid);
614 DEBUG(3,("Forced group %s for member %s\n",
615 groupname, username));
617 DEBUG(0,("find_forced_group: forced user %s is not a member "
618 "of forced group %s. Disallowing access.\n",
619 username, groupname ));
620 result = NT_STATUS_MEMBER_NOT_IN_GROUP;
624 sid_copy(pgroup_sid, &group_sid);
626 DEBUG(3,("Forced group %s\n", groupname));
629 result = NT_STATUS_OK;
631 TALLOC_FREE(mem_ctx);
635 /****************************************************************************
636 Make a connection, given the snum to connect to, and the vuser of the
637 connecting user if appropriate.
638 ****************************************************************************/
640 static connection_struct *make_connection_snum(int snum, user_struct *vuser,
645 struct passwd *pass = NULL;
647 connection_struct *conn;
655 SET_STAT_INVALID(st);
657 if (NT_STATUS_IS_ERR(*status = share_sanity_checks(snum, dev))) {
663 DEBUG(0,("Couldn't find free connection.\n"));
664 *status = NT_STATUS_INSUFFICIENT_RESOURCES;
668 conn->params->service = snum;
669 conn->nt_user_token = NULL;
671 if (lp_guest_only(snum)) {
672 const char *guestname = lp_guestaccount();
674 char *found_username = NULL;
677 pass = getpwnam_alloc(NULL, guestname);
679 DEBUG(0,("make_connection_snum: Invalid guest "
680 "account %s??\n",guestname));
682 *status = NT_STATUS_NO_SUCH_USER;
685 status2 = create_token_from_username(conn->mem_ctx, pass->pw_name, True,
686 &conn->uid, &conn->gid,
688 &conn->nt_user_token);
689 if (!NT_STATUS_IS_OK(status2)) {
695 fstrcpy(user, found_username);
696 string_set(&conn->user,user);
697 conn->force_user = True;
698 TALLOC_FREE(found_username);
700 DEBUG(3,("Guest only user %s\n",user));
703 if (!lp_guest_ok(snum)) {
704 DEBUG(2, ("guest user (from session setup) "
705 "not permitted to access this share "
706 "(%s)\n", lp_servicename(snum)));
708 *status = NT_STATUS_ACCESS_DENIED;
712 if (!user_ok_token(vuser->user.unix_name,
713 vuser->nt_user_token, snum)) {
714 DEBUG(2, ("user '%s' (from session setup) not "
715 "permitted to access this share "
716 "(%s)\n", vuser->user.unix_name,
717 lp_servicename(snum)));
719 *status = NT_STATUS_ACCESS_DENIED;
723 conn->vuid = vuser->vuid;
724 conn->uid = vuser->uid;
725 conn->gid = vuser->gid;
726 string_set(&conn->user,vuser->user.unix_name);
727 fstrcpy(user,vuser->user.unix_name);
728 guest = vuser->guest;
729 } else if (lp_security() == SEC_SHARE) {
731 char *found_username = NULL;
733 /* add it as a possible user name if we
734 are in share mode security */
735 add_session_user(lp_servicename(snum));
736 /* shall we let them in? */
737 if (!authorise_login(snum,user,password,&guest)) {
738 DEBUG( 2, ( "Invalid username/password for [%s]\n",
739 lp_servicename(snum)) );
741 *status = NT_STATUS_WRONG_PASSWORD;
744 pass = Get_Pwnam(user);
745 status2 = create_token_from_username(conn->mem_ctx, pass->pw_name, True,
746 &conn->uid, &conn->gid,
748 &conn->nt_user_token);
749 if (!NT_STATUS_IS_OK(status2)) {
754 fstrcpy(user, found_username);
755 string_set(&conn->user,user);
756 TALLOC_FREE(found_username);
757 conn->force_user = True;
759 DEBUG(0, ("invalid VUID (vuser) but not in security=share\n"));
761 *status = NT_STATUS_ACCESS_DENIED;
765 add_session_user(user);
767 safe_strcpy(conn->client_address, client_addr(),
768 sizeof(conn->client_address)-1);
769 conn->num_files_open = 0;
770 conn->lastused = conn->lastused_count = time(NULL);
772 conn->printer = (strncmp(dev,"LPT",3) == 0);
773 conn->ipc = ( (strncmp(dev,"IPC",3) == 0) ||
774 ( lp_enable_asu_support() && strequal(dev,"ADMIN$")) );
777 /* Case options for the share. */
778 if (lp_casesensitive(snum) == Auto) {
779 /* We will be setting this per packet. Set to be case
780 * insensitive for now. */
781 conn->case_sensitive = False;
783 conn->case_sensitive = (BOOL)lp_casesensitive(snum);
786 conn->case_preserve = lp_preservecase(snum);
787 conn->short_case_preserve = lp_shortpreservecase(snum);
789 conn->veto_list = NULL;
790 conn->hide_list = NULL;
791 conn->veto_oplock_list = NULL;
792 string_set(&conn->dirpath,"");
793 string_set(&conn->user,user);
795 conn->read_only = lp_readonly(SNUM(conn));
796 conn->admin_user = False;
799 * If force user is true, then store the given userid and the gid of
800 * the user we're forcing.
801 * For auxiliary groups see below.
804 if (*lp_force_user(snum)) {
807 status2 = find_forced_user(conn,
808 (vuser != NULL) && vuser->guest,
810 if (!NT_STATUS_IS_OK(status2)) {
815 string_set(&conn->user,user);
816 conn->force_user = True;
817 DEBUG(3,("Forced user %s\n",user));
821 * If force group is true, then override
822 * any groupid stored for the connecting user.
825 if (*lp_force_group(snum)) {
829 status2 = find_forced_group(conn->force_user,
831 &group_sid, &conn->gid);
832 if (!NT_STATUS_IS_OK(status2)) {
838 if ((conn->nt_user_token == NULL) && (vuser != NULL)) {
840 /* Not force user and not security=share, but force
841 * group. vuser has a token to copy */
843 conn->nt_user_token = dup_nt_token(
844 NULL, vuser->nt_user_token);
845 if (conn->nt_user_token == NULL) {
846 DEBUG(0, ("dup_nt_token failed\n"));
848 *status = NT_STATUS_NO_MEMORY;
853 /* If conn->nt_user_token is still NULL, we have
854 * security=share. This means ignore the SID, as we had no
855 * vuser to copy from */
857 if (conn->nt_user_token != NULL) {
858 /* Overwrite the primary group sid */
859 sid_copy(&conn->nt_user_token->user_sids[1],
863 conn->force_group = True;
866 if (conn->nt_user_token != NULL) {
869 /* We have a share-specific token from force [user|group].
870 * This means we have to create the list of unix groups from
871 * the list of sids. */
876 for (i=0; i<conn->nt_user_token->num_sids; i++) {
878 DOM_SID *sid = &conn->nt_user_token->user_sids[i];
880 if (!sid_to_gid(sid, &gid)) {
881 DEBUG(10, ("Could not convert SID %s to gid, "
883 sid_string_static(sid)));
886 if (!add_gid_to_array_unique(conn->mem_ctx, gid, &conn->groups,
888 DEBUG(0, ("add_gid_to_array_unique failed\n"));
890 *status = NT_STATUS_NO_MEMORY;
898 pstrcpy(s,lp_pathname(snum));
899 standard_sub_advanced(lp_servicename(SNUM(conn)), conn->user,
900 conn->connectpath, conn->gid,
901 get_current_username(),
902 current_user_info.domain,
904 set_conn_connectpath(conn,s);
905 DEBUG(3,("Connect path is '%s' for service [%s]\n",s,
906 lp_servicename(snum)));
910 * New code to check if there's a share security descripter
911 * added from NT server manager. This is done after the
912 * smb.conf checks are done as we need a uid and token. JRA.
917 BOOL can_write = False;
918 NT_USER_TOKEN *token = conn->nt_user_token ?
919 conn->nt_user_token :
920 (vuser ? vuser->nt_user_token : NULL);
923 * I don't believe this can happen. But the
924 * logic above is convoluted enough to confuse
925 * automated checkers, so be sure. JRA.
929 DEBUG(0,("make_connection: connection to %s "
930 "denied due to missing "
932 lp_servicename(snum)));
934 *status = NT_STATUS_ACCESS_DENIED;
938 can_write = share_access_check(token,
939 lp_servicename(snum),
943 if (!share_access_check(token,
944 lp_servicename(snum),
946 /* No access, read or write. */
947 DEBUG(0,("make_connection: connection to %s "
948 "denied due to security "
950 lp_servicename(snum)));
952 *status = NT_STATUS_ACCESS_DENIED;
955 conn->read_only = True;
959 /* Initialise VFS function pointers */
961 if (!smbd_vfs_init(conn)) {
962 DEBUG(0, ("vfs_init failed for service %s\n",
963 lp_servicename(snum)));
965 *status = NT_STATUS_BAD_NETWORK_NAME;
970 * If widelinks are disallowed we need to canonicalise the connect
971 * path here to ensure we don't have any symlinks in the
972 * connectpath. We will be checking all paths on this connection are
973 * below this directory. We must do this after the VFS init as we
974 * depend on the realpath() pointer in the vfs table. JRA.
976 if (!lp_widelinks(snum)) {
978 pstrcpy(s,conn->connectpath);
979 canonicalize_path(conn, s);
980 set_conn_connectpath(conn,s);
983 if ((!conn->printer) && (!conn->ipc)) {
984 conn->notify_ctx = notify_init(conn->mem_ctx, server_id_self(),
985 smbd_messaging_context(),
986 smbd_event_context(),
990 /* ROOT Activities: */
992 * Enforce the max connections parameter.
995 if ((lp_max_connections(snum) > 0)
996 && (count_current_connections(lp_servicename(SNUM(conn)), True) >=
997 lp_max_connections(snum))) {
999 DEBUG(1, ("Max connections (%d) exceeded for %s\n",
1000 lp_max_connections(snum), lp_servicename(snum)));
1002 *status = NT_STATUS_INSUFFICIENT_RESOURCES;
1007 * Get us an entry in the connections db
1009 if (!claim_connection(conn, lp_servicename(snum), 0)) {
1010 DEBUG(1, ("Could not store connections entry\n"));
1012 *status = NT_STATUS_INTERNAL_DB_ERROR;
1016 /* Preexecs are done here as they might make the dir we are to ChDir
1018 /* execute any "root preexec = " line */
1019 if (*lp_rootpreexec(snum)) {
1021 pstrcpy(cmd,lp_rootpreexec(snum));
1022 standard_sub_advanced(lp_servicename(SNUM(conn)), conn->user,
1023 conn->connectpath, conn->gid,
1024 get_current_username(),
1025 current_user_info.domain,
1027 DEBUG(5,("cmd=%s\n",cmd));
1028 ret = smbrun(cmd,NULL);
1029 if (ret != 0 && lp_rootpreexec_close(snum)) {
1030 DEBUG(1,("root preexec gave %d - failing "
1031 "connection\n", ret));
1032 yield_connection(conn, lp_servicename(snum));
1034 *status = NT_STATUS_ACCESS_DENIED;
1039 /* USER Activites: */
1040 if (!change_to_user(conn, conn->vuid)) {
1041 /* No point continuing if they fail the basic checks */
1042 DEBUG(0,("Can't become connected user!\n"));
1043 yield_connection(conn, lp_servicename(snum));
1045 *status = NT_STATUS_LOGON_FAILURE;
1049 /* Remember that a different vuid can connect later without these
1052 /* Preexecs are done here as they might make the dir we are to ChDir
1055 /* execute any "preexec = " line */
1056 if (*lp_preexec(snum)) {
1058 pstrcpy(cmd,lp_preexec(snum));
1059 standard_sub_advanced(lp_servicename(SNUM(conn)), conn->user,
1060 conn->connectpath, conn->gid,
1061 get_current_username(),
1062 current_user_info.domain,
1064 ret = smbrun(cmd,NULL);
1065 if (ret != 0 && lp_preexec_close(snum)) {
1066 DEBUG(1,("preexec gave %d - failing connection\n",
1068 change_to_root_user();
1069 yield_connection(conn, lp_servicename(snum));
1071 *status = NT_STATUS_ACCESS_DENIED;
1076 #ifdef WITH_FAKE_KASERVER
1077 if (lp_afs_share(snum)) {
1082 /* Add veto/hide lists */
1083 if (!IS_IPC(conn) && !IS_PRINT(conn)) {
1084 set_namearray( &conn->veto_list, lp_veto_files(snum));
1085 set_namearray( &conn->hide_list, lp_hide_files(snum));
1086 set_namearray( &conn->veto_oplock_list, lp_veto_oplocks(snum));
1089 /* Invoke VFS make connection hook - do this before the VFS_STAT call
1090 to allow any filesystems needing user credentials to initialize
1093 if (SMB_VFS_CONNECT(conn, lp_servicename(snum), user) < 0) {
1094 DEBUG(0,("make_connection: VFS make connection failed!\n"));
1095 change_to_root_user();
1096 yield_connection(conn, lp_servicename(snum));
1098 *status = NT_STATUS_UNSUCCESSFUL;
1102 /* win2000 does not check the permissions on the directory
1103 during the tree connect, instead relying on permission
1104 check during individual operations. To match this behaviour
1105 I have disabled this chdir check (tridge) */
1106 /* the alternative is just to check the directory exists */
1107 if ((ret = SMB_VFS_STAT(conn, conn->connectpath, &st)) != 0 ||
1108 !S_ISDIR(st.st_mode)) {
1109 if (ret == 0 && !S_ISDIR(st.st_mode)) {
1110 DEBUG(0,("'%s' is not a directory, when connecting to "
1111 "[%s]\n", conn->connectpath,
1112 lp_servicename(snum)));
1114 DEBUG(0,("'%s' does not exist or permission denied "
1115 "when connecting to [%s] Error was %s\n",
1116 conn->connectpath, lp_servicename(snum),
1119 change_to_root_user();
1120 /* Call VFS disconnect hook */
1121 SMB_VFS_DISCONNECT(conn);
1122 yield_connection(conn, lp_servicename(snum));
1124 *status = NT_STATUS_BAD_NETWORK_NAME;
1128 string_set(&conn->origpath,conn->connectpath);
1130 #if SOFTLINK_OPTIMISATION
1131 /* resolve any soft links early if possible */
1132 if (vfs_ChDir(conn,conn->connectpath) == 0) {
1134 pstrcpy(s,conn->connectpath);
1136 set_conn_connectpath(conn,s);
1137 vfs_ChDir(conn,conn->connectpath);
1142 * Print out the 'connected as' stuff here as we need
1143 * to know the effective uid and gid we will be using
1144 * (at least initially).
1147 if( DEBUGLVL( IS_IPC(conn) ? 3 : 1 ) ) {
1148 dbgtext( "%s (%s) ", get_remote_machine_name(),
1149 conn->client_address );
1150 dbgtext( "%s", srv_is_signing_active() ? "signed " : "");
1151 dbgtext( "connect to service %s ", lp_servicename(snum) );
1152 dbgtext( "initially as user %s ", user );
1153 dbgtext( "(uid=%d, gid=%d) ", (int)geteuid(), (int)getegid() );
1154 dbgtext( "(pid %d)\n", (int)sys_getpid() );
1157 /* we've finished with the user stuff - go back to root */
1158 change_to_root_user();
1162 /***************************************************************************************
1163 Simple wrapper function for make_connection() to include a call to
1165 **************************************************************************************/
1167 connection_struct *make_connection_with_chdir(const char *service_in,
1169 const char *dev, uint16 vuid,
1172 connection_struct *conn = NULL;
1174 conn = make_connection(service_in, password, dev, vuid, status);
1177 * make_connection() does not change the directory for us any more
1178 * so we have to do it as a separate step --jerry
1181 if ( conn && vfs_ChDir(conn,conn->connectpath) != 0 ) {
1182 DEBUG(0,("move_driver_to_download_area: Can't change "
1183 "directory to %s for [print$] (%s)\n",
1184 conn->connectpath,strerror(errno)));
1185 yield_connection(conn, lp_servicename(SNUM(conn)));
1187 *status = NT_STATUS_UNSUCCESSFUL;
1194 /****************************************************************************
1195 Make a connection to a service.
1198 ****************************************************************************/
1200 connection_struct *make_connection(const char *service_in, DATA_BLOB password,
1201 const char *pdev, uint16 vuid,
1205 user_struct *vuser = NULL;
1212 /* This must ONLY BE CALLED AS ROOT. As it exits this function as
1214 if (!non_root_mode() && (euid = geteuid()) != 0) {
1215 DEBUG(0,("make_connection: PANIC ERROR. Called as nonroot "
1216 "(%u)\n", (unsigned int)euid ));
1217 smb_panic("make_connection: PANIC ERROR. Called as nonroot\n");
1220 if (conn_num_open() > 2047) {
1221 *status = NT_STATUS_INSUFF_SERVER_RESOURCES;
1225 if(lp_security() != SEC_SHARE) {
1226 vuser = get_valid_user_struct(vuid);
1228 DEBUG(1,("make_connection: refusing to connect with "
1229 "no session setup\n"));
1230 *status = NT_STATUS_ACCESS_DENIED;
1235 /* Logic to try and connect to the correct [homes] share, preferably
1236 without too many getpwnam() lookups. This is particulary nasty for
1237 winbind usernames, where the share name isn't the same as unix
1240 The snum of the homes share is stored on the vuser at session setup
1244 if (strequal(service_in,HOMES_NAME)) {
1245 if(lp_security() != SEC_SHARE) {
1246 DATA_BLOB no_pw = data_blob_null;
1247 if (vuser->homes_snum == -1) {
1248 DEBUG(2, ("[homes] share not available for "
1249 "this user because it was not found "
1250 "or created at session setup "
1252 *status = NT_STATUS_BAD_NETWORK_NAME;
1255 DEBUG(5, ("making a connection to [homes] service "
1256 "created at session setup time\n"));
1257 return make_connection_snum(vuser->homes_snum,
1261 /* Security = share. Try with
1262 * current_user_info.smb_name as the username. */
1263 if (*current_user_info.smb_name) {
1264 fstring unix_username;
1265 fstrcpy(unix_username,
1266 current_user_info.smb_name);
1267 map_username(unix_username);
1268 snum = find_service(unix_username);
1271 DEBUG(5, ("making a connection to 'homes' "
1272 "service %s based on "
1273 "security=share\n", service_in));
1274 return make_connection_snum(snum, NULL,
1279 } else if ((lp_security() != SEC_SHARE) && (vuser->homes_snum != -1)
1280 && strequal(service_in,
1281 lp_servicename(vuser->homes_snum))) {
1282 DATA_BLOB no_pw = data_blob_null;
1283 DEBUG(5, ("making a connection to 'homes' service [%s] "
1284 "created at session setup time\n", service_in));
1285 return make_connection_snum(vuser->homes_snum,
1290 fstrcpy(service, service_in);
1292 strlower_m(service);
1294 snum = find_service(service);
1297 if (strequal(service,"IPC$") ||
1298 (lp_enable_asu_support() && strequal(service,"ADMIN$"))) {
1299 DEBUG(3,("refusing IPC connection to %s\n", service));
1300 *status = NT_STATUS_ACCESS_DENIED;
1304 DEBUG(0,("%s (%s) couldn't find service %s\n",
1305 get_remote_machine_name(), client_addr(), service));
1306 *status = NT_STATUS_BAD_NETWORK_NAME;
1310 /* Handle non-Dfs clients attempting connections to msdfs proxy */
1311 if (lp_host_msdfs() && (*lp_msdfs_proxy(snum) != '\0')) {
1312 DEBUG(3, ("refusing connection to dfs proxy share '%s' "
1313 "(pointing to %s)\n",
1314 service, lp_msdfs_proxy(snum)));
1315 *status = NT_STATUS_BAD_NETWORK_NAME;
1319 DEBUG(5, ("making a connection to 'normal' service %s\n", service));
1321 return make_connection_snum(snum, vuser,
1326 /****************************************************************************
1328 ****************************************************************************/
1330 void close_cnum(connection_struct *conn, uint16 vuid)
1333 pipe_close_conn(conn);
1335 file_close_conn(conn);
1336 dptr_closecnum(conn);
1339 change_to_root_user();
1341 DEBUG(IS_IPC(conn)?3:1, ("%s (%s) closed connection to service %s\n",
1342 get_remote_machine_name(),
1343 conn->client_address,
1344 lp_servicename(SNUM(conn))));
1346 /* Call VFS disconnect hook */
1347 SMB_VFS_DISCONNECT(conn);
1349 yield_connection(conn, lp_servicename(SNUM(conn)));
1351 /* make sure we leave the directory available for unmount */
1352 vfs_ChDir(conn, "/");
1354 /* execute any "postexec = " line */
1355 if (*lp_postexec(SNUM(conn)) &&
1356 change_to_user(conn, vuid)) {
1358 pstrcpy(cmd,lp_postexec(SNUM(conn)));
1359 standard_sub_advanced(lp_servicename(SNUM(conn)), conn->user,
1360 conn->connectpath, conn->gid,
1361 get_current_username(),
1362 current_user_info.domain,
1365 change_to_root_user();
1368 change_to_root_user();
1369 /* execute any "root postexec = " line */
1370 if (*lp_rootpostexec(SNUM(conn))) {
1372 pstrcpy(cmd,lp_rootpostexec(SNUM(conn)));
1373 standard_sub_advanced(lp_servicename(SNUM(conn)), conn->user,
1374 conn->connectpath, conn->gid,
1375 get_current_username(),
1376 current_user_info.domain,