使用LDAP接口获取AD域用户

下面代码是从<Windows 2000 Active Directory程序设计> 书上敲下来的, 做了一些修改改, 代码现在能通过编译, 

但是里面有些不足之处, 如很多地方没有判断是否处理成功(代码中// TODO: 处), 这样会导致程序挂掉.

等我有时间调试好程序后再传上来. 之后我会把书上相关的代码尽量多敲一些下来方便大家.




/************************************************************************/
/*    Windows2000ActiveDirectory程序设计.pdf P36                        */
/*		LDAPEnumTop.c						*/
/*	作用: 连接到AD域, 并查找顶层对象, 可以通过递归此函数列出所有对象*/
/************************************************************************/



#include <Windows.h>
#include <stdio.h>
#include <Winldap.h>

#pragma comment(lib,"wldap32.lib")	// 书上没有此项, 增加此项才能通过编译

int main(int argc, char **argv)
{
	PLDAP pldapSession;		// LDAP session data
	PLDAPMessage plmsgSearchResponse;	// server allocated response to search request
	PLDAPMessage plmsgEntry;	// server allocated response to entry request
	PCHAR pszDN;		// LDAP distinguished name stringn 
	PCHAR* ppszDomainDN = NULL;	// Domain DN(string allocated by LDAP library)
	
	// start an LDAP session to nearest LDAP server
	// 不用host 的方法只有Win2000上才能用, 2003 上不能使用
	pldapSession = ldap_init(NULL, LDAP_PORT);	// ldap_init(hostname, port)
	
	// TODO: 判断成功与否, 当初始化失败时不应继续
	
	// authenticate using user's current credentials
	ldap_bind_s(pldapSession, NULL, NULL, LDAP_AUTH_NEGOTIATE);
	
	// serarch the root of the LDAP server
	ldap_search_s(pldapSession,		// session handle
					NULL,	// locaation to start search, NULL specifies top level
					LDAP_SCOPE_BASE,	// search only the root entry (rootDSE)
					NULL,	// search for all objects (only one for the rootDSE)
					NULL,	// no attributes specified, return all attributes
					FALSE,	// return attributes types and values
					&plmsgSearchResponse);	// server allocates and fills with search results
	
	// TODO: 判断是否成功

	// using the defaultNameingContext attribute, get the distinguished name of the domain
	ppszDomainDN = ldap_get_values(pldapSession, plmsgSearchResponse, 
									"defaultNamingContest");

	// TODO: 判断是否成功

	// display info 
	printf("Listing objects at %s.\nPress CTRL+C to interrupt.\n", *ppszDomainDN);

	// search first level of root container
	ldap_search_s(pldapSession, // session handle
				  *ppszDomainDN, // location in directory to start search 
				  LDAP_SCOPE_ONELEVEL,	// search first level below the base entry
				  NULL,		// SEARCH FOR ALL objects
				  NULL,		// no attributes spectified, return all attributes
				  FALSE,	// return attributes types and values
				  &plmsgSearchResponse);	// server allocates and fills with search results

	// TODO: 判断是否成功

	// get the first entry form the search results
	plmsgEntry = ldap_first_entry(pldapSession, plmsgSearchResponse);

	while(plmsgEntry){
		// get the distinguished name of the entry
		pszDN = ldap_get_dn(pldapSession, plmsgEntry);

		// TODO: 判断是否成功

		// print the DN of the entry
		printf("%s\n", pszDN);

		// get next entry
		plmsgEntry = ldap_next_entry(pldapSession, plmsgEntry);
	}

	// instruct the library toi free the search results
	ldap_value_free(ppszDomainDN);

	// close the session
	ldap_unbind(pldapSession);

	return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章