📄 srv_srvsvc_nt.c
字号:
/* * Unix SMB/CIFS implementation. * RPC Pipe client / server routines * Copyright (C) Andrew Tridgell 1992-1997, * Copyright (C) Jeremy Allison 2001. * Copyright (C) Nigel Williams 2001. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *//* This is the implementation of the srvsvc pipe. */#include "includes.h"extern struct generic_mapping file_generic_mapping;#undef DBGC_CLASS#define DBGC_CLASS DBGC_RPC_SRV#define INVALID_SHARENAME_CHARS "<>*?|/\\+=;:\","/******************************************************************** Check a string for any occurrences of a specified list of invalid characters.********************************************************************/static BOOL validate_net_name( const char *name, const char *invalid_chars, int max_len ){ int i; for ( i=0; i<max_len && name[i]; i++ ) { /* fail if strchr_m() finds one of the invalid characters */ if ( name[i] && strchr_m( invalid_chars, name[i] ) ) return False; } return True;}/******************************************************************* Utility function to get the 'type' of a share from an snum. ********************************************************************/static uint32 get_share_type(int snum) { char *net_name = lp_servicename(snum); int len_net_name = strlen(net_name); /* work out the share type */ uint32 type = STYPE_DISKTREE; if (lp_print_ok(snum)) type = STYPE_PRINTQ; if (strequal(lp_fstype(snum), "IPC")) type = STYPE_IPC; if (net_name[len_net_name] == '$') type |= STYPE_HIDDEN; return type;} /******************************************************************* Fill in a share info level 0 structure. ********************************************************************/static void init_srv_share_info_0(pipes_struct *p, SRV_SHARE_INFO_0 *sh0, int snum){ pstring net_name; pstrcpy(net_name, lp_servicename(snum)); init_srv_share_info0(&sh0->info_0, net_name); init_srv_share_info0_str(&sh0->info_0_str, net_name);}/******************************************************************* Fill in a share info level 1 structure. ********************************************************************/static void init_srv_share_info_1(pipes_struct *p, SRV_SHARE_INFO_1 *sh1, int snum){ pstring remark; char *net_name = lp_servicename(snum); pstrcpy(remark, lp_comment(snum)); standard_sub_conn(p->conn, remark,sizeof(remark)); init_srv_share_info1(&sh1->info_1, net_name, get_share_type(snum), remark); init_srv_share_info1_str(&sh1->info_1_str, net_name, remark);}/******************************************************************* Fill in a share info level 2 structure. ********************************************************************/static void init_srv_share_info_2(pipes_struct *p, SRV_SHARE_INFO_2 *sh2, int snum){ pstring remark; pstring path; pstring passwd; int max_connections = lp_max_connections(snum); uint32 max_uses = max_connections!=0 ? max_connections : 0xffffffff; char *net_name = lp_servicename(snum); pstrcpy(remark, lp_comment(snum)); standard_sub_conn(p->conn, remark,sizeof(remark)); pstrcpy(path, "C:"); pstrcat(path, lp_pathname(snum)); /* * Change / to \\ so that win2k will see it as a valid path. This was added to * enable use of browsing in win2k add share dialog. */ string_replace(path, '/', '\\'); pstrcpy(passwd, ""); init_srv_share_info2(&sh2->info_2, net_name, get_share_type(snum), remark, 0, max_uses, 1, path, passwd); init_srv_share_info2_str(&sh2->info_2_str, net_name, remark, path, passwd);}/******************************************************************* What to do when smb.conf is updated. ********************************************************************/static void smb_conf_updated(int msg_type, struct process_id src, void *buf, size_t len){ DEBUG(10,("smb_conf_updated: Got message saying smb.conf was updated. Reloading.\n")); reload_services(False);}/******************************************************************* Create the share security tdb. ********************************************************************/static TDB_CONTEXT *share_tdb; /* used for share security descriptors */#define SHARE_DATABASE_VERSION_V1 1#define SHARE_DATABASE_VERSION_V2 2 /* version id in little endian. */BOOL share_info_db_init(void){ static pid_t local_pid; const char *vstring = "INFO/version"; int32 vers_id; if (share_tdb && local_pid == sys_getpid()) return True; share_tdb = tdb_open_log(lock_path("share_info.tdb"), 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600); if (!share_tdb) { DEBUG(0,("Failed to open share info database %s (%s)\n", lock_path("share_info.tdb"), strerror(errno) )); return False; } local_pid = sys_getpid(); /* handle a Samba upgrade */ tdb_lock_bystring(share_tdb, vstring, 0); /* Cope with byte-reversed older versions of the db. */ vers_id = tdb_fetch_int32(share_tdb, vstring); if ((vers_id == SHARE_DATABASE_VERSION_V1) || (IREV(vers_id) == SHARE_DATABASE_VERSION_V1)) { /* Written on a bigendian machine with old fetch_int code. Save as le. */ tdb_store_int32(share_tdb, vstring, SHARE_DATABASE_VERSION_V2); vers_id = SHARE_DATABASE_VERSION_V2; } if (vers_id != SHARE_DATABASE_VERSION_V2) { tdb_traverse(share_tdb, tdb_traverse_delete_fn, NULL); tdb_store_int32(share_tdb, vstring, SHARE_DATABASE_VERSION_V2); } tdb_unlock_bystring(share_tdb, vstring); message_register(MSG_SMB_CONF_UPDATED, smb_conf_updated); return True;}/******************************************************************* Fake up a Everyone, full access as a default. ********************************************************************/static SEC_DESC *get_share_security_default( TALLOC_CTX *ctx, int snum, size_t *psize){ SEC_ACCESS sa; SEC_ACE ace; SEC_ACL *psa = NULL; SEC_DESC *psd = NULL; uint32 def_access = GENERIC_ALL_ACCESS; se_map_generic(&def_access, &file_generic_mapping); init_sec_access(&sa, GENERIC_ALL_ACCESS | def_access ); init_sec_ace(&ace, &global_sid_World, SEC_ACE_TYPE_ACCESS_ALLOWED, sa, 0); if ((psa = make_sec_acl(ctx, NT4_ACL_REVISION, 1, &ace)) != NULL) { psd = make_sec_desc(ctx, SEC_DESC_REVISION, SEC_DESC_SELF_RELATIVE, NULL, NULL, NULL, psa, psize); } if (!psd) { DEBUG(0,("get_share_security: Failed to make SEC_DESC.\n")); return NULL; } return psd;}/******************************************************************* Pull a security descriptor from the share tdb. ********************************************************************/static SEC_DESC *get_share_security( TALLOC_CTX *ctx, int snum, size_t *psize){ prs_struct ps; fstring key; SEC_DESC *psd = NULL; *psize = 0; /* Fetch security descriptor from tdb */ slprintf(key, sizeof(key)-1, "SECDESC/%s", lp_servicename(snum)); if (tdb_prs_fetch(share_tdb, key, &ps, ctx)!=0 || !sec_io_desc("get_share_security", &psd, &ps, 1)) { DEBUG(4,("get_share_security: using default secdesc for %s\n", lp_servicename(snum) )); return get_share_security_default(ctx, snum, psize); } if (psd) *psize = sec_desc_size(psd); prs_mem_free(&ps); return psd;}/******************************************************************* Store a security descriptor in the share db. ********************************************************************/static BOOL set_share_security(TALLOC_CTX *ctx, const char *share_name, SEC_DESC *psd){ prs_struct ps; TALLOC_CTX *mem_ctx = NULL; fstring key; BOOL ret = False; mem_ctx = talloc_init("set_share_security"); if (mem_ctx == NULL) return False; prs_init(&ps, (uint32)sec_desc_size(psd), mem_ctx, MARSHALL); if (!sec_io_desc("share_security", &psd, &ps, 1)) goto out; slprintf(key, sizeof(key)-1, "SECDESC/%s", share_name); if (tdb_prs_store(share_tdb, key, &ps)==0) { ret = True; DEBUG(5,("set_share_security: stored secdesc for %s\n", share_name )); } else { DEBUG(1,("set_share_security: Failed to store secdesc for %s\n", share_name )); } /* Free malloc'ed memory */ out: prs_mem_free(&ps); if (mem_ctx) talloc_destroy(mem_ctx); return ret;}/******************************************************************* Delete a security descriptor.********************************************************************/static BOOL delete_share_security(int snum){ TDB_DATA kbuf; fstring key; slprintf(key, sizeof(key)-1, "SECDESC/%s", lp_servicename(snum)); kbuf.dptr = key; kbuf.dsize = strlen(key)+1; if (tdb_delete(share_tdb, kbuf) != 0) { DEBUG(0,("delete_share_security: Failed to delete entry for share %s\n", lp_servicename(snum) )); return False; } return True;}/******************************************************************* Map any generic bits to file specific bits.********************************************************************/void map_generic_share_sd_bits(SEC_DESC *psd){ int i; SEC_ACL *ps_dacl = NULL; if (!psd) return; ps_dacl = psd->dacl; if (!ps_dacl) return; for (i = 0; i < ps_dacl->num_aces; i++) { SEC_ACE *psa = &ps_dacl->ace[i]; uint32 orig_mask = psa->info.mask; se_map_generic(&psa->info.mask, &file_generic_mapping); psa->info.mask |= orig_mask; } }/******************************************************************* Can this user access with share with the required permissions ?********************************************************************/BOOL share_access_check(connection_struct *conn, int snum, user_struct *vuser, uint32 desired_access){ uint32 granted; NTSTATUS status; TALLOC_CTX *mem_ctx = NULL; SEC_DESC *psd = NULL; size_t sd_size; NT_USER_TOKEN *token = NULL; BOOL ret = True; mem_ctx = talloc_init("share_access_check"); if (mem_ctx == NULL) return False; psd = get_share_security(mem_ctx, snum, &sd_size); if (!psd) goto out; if (conn->nt_user_token) token = conn->nt_user_token; else token = vuser->nt_user_token; ret = se_access_check(psd, token, desired_access, &granted, &status);out: talloc_destroy(mem_ctx); return ret;}/******************************************************************* Fill in a share info level 501 structure.********************************************************************/static void init_srv_share_info_501(pipes_struct *p, SRV_SHARE_INFO_501 *sh501, int snum){ pstring remark; const char *net_name = lp_servicename(snum); pstrcpy(remark, lp_comment(snum)); standard_sub_conn(p->conn, remark, sizeof(remark)); init_srv_share_info501(&sh501->info_501, net_name, get_share_type(snum), remark, (lp_csc_policy(snum) << 4)); init_srv_share_info501_str(&sh501->info_501_str, net_name, remark);}/******************************************************************* Fill in a share info level 502 structure. ********************************************************************/static void init_srv_share_info_502(pipes_struct *p, SRV_SHARE_INFO_502 *sh502, int snum){ pstring net_name; pstring remark; pstring path; pstring passwd; SEC_DESC *sd; size_t sd_size; TALLOC_CTX *ctx = p->mem_ctx; ZERO_STRUCTP(sh502); pstrcpy(net_name, lp_servicename(snum)); pstrcpy(remark, lp_comment(snum)); standard_sub_conn(p->conn, remark,sizeof(remark)); pstrcpy(path, "C:"); pstrcat(path, lp_pathname(snum)); /* * Change / to \\ so that win2k will see it as a valid path. This was added to * enable use of browsing in win2k add share dialog. */ string_replace(path, '/', '\\'); pstrcpy(passwd, ""); sd = get_share_security(ctx, snum, &sd_size); init_srv_share_info502(&sh502->info_502, net_name, get_share_type(snum), remark, 0, 0xffffffff, 1, path, passwd, sd, sd_size); init_srv_share_info502_str(&sh502->info_502_str, net_name, remark, path, passwd, sd, sd_size);}/*************************************************************************** Fill in a share info level 1004 structure. ***************************************************************************/static void init_srv_share_info_1004(pipes_struct *p, SRV_SHARE_INFO_1004* sh1004, int snum){ pstring remark; pstrcpy(remark, lp_comment(snum)); standard_sub_conn(p->conn, remark, sizeof(remark)); ZERO_STRUCTP(sh1004); init_srv_share_info1004(&sh1004->info_1004, remark); init_srv_share_info1004_str(&sh1004->info_1004_str, remark);}/*************************************************************************** Fill in a share info level 1005 structure. ***************************************************************************/static void init_srv_share_info_1005(pipes_struct *p, SRV_SHARE_INFO_1005* sh1005, int snum){ sh1005->share_info_flags = 0; if(lp_host_msdfs() && lp_msdfs_root(snum)) sh1005->share_info_flags |= SHARE_1005_IN_DFS | SHARE_1005_DFS_ROOT; sh1005->share_info_flags |= lp_csc_policy(snum) << SHARE_1005_CSC_POLICY_SHIFT;}/*************************************************************************** Fill in a share info level 1006 structure. ***************************************************************************/static void init_srv_share_info_1006(pipes_struct *p, SRV_SHARE_INFO_1006* sh1006, int snum){ sh1006->max_uses = -1;}/***************************************************************************
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -