之前有段时间在做一个项目,跟samba有关,发现相关的编程资 料很少,都是直接配置的多,codeing的少。所以后来就自己写了一东西。
项目做完后,再来看了一下samba的源码,学习了一下,发现很多东酉都是可以直接调samba相关的库的。现在总结记录下来:
samba 套件中有samba-devel 开发包,samba开发包提供了许多接口,我们可以在代码中调用这些接口来对samba进行操作。
本节想对samba配置文件的操作进行详细的总结一下:
相关函数介绍:
/*************util/talloc_stack.h***************************/
/*
* Create a new talloc stack frame.
* 内存分配
* When free'd, it frees all stack frames that were created after this one and
* not explicitly freed.
*/
#define talloc_stackframe() _talloc_stackframe(__location__)
/*
* Get us the current top of the talloc stack.3
*/
#define talloc_tos() _talloc_tos(__location__)
TALLOC_CTX *_talloc_tos(const char *location);
/*
* return true if a talloc stackframe exists
* this can be used to prevent memory leaks for code that can
* optionally use a talloc stackframe (eg. nt_errstr())
*/
bool talloc_stackframe_exists(void);
/*******************param.h*******************************/
/**
* Initialise the global parameter structure.
初始化(内存)参数上下文
*/
struct loadparm_context *loadparm_init(TALLOC_CTX *mem_ctx);
struct loadparm_context *loadparm_init_global(bool load_default);
const char *lpcfg_configfile(struct loadparm_context *lp_ctx);
const char *lp_default_path(void);
/**
* Load the services array from the services file.
* 用配置文件填充(内存)参数上下文
* Return True on success, False on failure.
*/
bool lpcfg_load(struct loadparm_context *lp_ctx, const char *filename);
bool lpcfg_load_default(struct loadparm_context *lp_ctx);
/**
* Return the max number of services.
* 返回共享服务数目( [share name] 个数 )
*/
int lpcfg_numservices(struct loadparm_context *lp_ctx);
/**
* Display the contents of the services array in human-readable form.
* 显示当前所有共享的所有参数 ,输出重定向f,show_defaults决定是否显示默认参数
* 输出的服务数目由maxtoprint决定,
*/
void lpcfg_dump(struct loadparm_context *lp_ctx, FILE *f, bool show_defaults,
int maxtoprint);
/**
* Display the contents of one service in human-readable form.
*显示指定共享(service)的所有参数
*/
void lpcfg_dump_one(FILE *f, bool show_defaults,
struct loadparm_service *service,
struct loadparm_service *sDefault);
/*
* 获得指定的共享
*
*/
struct loadparm_service *lpcfg_servicebynum(struct loadparm_context *lp_ctx,
int snum);
struct loadparm_service *lpcfg_service(struct loadparm_context *lp_ctx,
const char *service_name);
const char *lpcfg_servicename(const struct loadparm_service *service);
/**
* Display the contents of a single services record.
* 显示指定共享(service)指定参数(parm_name) 的参数值
*/
bool lpcfg_dump_a_parameter(struct loadparm_context *lp_ctx,
struct loadparm_service *service,
const char *parm_name, FILE * f);
/*
*设置指定共享(service)的指定参数(ParmName)值(ParmValue)
*此设置只是在内存中修改,(要保存,需要写入文件)
*/
bool lpcfg_do_service_parameter(struct loadparm_context *lp_ctx,
struct loadparm_service *service,
const char *pszParmName, const char *pszParmValue);
/**
* Process a parameter.
* 设置全局 属性 ParaName=ParaValue
*/
bool lpcfg_do_global_parameter(struct loadparm_context *lp_ctx,
const char *pszParmName, const char *pszParmValue);
bool lpcfg_do_global_parameter_var(struct loadparm_context *lp_ctx,
const char *pszParmName, const char *fmt, ...);
bool lpcfg_set_cmdline(struct loadparm_context *lp_ctx, const char *pszParmName,
const char *pszParmValue);
bool lpcfg_set_option(struct loadparm_context *lp_ctx, const char *option);
/**
* Return info about the next service in a service. snum==-1 gives the globals.
* Return NULL when out of parameters.
*/
struct parm_struct *lpcfg_next_parameter(struct loadparm_context *lp_ctx, int snum, int *i,
int allparameters);
/* The following definitions come from lib/version.c
* 显示samba版本号
*/
const char *samba_version_string(void);
bool lpcfg_is_mydomain(struct loadparm_context *lp_ctx,
const char *domain);
bool lpcfg_is_my_domain_or_realm(struct loadparm_context *lp_ctx,
const char *domain);
/**
see if a string matches either our primary or one of our secondary
netbios aliases. do a case insensitive match
*/
bool lpcfg_is_myname(struct loadparm_context *lp_ctx, const char *name);
/*************查询全局属性的值***************/
const char **lpcfg_interfaces(struct loadparm_context *);
const char *lpcfg_realm(struct loadparm_context *);
const char *lpcfg_netbios_name(struct loadparm_context *);
const char *lpcfg_private_dir(struct loadparm_context *);
enum server_role {
ROLE_STANDALONE = 0,
ROLE_DOMAIN_MEMBER = 1,
ROLE_DOMAIN_BDC = 2,
ROLE_DOMAIN_PDC = 3,
/* not in samr.idl */
ROLE_ACTIVE_DIRECTORY_DC = 4,
/* To determine the role automatically, this is not a valid role */
ROLE_AUTO = 100
};
int lpcfg_server_role(struct loadparm_context *);
int lpcfg_allow_dns_updates(struct loadparm_context *);
bool lpcfg_is_mydomain(struct loadparm_context *lp_ctx,
const char *domain);
bool lpcfg_is_my_domain_or_realm(struct loadparm_context *lp_ctx,
const char *domain);
bool lpcfg_is_myname(struct loadparm_context *lp_ctx, const char *name);
/*
*查询指定文件所在的位置(目录)
*
*/
char *lpcfg_lock_path(TALLOC_CTX* mem_ctx, struct loadparm_context *lp_ctx,
const char *name);
char *lpcfg_config_path(TALLOC_CTX* mem_ctx, struct loadparm_context *lp_ctx,
const char *name);
char *lpcfg_private_path(TALLOC_CTX* mem_ctx,
struct loadparm_context *lp_ctx,
const char *name);
char *smbd_tmp_path(TALLOC_CTX *mem_ctx,
struct loadparm_context *lp_ctx,
const char *name);
/**
* Return the max print jobs per queue.
* 查询相关属性的值
*/
int lpcfg_maxprintjobs(struct loadparm_service *service, struct loadparm_service *sDefault);
struct smb_iconv_handle *lpcfg_iconv_handle(struct loadparm_context *lp_ctx);
void lpcfg_smbcli_options(struct loadparm_context *lp_ctx,
struct smbcli_options *options);
void lpcfg_smbcli_session_options(struct loadparm_context *lp_ctx,
struct smbcli_session_options *options);
const char **lpcfg_smb_ports(struct loadparm_context *);
const char *lpcfg_socket_options(struct loadparm_context *);
struct dcerpc_server_info *lpcfg_dcerpc_server_info(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx);
struct gensec_settings *lpcfg_gensec_settings(TALLOC_CTX *, struct loadparm_context *);
例子:viewsamba.c
#include <unistd.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#ifndef __cplusplus
typedef int bool;
#endif
#include <samba_util.h>
#include <util/time.h>
#include <util/talloc_stack.h>
#include <charset.h>
#include <util/data_blob.h>
#include <core/ntstatus.h>
#include <gen_ndr/lsa.h>
#include <gen_ndr/samr.h>
#include <passdb.h>
#include <machine_sid.h>
#include <param.h>
#include <tevent.h>
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();
lpcfg_load(lp,"/etc/samba/smb.conf"); //加载配置文件
int num_share=lpcfg_numservices(lp); //共享服务的数量
lpcfg_dump(lp,stdout,0,0); //显示所有配置信自己
int i=0;
char *p=lpcfg_realm(lp); //显示realm 属性的值
char *name=lpcfg_netbios_name(lp); //显示netbios名
char *privatedir=lpcfg_private_dir(lp); //查询samba的私有目录
char *smb=lpcfg_configfile(lp); //查询samba的配置文件
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,"vanstor","samba"); //设置global的属性(因为没有 vanstor这个属性,故会出错)
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); //把上面的修改保存到文件
//printf("samba version :%s\n",samba_version_string());
//printf("samba default path:%s \n",lp_default_path());
talloc_stackframe_exists();
}
编译:
gcc -I/usr/include/samba-4.0 -L/usr/lib64/samba -lsamba-hostconfig -lsamba-util -lsmbconf -ltalloc -lsecrets3 -lerrors -o viewsamba viewsamba.c