开源NAS项目--Samba

Samba开源项目:http://www.samba.org/samba/

samba 安装所需包:yum -y install  samba samba-client samba-libs samba-common samba-winbind 

samba-winbind-clients  samba-winbind-modules(Fedora19开始,成为一个独立的包)

samba开发所需要的包:yum -y install samba-devel  libwbclient-devel

Samba历史版本(供学习时研究):http://ftp.samba.org/pub/samba/


开发示例:

1,模仿wbinfo -u,列出系统所有可用户的samba用户,包括域用户

//wbinfo-u.c 模仿wbinfo -u,列出系统所有用户 
#include 
#include 
#include 
#include 
typedef int bool;

#include 
int main(int argc,char*argv[]){
        wbcErr wbc_status=0;
        uint32_t i=0;
        uint32_t num_users=0;
        const char**users=NULL;
        wbc_status=wbcListUsers(argv[1],&num_users,&users);
        if(!WBC_ERROR_IS_OK(wbc_status)){
                printf("wbclistusers failed ! \n");return 0;
        }
        for(i=0;i
2,模仿pdbedit -l,弄出所有本地数据库中的用户

//pdbedit-list.c;列出所有本地用户
#include 
#include 
#include 
#include 
typedef int bool;
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define true 1
#define false 0
#ifndef ARRAY_SIZE
#define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
#endif
#ifndef ZERO_STRUCTP
#define ZERO_STRUCTP(x) do { if ((x) != NULL) memset((char *)(x), 0, sizeof(*(x))); } while(0)
#endif
void sid_copy(struct dom_sid *dst, const struct dom_sid *src)
{
	int i;
	ZERO_STRUCTP(dst);
	dst->sid_rev_num = src->sid_rev_num;
	dst->num_auths = src->num_auths;
	memcpy(&dst->id_auth[0], &src->id_auth[0], sizeof(src->id_auth));
	for (i = 0; i < src->num_auths; i++)
		dst->sub_auths[i] = src->sub_auths[i];
}
bool sid_append_rid(struct dom_sid *sid, uint32_t rid)
{
	if (sid->num_auths < ARRAY_SIZE(sid->sub_auths)) {
		sid->sub_auths[sid->num_auths++] = rid;
		return true;
	}
	return false;
}
bool sid_compose(struct dom_sid *dst, const struct dom_sid *domain_sid, uint32_t rid)
{
	sid_copy(dst, domain_sid);
	return sid_append_rid(dst, rid);
}
static int print_users_list(){
	struct pdb_search *u_search;
	struct samr_displayentry userentry;
	struct samu *sam_pwent;
	TALLOC_CTX *tosctx;
	struct dom_sid user_sid;
	bool bret;
	int ret;

	tosctx = talloc_tos();
	if (!tosctx) {
		printf("talloc failed !\n");
		return 1;
	}
	u_search = pdb_search_users(tosctx, 0);
	if (!u_search) {
		printf("User Search failed! \n");
		ret = 1;
		goto done;
	}
	while (u_search->next_entry(u_search, &userentry)) {
		sam_pwent = samu_new(tosctx);
		if (sam_pwent == NULL) {
			printf("talloc failed !\n");
			ret = 1;
			goto done;
		}
		sid_compose(&user_sid, get_global_sam_sid(), userentry.rid);
		bret = pdb_getsampwsid(sam_pwent, &user_sid);
		if (!bret) {
			printf("getsampwsid failed! \n");
			TALLOC_FREE(sam_pwent);
			continue;
		}
	
		printf("%s \n",pdb_get_username(sam_pwent));
		TALLOC_FREE(sam_pwent);
	}
	ret = 0;
done:
	TALLOC_FREE(tosctx);
	return ret;

};

int main(){
	char *pp_err=NULL;
	char *pp_msg=NULL;
	NTSTATUS ret;
	int local_flags=0;
	char *backend=NULL;
	
	TALLOC_CTX *sf=talloc_stackframe();
	struct loadparm_context *lp=loadparm_init(sf);
	//lp_set_cmdline("log level","10");  //打开调试信息
	load_case_tables();
	if(!lp_load_global("/etc/samba/smb.conf")){
		printf("can't load config file! \n");
		return -1;
	}
	if(!initialize_password_db(0,NULL)){
		printf("Failed to open passdb!\n");
		return 1;
	}
	 print_users_list();
}
gcc pdbedit-list.c  -I/usr/include/samba-4.0/ -L/usr/lib64/samba/ -lpdb -lsecrets3 -ltalloc -lsamba-util -lsmbconf -lsamba-hostconfig
3,模仿smbpasswd,进行本地用户管理(添加,删除,修改密码,使能,禁止用户)。

//pdb_user.c
#include 
#include 
#include 
#include 

typedef int bool;
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

int main(){
	char *pp_err=NULL;
	char *pp_msg=NULL;
	NTSTATUS ret;
	int local_flags=0;
	TALLOC_CTX *sf=talloc_stackframe();
	struct loadparm_context *lp=loadparm_init(sf);
	if(getuid()==0){
		local_flags=LOCAL_AM_ROOT;
	}
	load_case_tables();
	if(!lp_load_global("/etc/samba/smb.conf")){
		printf("can't load config file! \n");
		return -1;
	}
	
	if(!init_names())
		return 1;
	if(!initialize_password_db(0,NULL)){
		printf("Failed to open passdb!\n");
		return 1;
	}
	get_global_sam_sid();
/***********passdb.h********************
	#define LOCAL_ADD_USER 0x1
#define LOCAL_DELETE_USER 0x2
#define LOCAL_DISABLE_USER 0x4
#define LOCAL_ENABLE_USER 0x8
#define LOCAL_TRUST_ACCOUNT 0x10
#define LOCAL_SET_NO_PASSWORD 0x20
#define LOCAL_SET_PASSWORD 0x40
#define LOCAL_SET_LDAP_ADMIN_PW 0x80
#define LOCAL_INTERDOM_ACCOUNT 0x100
#define LOCAL_AM_ROOT 0x200  // Act as root 
*/
	local_flags=LOCAL_ADD_USER|LOCAL_SET_PASSWORD;
				//LOCAL_DELETE_USER
				//LOCA_ENABLE_USER
				//LOCAL_DISABLE_USER
				
	ret=local_password_change("user",local_flags,"passwd",&pp_err,&pp_msg);
	if(pp_err){
		printf("%s \n",pp_err);
		free(pp_err);
	}
	if(pp_msg){
		printf("%s \n",pp_msg);
		free(pp_msg);
	}
	printf("%s \n",get_friendly_nt_error_msg(ret));
}

gcc pdb_user.c  -I/usr/include/samba-4.0/ -L/usr/lib64/samba/ -lpdb -lsecrets3 -lsamba-util -lerrors -lsmbconf -lsamba-hostconfig
4,配置Samba,对配置文件smb.conf进行处理

//viewsamba.c ,配置文件解析
#include 
#include 
#include 
#include 
#ifndef __cplusplus
typedef int bool;
#endif
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

int main(){

 	TALLOC_CTX *frame=talloc_stackframe();
	struct loadparm_context *lp=loadparm_init(frame);//初始化一个解析配置空间
	FILE*fp=fopen("/root/test/smb.conf","w+");
	//load_case_tables();
	
	if(!lpcfg_load(lp,"/etc/samba/smb.conf")){ //装载并解析配置文件
		printf(" failed to open passdb !");
		return 0;
	};
	int num_share=lpcfg_numservices(lp);//取得段的个数
	lpcfg_dump(lp,stdout,0,0);//lpcfg_dump(struct loadparm_context *lp_ctx, FILE *f, bool show_defaults,int maxtoprint)
	int i=0;

	char *p=lpcfg_realm(lp);
	char *name=lpcfg_netbios_name(lp);
	char *privatedir=lpcfg_private_dir(lp);
	char *smb=lpcfg_configfile(lp);
	printf("realm:%s\n netbios name=%s\n privatedir=%s\n",p,name,privatedir);
	printf("smb=%s\n",smb);
	//reload_charcnv(lp);
	//lpcfg_set_cmdline(lp,"chengm","samba");//设置一个健-值对,无意义的健会过滤掉
	
	struct loadparm_service *ser=lpcfg_service(lp,"homes");//取得配置文件中的一个段
	lpcfg_dump_one(stdout,0,ser,ser);	//显示homes段的所有健-值对
	lpcfg_dump_a_parameter(lp,ser,"path",stdout); //显示homes段path的值
	printf("=====================================\n");
	lpcfg_do_service_parameter(lp,ser,"path","/var");//修改homes段path的值为 /var
	lpcfg_dump(lp,fp,0,num_share); //保存到文件(fp为测试用临时文件)
	//printf("samba version :%s\n",samba_version_string());
	//printf("samba default path:%s \n",lp_default_path());
	talloc_stackframe_exists();//退出
}

gcc viewsamba.c -I/usr/include/samba-4.0/ -I/usr/lib64/samba/ -lsamba-hostconfig -lsamba-util -lsmbconf




你可能感兴趣的:(开源项目)