DTS Application Library  0.2.3
Application library containing referenced objects and interfaces to common libraries
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
Openldap/SASL Interface

Functions to interface with a LDAP server. More...

Files

file  openldap.c
 Openldap/SASL Implementation.
 

Data Structures

struct  ldap_rdn
 LDAP Relative distingushed name linked list. More...
 
struct  ldap_attrval
 LDAP attribute value. More...
 
struct  ldap_attr
 LDAP attirbute. More...
 
struct  ldap_entry
 LDAP entry. More...
 
struct  ldap_results
 LDAP results. More...
 
struct  sasl_defaults
 SASL Paramaters used in authentification. More...
 
struct  ldap_simple
 LDAP Simple bind. More...
 
struct  ldap_conn
 LDAP connection. More...
 
struct  ldap_modify
 LDAP Modify structure. More...
 
struct  ldap_add
 LDAP Add structure. More...
 
struct  ldap_modval
 Linked list of mod values. More...
 
struct  ldap_modreq
 LDAP mod request. More...
 

Typedefs

typedef struct ldap_conn ldap_conn
 Forward decleration of structure. More...
 
typedef struct ldap_modify ldap_modify
 Forward decleration of structure. More...
 
typedef struct ldap_add ldap_add
 Forward decleration of structure. More...
 

Enumerations

enum  ldap_starttls { LDAP_STARTTLS_NONE, LDAP_STARTTLS_ATTEMPT, LDAP_STARTTLS_ENFORCE }
 SSL connection requirements. More...
 
enum  ldap_attrtype { LDAP_ATTRTYPE_CHAR, LDAP_ATTRTYPE_B64, LDAP_ATTRTYPE_OCTET }
 LDAP attribute types. More...
 

Functions

struct ldap_connldap_connect (const char *uri, enum ldap_starttls starttls, int timelimit, int limit, int debug, int *err)
 Connect to a LDAP server. More...
 
int ldap_simplebind (struct ldap_conn *ld, const char *dn, const char *passwd)
 Bind to the connection with simple bind requireing a distingushed name and password. More...
 
int ldap_simplerebind (struct ldap_conn *ldap, const char *initialdn, const char *initialpw, const char *base, const char *filter, const char *uidrdn, const char *uid, const char *passwd)
 Bind to LDAP connection using rebind. More...
 
int ldap_saslbind (struct ldap_conn *ld, const char *mech, const char *realm, const char *authcid, const char *passwd, const char *authzid)
 Bind to the server with SASL. More...
 
const char * ldap_errmsg (int res)
 Return LDAP error for a ldap error. More...
 
struct ldap_resultsldap_search_sub (struct ldap_conn *ld, const char *base, const char *filter, int b64enc, int *res,...)
 Search LDAP connection subtree. More...
 
struct ldap_resultsldap_search_one (struct ldap_conn *ld, const char *base, const char *filter, int b64enc, int *res,...)
 Search LDAP connection one level. More...
 
struct ldap_resultsldap_search_base (struct ldap_conn *ld, const char *base, const char *filter, int b64enc, int *res,...)
 Search LDAP connection base. More...
 
void ldap_unref_attr (struct ldap_entry *entry, struct ldap_attr *attr)
 Remove a attribute from a entry. More...
 
void ldap_unref_entry (struct ldap_results *results, struct ldap_entry *entry)
 Remove a entry from a result. More...
 
struct ldap_entryldap_getentry (struct ldap_results *results, const char *dn)
 Find and return the entry from the results for a specific dn. More...
 
struct ldap_attrldap_getattr (struct ldap_entry *entry, const char *attr)
 Find and return attribute in a entry. More...
 
struct ldap_modifyldap_modifyinit (const char *dn)
 Create a modification reference for a DN. More...
 
int ldap_mod_del (struct ldap_modify *lmod, const char *attr,...)
 Delete values from a attribute. More...
 
int ldap_mod_add (struct ldap_modify *lmod, const char *attr,...)
 Add values to a attribute. More...
 
int ldap_mod_rep (struct ldap_modify *lmod, const char *attr,...)
 Replace a attribute. More...
 
int ldap_domodify (struct ldap_conn *ld, struct ldap_modify *lmod)
 Apply the modification to the server. More...
 
int ldap_mod_delattr (struct ldap_conn *ldap, const char *dn, const char *attr, const char *value)
 Delete a value from a attribute in a DN. More...
 
int ldap_mod_remattr (struct ldap_conn *ldap, const char *dn, const char *attr)
 Delete a attribute from a DN. More...
 
int ldap_mod_addattr (struct ldap_conn *ldap, const char *dn, const char *attr, const char *value)
 Add a value for a attribute in a DN. More...
 
int ldap_mod_repattr (struct ldap_conn *ldap, const char *dn, const char *attr, const char *value)
 Replace the value of a attribute in a DN. More...
 
struct ldap_addldap_addinit (const char *dn)
 Create a reference to add a new DN. More...
 
int ldap_add_attr (struct ldap_add *ladd, const char *attr,...)
 Add a attribute to new DN. More...
 
int ldap_doadd (struct ldap_conn *ld, struct ldap_add *ladd)
 Write new DN to server. More...
 

Detailed Description

Functions to interface with a LDAP server.

Typedef Documentation

typedef struct ldap_add ldap_add

Forward decleration of structure.

Definition at line 789 of file dtsapp.h.

typedef struct ldap_conn ldap_conn

Forward decleration of structure.

Definition at line 785 of file dtsapp.h.

typedef struct ldap_modify ldap_modify

Forward decleration of structure.

Definition at line 787 of file dtsapp.h.

Enumeration Type Documentation

LDAP attribute types.

Enumerator
LDAP_ATTRTYPE_CHAR 

Plain text.

LDAP_ATTRTYPE_B64 

Base64 encoded.

LDAP_ATTRTYPE_OCTET 

Binary data.

Definition at line 707 of file dtsapp.h.

707  {
714 };
Plain text.
Definition: dtsapp.h:709
Base64 encoded.
Definition: dtsapp.h:711
Binary data.
Definition: dtsapp.h:713

SSL connection requirements.

Enumerator
LDAP_STARTTLS_NONE 

SSL not attempted at all.

LDAP_STARTTLS_ATTEMPT 

SSL attempted but not required.

LDAP_STARTTLS_ENFORCE 

SSL is required.

Definition at line 697 of file dtsapp.h.

697  {
704 };
SSL not attempted at all.
Definition: dtsapp.h:699
SSL attempted but not required.
Definition: dtsapp.h:701
SSL is required.
Definition: dtsapp.h:703

Function Documentation

int ldap_add_attr ( struct ldap_add ladd,
const char *  attr,
  ... 
)

Add a attribute to new DN.

Parameters
laddReference to new DN structure.
attrAttribute to add.
...NULL terminated list of values.
Returns
0 on success.

Definition at line 1500 of file openldap.c.

References objunref().

1500  {
1501  va_list a_list;
1502  char *val;
1503  struct ldap_modreq *modr;
1504 
1505  if (!(modr = getaddreq(ladd, attr))) {
1506  return 1;
1507  }
1508 
1509  va_start(a_list, attr);
1510  while((val = va_arg(a_list, void *))) {
1511  if (add_modifyval(modr, val)) {
1512  objunref(modr);
1513  return(1);
1514  }
1515  }
1516 
1517  objunref(modr);
1518  va_end(a_list);
1519  return 0;
1520 }
const char * attr
Attribute modified.
Definition: openldap.c:94
LDAP mod request.
Definition: openldap.c:92
int objunref(void *data)
Drop reference held.
Definition: refobj.c:184
struct ldap_add* ldap_addinit ( const char *  dn)

Create a reference to add a new DN.

Parameters
dnDN to be created.
Returns
Reference to a structure to configure for adding a new dn.

Definition at line 1462 of file openldap.c.

References ALLOC_CONST, ldap_add::bl, create_bucketlist(), ldap_add::dn, objalloc(), and objunref().

1462  {
1463  struct ldap_add *mod;
1464 
1465  if (!(mod = objalloc(sizeof(*mod), free_add))) {
1466  return NULL;
1467  }
1468 
1469  ALLOC_CONST(mod->dn, dn);
1470  if (!mod->dn) {
1471  objunref(mod);
1472  return NULL;
1473  }
1474 
1475  if (!(mod->bl = create_bucketlist(4, modify_hash))) {
1476  objunref(mod);
1477  return NULL;
1478  }
1479 
1480  return mod;
1481 }
void * create_bucketlist(int bitmask, blisthash hash_function)
Definition: refobj.c:356
struct bucket_list * bl
bucket containing item to add
Definition: openldap.c:80
void * objalloc(int size, objdestroy)
Allocate a referenced lockable object.
Definition: refobj.c:129
const char * dn
Distingushed name.
Definition: openldap.c:78
LDAP Add structure.
Definition: openldap.c:76
#define ALLOC_CONST(const_var, val)
Macro to assign values to char const.
Definition: dtsapp.h:959
int objunref(void *data)
Drop reference held.
Definition: refobj.c:184
struct ldap_conn* ldap_connect ( const char *  uri,
enum ldap_starttls  starttls,
int  timelimit,
int  limit,
int  debug,
int *  err 
)

Connect to a LDAP server.

Parameters
uriServer to connect too.
starttlsStarttls flags to disallow,allow or enforce SSL.
timelimitQuery timelimit.
limitResults limit.
debugSet LDAP_OPT_DEBUG_LEVEL and LBER_OPT_DEBUG_LEVEL to this level.
errPointer to a int that will contain the ldap error on failure.
Returns
Reference to LDAP connection if its NULL the error is returned in err.

Definition at line 335 of file openldap.c.

References ldap_conn::ldap, LDAP_STARTTLS_ENFORCE, LDAP_STARTTLS_NONE, ldap_conn::limit, objalloc(), objunref(), ldap_conn::sasl, ldap_conn::sctrlsp, ldap_conn::timelim, and ldap_conn::uri.

335  {
336  struct ldap_conn *ld;
337  int version = 3;
338  int res, sslres;
339  struct timeval timeout;
340 
341  if (!(ld = objalloc(sizeof(*ld), free_ldapconn))) {
342  return NULL;
343  }
344 
345  ld->uri = strdup(uri);
346  ld->sctrlsp = NULL;
347  ld->timelim = timelimit;
348  ld->limit = limit;
349  ld->sasl = NULL;
350 
351  if ((res = ldap_initialize(&ld->ldap, ld->uri) != LDAP_SUCCESS)) {
352  objunref(ld);
353  ld = NULL;
354  } else {
355  if (debug) {
356  ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &debug);
357  ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &debug);
358  }
359  if (timelimit) {
360  timeout.tv_sec = timelimit;
361  timeout.tv_usec = 0;
362  ldap_set_option(ld->ldap, LDAP_OPT_NETWORK_TIMEOUT, (void *)&timeout);
363  }
364  ldap_set_option(ld->ldap, LDAP_OPT_PROTOCOL_VERSION, &version);
365  ldap_set_option(ld->ldap, LDAP_OPT_REFERRALS, (void *)LDAP_OPT_ON);
366  ldap_set_rebind_proc(ld->ldap, ldap_rebind_proc, ld);
367 
368  if ((starttls != LDAP_STARTTLS_NONE) & !ldap_tls_inplace(ld->ldap) && (sslres = ldap_start_tls_s(ld->ldap, ld->sctrlsp, NULL))) {
369  if (starttls == LDAP_STARTTLS_ENFORCE) {
370  objunref(ld);
371  ld = NULL;
372  res = sslres;
373  }
374  }
375  }
376  *err = res;
377  return ld;
378 }
SSL not attempted at all.
Definition: dtsapp.h:699
LDAPControl ** sctrlsp
LDAP control.
Definition: openldap.c:60
void * objalloc(int size, objdestroy)
Allocate a referenced lockable object.
Definition: refobj.c:129
int limit
Results limit.
Definition: openldap.c:58
struct sasl_defaults * sasl
SASL auth information.
Definition: openldap.c:62
SSL is required.
Definition: dtsapp.h:703
int timelim
Time limit.
Definition: openldap.c:56
char * uri
Address.
Definition: openldap.c:54
LDAP * ldap
LDAP pointer.
Definition: openldap.c:52
LDAP connection.
Definition: openldap.c:50
int objunref(void *data)
Drop reference held.
Definition: refobj.c:184
int ldap_doadd ( struct ldap_conn ld,
struct ldap_add ladd 
)

Write new DN to server.

Parameters
ldReference to connection to the LDAP server.
laddReference to new DN to commit to server.
Returns
non zero LDAP error on failure.

Definition at line 1526 of file openldap.c.

References ldap_add::bl, bucket_list_cnt(), ldap_add::dn, init_bucket_loop(), ldap_conn::ldap, next_bucket_loop(), objlock(), objunlock(), objunref(), and ldap_conn::sctrlsp.

1526  {
1527  struct bucket_loop *bloop;
1528  struct ldap_modreq *modr;
1529  LDAPMod **modarr, **tmp, *item;
1530  int tot=0, res;
1531 
1532  tot = bucket_list_cnt(ladd->bl);
1533  tmp = modarr = calloc(sizeof(void *), (tot+1));
1534 
1535  bloop = init_bucket_loop(ladd->bl);
1536  while(bloop && ((modr = next_bucket_loop(bloop)))) {
1537  if (!(item = ldap_reqtoarr(modr, -1))) {
1538  ldap_mods_free(modarr, 1);
1539  return LDAP_NO_MEMORY;
1540  }
1541  *tmp = item;
1542  tmp++;
1543  objunref(modr);
1544  }
1545  objunref(bloop);
1546  *tmp = NULL;
1547 
1548  objlock(ld);
1549  res = ldap_modify_ext_s(ld->ldap, ladd->dn, modarr, ld->sctrlsp, NULL);
1550  objunlock(ld);
1551  ldap_mods_free(modarr, 1);
1552 
1553  return res;
1554 }
Bucket iterator.
Definition: refobj.c:97
LDAPControl ** sctrlsp
LDAP control.
Definition: openldap.c:60
int objlock(void *data)
Lock the reference.
Definition: refobj.c:269
struct bucket_list * bl
bucket containing item to add
Definition: openldap.c:80
const char * dn
Distingushed name.
Definition: openldap.c:78
int bucket_list_cnt(struct bucket_list *blist)
Return number of items in the list.
Definition: refobj.c:552
void * next_bucket_loop(struct bucket_loop *bloop)
Return a reference to the next item in the list this could be the first item.
Definition: refobj.c:662
int objunlock(void *data)
Unlock a reference.
Definition: refobj.c:301
LDAP mod request.
Definition: openldap.c:92
LDAP * ldap
LDAP pointer.
Definition: openldap.c:52
int objunref(void *data)
Drop reference held.
Definition: refobj.c:184
struct bucket_loop * init_bucket_loop(struct bucket_list *blist)
Create a bucket list iterator to safely iterate the list.
Definition: refobj.c:640
int ldap_domodify ( struct ldap_conn ld,
struct ldap_modify lmod 
)

Apply the modification to the server.

Parameters
ldReference to LDAP connection.
lmodReference to modification structure.
Returns
Non zero ldap error on error.

Definition at line 1339 of file openldap.c.

References ldap_modify::bl, bucket_list_cnt(), ldap_modreq::cnt, ldap_modify::dn, init_bucket_loop(), ldap_conn::ldap, next_bucket_loop(), objlock(), objref(), objunlock(), objunref(), and ldap_conn::sctrlsp.

Referenced by ldap_mod_addattr(), ldap_mod_delattr(), and ldap_mod_repattr().

1339  {
1340  struct bucket_loop *bloop;
1341  struct ldap_modreq *modr;
1342  LDAPMod **modarr, **tmp, *item;
1343  int cnt, tot=0, res;
1344 
1345  if (!objref(ld)) {
1346  return LDAP_UNAVAILABLE;
1347  }
1348 
1349  for(cnt = 0; cnt < 3; cnt++) {
1350  tot += bucket_list_cnt(lmod->bl[cnt]);
1351  }
1352  tmp = modarr = calloc(sizeof(void *), (tot+1));
1353 
1354  for(cnt = 0; cnt < 3; cnt++) {
1355  bloop = init_bucket_loop(lmod->bl[cnt]);
1356  while(bloop && ((modr = next_bucket_loop(bloop)))) {
1357  if (!(item = ldap_reqtoarr(modr, cnt))) {
1358  ldap_mods_free(modarr, 1);
1359  objunref(ld);
1360  return LDAP_NO_MEMORY;
1361  }
1362  *tmp = item;
1363  tmp++;
1364  objunref(modr);
1365  }
1366  objunref(bloop);
1367  }
1368  *tmp = NULL;
1369 
1370  objlock(ld);
1371  res = ldap_modify_ext_s(ld->ldap, lmod->dn, modarr, ld->sctrlsp, NULL);
1372  objunlock(ld);
1373  ldap_mods_free(modarr, 1);
1374  objunref(ld);
1375  return res;
1376 }
Bucket iterator.
Definition: refobj.c:97
LDAPControl ** sctrlsp
LDAP control.
Definition: openldap.c:60
int objref(void *data)
Reference a object.
Definition: refobj.c:153
int objlock(void *data)
Lock the reference.
Definition: refobj.c:269
int bucket_list_cnt(struct bucket_list *blist)
Return number of items in the list.
Definition: refobj.c:552
struct bucket_list * bl[3]
Bucket list containg modify / modify_add / delete requests.
Definition: openldap.c:72
void * next_bucket_loop(struct bucket_loop *bloop)
Return a reference to the next item in the list this could be the first item.
Definition: refobj.c:662
int cnt
Count.
Definition: openldap.c:96
int objunlock(void *data)
Unlock a reference.
Definition: refobj.c:301
LDAP mod request.
Definition: openldap.c:92
const char * dn
Distingushed name.
Definition: openldap.c:70
LDAP * ldap
LDAP pointer.
Definition: openldap.c:52
int objunref(void *data)
Drop reference held.
Definition: refobj.c:184
struct bucket_loop * init_bucket_loop(struct bucket_list *blist)
Create a bucket list iterator to safely iterate the list.
Definition: refobj.c:640
const char* ldap_errmsg ( int  res)

Return LDAP error for a ldap error.

Parameters
resLDAP error id.
Returns
Error string.

Definition at line 576 of file openldap.c.

576  {
577  return ldap_err2string(res);
578 }
struct ldap_attr* ldap_getattr ( struct ldap_entry entry,
const char *  attr 
)

Find and return attribute in a entry.

Parameters
entryEntry to return attribute from.
attrAtttribute to return.
Returns
Attribute reference matching attr.

Definition at line 1108 of file openldap.c.

References ldap_entry::attrs, and bucket_list_find_key().

1108  {
1109  if (!entry || !entry->attrs) {
1110  return NULL;
1111  }
1112  return (struct ldap_attr *)bucket_list_find_key(entry->attrs, attr);
1113 }
const char * attr
Attribute modified.
Definition: openldap.c:94
struct bucket_list * attrs
Bucket list of attributes.
Definition: dtsapp.h:765
void * bucket_list_find_key(struct bucket_list *list, const void *key)
Find and return a reference to a item matching supplied key.
Definition: refobj.c:572
LDAP attirbute.
Definition: dtsapp.h:739
struct ldap_entry* ldap_getentry ( struct ldap_results results,
const char *  dn 
)

Find and return the entry from the results for a specific dn.

Parameters
resultsResults to search in.
dnDN search for.
Returns
Entry for a DN in the results or NULL.

Definition at line 1096 of file openldap.c.

References bucket_list_find_key(), and ldap_results::entries.

1096  {
1097  if (!results || !dn) {
1098  return NULL;
1099  }
1100  return (struct ldap_entry *)bucket_list_find_key(results->entries, dn);
1101 }
LDAP entry.
Definition: dtsapp.h:753
struct bucket_list * entries
Bucket list of entries.
Definition: dtsapp.h:781
void * bucket_list_find_key(struct bucket_list *list, const void *key)
Find and return a reference to a item matching supplied key.
Definition: refobj.c:572
int ldap_mod_add ( struct ldap_modify lmod,
const char *  attr,
  ... 
)

Add values to a attribute.

Parameters
lmodLDAP modification referenece.
attrAttribute to modify.
...Values to add.
Returns
Zero on success.

Definition at line 1238 of file openldap.c.

References objunref().

Referenced by ldap_mod_addattr().

1238  {
1239  va_list a_list;
1240  char *val;
1241  struct ldap_modreq *modr;
1242 
1243  if (!(modr = getmodreq(lmod, attr, LDAP_MOD_ADD))) {
1244  return 1;
1245  }
1246 
1247  va_start(a_list, attr);
1248  while((val = va_arg(a_list, void *))) {
1249  if (add_modifyval(modr, val)) {
1250  objunref(modr);
1251  return(1);
1252  }
1253  }
1254 
1255  objunref(modr);
1256  va_end(a_list);
1257  return 0;
1258 }
const char * attr
Attribute modified.
Definition: openldap.c:94
LDAP mod request.
Definition: openldap.c:92
int objunref(void *data)
Drop reference held.
Definition: refobj.c:184
int ldap_mod_addattr ( struct ldap_conn ldap,
const char *  dn,
const char *  attr,
const char *  value 
)

Add a value for a attribute in a DN.

Parameters
ldapReference to the connection.
dnDN to remove values from.
attrAttribute to add value to.
valueValue to remove from attribute.
Returns
Non zero ldap error on failure

Definition at line 1416 of file openldap.c.

References ldap_domodify(), ldap_mod_add(), ldap_modifyinit(), and objunref().

1416  {
1417  int res = 0;
1418  struct ldap_modify *lmod;
1419 
1420  if (!(lmod = ldap_modifyinit(dn))) {
1421  return LDAP_NO_MEMORY;
1422  }
1423 
1424  if (ldap_mod_add(lmod, attr, value, NULL)) {
1425  objunref(lmod);
1426  return LDAP_NO_MEMORY;
1427  }
1428 
1429  res = ldap_domodify(ldap, lmod);
1430  objunref(lmod);
1431  return res;
1432 }
int ldap_domodify(struct ldap_conn *ld, struct ldap_modify *lmod)
Apply the modification to the server.
Definition: openldap.c:1339
struct ldap_modify * ldap_modifyinit(const char *dn)
Create a modification reference for a DN.
Definition: openldap.c:1118
int ldap_mod_add(struct ldap_modify *lmod, const char *attr,...)
Add values to a attribute.
Definition: openldap.c:1238
const char * dn
Distingushed name.
Definition: openldap.c:70
int objunref(void *data)
Drop reference held.
Definition: refobj.c:184
LDAP Modify structure.
Definition: openldap.c:68
int ldap_mod_del ( struct ldap_modify lmod,
const char *  attr,
  ... 
)

Delete values from a attribute.

Parameters
lmodLDAP modification referenece.
attrAttribute to modify.
...Values to remove.
Returns
Zero on success.

Definition at line 1211 of file openldap.c.

References objunref().

Referenced by ldap_mod_delattr().

1211  {
1212  va_list a_list;
1213  char *val;
1214  struct ldap_modreq *modr;
1215 
1216  if (!(modr = getmodreq(lmod, attr, LDAP_MOD_DELETE))) {
1217  return 1;
1218  }
1219 
1220  va_start(a_list, attr);
1221  while((val = va_arg(a_list, void *))) {
1222  if (add_modifyval(modr, val)) {
1223  objunref(modr);
1224  return(1);
1225  }
1226  }
1227 
1228  objunref(modr);
1229  va_end(a_list);
1230  return 0;
1231 }
const char * attr
Attribute modified.
Definition: openldap.c:94
LDAP mod request.
Definition: openldap.c:92
int objunref(void *data)
Drop reference held.
Definition: refobj.c:184
int ldap_mod_delattr ( struct ldap_conn ldap,
const char *  dn,
const char *  attr,
const char *  value 
)

Delete a value from a attribute in a DN.

Parameters
ldapReference to the connection.
dnDN to remove values from.
attrAttribute to remove values from.
valueValue to remove from attribute.
Returns
Non zero ldap error on failure

Definition at line 1384 of file openldap.c.

References ldap_domodify(), ldap_mod_del(), ldap_modifyinit(), and objunref().

Referenced by ldap_mod_remattr().

1384  {
1385  struct ldap_modify *lmod;
1386  int res;
1387 
1388  if (!(lmod = ldap_modifyinit(dn))) {
1389  return LDAP_NO_MEMORY;
1390  }
1391  if (ldap_mod_del(lmod, attr, value, NULL)) {
1392  objunref(lmod);
1393  return LDAP_NO_MEMORY;
1394  }
1395 
1396  res = ldap_domodify(ldap, lmod);
1397  objunref(lmod);
1398  return res;
1399 }
int ldap_domodify(struct ldap_conn *ld, struct ldap_modify *lmod)
Apply the modification to the server.
Definition: openldap.c:1339
struct ldap_modify * ldap_modifyinit(const char *dn)
Create a modification reference for a DN.
Definition: openldap.c:1118
int ldap_mod_del(struct ldap_modify *lmod, const char *attr,...)
Delete values from a attribute.
Definition: openldap.c:1211
const char * dn
Distingushed name.
Definition: openldap.c:70
int objunref(void *data)
Drop reference held.
Definition: refobj.c:184
LDAP Modify structure.
Definition: openldap.c:68
int ldap_mod_remattr ( struct ldap_conn ldap,
const char *  dn,
const char *  attr 
)

Delete a attribute from a DN.

Parameters
ldapReference to the connection.
dnDN to remove attribute from.
attrAttribute to remove.
Returns
Non zero ldap error on failure

Definition at line 1406 of file openldap.c.

References ldap_mod_delattr().

1406  {
1407  return ldap_mod_delattr(ldap, dn, attr, NULL);
1408 }
int ldap_mod_delattr(struct ldap_conn *ldap, const char *dn, const char *attr, const char *value)
Delete a value from a attribute in a DN.
Definition: openldap.c:1384
const char * dn
Distingushed name.
Definition: openldap.c:70
int ldap_mod_rep ( struct ldap_modify lmod,
const char *  attr,
  ... 
)

Replace a attribute.

Parameters
lmodLDAP modification referenece.
attrAttribute to modify.
...Values to replace.
Returns
Zero on success.

Definition at line 1265 of file openldap.c.

References objunref().

Referenced by ldap_mod_repattr().

1265  {
1266  va_list a_list;
1267  char *val;
1268  struct ldap_modreq *modr;
1269 
1270  if (!(modr = getmodreq(lmod, attr, LDAP_MOD_REPLACE))) {
1271  return 1;
1272  }
1273 
1274  va_start(a_list, attr);
1275  while((val = va_arg(a_list, void *))) {
1276  if (add_modifyval(modr, val)) {
1277  objunref(modr);
1278  return(1);
1279  }
1280  }
1281 
1282  objunref(modr);
1283  va_end(a_list);
1284  return 0;
1285 }
const char * attr
Attribute modified.
Definition: openldap.c:94
LDAP mod request.
Definition: openldap.c:92
int objunref(void *data)
Drop reference held.
Definition: refobj.c:184
int ldap_mod_repattr ( struct ldap_conn ldap,
const char *  dn,
const char *  attr,
const char *  value 
)

Replace the value of a attribute in a DN.

Parameters
ldapReference to the connection.
dnDN to replace attribute in.
attrAttribute to replace.
valueValue to replace attr with.
Returns
Non zero ldap error on failure

Definition at line 1441 of file openldap.c.

References ldap_domodify(), ldap_mod_rep(), ldap_modifyinit(), and objunref().

1441  {
1442  struct ldap_modify *lmod;
1443  int res;
1444 
1445  if (!(lmod = ldap_modifyinit(dn))) {
1446  return LDAP_NO_MEMORY;
1447  }
1448 
1449  if (ldap_mod_rep(lmod, attr, value, NULL)) {
1450  objunref(lmod);
1451  return LDAP_NO_MEMORY;
1452  }
1453 
1454  res = ldap_domodify(ldap, lmod);
1455  objunref(lmod);
1456  return res;
1457 }
int ldap_domodify(struct ldap_conn *ld, struct ldap_modify *lmod)
Apply the modification to the server.
Definition: openldap.c:1339
struct ldap_modify * ldap_modifyinit(const char *dn)
Create a modification reference for a DN.
Definition: openldap.c:1118
int ldap_mod_rep(struct ldap_modify *lmod, const char *attr,...)
Replace a attribute.
Definition: openldap.c:1265
const char * dn
Distingushed name.
Definition: openldap.c:70
int objunref(void *data)
Drop reference held.
Definition: refobj.c:184
LDAP Modify structure.
Definition: openldap.c:68
struct ldap_modify* ldap_modifyinit ( const char *  dn)

Create a modification reference for a DN.

Parameters
dnDN to modify.
Returns
Reference to a modification structure used to modify a DN.

Definition at line 1118 of file openldap.c.

References ALLOC_CONST, ldap_modify::bl, create_bucketlist(), ldap_modify::dn, objalloc(), and objunref().

Referenced by ldap_mod_addattr(), ldap_mod_delattr(), and ldap_mod_repattr().

1118  {
1119  struct ldap_modify *mod;
1120  int cnt;
1121 
1122  if (!(mod = objalloc(sizeof(*mod), free_modify))) {
1123  return NULL;
1124  }
1125 
1126  ALLOC_CONST(mod->dn, dn);
1127  if (!mod->dn) {
1128  objunref(mod);
1129  return NULL;
1130  }
1131 
1132  for(cnt=0; cnt < 3; cnt++) {
1133  if (!(mod->bl[cnt] = create_bucketlist(4, modify_hash))) {
1134  objunref(mod);
1135  return NULL;
1136  }
1137  }
1138 
1139  return mod;
1140 }
void * create_bucketlist(int bitmask, blisthash hash_function)
Definition: refobj.c:356
void * objalloc(int size, objdestroy)
Allocate a referenced lockable object.
Definition: refobj.c:129
struct bucket_list * bl[3]
Bucket list containg modify / modify_add / delete requests.
Definition: openldap.c:72
const char * dn
Distingushed name.
Definition: openldap.c:70
#define ALLOC_CONST(const_var, val)
Macro to assign values to char const.
Definition: dtsapp.h:959
int objunref(void *data)
Drop reference held.
Definition: refobj.c:184
LDAP Modify structure.
Definition: openldap.c:68
int ldap_saslbind ( struct ldap_conn ld,
const char *  mech,
const char *  realm,
const char *  authcid,
const char *  passwd,
const char *  authzid 
)

Bind to the server with SASL.

Parameters
ldReference to LDAP connection.
mechSASL mechanisim.
realmSASL realm.
authcidSASL auth id.
passwdPassword for authid.
authzidProxy authid.
Returns
-1 on error.

Definition at line 524 of file openldap.c.

References ALLOC_CONST, sasl_defaults::authcid, sasl_defaults::authzid, ldap_conn::ldap, sasl_defaults::mech, objalloc(), objlock(), objref(), objunlock(), objunref(), sasl_defaults::passwd, sasl_defaults::realm, ldap_conn::sasl, and ldap_conn::sctrlsp.

524  {
525  struct sasl_defaults *sasl;
526  int res, sasl_flags = LDAP_SASL_AUTOMATIC | LDAP_SASL_QUIET;
527 
528  if (!objref(ld)) {
529  return LDAP_UNAVAILABLE;
530  }
531 
532  if (!(sasl = objalloc(sizeof(*sasl), free_sasl))) {
533  return LDAP_NO_MEMORY;
534  }
535 
536  ALLOC_CONST(sasl->passwd, passwd);
537 
538  if (mech) {
539  ALLOC_CONST(sasl->mech, mech);
540  } else {
541  ldap_get_option(ld->ldap, LDAP_OPT_X_SASL_MECH, &sasl->mech);
542  }
543 
544  if (realm) {
545  ALLOC_CONST(sasl->realm, realm);
546  } else {
547  ldap_get_option(ld->ldap, LDAP_OPT_X_SASL_REALM, &sasl->realm );
548  }
549 
550  if (authcid) {
551  ALLOC_CONST(sasl->authcid, authcid);
552  } else {
553  ldap_get_option(ld->ldap, LDAP_OPT_X_SASL_AUTHCID, &sasl->authcid );
554  }
555 
556  if (authzid) {
557  ALLOC_CONST(sasl->authzid, authzid);
558  } else {
559  ldap_get_option(ld->ldap, LDAP_OPT_X_SASL_AUTHZID, &sasl->authzid );
560  }
561 
562  objlock(ld);
563  if (ld->sasl) {
564  objunref(ld->sasl);
565  }
566  ld->sasl = sasl;
567  res = ldap_sasl_interactive_bind_s(ld->ldap, NULL, sasl->mech, ld->sctrlsp , NULL, sasl_flags, dts_sasl_interact, sasl);
568  objunlock(ld);
569  objunref(ld);
570  return res;
571 }
LDAPControl ** sctrlsp
LDAP control.
Definition: openldap.c:60
const char * authcid
Auth ID.
Definition: openldap.c:34
int objref(void *data)
Reference a object.
Definition: refobj.c:153
const char * mech
SASL Mechanisim.
Definition: openldap.c:30
int objlock(void *data)
Lock the reference.
Definition: refobj.c:269
void * objalloc(int size, objdestroy)
Allocate a referenced lockable object.
Definition: refobj.c:129
const char * realm
SASL Realm.
Definition: openldap.c:32
const char * authzid
Proxy auth ID.
Definition: openldap.c:38
struct sasl_defaults * sasl
SASL auth information.
Definition: openldap.c:62
const char * passwd
Password.
Definition: openldap.c:36
SASL Paramaters used in authentification.
Definition: openldap.c:28
int objunlock(void *data)
Unlock a reference.
Definition: refobj.c:301
LDAP * ldap
LDAP pointer.
Definition: openldap.c:52
#define ALLOC_CONST(const_var, val)
Macro to assign values to char const.
Definition: dtsapp.h:959
int objunref(void *data)
Drop reference held.
Definition: refobj.c:184
struct ldap_results* ldap_search_base ( struct ldap_conn ld,
const char *  base,
const char *  filter,
int  b64enc,
int *  res,
  ... 
)

Search LDAP connection base.

Parameters
ldReference to LDAP connection.
baseSearch base dn.
filterSearch filter.
b64encBase 64 encode attributes.
resPointer containing LDAP error.
...NULL termincated list of attributes to include.
Returns
Search results structure.

Definition at line 669 of file openldap.c.

669  {
670  va_list a_list;
671  char *attr, **tmp, **attrs = NULL;
672  int cnt = 1;
673 
674  va_start(a_list, res);
675  while (( attr=va_arg(a_list, void *))) {
676  cnt++;
677  }
678  va_end(a_list);
679 
680  if (cnt > 1) {
681  tmp = attrs = malloc(sizeof(void *)*cnt);
682 
683  va_start(a_list, res);
684  while (( attr=va_arg(a_list, char *))) {
685  *tmp = attr;
686  tmp++;
687  }
688  va_end(a_list);
689  *tmp=NULL;
690  }
691 
692  return _dtsldapsearch(ld, base, LDAP_SCOPE_BASE, filter, attrs, b64enc, res);
693 }
char * b64enc(const char *message, int nonl)
Base 64 encode a string.
Definition: util.c:539
struct ldap_results* ldap_search_one ( struct ldap_conn ld,
const char *  base,
const char *  filter,
int  b64enc,
int *  res,
  ... 
)

Search LDAP connection one level.

Parameters
ldReference to LDAP connection.
baseSearch base dn.
filterSearch filter.
b64encBase 64 encode attributes.
resPointer containing LDAP error.
...NULL termincated list of attributes to include.
Returns
Search results structure.

Definition at line 635 of file openldap.c.

635  {
636  va_list a_list;
637  char *attr, **tmp, **attrs = NULL;
638  int cnt = 1;
639 
640  va_start(a_list, res);
641  while (( attr=va_arg(a_list, void *))) {
642  cnt++;
643  }
644  va_end(a_list);
645 
646  if (cnt > 1) {
647  tmp = attrs = malloc(sizeof(void *)*cnt);
648 
649  va_start(a_list, res);
650  while (( attr=va_arg(a_list, char *))) {
651  *tmp = attr;
652  tmp++;
653  }
654  va_end(a_list);
655  *tmp=NULL;
656  }
657 
658  return _dtsldapsearch(ld, base, LDAP_SCOPE_ONELEVEL, filter, attrs, b64enc, res);
659 }
char * b64enc(const char *message, int nonl)
Base 64 encode a string.
Definition: util.c:539
struct ldap_results* ldap_search_sub ( struct ldap_conn ld,
const char *  base,
const char *  filter,
int  b64enc,
int *  res,
  ... 
)

Search LDAP connection subtree.

Parameters
ldReference to LDAP connection.
baseSearch base dn.
filterSearch filter.
b64encBase 64 encode attributes.
resPointer containing LDAP error.
...NULL termincated list of attributes to include.
Returns
Search results structure.

Definition at line 601 of file openldap.c.

Referenced by ldap_simplerebind().

601  {
602  va_list a_list;
603  char *attr, **tmp, **attrs = NULL;
604  int cnt = 1;
605 
606  va_start(a_list, res);
607  while (( attr=va_arg(a_list, void *))) {
608  cnt++;
609  }
610  va_end(a_list);
611 
612  if (cnt > 1) {
613  tmp = attrs = malloc(sizeof(void *)*cnt);
614 
615  va_start(a_list, res);
616  while (( attr=va_arg(a_list, char *))) {
617  *tmp = attr;
618  tmp++;
619  }
620  va_end(a_list);
621  *tmp=NULL;
622  }
623 
624  return _dtsldapsearch(ld, base, LDAP_SCOPE_SUBTREE, filter, attrs, b64enc, res);
625 }
char * b64enc(const char *message, int nonl)
Base 64 encode a string.
Definition: util.c:539
int ldap_simplebind ( struct ldap_conn ld,
const char *  dn,
const char *  passwd 
)

Bind to the connection with simple bind requireing a distingushed name and password.

Parameters
ldLDAP connection to bind to.
dnDistinguished name to bind with.
passwdPassword for dn.
Returns
-1 on error.

Definition at line 434 of file openldap.c.

References ldap_simple::cred, ldap_simple::dn, ldap_conn::ldap, objalloc(), objlock(), objref(), objunlock(), objunref(), ldap_conn::sctrlsp, and ldap_conn::simple.

Referenced by ldap_simplerebind().

434  {
435  struct ldap_simple *simple;
436  struct berval *cred;
437  int res, len = 0;
438 
439  if (!objref(ld)) {
440  return LDAP_UNAVAILABLE;
441  }
442 
443  if (passwd) {
444  len = strlen(passwd);
445  }
446  simple = objalloc(sizeof(*simple), free_simple);
447  cred = calloc(sizeof(*cred), 1);
448  cred->bv_val = malloc(len);
449  memcpy(cred->bv_val, passwd, len);
450  cred->bv_len=len;
451  simple->cred = cred;
452  simple->dn = strdup(dn);
453 
454  objlock(ld);
455  if (ld->simple) {
456  objunref(ld->simple);
457  }
458  ld->simple = simple;
459  res = ldap_sasl_bind_s(ld->ldap, simple->dn, LDAP_SASL_SIMPLE, simple->cred, ld->sctrlsp, NULL, NULL);
460  objunlock(ld);
461  objunref(ld);
462  return res;
463 }
struct ldap_simple * simple
LDAP Simple bind information.
Definition: openldap.c:64
const char * dn
Distingushed Name.
Definition: openldap.c:44
LDAPControl ** sctrlsp
LDAP control.
Definition: openldap.c:60
LDAP Simple bind.
Definition: openldap.c:42
int objref(void *data)
Reference a object.
Definition: refobj.c:153
int objlock(void *data)
Lock the reference.
Definition: refobj.c:269
void * objalloc(int size, objdestroy)
Allocate a referenced lockable object.
Definition: refobj.c:129
struct berval * cred
Credentials (password).
Definition: openldap.c:46
int objunlock(void *data)
Unlock a reference.
Definition: refobj.c:301
LDAP * ldap
LDAP pointer.
Definition: openldap.c:52
int objunref(void *data)
Drop reference held.
Definition: refobj.c:184
int ldap_simplerebind ( struct ldap_conn ldap,
const char *  initialdn,
const char *  initialpw,
const char *  base,
const char *  filter,
const char *  uidrdn,
const char *  uid,
const char *  passwd 
)

Bind to LDAP connection using rebind.

Bind to a connection with a lower privlidge distingushed name and password search for a user dn, bind to the connection with the retrieved dn and user password.

Parameters
ldapLDAP connection to bind too.
initialdnInitial dn to bind with.
initialpwPassword for the initial dn.
baseSearch base to find user.
filterLDAP filter to apply to find user.
uidrdnAttribute containing user id.
uidTo search and bind as.
passwdPassword for the user id.
Returns
-1 on error.

Definition at line 478 of file openldap.c.

References ldap_results::count, ldap_entry::dn, ldap_results::first_entry, ldap_search_sub(), ldap_simplebind(), objref(), and objunref().

479  {
480  int res, flen;
481  struct ldap_results *results;
482  const char *sfilt;
483 
484  if (!objref(ldap)) {
485  return LDAP_UNAVAILABLE;
486  }
487 
488  if ((res = ldap_simplebind(ldap, initialdn, initialpw))) {
489  objunref(ldap);
490  return res;
491  }
492 
493  flen=strlen(uidrdn) + strlen(filter) + strlen(uid) + 7;
494  sfilt = malloc(flen);
495  snprintf((char *)sfilt, flen, "(&(%s=%s)%s)", uidrdn, uid, filter);
496 
497  if (!(results = ldap_search_sub(ldap, base, sfilt, 0, &res, uidrdn, NULL))) {
498  free((void *)sfilt);
499  objunref(ldap);
500  return res;
501  }
502  free((void *)sfilt);
503 
504  if (results->count != 1) {
505  objunref(results);
506  objunref(ldap);
507  return LDAP_INAPPROPRIATE_AUTH;
508  }
509 
510  res = ldap_simplebind(ldap, results->first_entry->dn, passwd);
511  objunref(ldap);
512  objunref(results);
513  return res;
514 }
const char * dn
LDAP distiguished name.
Definition: dtsapp.h:755
int ldap_simplebind(struct ldap_conn *ld, const char *dn, const char *passwd)
Bind to the connection with simple bind requireing a distingushed name and password.
Definition: openldap.c:434
int objref(void *data)
Reference a object.
Definition: refobj.c:153
LDAP results.
Definition: dtsapp.h:775
int count
Number of entries.
Definition: dtsapp.h:777
struct ldap_entry * first_entry
Linked list of entries.
Definition: dtsapp.h:779
struct ldap_results * ldap_search_sub(struct ldap_conn *ld, const char *base, const char *filter, int b64enc, int *res,...)
Search LDAP connection subtree.
Definition: openldap.c:601
int objunref(void *data)
Drop reference held.
Definition: refobj.c:184
void ldap_unref_attr ( struct ldap_entry entry,
struct ldap_attr attr 
)

Remove a attribute from a entry.

Parameters
entryThe entry to remove attr from.
attrAttribute to remove.

Definition at line 1059 of file openldap.c.

References ldap_entry::attrs, ldap_entry::first_attr, ldap_attr::next, objcnt(), objunref(), and remove_bucket_item().

1059  {
1060  if (!entry || !attr) {
1061  return;
1062  }
1063 
1064  if (objcnt(attr) > 1) {
1065  objunref(attr);
1066  } else {
1067  if (attr == entry->first_attr) {
1068  entry->first_attr = attr->next;
1069  }
1070  remove_bucket_item(entry->attrs, attr);
1071  }
1072 }
struct ldap_attr * first_attr
First attr (head of list).
Definition: dtsapp.h:767
int objcnt(void *data)
Return current reference count.
Definition: refobj.c:222
struct bucket_list * attrs
Bucket list of attributes.
Definition: dtsapp.h:765
struct ldap_attr * next
Next attribute.
Definition: dtsapp.h:747
void remove_bucket_item(struct bucket_list *blist, void *data)
Remove and unreference a item from the list.
Definition: refobj.c:517
int objunref(void *data)
Drop reference held.
Definition: refobj.c:184
void ldap_unref_entry ( struct ldap_results results,
struct ldap_entry entry 
)

Remove a entry from a result.

Parameters
resultsThe result to remove entry from.
entryEntry to remove.

Definition at line 1077 of file openldap.c.

References ldap_results::entries, ldap_results::first_entry, ldap_entry::next, objcnt(), objunref(), and remove_bucket_item().

1077  {
1078  if (!results || !entry) {
1079  return;
1080  }
1081 
1082  if (objcnt(entry) > 1) {
1083  objunref(entry);
1084  } else {
1085  if (entry == results->first_entry) {
1086  results->first_entry = entry->next;
1087  }
1088  remove_bucket_item(results->entries, entry);
1089  }
1090 }
int objcnt(void *data)
Return current reference count.
Definition: refobj.c:222
struct ldap_entry * first_entry
Linked list of entries.
Definition: dtsapp.h:779
struct ldap_entry * next
Next entry.
Definition: dtsapp.h:769
struct bucket_list * entries
Bucket list of entries.
Definition: dtsapp.h:781
void remove_bucket_item(struct bucket_list *blist, void *data)
Remove and unreference a item from the list.
Definition: refobj.c:517
int objunref(void *data)
Drop reference held.
Definition: refobj.c:184