xLib/ldap_tool.c File Reference

LDAP用ライブラリ. More...

#include "ldap_tool.h"
#include "jbxl_state.h"
#include "tlist.h"
Include dependency graph for ldap_tool.c:

Go to the source code of this file.

Functions

void read_ldap_config (char *fn, JBXL_LDAP_Host *ldap_host, JBXL_LDAP_Dn *ldap_bind)
LDAP * open_ldap_connection (JBXL_LDAP_Host *ldap_host, JBXL_LDAP_Dn *ldap_bind)
int simple_check_ldap_passwd (LDAP *ld, char *userid, char *passwd, JBXL_LDAP_Dn *ldap_bind)
int check_ldap_passwd (LDAP *ld, JBXL_LDAP_Dn *user, JBXL_LDAP_Dn *ldap_bind)
void close_ldap_connection (LDAP *ld, JBXL_LDAP_Host **p_ldap_host, JBXL_LDAP_Dn **p_ldap_bind)
void init_LDAP_Host (JBXL_LDAP_Host *host)
void init_LDAP_Dn (JBXL_LDAP_Dn *dn)
void free_LDAP_Host (JBXL_LDAP_Host *host)
void free_LDAP_Dn (JBXL_LDAP_Dn *dn)
JBXL_LDAP_Host * new_LDAP_Host (void)
JBXL_LDAP_Dn * new_LDAP_Dn (void)
void del_LDAP_Host (JBXL_LDAP_Host **p_host)
void del_LDAP_Dn (JBXL_LDAP_Dn **p_dn)

Detailed Description

Author:
Fumi.Iseki (C)
ライブラリ
--L/usr/lib -lldap
Attention:
このプログラムは OpenLDAP を使用しています.
This work is part of OpenLDAP Software <http://www.openldap.org/>.

Definition in file ldap_tool.c.


Function Documentation

int check_ldap_passwd ( LDAP *  ld,
JBXL_LDAP_Dn *  user,
JBXL_LDAP_Dn *  ldap_bind 
)

int check_ldap_passwd(LDAP* ld, JBXL_LDAP_Dn* user, JBXL_LDAP_Dn* ldap_bind)

LDAP を使用してユーザ認証を行う.

Parameters:
ld LDAPサーバへのセッションハンドラ
user ユーザ情報が格納された JBXL_LDAP_Dn
ldap_bind LDAP の BIND情報
Return values:
0 正常終了.
JBXL_LDAP_PASSWD_ERROR ユーザ認証失敗(ユーザは存在するが,パスワードが一致しない)
JBXL_LDAP_USER_ERROR ユーザ認証失敗(ユーザが存在しない)
JBXL_LDAP_BASE_ERROR BASE名が不明
JBXL_ARGS_ERROR LDAPサーバへのセッションハンドラが NULL

Definition at line 265 of file ldap_tool.c.

References _tochar, Buffer::buf, cat_Buffer(), cat_s2Buffer, copy_Buffer(), dup_Buffer(), erase_sBuffer, free_Buffer(), ins_s2Buffer, JBXL_ARGS_ERROR, JBXL_LDAP_BASE_ERROR, JBXL_LDAP_NO_ATTR_ERROR, JBXL_LDAP_NO_ENTRY_ERROR, JBXL_LDAP_NO_USER_ERROR, JBXL_LDAP_NO_VAL_ERROR, JBXL_LDAP_PASSWD_ERROR, JBXL_LDAP_USER_ERROR, and make_Buffer_bystr.

Referenced by simple_check_ldap_passwd().

00266 {
00267     //char* dn_attr[] = {_tochar("distinguishedName"), NULL};
00268     char* dn_attr[] = {_tochar("distinguishedName"), _tochar("commonName"), NULL};
00269 
00270     if (ld==NULL) return JBXL_ARGS_ERROR;
00271 
00272     if (user->base.buf==NULL) user->base = dup_Buffer(ldap_bind->base);
00273     if (user->base.buf==NULL) return JBXL_LDAP_BASE_ERROR;
00274 
00275     //
00276     if (user->dnbind.buf==NULL) {
00277         return JBXL_LDAP_NO_USER_ERROR;
00278     }
00279     else {
00280         Buffer tmp = erase_sBuffer(user->dnbind, "*");
00281         copy_Buffer(&tmp, &user->dnbind);
00282         free_Buffer(&tmp);
00283     }
00284     if (user->dnbind.buf[0]=='\0') {
00285         return JBXL_LDAP_NO_USER_ERROR;
00286     }
00287 
00288     Buffer cond = make_Buffer_bystr("uid=");
00289     cat_Buffer(&user->dnbind, &cond);
00290 
00291     LDAPMessage* res = NULL;
00292     ldap_search_s(ld, (char*)user->base.buf, LDAP_SCOPE_SUBTREE, (char*)cond.buf, dn_attr, 0, &res);
00293     free_Buffer(&cond);
00294     if (res==NULL) {
00295         return JBXL_LDAP_USER_ERROR;
00296     }
00297 
00298     char* attr;
00299     LDAPMessage* ent = NULL;
00300     BerElement*  ber = NULL;
00301 
00302 /*  for debug
00303     for (ent = ldap_first_entry(ld, res); ent != NULL; ent = ldap_next_entry(ld, ent)) {
00304         for (attr = ldap_first_attribute(ld, ent, &ber); attr != NULL; attr = ldap_next_attribute(ld, ent, ber)) {
00305             char** dn = ldap_get_values(ld, ent, attr);
00306             print_message("%s => %s\n", attr, *dn);
00307             ldap_memfree(attr);
00308             free(*dn);
00309         }
00310         ber_free(ber, 0);
00311     } 
00312     ldap_msgfree(res);
00313     return 0;
00314 */
00315 
00316     ent = ldap_first_entry(ld, res);
00317     if (ent==NULL) {
00318         ldap_msgfree(res);
00319         return JBXL_LDAP_NO_ENTRY_ERROR;
00320     }
00321 
00322     attr = ldap_first_attribute(ld, ent, &ber);
00323     ber_free(ber, 0);
00324     if (attr==NULL) {
00325         ldap_msgfree(res);
00326         return JBXL_LDAP_NO_ATTR_ERROR;
00327     }
00328 
00329     char** dn = ldap_get_values(ld, ent, attr); 
00330     if (dn==NULL || *dn==NULL) {
00331         ldap_memfree(attr);
00332         ldap_msgfree(res);
00333         return JBXL_LDAP_NO_VAL_ERROR;
00334     }
00335     
00336     // ユーザチェック   Password "" is OK!! Ohhh GeroGero!!
00337     if (user->passwd.buf==NULL || user->passwd.buf[0]=='\0') return JBXL_LDAP_PASSWD_ERROR;
00338 
00339     Buffer dName = make_Buffer_bystr(*dn);
00340     // distinguishedName 情報がない場合に,DNを生成する.
00341     if (!strncasecmp(attr, "commonName", 10)) {
00342         ins_s2Buffer("cn=", &dName);
00343         cat_s2Buffer(",", &dName);
00344         cat_Buffer(&ldap_bind->base, &dName);
00345     }
00346     ldap_memfree(attr);
00347     ldap_msgfree(res);
00348     free(*dn);
00349         
00350     // パスワード確認
00351     int ret = ldap_simple_bind_s(ld, (const char*)dName.buf, (char*)user->passwd.buf);
00352     free_Buffer(&dName);
00353     if (ret!=LDAP_SUCCESS) return JBXL_LDAP_PASSWD_ERROR;
00354 
00355     // 念のため,セッションを確認
00356     //ret = ldap_compare_s(ld, *dn, "name", (char*)user->dnbind.buf);
00357     //if (ret!=LDAP_COMPARE_TRUE) return 1;
00358 
00359     return 0;
00360 }

Here is the call graph for this function:

Here is the caller graph for this function:

void close_ldap_connection ( LDAP *  ld,
JBXL_LDAP_Host **  p_ldap_host,
JBXL_LDAP_Dn **  p_ldap_bind 
)

void close_ldap_connection(LDAP* ld, JBXL_LDAP_Host** p_ldap_host, JBXL_LDAP_Dn** p_ldap_bind)

LDAPサーバとの接続を閉じる

Parameters:
ld LDAPサーバへのセッションハンドラ
p_ldap_host LDAP の ホスト情報
p_ldap_bind LDAP の BIND情報

Definition at line 372 of file ldap_tool.c.

References del_LDAP_Dn(), and del_LDAP_Host().

Referenced by command_PASSWD(), and command_USERID().

00373 {
00374     del_LDAP_Host(p_ldap_host);
00375     del_LDAP_Dn  (p_ldap_bind);
00376 
00377     ldap_unbind_s(ld);
00378 }

Here is the call graph for this function:

Here is the caller graph for this function:

void del_LDAP_Dn ( JBXL_LDAP_Dn **  p_dn  ) 

Definition at line 452 of file ldap_tool.c.

References free_LDAP_Dn().

Referenced by close_ldap_connection().

00453 {
00454     if (p_dn==NULL) return;
00455 
00456     free_LDAP_Dn(*p_dn);
00457     if (*p_dn!=NULL) free(*p_dn);
00458     *p_dn = NULL;
00459 }

Here is the call graph for this function:

Here is the caller graph for this function:

void del_LDAP_Host ( JBXL_LDAP_Host **  p_host  ) 

Definition at line 442 of file ldap_tool.c.

References free_LDAP_Host().

Referenced by close_ldap_connection().

00443 {
00444     if (p_host==NULL) return;
00445 
00446     free_LDAP_Host(*p_host);
00447     if (*p_host!=NULL) free(*p_host);
00448     *p_host = NULL;
00449 }

Here is the call graph for this function:

Here is the caller graph for this function:

void free_LDAP_Dn ( JBXL_LDAP_Dn *  dn  ) 

Definition at line 414 of file ldap_tool.c.

References free_Buffer().

Referenced by del_LDAP_Dn(), and simple_check_ldap_passwd().

00415 {
00416     if (dn==NULL) return;
00417 
00418     free_Buffer(&(dn->base));
00419     free_Buffer(&(dn->dnbind));
00420     free_Buffer(&(dn->passwd));
00421 }

Here is the call graph for this function:

Here is the caller graph for this function:

void free_LDAP_Host ( JBXL_LDAP_Host *  host  ) 

Definition at line 405 of file ldap_tool.c.

References free_Buffer(), and init_LDAP_Host().

Referenced by del_LDAP_Host().

00406 {
00407     if (host==NULL) return;
00408 
00409     free_Buffer(&(host->hostname));
00410     init_LDAP_Host(host);
00411 }

Here is the call graph for this function:

Here is the caller graph for this function:

void init_LDAP_Dn ( JBXL_LDAP_Dn *  dn  ) 

Definition at line 395 of file ldap_tool.c.

References init_Buffer().

Referenced by new_LDAP_Dn(), and simple_check_ldap_passwd().

00396 {
00397     if (dn==NULL) return;
00398 
00399     dn->base   = init_Buffer();
00400     dn->dnbind = init_Buffer();
00401     dn->passwd = init_Buffer();
00402 }

Here is the call graph for this function:

Here is the caller graph for this function:

void init_LDAP_Host ( JBXL_LDAP_Host *  host  ) 

Definition at line 384 of file ldap_tool.c.

References FALSE, and init_Buffer().

Referenced by free_LDAP_Host(), and new_LDAP_Host().

00385 {
00386     if (host==NULL) return;
00387 
00388     host->hostname = init_Buffer();
00389     host->port     = 0;
00390     host->useSSL   = FALSE;
00391     host->reqCert  = LDAP_OPT_X_TLS_HARD;
00392 }

Here is the call graph for this function:

Here is the caller graph for this function:

JBXL_LDAP_Dn* new_LDAP_Dn ( void   ) 

Definition at line 433 of file ldap_tool.c.

References init_LDAP_Dn().

Referenced by command_PASSWD(), and command_USERID().

00434 {
00435     JBXL_LDAP_Dn* dn = (JBXL_LDAP_Dn*)malloc(sizeof(JBXL_LDAP_Dn));
00436     init_LDAP_Dn(dn);
00437 
00438     return dn;
00439 }

Here is the call graph for this function:

Here is the caller graph for this function:

JBXL_LDAP_Host* new_LDAP_Host ( void   ) 

Definition at line 424 of file ldap_tool.c.

References init_LDAP_Host().

Referenced by command_PASSWD(), and command_USERID().

00425 {
00426     JBXL_LDAP_Host* host = (JBXL_LDAP_Host*)malloc(sizeof(JBXL_LDAP_Host));
00427     init_LDAP_Host(host);
00428 
00429     return host;
00430 }

Here is the call graph for this function:

Here is the caller graph for this function:

LDAP* open_ldap_connection ( JBXL_LDAP_Host *  ldap_host,
JBXL_LDAP_Dn *  ldap_bind 
)

LDAP* open_ldap_connection(JBXL_LDAP_Host* ldap_host, JBXL_LDAP_Dn* ldap_bind)

設定ファイルを読み込み後,LDAPサーバに接続する

Parameters:
ldap_host サーバの情報を格納する JBXL_LDAP_Host へのポインタ
ldap_bind 接続情報を格納する JBXL_LDAP_Dn へのポインタ
Returns:
LDAPサーバへのセッションハンドラ.接続に失敗した場合は NULL

Definition at line 133 of file ldap_tool.c.

References Buffer::buf, cat_Buffer(), cat_s2Buffer, DEBUG_MODE, free_Buffer(), freeNull, itostr_ts(), make_Buffer_bystr, PRINT_MESG, and TRUE.

Referenced by command_PASSWD(), and command_USERID().

00134 {
00135     if (ldap_host==NULL) return NULL;
00136     if (ldap_bind==NULL) return NULL;
00137     //
00138     if (ldap_bind->dnbind.buf==NULL)    return NULL;
00139     if (ldap_bind->passwd.buf==NULL)    return NULL;
00140     if (ldap_bind->passwd.buf[0]=='\0') return NULL;
00141     if (ldap_host->hostname.buf==NULL)  return NULL;
00142     if (ldap_host->port<=0)             return NULL;
00143 
00144     int ret;
00145     LDAP* ld = NULL;
00146 
00147     if (ldap_host->useSSL!=TRUE || ldap_host->port==389) {
00148         DEBUG_MODE PRINT_MESG("INFO LDAP NORMAL Mode\n");
00149         ld = ldap_init((char*)ldap_host->hostname.buf, ldap_host->port);
00150         if (ld==NULL) {
00151             DEBUG_MODE PRINT_MESG("ERR  LDAP Init error.\n");
00152             return NULL;
00153         }
00154 
00155         if (ldap_host->useSSL==TRUE) {    // STARTTLS (動作未確認)
00156             DEBUG_MODE PRINT_MESG("INFO LDAP STARTTLS Mode\n");
00157             ret = ldap_set_option(NULL, LDAP_OPT_X_TLS_REQUIRE_CERT, &ldap_host->reqCert);
00158             if (ret!=LDAP_SUCCESS) {
00159                 DEBUG_MODE PRINT_MESG("ERR  LDAP STARTTLS Require Cert = %s\n", ldap_err2string(ret));
00160                 ldap_unbind_s(ld);
00161                 return NULL;
00162             }
00163 
00164             int ldap_vers = LDAP_VERSION3;
00165             ret = ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, &ldap_vers);
00166             if (ret!=LDAP_SUCCESS) {
00167                 DEBUG_MODE PRINT_MESG("ERR  LDAP STARTTLS Version = %s\n", ldap_err2string(ret));
00168                 ldap_unbind_s(ld);
00169                 return NULL;
00170             }
00171             //
00172             ret = ldap_start_tls_s(ld, NULL, NULL);
00173             if (ret!=LDAP_SUCCESS) {
00174                 DEBUG_MODE PRINT_MESG("ERR  LDAP STARTTLS Start = %s\n", ldap_err2string(ret));
00175                 ldap_unbind_s(ld);
00176                 return NULL;
00177             }
00178         }
00179     }
00180     // 
00181     else {            // LDAP over SSL
00182         DEBUG_MODE PRINT_MESG("INFO LDAP Over SSL Mode\n");
00183         Buffer url = make_Buffer_bystr("ldaps://");
00184         cat_Buffer(&ldap_host->hostname, &url);
00185         cat_s2Buffer(":", &url);
00186         char* str = itostr_ts(ldap_host->port);
00187         cat_s2Buffer(str, &url);
00188         freeNull(str);
00189         DEBUG_MODE PRINT_MESG("INFO LDAP SSL URL = %s\n", (char*)url.buf);
00190         //
00191         ret = ldap_initialize(&ld, (char*)url.buf);
00192         free_Buffer(&url);
00193         if (ret!=LDAP_SUCCESS) {
00194             DEBUG_MODE PRINT_MESG("ERR  LDAP SSL Init = %s\n", ldap_err2string(ret));
00195             return NULL;
00196         }
00197         //
00198         ret = ldap_set_option(NULL, LDAP_OPT_X_TLS_REQUIRE_CERT, &ldap_host->reqCert);
00199         if (ret!=LDAP_SUCCESS) {
00200             DEBUG_MODE PRINT_MESG("ERR  LDAP SSL Require Cert = %s\n", ldap_err2string(ret));
00201             ldap_unbind_s(ld);
00202             return NULL;
00203         }
00204     }
00205 
00206     ret = ldap_simple_bind_s(ld, (char*)ldap_bind->dnbind.buf, (char*)ldap_bind->passwd.buf);
00207     if (ret!=LDAP_SUCCESS) {
00208         DEBUG_MODE PRINT_MESG("ERR  LDAP Bind = %s\n", ldap_err2string(ret));
00209         ldap_unbind_s(ld);
00210         return NULL;
00211     }
00212 
00213     return ld;
00214 }

Here is the call graph for this function:

Here is the caller graph for this function:

void read_ldap_config ( char *  fn,
JBXL_LDAP_Host *  ldap_host,
JBXL_LDAP_Dn *  ldap_bind 
)

void read_ldap_config(char* fn, JBXL_LDAP_Host* ldap_host, JBXL_LDAP_Dn* ldap_bind)

ファイル /etc/openldap/ldap.conf, /etc/ldap.conf, /etc/nslcd.conf, fn を順に読んで JBXL_LDAP_Host* ldap_host, JBXL_LDAP_Dn* ldap_bind に情報を格納する.

Parameters:
fn 設定の格納されたファイル名.
ldap_host サーバの情報を格納する JBXL_LDAP_Host へのポインタ
ldap_bind 接続情報を格納する JBXL_LDAP_Dn へのポインタ

Definition at line 40 of file ldap_tool.c.

References add_tList_end(), Buffer::buf, buffer_key_tList(), copy_Buffer(), decomp_url(), del_all_tList(), dup_Buffer(), erase_sBuffer, free_Buffer(), init_Buffer(), read_index_tList_file(), and TRUE.

Referenced by command_PASSWD(), and command_USERID().

00041 {
00042     if (ldap_host==NULL || ldap_bind==NULL) return;
00043 
00044     tList* lp    = NULL;
00045     tList* cnfg1 = NULL;
00046     tList* cnfg2 = NULL;
00047     tList* cnfg3 = NULL;
00048     tList* cnfg4 = NULL;
00049     Buffer protocol = init_Buffer();
00050 
00051     //
00052     cnfg1 = read_index_tList_file("/etc/ldap.conf", ' ');
00053     cnfg2 = read_index_tList_file("/etc/openldap/ldap.conf", ' ');
00054     cnfg3 = read_index_tList_file("/etc/nslcd.conf", ' ');
00055     if (fn!=NULL) cnfg4 = read_index_tList_file(fn, ' ');
00056 
00057     lp = add_tList_end(cnfg1, cnfg2);
00058     lp = add_tList_end(lp,    cnfg3);
00059     lp = add_tList_end(lp,    cnfg4);
00060     if (lp==NULL) return;
00061 
00062     //
00063     Buffer uri = buffer_key_tList(lp, "uri", 1);
00064     if (uri.buf!=NULL) {
00065         decomp_url(uri, NULL, &protocol, &ldap_host->hostname, &ldap_host->port, NULL);
00066         if (!strcmp((const char*)protocol.buf, "ldaps")) {
00067             ldap_host->useSSL = TRUE;
00068         }
00069         free_Buffer(&protocol);
00070         free_Buffer(&uri);
00071     }
00072 
00073     ldap_bind->base   = buffer_key_tList(lp, "base",   1);
00074     ldap_bind->dnbind = buffer_key_tList(lp, "rootdn", 1);
00075     ldap_bind->passwd = buffer_key_tList(lp, "rootpw", 1);
00076 
00077     if (ldap_bind->dnbind.buf==NULL || ldap_bind->passwd.buf==NULL) {
00078         free_Buffer(&ldap_bind->dnbind);
00079         free_Buffer(&ldap_bind->passwd);
00080         ldap_bind->dnbind = buffer_key_tList(lp, "binddn", 1);
00081         ldap_bind->passwd = buffer_key_tList(lp, "bindpw", 1);
00082     }
00083     if (ldap_bind->base.buf==NULL) {
00084         ldap_bind->base = dup_Buffer(ldap_bind->dnbind);
00085     }
00086 
00087     //
00088     if (ldap_bind->dnbind.buf!=NULL) {
00089         Buffer tmp = erase_sBuffer(ldap_bind->dnbind, "\"\'");
00090         copy_Buffer(&tmp, &ldap_bind->dnbind);
00091         free_Buffer(&tmp);
00092     }
00093     if (ldap_bind->base.buf!=NULL) {
00094         Buffer tmp = erase_sBuffer(ldap_bind->base, "\"\'");
00095         copy_Buffer(&tmp, &ldap_bind->base);
00096         free_Buffer(&tmp);
00097     }
00098     if (ldap_host->port<=0) {
00099         if (ldap_host->useSSL==TRUE) ldap_host->port = 636;
00100         else                         ldap_host->port = 389;
00101    }
00102 
00103     // Parameters
00104     Buffer param = buffer_key_tList(lp, "TLS_REQCERT", 1);
00105     if (param.buf!=NULL) {
00106         if      (!strcasecmp((const char*)param.buf, "never"))  ldap_host->reqCert = LDAP_OPT_X_TLS_NEVER;
00107         else if (!strcasecmp((const char*)param.buf, "hard"))   ldap_host->reqCert = LDAP_OPT_X_TLS_HARD;
00108         else if (!strcasecmp((const char*)param.buf, "demand")) ldap_host->reqCert = LDAP_OPT_X_TLS_DEMAND;
00109         else if (!strcasecmp((const char*)param.buf, "allow"))  ldap_host->reqCert = LDAP_OPT_X_TLS_ALLOW;
00110         else if (!strcasecmp((const char*)param.buf, "try"))    ldap_host->reqCert = LDAP_OPT_X_TLS_TRY;
00111         free_Buffer(&param);
00112     }
00113 
00114     //
00115     //print_tList(stdout, lp);
00116     del_all_tList(&lp);
00117 
00118     return;
00119 }

Here is the call graph for this function:

Here is the caller graph for this function:

int simple_check_ldap_passwd ( LDAP *  ld,
char *  userid,
char *  passwd,
JBXL_LDAP_Dn *  ldap_bind 
)

int simple_check_ldap_passwd(LDAP* ld, char* userid, char* passwd, JBXL_LDAP_Dn* ldap_bind)

LDAPを使用してユーザ認証を行う.
データは JBXL_LDAP_Dn型の変数に格納されてから検証される.

Parameters:
ld LDAPサーバへのセッションハンドラ
userid ユーザID
passwd パスワード
ldap_bind LDAP の BIND情報
Return values:
0 正常終了.
JBXL_LDAP_PASSWD_ERROR ユーザ認証失敗(ユーザは存在するが,パスワードが一致しない)
JBXL_LDAP_USER_ERROR ユーザ認証失敗(ユーザが存在しない)
JBXL_LDAP_BASE_ERROR BASE名が不明
JBXL_ARGS_ERROR LDAPサーバへのセッションハンドラが NULL

Definition at line 234 of file ldap_tool.c.

References check_ldap_passwd(), dup_Buffer(), free_LDAP_Dn(), init_LDAP_Dn(), and make_Buffer_bystr.

Referenced by command_PASSWD(), and command_USERID().

00235 {
00236     JBXL_LDAP_Dn user;
00237     init_LDAP_Dn(&user);
00238 
00239     if (userid!=NULL) user.dnbind = make_Buffer_bystr(userid);
00240     if (passwd!=NULL) user.passwd = make_Buffer_bystr(passwd);
00241     user.base = dup_Buffer(ldap_bind->base);
00242 
00243     int ret = check_ldap_passwd(ld, &user, ldap_bind);
00244     free_LDAP_Dn(&user);
00245 
00246     return ret;
00247 }

Here is the call graph for this function:

Here is the caller graph for this function:


Generated on 15 Nov 2023 for JunkBox_Lib by  doxygen 1.6.1