samba 4.0.7 devel 编程之用户管理篇

/******类似smbpasswd 可以进行用户的增,删,改,禁止与使能**************************

*******pdb_user.c

*/

#include <unistd.h>

#include <stdio.h>
#include <stdint.h>

#include <string.h>


typedef int bool;
#include <samba_util.h>
#include <charset.h>
#include <util/time.h>
#include <util/data_blob.h>
#include <util/talloc_stack.h>
#include <param.h>
#include <passdb.h>
#include <machine_sid.h>
#include <tevent.h>
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();

        //struct loadparm_context *lp=loadparm_init(sf);
        //printf("smb.conf:%s\n",lpcfg_configfile(lp));   //不清楚之前可以调成功,在这里调不成功
       local_flags=LOCAL_ADD_USER|LOCAL_SET_PASSWORD

        ret=local_password_change("vanstor",local_flags,"passwd",&pp_err,&pp_msg);
if(pp_msg){

printf("%s",pp_msg);

free(pp_msg);

}     

if(pp_err){

printf("%s ",pp_err);

free(pp_err);

}

if(!NT_STATUS_IS_OK(ret)&&!pp_err){

  printf("failed!");

}

   printf("%s \n",get_friendly_nt_error_msg(ret));
}


/**********************

*Makefile

*/

passwd:pdb_user.o
        gcc -I/usr/include/samba-4.0 -L/usr/lib64/samba -lpdb -lsamba-hostconfig -lsamba-util -lsmbconf -ltalloc -lsecrets3 -lerrors -o passwd pdb_user.c


/***************类似pdbedit - L 显示当前samba数据库中的所有用户************************

****** pdbedit-list.c

*/

#include <unistd.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>


typedef int bool;


#include <samba_util.h>
#include <charset.h>
#include <util/time.h>
#include <util/data_blob.h>
#include <util/talloc_stack.h>
#include <param.h>
#include <passdb.h>
#include <machine_sid.h>
#include <tevent.h>
#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;
}
//print_sam_info(sam_pwent, verbosity, smbpwdstyle);
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);

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;
//backend=lp_passdb_backend();
if(!initialize_password_db(0,NULL)){
printf("Failed to open passdb!\n");
return 1;
}
//get_global_sam_sid();

print_users_list();
//struct loadparm_context *lp=loadparm_init(sf);
//printf("smb.conf:%s\n",lpcfg_configfile(lp));


//ret=local_password_change("chengm",0x202,"passwd",&pp_err,&pp_msg);
//printf("%s \n",get_friendly_nt_error_msg(ret));
}


/***********************

********Makefile

***************************/

userlist:pdbedit-list.o
        gcc -I/usr/include/samba-4.0 -L/usr/lib64/ -L/usr/lib64/samba  -lpdb -lsecrets3 -o userlist pdbedit-list.c


/**************类似wbinfo  -u/-g *wbinfo-utest.c***********************

****** 模仿wbinfo -u /-g ,显示本地和域用户

******/

#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
typedef int bool;

#include <wbclient.h>         //这个wbclient头文件: yum install libwbclient-devel
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);  //wbcListGroups(&domain,&num_groups,&groups);
if(!WBC_ERROR_IS_OK(wbc_status)){
printf("wbclistusers failed ! \n");return 0;
}
for(i=0;i<num_users;i++){
printf("%s \n",users[i]);
}
wbcFreeMemory(users);
return 1;
}


/**********

****Makefile

******/

wbinfo:wbinfo-utest.o
gcc -I/usr/include/samba-4.0 -L/usr/lib64/ -L/usr/lib64/samba  -lwbclient -o wbinfo wbinfo-utest.c

你可能感兴趣的:(samba 4.0.7 devel 编程之用户管理篇)