static bool initialize_async_io_handler(void) { static bool tried_signal_setup = false; if (aio_signal_event) { return true; } if (tried_signal_setup) { return false; } tried_signal_setup = true; aio_signal_event = tevent_add_signal(server_event_context(), server_event_context(), RT_SIGNAL_AIO, SA_SIGINFO, smbd_aio_signal_handler, NULL); if (!aio_signal_event) { DEBUG(10, ("Failed to setup RT_SIGNAL_AIO handler\n")); return false; } /* tevent supports 100 signal with SA_SIGINFO */ aio_pending_size = 100; return true; }
static void bq_setup_sig_term_handler(void) { struct tevent_signal *se; se = tevent_add_signal(server_event_context(), server_event_context(), SIGTERM, 0, bq_sig_term_handler, NULL); if (!se) { exit_server("failed to setup SIGTERM handler"); } }
struct kernel_oplocks *linux_init_kernel_oplocks(TALLOC_CTX *mem_ctx) { struct kernel_oplocks *ctx; struct tevent_signal *se; if (!linux_oplocks_available()) { DEBUG(3,("Linux kernel oplocks not available\n")); return NULL; } ctx = talloc_zero(mem_ctx, struct kernel_oplocks); if (!ctx) { DEBUG(0,("Linux Kernel oplocks talloc_Zero failed\n")); return NULL; } ctx->ops = &linux_koplocks; se = tevent_add_signal(server_event_context(), ctx, RT_SIGNAL_LEASE, SA_SIGINFO, linux_oplock_signal_handler, ctx); if (!se) { DEBUG(0,("Failed to setup RT_SIGNAL_LEASE handler")); TALLOC_FREE(ctx); return NULL; } ctx->private_data = se; DEBUG(3,("Linux kernel oplocks enabled\n")); return ctx; }
static bool init_aio_threadpool(struct vfs_handle_struct *handle) { struct fd_event *sock_event = NULL; int ret = 0; int num_threads; if (pool) { return true; } num_threads = aio_get_num_threads(handle); ret = pthreadpool_init(num_threads, &pool); if (ret) { errno = ret; return false; } sock_event = tevent_add_fd(server_event_context(), NULL, pthreadpool_signal_fd(pool), TEVENT_FD_READ, aio_pthread_handle_completion, NULL); if (sock_event == NULL) { pthreadpool_destroy(pool); pool = NULL; return false; } DEBUG(10,("init_aio_threadpool: initialized with up to %d threads\n", num_threads)); return true; }
uint32_t _fss_IsPathSupported(struct pipes_struct *p, struct fss_IsPathSupported *r) { int snum; char *service; char *base_vol; NTSTATUS status; struct connection_struct *conn; char *share; TALLOC_CTX *tmp_ctx = talloc_new(p->mem_ctx); if (tmp_ctx == NULL) { return HRES_ERROR_V(HRES_E_OUTOFMEMORY); } if (!fss_permitted(p)) { talloc_free(tmp_ctx); return HRES_ERROR_V(HRES_E_ACCESSDENIED); } status = fss_unc_parse(tmp_ctx, r->in.ShareName, NULL, &share); if (!NT_STATUS_IS_OK(status)) { talloc_free(tmp_ctx); return fss_ntstatus_map(status); } snum = find_service(tmp_ctx, share, &service); if ((snum == -1) || (service == NULL)) { DEBUG(0, ("share at %s not found\n", r->in.ShareName)); talloc_free(tmp_ctx); return HRES_ERROR_V(HRES_E_INVALIDARG); } status = fss_vfs_conn_create(tmp_ctx, server_event_context(), p->msg_ctx, p->session_info, snum, &conn); if (!NT_STATUS_IS_OK(status)) { talloc_free(tmp_ctx); return HRES_ERROR_V(HRES_E_ACCESSDENIED); } if (!become_user_by_session(conn, p->session_info)) { DEBUG(0, ("failed to become user\n")); talloc_free(tmp_ctx); fss_vfs_conn_destroy(conn); return HRES_ERROR_V(HRES_E_ACCESSDENIED); } status = SMB_VFS_SNAP_CHECK_PATH(conn, tmp_ctx, lp_path(tmp_ctx, snum), &base_vol); unbecome_user(); fss_vfs_conn_destroy(conn); if (!NT_STATUS_IS_OK(status)) { talloc_free(tmp_ctx); return FSRVP_E_NOT_SUPPORTED; } *r->out.OwnerMachineName = lp_netbios_name(); *r->out.SupportedByThisProvider = 1; talloc_free(tmp_ctx); return 0; }
static bool init_aio_linux(struct vfs_handle_struct *handle) { struct tevent_timer *te = NULL; if (event_fd != -1) { /* Already initialized. */ return true; } /* Schedule a shutdown event for 30 seconds from now. */ te = tevent_add_timer(handle->conn->sconn->ev_ctx, NULL, timeval_current_ofs(30, 0), aio_linux_housekeeping, NULL); if (te == NULL) { goto fail; } event_fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC); if (event_fd == -1) { goto fail; } aio_read_event = tevent_add_fd(server_event_context(), NULL, event_fd, TEVENT_FD_READ, aio_linux_done, NULL); if (aio_read_event == NULL) { goto fail; } if (io_queue_init(lp_aio_max_threads(), &io_ctx)) { goto fail; } DEBUG(10,("init_aio_linux: initialized with up to %d events\n", (int)lp_aio_max_threads())); return true; fail: DEBUG(10,("init_aio_linux: initialization failed\n")); TALLOC_FREE(te); TALLOC_FREE(aio_read_event); if (event_fd != -1) { close(event_fd); event_fd = -1; } memset(&io_ctx, '\0', sizeof(io_ctx)); return false; }
/* test if system path exists */ static bool snap_path_exists(TALLOC_CTX *ctx, struct messaging_context *msg_ctx, struct fss_sc *sc) { SMB_STRUCT_STAT st; struct connection_struct *conn = NULL; struct smb_filename *smb_fname = NULL; char *service = NULL; char *share; int snum; int ret; NTSTATUS status; bool result = false; ZERO_STRUCT(st); if ((sc->smaps_count == 0) || (sc->sc_path == NULL)) { goto out; } share = sc->smaps->share_name; snum = find_service(ctx, share, &service); if ((snum == -1) || (service == NULL)) { goto out; } status = fss_vfs_conn_create(ctx, server_event_context(), msg_ctx, NULL, snum, &conn); if(!NT_STATUS_IS_OK(status)) { goto out; } smb_fname = synthetic_smb_fname(service, sc->sc_path, NULL, NULL); if (smb_fname == NULL) { goto out; } ret = SMB_VFS_STAT(conn, smb_fname); if ((ret == -1) && (errno == ENOENT)) { goto out; } result = true; out: if (conn) { fss_vfs_conn_destroy(conn); } TALLOC_FREE(service); return result; }
static void aio_child_cleanup(struct event_context *event_ctx, struct timed_event *te, struct timeval now, void *private_data) { struct aio_child_list *list = talloc_get_type_abort( private_data, struct aio_child_list); struct aio_child *child, *next; TALLOC_FREE(list->cleanup_event); for (child = list->children; child != NULL; child = next) { next = child->next; if (child->aiocb != NULL) { DEBUG(10, ("child %d currently active\n", (int)child->pid)); continue; } if (child->dont_delete) { DEBUG(10, ("Child %d was active since last cleanup\n", (int)child->pid)); child->dont_delete = false; continue; } DEBUG(10, ("Child %d idle for more than 30 seconds, " "deleting\n", (int)child->pid)); TALLOC_FREE(child); child = next; } if (list->children != NULL) { /* * Re-schedule the next cleanup round */ list->cleanup_event = event_add_timed(server_event_context(), list, timeval_add(&now, 30, 0), aio_child_cleanup, list); } }
static void fss_seq_tout_set(TALLOC_CTX *mem_ctx, uint32_t timeout_s, struct fss_sc_set *sc_set, struct tevent_timer **tmr_out) { struct tevent_timer *tmr; struct GUID *sc_set_id = NULL; uint32_t tout; /* allow changes to timeout for testing/debugging purposes */ tout = lp_parm_int(GLOBAL_SECTION_SNUM, "fss", "sequence timeout", timeout_s); if (tout == 0) { DEBUG(2, ("FSRVP message sequence timeout disabled\n")); *tmr_out = NULL; return; } if (sc_set) { /* don't use talloc_memdup(), need explicit type for callback */ sc_set_id = talloc(mem_ctx, struct GUID); if (sc_set_id == NULL) { smb_panic("no memory"); } memcpy(sc_set_id, &sc_set->id, sizeof(*sc_set_id)); } tmr = tevent_add_timer(server_event_context(), mem_ctx, timeval_current_ofs(tout, 0), fss_seq_tout_handler, sc_set_id); if (tmr == NULL) { talloc_free(sc_set_id); smb_panic("no memory"); } *tmr_out = tmr; }
int main(int argc, const char *argv[]) { bool is_daemon = false; bool opt_interactive = false; bool Fork = true; bool no_process_group = false; bool log_stdout = false; poptContext pc; char *p_lmhosts = NULL; int opt; struct messaging_context *msg; enum { OPT_DAEMON = 1000, OPT_INTERACTIVE, OPT_FORK, OPT_NO_PROCESS_GROUP, OPT_LOG_STDOUT }; struct poptOption long_options[] = { POPT_AUTOHELP {"daemon", 'D', POPT_ARG_NONE, NULL, OPT_DAEMON, "Become a daemon(default)" }, {"interactive", 'i', POPT_ARG_NONE, NULL, OPT_INTERACTIVE, "Run interactive (not a daemon)" }, {"foreground", 'F', POPT_ARG_NONE, NULL, OPT_FORK, "Run daemon in foreground (for daemontools & etc)" }, {"no-process-group", 0, POPT_ARG_NONE, NULL, OPT_NO_PROCESS_GROUP, "Don't create a new process group" }, {"log-stdout", 'S', POPT_ARG_NONE, NULL, OPT_LOG_STDOUT, "Log to stdout" }, {"hosts", 'H', POPT_ARG_STRING, &p_lmhosts, 0, "Load a netbios hosts file"}, {"port", 'p', POPT_ARG_INT, &global_nmb_port, 0, "Listen on the specified port" }, POPT_COMMON_SAMBA { NULL } }; TALLOC_CTX *frame; NTSTATUS status; /* * Do this before any other talloc operation */ talloc_enable_null_tracking(); frame = talloc_stackframe(); setup_logging(argv[0], DEBUG_DEFAULT_STDOUT); load_case_tables(); global_nmb_port = NMB_PORT; pc = poptGetContext("nmbd", argc, argv, long_options, 0); while ((opt = poptGetNextOpt(pc)) != -1) { switch (opt) { case OPT_DAEMON: is_daemon = true; break; case OPT_INTERACTIVE: opt_interactive = true; break; case OPT_FORK: Fork = false; break; case OPT_NO_PROCESS_GROUP: no_process_group = true; break; case OPT_LOG_STDOUT: log_stdout = true; break; default: d_fprintf(stderr, "\nInvalid option %s: %s\n\n", poptBadOption(pc, 0), poptStrerror(opt)); poptPrintUsage(pc, stderr, 0); exit(1); } }; poptFreeContext(pc); global_in_nmbd = true; StartupTime = time(NULL); sys_srandom(time(NULL) ^ getpid()); if (!override_logfile) { char *lfile = NULL; if (asprintf(&lfile, "%s/log.nmbd", get_dyn_LOGFILEBASE()) < 0) { exit(1); } lp_set_logfile(lfile); SAFE_FREE(lfile); } fault_setup(); dump_core_setup("nmbd", lp_logfile()); /* POSIX demands that signals are inherited. If the invoking process has * these signals masked, we will have problems, as we won't receive them. */ BlockSignals(False, SIGHUP); BlockSignals(False, SIGUSR1); BlockSignals(False, SIGTERM); #if defined(SIGFPE) /* we are never interested in SIGFPE */ BlockSignals(True,SIGFPE); #endif /* We no longer use USR2... */ #if defined(SIGUSR2) BlockSignals(True, SIGUSR2); #endif if ( opt_interactive ) { Fork = False; log_stdout = True; } if ( log_stdout && Fork ) { DEBUG(0,("ERROR: Can't log to stdout (-S) unless daemon is in foreground (-F) or interactive (-i)\n")); exit(1); } if (log_stdout) { setup_logging(argv[0], DEBUG_STDOUT); } else { setup_logging( argv[0], DEBUG_FILE); } reopen_logs(); DEBUG(0,("nmbd version %s started.\n", samba_version_string())); DEBUGADD(0,("%s\n", COPYRIGHT_STARTUP_MESSAGE)); if (!lp_load_initial_only(get_dyn_CONFIGFILE())) { DEBUG(0, ("error opening config file '%s'\n", get_dyn_CONFIGFILE())); exit(1); } msg = messaging_init(NULL, server_event_context()); if (msg == NULL) { return 1; } if ( !reload_nmbd_services(False) ) return(-1); if(!init_names()) return -1; reload_nmbd_services( True ); if (strequal(lp_workgroup(),"*")) { DEBUG(0,("ERROR: a workgroup name of * is no longer supported\n")); exit(1); } set_samba_nb_type(); if (!is_daemon && !is_a_socket(0)) { DEBUG(0,("standard input is not a socket, assuming -D option\n")); is_daemon = True; } if (is_daemon && !opt_interactive) { DEBUG( 2, ( "Becoming a daemon.\n" ) ); become_daemon(Fork, no_process_group, log_stdout); } #if HAVE_SETPGID /* * If we're interactive we want to set our own process group for * signal management. */ if (opt_interactive && !no_process_group) setpgid( (pid_t)0, (pid_t)0 ); #endif #ifndef SYNC_DNS /* Setup the async dns. We do it here so it doesn't have all the other stuff initialised and thus chewing memory and sockets */ if(lp_we_are_a_wins_server() && lp_dns_proxy()) { start_async_dns(msg); } #endif if (!directory_exist(lp_lockdir())) { mkdir(lp_lockdir(), 0755); } if (!directory_exist(lp_piddir())) { mkdir(lp_piddir(), 0755); } pidfile_create("nmbd"); status = reinit_after_fork(msg, nmbd_event_context(), false); if (!NT_STATUS_IS_OK(status)) { DEBUG(0,("reinit_after_fork() failed\n")); exit(1); } /* * Do not initialize the parent-child-pipe before becoming * a daemon: this is used to detect a died parent in the child * process. */ status = init_before_fork(); if (!NT_STATUS_IS_OK(status)) { DEBUG(0, ("init_before_fork failed: %s\n", nt_errstr(status))); exit(1); } if (!nmbd_setup_sig_term_handler(msg)) exit(1); if (!nmbd_setup_stdin_handler(msg, !Fork)) exit(1); if (!nmbd_setup_sig_hup_handler(msg)) exit(1); /* get broadcast messages */ if (!serverid_register(messaging_server_id(msg), FLAG_MSG_GENERAL | FLAG_MSG_NMBD | FLAG_MSG_DBWRAP)) { DEBUG(1, ("Could not register myself in serverid.tdb\n")); exit(1); } messaging_register(msg, NULL, MSG_FORCE_ELECTION, nmbd_message_election); #if 0 /* Until winsrepl is done. */ messaging_register(msg, NULL, MSG_WINS_NEW_ENTRY, nmbd_wins_new_entry); #endif messaging_register(msg, NULL, MSG_SHUTDOWN, nmbd_terminate); messaging_register(msg, NULL, MSG_SMB_CONF_UPDATED, msg_reload_nmbd_services); messaging_register(msg, NULL, MSG_SEND_PACKET, msg_nmbd_send_packet); TimeInit(); DEBUG( 3, ( "Opening sockets %d\n", global_nmb_port ) ); if ( !open_sockets( is_daemon, global_nmb_port ) ) { kill_async_dns_child(); return 1; } /* Determine all the IP addresses we have. */ load_interfaces(); /* Create an nmbd subnet record for each of the above. */ if( False == create_subnets() ) { DEBUG(0,("ERROR: Failed when creating subnet lists. Exiting.\n")); kill_async_dns_child(); exit(1); } /* Load in any static local names. */ if (p_lmhosts) { set_dyn_LMHOSTSFILE(p_lmhosts); } load_lmhosts_file(get_dyn_LMHOSTSFILE()); DEBUG(3,("Loaded hosts file %s\n", get_dyn_LMHOSTSFILE())); /* If we are acting as a WINS server, initialise data structures. */ if( !initialise_wins() ) { DEBUG( 0, ( "nmbd: Failed when initialising WINS server.\n" ) ); kill_async_dns_child(); exit(1); } /* * Register nmbd primary workgroup and nmbd names on all * the broadcast subnets, and on the WINS server (if specified). * Also initiate the startup of our primary workgroup (start * elections if we are setup as being able to be a local * master browser. */ if( False == register_my_workgroup_and_names() ) { DEBUG(0,("ERROR: Failed when creating my my workgroup. Exiting.\n")); kill_async_dns_child(); exit(1); } if (!initialize_nmbd_proxy_logon()) { DEBUG(0,("ERROR: Failed setup nmbd_proxy_logon.\n")); kill_async_dns_child(); exit(1); } if (!nmbd_init_packet_server()) { kill_async_dns_child(); exit(1); } TALLOC_FREE(frame); process(msg); kill_async_dns_child(); return(0); }
uint32_t _fss_CommitShadowCopySet(struct pipes_struct *p, struct fss_CommitShadowCopySet *r) { struct fss_sc_set *sc_set; struct fss_sc *sc; uint32_t commit_count; NTSTATUS status; NTSTATUS saved_status; TALLOC_CTX *tmp_ctx; if (!fss_permitted(p)) { status = NT_STATUS_ACCESS_DENIED; goto err_out; } tmp_ctx = talloc_new(p->mem_ctx); if (tmp_ctx == NULL) { status = NT_STATUS_NO_MEMORY; goto err_out; } sc_set = sc_set_lookup(fss_global.sc_sets, &r->in.ShadowCopySetId); if (sc_set == NULL) { status = NT_STATUS_INVALID_PARAMETER; goto err_tmp_free; } if (sc_set->state != FSS_SC_ADDED) { status = NT_STATUS_INVALID_SERVER_STATE; goto err_tmp_free; } /* stop Message Sequence Timer */ TALLOC_FREE(fss_global.seq_tmr); sc_set->state = FSS_SC_CREATING; commit_count = 0; saved_status = NT_STATUS_OK; for (sc = sc_set->scs; sc; sc = sc->next) { char *base_path; char *snap_path; status = commit_sc_with_conn(tmp_ctx, server_event_context(), p->msg_ctx, p->session_info, sc, &base_path, &snap_path); if (!NT_STATUS_IS_OK(status)) { DEBUG(0, ("snap create failed for shadow copy of " "%s\n", sc->volume_name)); /* dispatch all scs in set, but retain last error */ saved_status = status; continue; } /* XXX set timeout r->in.TimeOutInMilliseconds */ commit_count++; DEBUG(10, ("good snap create %d\n", commit_count)); sc->sc_path = talloc_steal(sc, snap_path); } if (!NT_STATUS_IS_OK(saved_status)) { status = saved_status; goto err_state_revert; } sc_set->state = FSS_SC_COMMITED; become_root(); status = fss_state_store(fss_global.mem_ctx, fss_global.sc_sets, fss_global.sc_sets_count, fss_global.db_path); unbecome_root(); if (!NT_STATUS_IS_OK(status)) { DEBUG(1, ("failed to store fss server state: %s\n", nt_errstr(status))); } fss_seq_tout_set(fss_global.mem_ctx, 180, sc_set, &fss_global.seq_tmr); talloc_free(tmp_ctx); return 0; err_state_revert: sc_set->state = FSS_SC_ADDED; fss_seq_tout_set(fss_global.mem_ctx, 180, sc_set, &fss_global.seq_tmr); err_tmp_free: talloc_free(tmp_ctx); err_out: return fss_ntstatus_map(status); }
uint32_t _fss_AddToShadowCopySet(struct pipes_struct *p, struct fss_AddToShadowCopySet *r) { uint32_t ret; struct fss_sc_set *sc_set; struct fss_sc *sc; struct fss_sc_smap *sc_smap; int snum; char *service; char *base_vol; char *share; char *path_name; struct connection_struct *conn; NTSTATUS status; TALLOC_CTX *tmp_ctx = talloc_new(p->mem_ctx); if (tmp_ctx == NULL) { ret = HRES_ERROR_V(HRES_E_OUTOFMEMORY); goto err_out; } if (!fss_permitted(p)) { ret = HRES_ERROR_V(HRES_E_ACCESSDENIED); goto err_tmp_free; } sc_set = sc_set_lookup(fss_global.sc_sets, &r->in.ShadowCopySetId); if (sc_set == NULL) { ret = HRES_ERROR_V(HRES_E_INVALIDARG); goto err_tmp_free; } status = fss_unc_parse(tmp_ctx, r->in.ShareName, NULL, &share); if (!NT_STATUS_IS_OK(status)) { ret = fss_ntstatus_map(status); goto err_tmp_free; } snum = find_service(tmp_ctx, share, &service); if ((snum == -1) || (service == NULL)) { DEBUG(0, ("share at %s not found\n", r->in.ShareName)); ret = HRES_ERROR_V(HRES_E_INVALIDARG); goto err_tmp_free; } path_name = lp_path(tmp_ctx, snum); if (path_name == NULL) { ret = HRES_ERROR_V(HRES_E_OUTOFMEMORY); goto err_tmp_free; } status = fss_vfs_conn_create(tmp_ctx, server_event_context(), p->msg_ctx, p->session_info, snum, &conn); if (!NT_STATUS_IS_OK(status)) { ret = HRES_ERROR_V(HRES_E_ACCESSDENIED); goto err_tmp_free; } if (!become_user_by_session(conn, p->session_info)) { DEBUG(0, ("failed to become user\n")); fss_vfs_conn_destroy(conn); ret = HRES_ERROR_V(HRES_E_ACCESSDENIED); goto err_tmp_free; } status = SMB_VFS_SNAP_CHECK_PATH(conn, tmp_ctx, path_name, &base_vol); unbecome_user(); fss_vfs_conn_destroy(conn); if (!NT_STATUS_IS_OK(status)) { ret = FSRVP_E_NOT_SUPPORTED; goto err_tmp_free; } if ((sc_set->state != FSS_SC_STARTED) && (sc_set->state != FSS_SC_ADDED)) { ret = FSRVP_E_BAD_STATE; goto err_tmp_free; } /* stop msg seq timer */ TALLOC_FREE(fss_global.seq_tmr); /* * server MUST look up the ShadowCopy in ShadowCopySet.ShadowCopyList * where ShadowCopy.VolumeName matches the file store on which the * share identified by ShareName is hosted. If an entry is found, the * server MUST fail the call with FSRVP_E_OBJECT_ALREADY_EXISTS. * If no entry is found, the server MUST create a new ShadowCopy * object * XXX Windows appears to allow multiple mappings for the same vol! */ sc = sc_lookup_volname(sc_set->scs, base_vol); if (sc != NULL) { ret = FSRVP_E_OBJECT_ALREADY_EXISTS; goto err_tmr_restart; } sc = talloc_zero(sc_set, struct fss_sc); if (sc == NULL) { ret = HRES_ERROR_V(HRES_E_OUTOFMEMORY); goto err_tmr_restart; } talloc_steal(sc, base_vol); sc->volume_name = base_vol; sc->sc_set = sc_set; sc->create_ts = time(NULL); sc->id = GUID_random(); /* Windows servers ignore client ids */ sc->id_str = GUID_string(sc, &sc->id); if (sc->id_str == NULL) { ret = HRES_ERROR_V(HRES_E_OUTOFMEMORY); goto err_sc_free; } sc_smap = talloc_zero(sc, struct fss_sc_smap); if (sc_smap == NULL) { ret = HRES_ERROR_V(HRES_E_OUTOFMEMORY); goto err_sc_free; } talloc_steal(sc_smap, service); sc_smap->share_name = service; sc_smap->is_exposed = false; /* * generate the sc_smap share name now. It is a unique identifier for * the smap used as a tdb key for state storage. */ ret = map_share_name(sc_smap, sc); if (ret) { goto err_sc_free; } /* add share map to shadow-copy */ DLIST_ADD_END(sc->smaps, sc_smap, struct fss_sc_smap *); sc->smaps_count++; /* add shadow-copy to shadow-copy set */ DLIST_ADD_END(sc_set->scs, sc, struct fss_sc *); sc_set->scs_count++; DEBUG(4, ("added volume %s to shadow copy set with GUID %s\n", sc->volume_name, sc_set->id_str)); /* start the Message Sequence Timer with timeout of 1800 seconds */ fss_seq_tout_set(fss_global.mem_ctx, 1800, sc_set, &fss_global.seq_tmr); sc_set->state = FSS_SC_ADDED; r->out.pShadowCopyId = &sc->id; talloc_free(tmp_ctx); return 0; err_sc_free: talloc_free(sc); err_tmr_restart: fss_seq_tout_set(fss_global.mem_ctx, 180, sc_set, &fss_global.seq_tmr); err_tmp_free: talloc_free(tmp_ctx); err_out: return ret; }
static bool recalc_brl_timeout(struct smbd_server_connection *sconn) { struct blocking_lock_record *blr; struct timeval next_timeout; int max_brl_timeout = lp_parm_int(-1, "brl", "recalctime", 5); TALLOC_FREE(sconn->smb1.locks.brl_timeout); next_timeout = timeval_zero(); for (blr = sconn->smb1.locks.blocking_lock_queue; blr; blr = blr->next) { if (timeval_is_zero(&blr->expire_time)) { /* * If we're blocked on pid 0xFFFFFFFFFFFFFFFFLL this is * a POSIX lock, so calculate a timeout of * 10 seconds into the future. */ if (blr->blocking_smblctx == 0xFFFFFFFFFFFFFFFFLL) { struct timeval psx_to = timeval_current_ofs(10, 0); next_timeout = timeval_brl_min(&next_timeout, &psx_to); } continue; } next_timeout = timeval_brl_min(&next_timeout, &blr->expire_time); } if (timeval_is_zero(&next_timeout)) { DEBUG(10, ("Next timeout = Infinite.\n")); return True; } /* to account for unclean shutdowns by clients we need a maximum timeout that we use for checking pending locks. If we have any pending locks at all, then check if the pending lock can continue at least every brl:recalctime seconds (default 5 seconds). This saves us needing to do a message_send_all() in the SIGCHLD handler in the parent daemon. That message_send_all() caused O(n^2) work to be done when IP failovers happened in clustered Samba, which could make the entire system unusable for many minutes. */ if (max_brl_timeout > 0) { struct timeval min_to = timeval_current_ofs(max_brl_timeout, 0); next_timeout = timeval_min(&next_timeout, &min_to); } if (DEBUGLVL(10)) { struct timeval cur, from_now; cur = timeval_current(); from_now = timeval_until(&cur, &next_timeout); DEBUG(10, ("Next timeout = %d.%d seconds from now.\n", (int)from_now.tv_sec, (int)from_now.tv_usec)); } sconn->smb1.locks.brl_timeout = event_add_timed(server_event_context(), NULL, next_timeout, brl_timeout_fn, sconn); if (sconn->smb1.locks.brl_timeout == NULL) { return False; } return True; }
static NTSTATUS close_remove_share_mode(files_struct *fsp, enum file_close_type close_type) { connection_struct *conn = fsp->conn; bool delete_file = false; bool changed_user = false; struct share_mode_lock *lck = NULL; NTSTATUS status = NT_STATUS_OK; NTSTATUS tmp_status; struct file_id id; const struct security_unix_token *del_token = NULL; /* Ensure any pending write time updates are done. */ if (fsp->update_write_time_event) { update_write_time_handler(server_event_context(), fsp->update_write_time_event, timeval_current(), (void *)fsp); } /* * Lock the share entries, and determine if we should delete * on close. If so delete whilst the lock is still in effect. * This prevents race conditions with the file being created. JRA. */ lck = get_share_mode_lock(talloc_tos(), fsp->file_id, NULL, NULL, NULL); if (lck == NULL) { DEBUG(0, ("close_remove_share_mode: Could not get share mode " "lock for file %s\n", fsp_str_dbg(fsp))); status = NT_STATUS_INVALID_PARAMETER; goto done; } if (fsp->write_time_forced) { DEBUG(10,("close_remove_share_mode: write time forced " "for file %s\n", fsp_str_dbg(fsp))); set_close_write_time(fsp, lck->changed_write_time); } else if (fsp->update_write_time_on_close) { /* Someone had a pending write. */ if (null_timespec(fsp->close_write_time)) { DEBUG(10,("close_remove_share_mode: update to current time " "for file %s\n", fsp_str_dbg(fsp))); /* Update to current time due to "normal" write. */ set_close_write_time(fsp, timespec_current()); } else { DEBUG(10,("close_remove_share_mode: write time pending " "for file %s\n", fsp_str_dbg(fsp))); /* Update to time set on close call. */ set_close_write_time(fsp, fsp->close_write_time); } } if (!del_share_mode(lck, fsp)) { DEBUG(0, ("close_remove_share_mode: Could not delete share " "entry for file %s\n", fsp_str_dbg(fsp))); } if (fsp->initial_delete_on_close && !is_delete_on_close_set(lck, fsp->name_hash)) { bool became_user = False; /* Initial delete on close was set and no one else * wrote a real delete on close. */ if (get_current_vuid(conn) != fsp->vuid) { become_user(conn, fsp->vuid); became_user = True; } fsp->delete_on_close = true; set_delete_on_close_lck(fsp, lck, True, get_current_utok(conn)); if (became_user) { unbecome_user(); } } delete_file = is_delete_on_close_set(lck, fsp->name_hash); if (delete_file) { int i; /* See if others still have the file open via this pathname. If this is the case, then don't delete. If all opens are POSIX delete now. */ for (i=0; i<lck->num_share_modes; i++) { struct share_mode_entry *e = &lck->share_modes[i]; if (is_valid_share_mode_entry(e) && e->name_hash == fsp->name_hash) { if (fsp->posix_open && (e->flags & SHARE_MODE_FLAG_POSIX_OPEN)) { continue; } delete_file = False; break; } } } /* Notify any deferred opens waiting on this close. */ notify_deferred_opens(conn->sconn->msg_ctx, lck); reply_to_oplock_break_requests(fsp); /* * NT can set delete_on_close of the last open * reference to a file. */ if (!(close_type == NORMAL_CLOSE || close_type == SHUTDOWN_CLOSE) || !delete_file) { TALLOC_FREE(lck); return NT_STATUS_OK; } /* * Ok, we have to delete the file */ DEBUG(5,("close_remove_share_mode: file %s. Delete on close was set " "- deleting file.\n", fsp_str_dbg(fsp))); /* * Don't try to update the write time when we delete the file */ fsp->update_write_time_on_close = false; del_token = get_delete_on_close_token(lck, fsp->name_hash); SMB_ASSERT(del_token != NULL); if (!unix_token_equal(del_token, get_current_utok(conn))) { /* Become the user who requested the delete. */ DEBUG(5,("close_remove_share_mode: file %s. " "Change user to uid %u\n", fsp_str_dbg(fsp), (unsigned int)del_token->uid)); if (!push_sec_ctx()) { smb_panic("close_remove_share_mode: file %s. failed to push " "sec_ctx.\n"); } set_sec_ctx(del_token->uid, del_token->gid, del_token->ngroups, del_token->groups, NULL); changed_user = true; } /* We can only delete the file if the name we have is still valid and hasn't been renamed. */ tmp_status = vfs_stat_fsp(fsp); if (!NT_STATUS_IS_OK(tmp_status)) { DEBUG(5,("close_remove_share_mode: file %s. Delete on close " "was set and stat failed with error %s\n", fsp_str_dbg(fsp), nt_errstr(tmp_status))); /* * Don't save the errno here, we ignore this error */ goto done; } id = vfs_file_id_from_sbuf(conn, &fsp->fsp_name->st); if (!file_id_equal(&fsp->file_id, &id)) { DEBUG(5,("close_remove_share_mode: file %s. Delete on close " "was set and dev and/or inode does not match\n", fsp_str_dbg(fsp))); DEBUG(5,("close_remove_share_mode: file %s. stored file_id %s, " "stat file_id %s\n", fsp_str_dbg(fsp), file_id_string_tos(&fsp->file_id), file_id_string_tos(&id))); /* * Don't save the errno here, we ignore this error */ goto done; } if ((conn->fs_capabilities & FILE_NAMED_STREAMS) && !is_ntfs_stream_smb_fname(fsp->fsp_name)) { status = delete_all_streams(conn, fsp->fsp_name->base_name); if (!NT_STATUS_IS_OK(status)) { DEBUG(5, ("delete_all_streams failed: %s\n", nt_errstr(status))); goto done; } } if (SMB_VFS_UNLINK(conn, fsp->fsp_name) != 0) { /* * This call can potentially fail as another smbd may * have had the file open with delete on close set and * deleted it when its last reference to this file * went away. Hence we log this but not at debug level * zero. */ DEBUG(5,("close_remove_share_mode: file %s. Delete on close " "was set and unlink failed with error %s\n", fsp_str_dbg(fsp), strerror(errno))); status = map_nt_error_from_unix(errno); } /* As we now have POSIX opens which can unlink * with other open files we may have taken * this code path with more than one share mode * entry - ensure we only delete once by resetting * the delete on close flag. JRA. */ fsp->delete_on_close = false; set_delete_on_close_lck(fsp, lck, false, NULL); done: if (changed_user) { /* unbecome user. */ pop_sec_ctx(); } TALLOC_FREE(lck); if (delete_file) { /* * Do the notification after we released the share * mode lock. Inside notify_fname we take out another * tdb lock. With ctdb also accessing our databases, * this can lead to deadlocks. Putting this notify * after the TALLOC_FREE(lck) above we avoid locking * two records simultaneously. Notifies are async and * informational only, so calling the notify_fname * without holding the share mode lock should not do * any harm. */ notify_fname(conn, NOTIFY_ACTION_REMOVED, FILE_NOTIFY_CHANGE_FILE_NAME, fsp->fsp_name->base_name); } return status; }
bool run_dbwrap_do_locked1(int dummy) { struct tevent_context *ev; struct messaging_context *msg; struct db_context *backend; struct db_context *db; const char *dbname = "test_do_locked.tdb"; const char *keystr = "key"; TDB_DATA key = string_term_tdb_data(keystr); const char *valuestr = "value"; TDB_DATA value = string_term_tdb_data(valuestr); struct do_locked1_state state = { .value = value }; int ret = false; NTSTATUS status; ev = server_event_context(); if (ev == NULL) { fprintf(stderr, "server_event_context() failed\n"); return false; } msg = server_messaging_context(); if (msg == NULL) { fprintf(stderr, "server_messaging_context() failed\n"); return false; } backend = db_open(talloc_tos(), dbname, 0, TDB_CLEAR_IF_FIRST, O_CREAT|O_RDWR, 0644, DBWRAP_LOCK_ORDER_1, DBWRAP_FLAG_NONE); if (backend == NULL) { fprintf(stderr, "db_open failed: %s\n", strerror(errno)); return false; } db = db_open_watched(talloc_tos(), backend, msg); if (db == NULL) { fprintf(stderr, "db_open_watched failed: %s\n", strerror(errno)); return false; } status = dbwrap_do_locked(db, key, do_locked1_cb, &state); if (!NT_STATUS_IS_OK(status)) { fprintf(stderr, "dbwrap_do_locked failed: %s\n", nt_errstr(status)); goto fail; } if (!NT_STATUS_IS_OK(state.status)) { fprintf(stderr, "store returned %s\n", nt_errstr(state.status)); goto fail; } status = dbwrap_parse_record(db, key, do_locked1_check, &state); if (!NT_STATUS_IS_OK(status)) { fprintf(stderr, "dbwrap_parse_record failed: %s\n", nt_errstr(status)); goto fail; } if (!NT_STATUS_IS_OK(state.status)) { fprintf(stderr, "data compare returned %s\n", nt_errstr(status)); goto fail; } status = dbwrap_do_locked(db, key, do_locked1_del, &state); if (!NT_STATUS_IS_OK(status)) { fprintf(stderr, "dbwrap_do_locked failed: %s\n", nt_errstr(status)); goto fail; } if (!NT_STATUS_IS_OK(state.status)) { fprintf(stderr, "delete returned %s\n", nt_errstr(status)); goto fail; } status = dbwrap_parse_record(db, key, do_locked1_check, &state); if (!NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) { fprintf(stderr, "parse_record returned %s, " "expected NOT_FOUND\n", nt_errstr(status)); goto fail; } ret = true; fail: TALLOC_FREE(db); unlink(dbname); return ret; }
struct event_context *nmbd_event_context(void) { return server_event_context(); }
int main(int argc,const char *argv[]) { /* shall I run as a daemon */ bool is_daemon = false; bool interactive = false; bool Fork = true; bool no_process_group = false; bool log_stdout = false; char *ports = NULL; char *profile_level = NULL; int opt; poptContext pc; bool print_build_options = False; enum { OPT_DAEMON = 1000, OPT_INTERACTIVE, OPT_FORK, OPT_NO_PROCESS_GROUP, OPT_LOG_STDOUT }; struct poptOption long_options[] = { POPT_AUTOHELP {"daemon", 'D', POPT_ARG_NONE, NULL, OPT_DAEMON, "Become a daemon (default)" }, {"interactive", 'i', POPT_ARG_NONE, NULL, OPT_INTERACTIVE, "Run interactive (not a daemon)"}, {"foreground", 'F', POPT_ARG_NONE, NULL, OPT_FORK, "Run daemon in foreground (for daemontools, etc.)" }, {"no-process-group", '\0', POPT_ARG_NONE, NULL, OPT_NO_PROCESS_GROUP, "Don't create a new process group" }, {"log-stdout", 'S', POPT_ARG_NONE, NULL, OPT_LOG_STDOUT, "Log to stdout" }, {"build-options", 'b', POPT_ARG_NONE, NULL, 'b', "Print build options" }, {"port", 'p', POPT_ARG_STRING, &ports, 0, "Listen on the specified ports"}, {"profiling-level", 'P', POPT_ARG_STRING, &profile_level, 0, "Set profiling level","PROFILE_LEVEL"}, POPT_COMMON_SAMBA POPT_COMMON_DYNCONFIG POPT_TABLEEND }; struct smbd_parent_context *parent = NULL; TALLOC_CTX *frame; NTSTATUS status; uint64_t unique_id; struct tevent_context *ev_ctx; struct messaging_context *msg_ctx; /* * Do this before any other talloc operation */ talloc_enable_null_tracking(); frame = talloc_stackframe(); setup_logging(argv[0], DEBUG_DEFAULT_STDOUT); load_case_tables(); smbd_init_globals(); TimeInit(); #ifdef HAVE_SET_AUTH_PARAMETERS set_auth_parameters(argc,argv); #endif pc = poptGetContext("smbd", argc, argv, long_options, 0); while((opt = poptGetNextOpt(pc)) != -1) { switch (opt) { case OPT_DAEMON: is_daemon = true; break; case OPT_INTERACTIVE: interactive = true; break; case OPT_FORK: Fork = false; break; case OPT_NO_PROCESS_GROUP: no_process_group = true; break; case OPT_LOG_STDOUT: log_stdout = true; break; case 'b': print_build_options = True; break; default: d_fprintf(stderr, "\nInvalid option %s: %s\n\n", poptBadOption(pc, 0), poptStrerror(opt)); poptPrintUsage(pc, stderr, 0); exit(1); } } poptFreeContext(pc); if (interactive) { Fork = False; log_stdout = True; } if (log_stdout) { setup_logging(argv[0], DEBUG_STDOUT); } else { setup_logging(argv[0], DEBUG_FILE); } if (print_build_options) { build_options(True); /* Display output to screen as well as debug */ exit(0); } #ifdef HAVE_SETLUID /* needed for SecureWare on SCO */ setluid(0); #endif set_remote_machine_name("smbd", False); if (interactive && (DEBUGLEVEL >= 9)) { talloc_enable_leak_report(); } if (log_stdout && Fork) { DEBUG(0,("ERROR: Can't log to stdout (-S) unless daemon is in foreground (-F) or interactive (-i)\n")); exit(1); } /* we want to re-seed early to prevent time delays causing client problems at a later date. (tridge) */ generate_random_buffer(NULL, 0); /* get initial effective uid and gid */ sec_init(); /* make absolutely sure we run as root - to handle cases where people are crazy enough to have it setuid */ gain_root_privilege(); gain_root_group_privilege(); fault_setup(); dump_core_setup("smbd", lp_logfile()); /* we are never interested in SIGPIPE */ BlockSignals(True,SIGPIPE); #if defined(SIGFPE) /* we are never interested in SIGFPE */ BlockSignals(True,SIGFPE); #endif #if defined(SIGUSR2) /* We are no longer interested in USR2 */ BlockSignals(True,SIGUSR2); #endif /* POSIX demands that signals are inherited. If the invoking process has * these signals masked, we will have problems, as we won't recieve them. */ BlockSignals(False, SIGHUP); BlockSignals(False, SIGUSR1); BlockSignals(False, SIGTERM); /* Ensure we leave no zombies until we * correctly set up child handling below. */ CatchChild(); /* we want total control over the permissions on created files, so set our umask to 0 */ umask(0); reopen_logs(); DEBUG(0,("smbd version %s started.\n", samba_version_string())); DEBUGADD(0,("%s\n", COPYRIGHT_STARTUP_MESSAGE)); DEBUG(2,("uid=%d gid=%d euid=%d egid=%d\n", (int)getuid(),(int)getgid(),(int)geteuid(),(int)getegid())); /* Output the build options to the debug log */ build_options(False); if (sizeof(uint16) < 2 || sizeof(uint32) < 4) { DEBUG(0,("ERROR: Samba is not configured correctly for the word size on your machine\n")); exit(1); } if (!lp_load_initial_only(get_dyn_CONFIGFILE())) { DEBUG(0, ("error opening config file '%s'\n", get_dyn_CONFIGFILE())); exit(1); } /* Init the security context and global current_user */ init_sec_ctx(); /* * Initialize the event context. The event context needs to be * initialized before the messaging context, cause the messaging * context holds an event context. * FIXME: This should be s3_tevent_context_init() */ ev_ctx = server_event_context(); if (ev_ctx == NULL) { exit(1); } /* * Init the messaging context * FIXME: This should only call messaging_init() */ msg_ctx = server_messaging_context(); if (msg_ctx == NULL) { exit(1); } /* * Reloading of the printers will not work here as we don't have a * server info and rpc services set up. It will be called later. */ if (!reload_services(NULL, -1, False)) { exit(1); } /* ...NOTE... Log files are working from this point! */ DEBUG(3,("loaded services\n")); init_structs(); #ifdef WITH_PROFILE if (!profile_setup(msg_ctx, False)) { DEBUG(0,("ERROR: failed to setup profiling\n")); return -1; } if (profile_level != NULL) { int pl = atoi(profile_level); struct server_id src; DEBUG(1, ("setting profiling level: %s\n",profile_level)); src.pid = getpid(); set_profile_level(pl, src); } #endif if (!is_daemon && !is_a_socket(0)) { if (!interactive) DEBUG(0,("standard input is not a socket, assuming -D option\n")); /* * Setting is_daemon here prevents us from eventually calling * the open_sockets_inetd() */ is_daemon = True; } if (is_daemon && !interactive) { DEBUG( 3, ( "Becoming a daemon.\n" ) ); become_daemon(Fork, no_process_group, log_stdout); } generate_random_buffer((uint8_t *)&unique_id, sizeof(unique_id)); set_my_unique_id(unique_id); #if HAVE_SETPGID /* * If we're interactive we want to set our own process group for * signal management. */ if (interactive && !no_process_group) setpgid( (pid_t)0, (pid_t)0); #endif if (!directory_exist(lp_lockdir())) mkdir(lp_lockdir(), 0755); if (is_daemon) pidfile_create("smbd"); status = reinit_after_fork(msg_ctx, ev_ctx, procid_self(), false); if (!NT_STATUS_IS_OK(status)) { DEBUG(0,("reinit_after_fork() failed\n")); exit(1); } smbd_server_conn->msg_ctx = msg_ctx; smbd_setup_sig_term_handler(); smbd_setup_sig_hup_handler(ev_ctx, msg_ctx); /* Setup all the TDB's - including CLEAR_IF_FIRST tdb's. */ if (smbd_memcache() == NULL) { exit(1); } memcache_set_global(smbd_memcache()); /* Initialise the password backed before the global_sam_sid to ensure that we fetch from ldap before we make a domain sid up */ if(!initialize_password_db(false, ev_ctx)) exit(1); if (!secrets_init()) { DEBUG(0, ("ERROR: smbd can not open secrets.tdb\n")); exit(1); } if (lp_server_role() == ROLE_DOMAIN_BDC || lp_server_role() == ROLE_DOMAIN_PDC) { struct loadparm_context *lp_ctx = loadparm_init_s3(NULL, loadparm_s3_context()); if (!open_schannel_session_store(NULL, lp_ctx)) { DEBUG(0,("ERROR: Samba cannot open schannel store for secured NETLOGON operations.\n")); exit(1); } TALLOC_FREE(lp_ctx); } if(!get_global_sam_sid()) { DEBUG(0,("ERROR: Samba cannot create a SAM SID.\n")); exit(1); } if (!sessionid_init()) { exit(1); } if (!connections_init(True)) exit(1); if (!locking_init()) exit(1); if (!messaging_tdb_parent_init(ev_ctx)) { exit(1); } if (!notify_internal_parent_init(ev_ctx)) { exit(1); } if (!serverid_parent_init(ev_ctx)) { exit(1); } if (!W_ERROR_IS_OK(registry_init_full())) exit(1); /* Open the share_info.tdb here, so we don't have to open after the fork on every single connection. This is a small performance improvment and reduces the total number of system fds used. */ if (!share_info_db_init()) { DEBUG(0,("ERROR: failed to load share info db.\n")); exit(1); } status = init_system_info(); if (!NT_STATUS_IS_OK(status)) { DEBUG(1, ("ERROR: failed to setup system user info: %s.\n", nt_errstr(status))); return -1; } if (!init_guest_info()) { DEBUG(0,("ERROR: failed to setup guest info.\n")); return -1; } if (!file_init(smbd_server_conn)) { DEBUG(0, ("ERROR: file_init failed\n")); return -1; } /* This MUST be done before start_epmd() because otherwise * start_epmd() forks and races against dcesrv_ep_setup() to * call directory_create_or_exist() */ if (!directory_create_or_exist(lp_ncalrpc_dir(), geteuid(), 0755)) { DEBUG(0, ("Failed to create pipe directory %s - %s\n", lp_ncalrpc_dir(), strerror(errno))); return -1; } if (is_daemon && !interactive) { if (rpc_epmapper_daemon() == RPC_DAEMON_FORK) { start_epmd(ev_ctx, msg_ctx); } } if (!dcesrv_ep_setup(ev_ctx, msg_ctx)) { exit(1); } /* only start other daemons if we are running as a daemon * -- bad things will happen if smbd is launched via inetd * and we fork a copy of ourselves here */ if (is_daemon && !interactive) { if (rpc_lsasd_daemon() == RPC_DAEMON_FORK) { start_lsasd(ev_ctx, msg_ctx); } if (!_lp_disable_spoolss() && (rpc_spoolss_daemon() != RPC_DAEMON_DISABLED)) { bool bgq = lp_parm_bool(-1, "smbd", "backgroundqueue", true); if (!printing_subsystem_init(ev_ctx, msg_ctx, true, bgq)) { exit(1); } } } else if (!_lp_disable_spoolss() && (rpc_spoolss_daemon() != RPC_DAEMON_DISABLED)) { if (!printing_subsystem_init(ev_ctx, msg_ctx, false, false)) { exit(1); } } if (!is_daemon) { /* inetd mode */ TALLOC_FREE(frame); /* Started from inetd. fd 0 is the socket. */ /* We will abort gracefully when the client or remote system goes away */ smbd_server_conn->sock = dup(0); /* close our standard file descriptors */ if (!debug_get_output_is_stdout()) { close_low_fds(False); /* Don't close stderr */ } #ifdef HAVE_ATEXIT atexit(killkids); #endif /* Stop zombies */ smbd_setup_sig_chld_handler(ev_ctx); smbd_process(ev_ctx, smbd_server_conn); exit_server_cleanly(NULL); return(0); } parent = talloc_zero(ev_ctx, struct smbd_parent_context); if (!parent) { exit_server("talloc(struct smbd_parent_context) failed"); } parent->interactive = interactive; if (!open_sockets_smbd(parent, ev_ctx, msg_ctx, ports)) exit_server("open_sockets_smbd() failed"); /* do a printer update now that all messaging has been set up, * before we allow clients to start connecting */ printing_subsystem_update(ev_ctx, msg_ctx, false); TALLOC_FREE(frame); /* make sure we always have a valid stackframe */ frame = talloc_stackframe(); smbd_parent_loop(ev_ctx, parent); exit_server_cleanly(NULL); TALLOC_FREE(frame); return(0); }
uint32_t _fss_DeleteShareMapping(struct pipes_struct *p, struct fss_DeleteShareMapping *r) { struct fss_sc_set *sc_set; struct fss_sc *sc; struct fss_sc_smap *sc_smap; char *share; NTSTATUS status; TALLOC_CTX *tmp_ctx; struct connection_struct *conn; int snum; char *service; if (!fss_permitted(p)) { status = NT_STATUS_ACCESS_DENIED; goto err_out; } tmp_ctx = talloc_new(p->mem_ctx); if (tmp_ctx == NULL) { status = NT_STATUS_NO_MEMORY; goto err_out; } sc_set = sc_set_lookup(fss_global.sc_sets, &r->in.ShadowCopySetId); if (sc_set == NULL) { /* docs say HRES_E_INVALIDARG */ status = NT_STATUS_OBJECTID_NOT_FOUND; goto err_tmp_free; } if ((sc_set->state != FSS_SC_EXPOSED) && (sc_set->state != FSS_SC_RECOVERED)) { status = NT_STATUS_INVALID_SERVER_STATE; goto err_tmp_free; } sc = sc_lookup(sc_set->scs, &r->in.ShadowCopyId); if (sc == NULL) { status = NT_STATUS_INVALID_PARAMETER; goto err_tmp_free; } status = fss_unc_parse(tmp_ctx, r->in.ShareName, NULL, &share); if (!NT_STATUS_IS_OK(status)) { goto err_tmp_free; } sc_smap = sc_smap_lookup(sc->smaps, share); if (sc_smap == NULL) { status = NT_STATUS_INVALID_PARAMETER; goto err_tmp_free; } status = sc_smap_unexpose(p->msg_ctx, sc_smap, false); if (!NT_STATUS_IS_OK(status)) { DEBUG(0, ("failed to remove share %s: %s\n", sc_smap->sc_share_name, nt_errstr(status))); goto err_tmp_free; } message_send_all(p->msg_ctx, MSG_SMB_FORCE_TDIS, sc_smap->sc_share_name, strlen(sc_smap->sc_share_name) + 1, NULL); if (sc->smaps_count > 1) { /* do not delete the underlying snapshot - still in use */ status = NT_STATUS_OK; goto err_tmp_free; } snum = find_service(tmp_ctx, sc_smap->share_name, &service); if ((snum == -1) || (service == NULL)) { DEBUG(0, ("share at %s not found\n", sc_smap->share_name)); status = NT_STATUS_UNSUCCESSFUL; goto err_tmp_free; } status = fss_vfs_conn_create(tmp_ctx, server_event_context(), p->msg_ctx, p->session_info, snum, &conn); if (!NT_STATUS_IS_OK(status)) { goto err_tmp_free; } if (!become_user_by_session(conn, p->session_info)) { DEBUG(0, ("failed to become user\n")); status = NT_STATUS_ACCESS_DENIED; goto err_conn_destroy; } status = SMB_VFS_SNAP_DELETE(conn, tmp_ctx, sc->volume_name, sc->sc_path); unbecome_user(); if (!NT_STATUS_IS_OK(status)) { goto err_conn_destroy; } /* XXX set timeout r->in.TimeOutInMilliseconds */ DEBUG(6, ("good snap delete\n")); DLIST_REMOVE(sc->smaps, sc_smap); sc->smaps_count--; talloc_free(sc_smap); if (sc->smaps_count == 0) { DLIST_REMOVE(sc_set->scs, sc); sc_set->scs_count--; talloc_free(sc); if (sc_set->scs_count == 0) { DLIST_REMOVE(fss_global.sc_sets, sc_set); fss_global.sc_sets_count--; talloc_free(sc_set); } } become_root(); status = fss_state_store(fss_global.mem_ctx, fss_global.sc_sets, fss_global.sc_sets_count, fss_global.db_path); unbecome_root(); if (!NT_STATUS_IS_OK(status)) { DEBUG(1, ("failed to store fss server state: %s\n", nt_errstr(status))); } status = NT_STATUS_OK; err_conn_destroy: fss_vfs_conn_destroy(conn); err_tmp_free: talloc_free(tmp_ctx); err_out: return fss_ntstatus_map(status); }
static struct tevent_req *smbd_smb2_read_send(TALLOC_CTX *mem_ctx, struct tevent_context *ev, struct smbd_smb2_request *smb2req, uint32_t in_smbpid, uint64_t in_file_id_volatile, uint32_t in_length, uint64_t in_offset, uint32_t in_minimum, uint32_t in_remaining) { NTSTATUS status; struct tevent_req *req = NULL; struct smbd_smb2_read_state *state = NULL; struct smb_request *smbreq = NULL; connection_struct *conn = smb2req->tcon->compat_conn; files_struct *fsp = NULL; ssize_t nread = -1; struct lock_struct lock; int saved_errno; req = tevent_req_create(mem_ctx, &state, struct smbd_smb2_read_state); if (req == NULL) { return NULL; } state->smb2req = smb2req; state->in_length = in_length; state->in_offset = in_offset; state->in_minimum = in_minimum; state->out_data = data_blob_null; state->out_remaining = 0; DEBUG(10,("smbd_smb2_read: file_id[0x%016llX]\n", (unsigned long long)in_file_id_volatile)); smbreq = smbd_smb2_fake_smb_request(smb2req); if (tevent_req_nomem(smbreq, req)) { return tevent_req_post(req, ev); } fsp = file_fsp(smbreq, (uint16_t)in_file_id_volatile); if (fsp == NULL) { tevent_req_nterror(req, NT_STATUS_FILE_CLOSED); return tevent_req_post(req, ev); } if (conn != fsp->conn) { tevent_req_nterror(req, NT_STATUS_FILE_CLOSED); return tevent_req_post(req, ev); } if (smb2req->session->vuid != fsp->vuid) { tevent_req_nterror(req, NT_STATUS_FILE_CLOSED); return tevent_req_post(req, ev); } if (fsp->is_directory) { tevent_req_nterror(req, NT_STATUS_INVALID_DEVICE_REQUEST); return tevent_req_post(req, ev); } state->fsp = fsp; state->in_file_id_volatile = in_file_id_volatile; if (IS_IPC(smbreq->conn)) { struct tevent_req *subreq = NULL; state->out_data = data_blob_talloc(state, NULL, in_length); if (in_length > 0 && tevent_req_nomem(state->out_data.data, req)) { return tevent_req_post(req, ev); } if (!fsp_is_np(fsp)) { tevent_req_nterror(req, NT_STATUS_FILE_CLOSED); return tevent_req_post(req, ev); } subreq = np_read_send(state, server_event_context(), fsp->fake_file_handle, state->out_data.data, state->out_data.length); if (tevent_req_nomem(subreq, req)) { return tevent_req_post(req, ev); } tevent_req_set_callback(subreq, smbd_smb2_read_pipe_done, req); return req; } if (!CHECK_READ(fsp, smbreq)) { tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED); return tevent_req_post(req, ev); } status = schedule_smb2_aio_read(fsp->conn, smbreq, fsp, state, &state->out_data, (SMB_OFF_T)in_offset, (size_t)in_length); if (NT_STATUS_IS_OK(status)) { /* * Doing an async read. Don't * send a "gone async" message * as we expect this to be less * than the client timeout period. * JRA. FIXME for offline files.. * FIXME. Add cancel code.. */ smb2req->async = true; return req; } if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) { /* Real error in setting up aio. Fail. */ tevent_req_nterror(req, NT_STATUS_FILE_CLOSED); return tevent_req_post(req, ev); } /* Fallback to synchronous. */ init_strict_lock_struct(fsp, in_file_id_volatile, in_offset, in_length, READ_LOCK, &lock); if (!SMB_VFS_STRICT_LOCK(conn, fsp, &lock)) { tevent_req_nterror(req, NT_STATUS_FILE_LOCK_CONFLICT); return tevent_req_post(req, ev); } /* Try sendfile in preference. */ status = schedule_smb2_sendfile_read(smb2req, state); if (NT_STATUS_IS_OK(status)) { tevent_req_done(req); return tevent_req_post(req, ev); } else { if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) { SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock); tevent_req_nterror(req, status); return tevent_req_post(req, ev); } } /* Ok, read into memory. Allocate the out buffer. */ state->out_data = data_blob_talloc(state, NULL, in_length); if (in_length > 0 && tevent_req_nomem(state->out_data.data, req)) { SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock); return tevent_req_post(req, ev); } nread = read_file(fsp, (char *)state->out_data.data, in_offset, in_length); saved_errno = errno; SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock); DEBUG(10,("smbd_smb2_read: file %s handle [0x%016llX] offset=%llu " "len=%llu returned %lld\n", fsp_str_dbg(fsp), (unsigned long long)in_file_id_volatile, (unsigned long long)in_offset, (unsigned long long)in_length, (long long)nread)); status = smb2_read_complete(req, nread, saved_errno); if (!NT_STATUS_IS_OK(status)) { tevent_req_nterror(req, status); } else { /* Success. */ tevent_req_done(req); } return tevent_req_post(req, ev); }
static ssize_t read_fd(int fd, void *ptr, size_t nbytes, int *recvfd) { struct iovec iov[1]; struct msghdr msg = { .msg_iov = iov, .msg_iovlen = 1 }; ssize_t n; size_t bufsize = msghdr_prep_recv_fds(NULL, NULL, 0, 1); uint8_t buf[bufsize]; msghdr_prep_recv_fds(&msg, buf, bufsize, 1); iov[0].iov_base = (void *)ptr; iov[0].iov_len = nbytes; do { n = recvmsg(fd, &msg, 0); } while ((n == -1) && (errno == EINTR)); if (n <= 0) { return n; } { size_t num_fds = msghdr_extract_fds(&msg, NULL, 0); int fds[num_fds]; msghdr_extract_fds(&msg, fds, num_fds); if (num_fds != 1) { size_t i; for (i=0; i<num_fds; i++) { close(fds[i]); } *recvfd = -1; return n; } *recvfd = fds[0]; } return(n); } static ssize_t write_fd(int fd, void *ptr, size_t nbytes, int sendfd) { struct msghdr msg = {0}; size_t bufsize = msghdr_prep_fds(NULL, NULL, 0, &sendfd, 1); uint8_t buf[bufsize]; struct iovec iov; ssize_t sent; msghdr_prep_fds(&msg, buf, bufsize, &sendfd, 1); iov.iov_base = (void *)ptr; iov.iov_len = nbytes; msg.msg_iov = &iov; msg.msg_iovlen = 1; do { sent = sendmsg(fd, &msg, 0); } while ((sent == -1) && (errno == EINTR)); return sent; } static void aio_child_cleanup(struct tevent_context *event_ctx, struct tevent_timer *te, struct timeval now, void *private_data) { struct aio_child_list *list = talloc_get_type_abort( private_data, struct aio_child_list); struct aio_child *child, *next; TALLOC_FREE(list->cleanup_event); for (child = list->children; child != NULL; child = next) { next = child->next; if (child->busy) { DEBUG(10, ("child %d currently active\n", (int)child->pid)); continue; } if (child->dont_delete) { DEBUG(10, ("Child %d was active since last cleanup\n", (int)child->pid)); child->dont_delete = false; continue; } DEBUG(10, ("Child %d idle for more than 30 seconds, " "deleting\n", (int)child->pid)); TALLOC_FREE(child); child = next; } if (list->children != NULL) { /* * Re-schedule the next cleanup round */ list->cleanup_event = tevent_add_timer(server_event_context(), list, timeval_add(&now, 30, 0), aio_child_cleanup, list); } } static struct aio_child_list *init_aio_children(struct vfs_handle_struct *handle) { struct aio_child_list *data = NULL; if (SMB_VFS_HANDLE_TEST_DATA(handle)) { SMB_VFS_HANDLE_GET_DATA(handle, data, struct aio_child_list, return NULL); } if (data == NULL) { data = talloc_zero(NULL, struct aio_child_list); if (data == NULL) { return NULL; } } /* * Regardless of whether the child_list had been around or not, make * sure that we have a cleanup timed event. This timed event will * delete itself when it finds that no children are around anymore. */ if (data->cleanup_event == NULL) { data->cleanup_event = tevent_add_timer(server_event_context(), data, timeval_current_ofs(30, 0), aio_child_cleanup, data); if (data->cleanup_event == NULL) { TALLOC_FREE(data); return NULL; } } if (!SMB_VFS_HANDLE_TEST_DATA(handle)) { SMB_VFS_HANDLE_SET_DATA(handle, data, free_aio_children, struct aio_child_list, return False); }