📄 scldap.c
字号:
/* * $Id: scldap.c,v 1.18 2002/10/19 14:04:50 aet Exp $ * * Copyright (C) 2001, 2002 * Antti Tapaninen <aet@cc.hut.fi> * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */#ifdef HAVE_CONFIG_H#include <config.h>#endif#include <stdio.h>#include <stdarg.h>#include <stdlib.h>#include <string.h>#ifdef HAVE_STRINGS_H#include <strings.h>#endif#ifdef HAVE_UNISTD_H#include <unistd.h>#endif#include <errno.h>#include <ctype.h>#include <sys/stat.h>#include <sys/types.h>#ifdef HAVE_LBER_H#include <lber.h>#endif#ifdef HAVE_LDAP_H#include <ldap.h>#endif#ifdef HAVE_LDAP_SSL_H#include <ldap_ssl.h>#endif#include "scldap.h"extern char **environ;typedef struct _cb_data { scldap_context *ctx; char *cardprefix;} cb_data;static int attrs_cb(scconf_context * config, const scconf_block * block, scconf_entry * entry, int depth){ scldap_param_entry *lentry = (scldap_param_entry *) entry->arg; const scconf_list *list = scconf_find_list(block, entry->name); for (; list; list = list->next) { if (lentry->numattrs >= SCLDAP_MAX_ATTRIBUTES) { break; } lentry->attributes = (char **) realloc(lentry->attributes, (lentry->numattrs + 2) * sizeof(char *)); if (!lentry->attributes) return 1; lentry->attributes[lentry->numattrs] = strdup(list->data); lentry->numattrs++; lentry->attributes[lentry->numattrs] = NULL; } return 0; /* 0 for ok, 1 for error */}static int ldap_cb(scconf_context * config, const scconf_block * block, scconf_entry * entry, int depth){ cb_data *trans = (cb_data *) entry->arg; scldap_context *ctx = trans->ctx; char *cardprefix = trans->cardprefix; scldap_param_entry *lentry = (scldap_param_entry *) &ctx->entry[ctx->entries]; scconf_entry centry[] = { {"ldaphost", SCCONF_STRING, SCCONF_ALLOC, &lentry->ldaphost, NULL}, {"ldapport", SCCONF_INTEGER, SCCONF_ALLOC, &lentry->ldapport, NULL}, {"scope", SCCONF_INTEGER, SCCONF_ALLOC, &lentry->scope, NULL}, {"binddn", SCCONF_STRING, SCCONF_ALLOC, &lentry->binddn, NULL}, {"passwd", SCCONF_STRING, SCCONF_ALLOC, &lentry->passwd, NULL}, {"base", SCCONF_STRING, SCCONF_ALLOC, &lentry->base, NULL}, {"attributes", SCCONF_CALLBACK, 0, (void *) attrs_cb, lentry}, {"filter", SCCONF_STRING, SCCONF_ALLOC, &lentry->filter, NULL}, {NULL} }; char *ldapsuffix = NULL; size_t len = 0; if (ctx->entries >= SCLDAP_MAX_ENTRIES) return 0; /* Hard limit reached, just return OK */ ldapsuffix = scconf_list_strdup(block->name, " "); if (!ldapsuffix) { return 1; } if (cardprefix) { len = strlen(cardprefix) + 1; } len += strlen(ldapsuffix) + 1; lentry->entry = (char *) malloc(len); if (!lentry->entry) { free(ldapsuffix); return 1; } memset(lentry->entry, 0, len); snprintf(lentry->entry, len, "%s%s%s", cardprefix ? cardprefix : "", cardprefix ? " " : "", ldapsuffix); free(ldapsuffix); if (scconf_parse_entries(config, block, centry) != 0) { return 1; } ctx->entries++; ctx->entry = (scldap_param_entry *) realloc(ctx->entry, (ctx->entries + 2) * sizeof(scldap_param_entry)); if (!ctx->entry) return 1; memset(&ctx->entry[ctx->entries], 0, sizeof(scldap_param_entry)); return 0; /* 0 for ok, 1 for error */}static int card_cb(scconf_context * config, const scconf_block * block, scconf_entry * entry, int depth){ cb_data *trans = (cb_data *) entry->arg; scconf_entry card_entry[] = { {"ldap", SCCONF_CALLBACK, SCCONF_ALL_BLOCKS, (void *) ldap_cb, trans}, {NULL} }; trans->cardprefix = scconf_list_strdup(block->name, " "); if (scconf_parse_entries(config, block, card_entry) != 0) { free(trans->cardprefix); trans->cardprefix = NULL; return 1; } free(trans->cardprefix); trans->cardprefix = NULL; return 0; /* 0 for ok, 1 for error */}scldap_context *scldap_parse_parameters(const char *filename){ scldap_context *ctx = NULL; ctx = (scldap_context *) malloc(sizeof(scldap_context)); if (!ctx) { return NULL; } memset(ctx, 0, sizeof(scldap_context)); ctx->entry = (scldap_param_entry *) realloc(ctx->entry, (ctx->entries + 2) * sizeof(scldap_param_entry)); if (!ctx->entry) { scldap_free_parameters(ctx); return NULL; } memset(&ctx->entry[ctx->entries], 0, sizeof(scldap_param_entry)); if (filename) { cb_data trans = {ctx, NULL}; scconf_entry entry[] = { {"ldap", SCCONF_CALLBACK, SCCONF_ALL_BLOCKS, (void *) ldap_cb, &trans}, {"card", SCCONF_CALLBACK, SCCONF_ALL_BLOCKS, (void *) card_cb, &trans}, {NULL} }; ctx->conf = scconf_new(filename); if (!ctx->conf) { scldap_free_parameters(ctx); return NULL; } if (scconf_parse(ctx->conf) < 1) { scldap_free_parameters(ctx); return NULL; } if (scconf_parse_entries(ctx->conf, NULL, entry) != 0) { scldap_free_parameters(ctx); return NULL; } } ctx->entries++; ctx->active = 0; return ctx;}void scldap_show_parameters(scldap_context * ctx){ unsigned int i, j; if (!ctx) return; for (i = 0; i < ctx->entries; i++) { if (ctx->entry[i].entry) printf("[%i]->entry=%s\n", i, ctx->entry[i].entry); if (ctx->entry[i].ldaphost) printf("[%i]->ldaphost=%s\n", i, ctx->entry[i].ldaphost); printf("[%i]->ldapport=%i\n", i, ctx->entry[i].ldapport); printf("[%i]->scope=%i\n", i, ctx->entry[i].scope); if (ctx->entry[i].binddn) printf("[%i]->binddn=%s\n", i, ctx->entry[i].binddn); if (ctx->entry[i].passwd) printf("[%i]->passwd=%s\n", i, ctx->entry[i].passwd); if (ctx->entry[i].base) printf("[%i]->base=%s\n", i, ctx->entry[i].base); for (j = 0; j < ctx->entry[i].numattrs; j++) { if (ctx->entry[i].attributes[j]) printf("[%i]->attribute[%i]=%s\n", i, j, ctx->entry[i].attributes[j]); } if (ctx->entry[i].filter) printf("[%i]->filter=%s\n\n", i, ctx->entry[i].filter); }}void scldap_free_parameters(scldap_context * ctx){ unsigned int i, j; if (!ctx) return; if (ctx) { for (i = 0; i < ctx->entries; i++) { if (ctx->entry[i].entry) { free(ctx->entry[i].entry); } ctx->entry[i].entry = NULL; if (ctx->entry[i].ldaphost) { free(ctx->entry[i].ldaphost); } ctx->entry[i].ldaphost = NULL; ctx->entry[i].ldapport = 0; ctx->entry[i].scope = 0; if (ctx->entry[i].binddn) { free(ctx->entry[i].binddn); } ctx->entry[i].binddn = NULL; if (ctx->entry[i].passwd) { free(ctx->entry[i].passwd); } ctx->entry[i].passwd = NULL; if (ctx->entry[i].base) { free(ctx->entry[i].base); } ctx->entry[i].base = NULL; for (j = 0; j < ctx->entry[i].numattrs; j++) { free(ctx->entry[i].attributes[j]); ctx->entry[i].attributes[j] = NULL; } if (ctx->entry[i].attributes) { free(ctx->entry[i].attributes); } ctx->entry[i].attributes = NULL; ctx->entry[i].numattrs = 0; if (ctx->entry[i].filter) { free(ctx->entry[i].filter); } ctx->entry[i].filter = NULL; } if (ctx->entry) { free(ctx->entry); } ctx->entry = NULL; ctx->entries = 0; if (ctx->conf) { scconf_free(ctx->conf); } ctx->conf = NULL; free(ctx); ctx = NULL; }}void scldap_parse_arguments(scldap_context ** ctx, int argc, const char **argv){ scldap_context *ptr = *ctx; int i; if (!ptr || !argv || argc < 0) return; for (i = 0; i < argc; i++) { if (argv[i][0] == '-') { char *optarg = (char *) argv[i + 1]; if (!optarg) continue; switch (argv[i][1]) {#define ADD(x) \{ \ if (x) { \ free(x); \ x = NULL; \ } \ x = ((optarg) ? strdup(optarg) : NULL); \} case 'A': scldap_add_entry(ptr, optarg); break; case 'E': scldap_set_entry(ptr, optarg); break; case 'H': ADD(ptr->entry[ptr->active].ldaphost); break; case 'P': ptr->entry[ptr->active].ldapport = atoi(optarg); break; case 'S': ptr->entry[ptr->active].scope = atoi(optarg); break; case 'b': ADD(ptr->entry[ptr->active].binddn); break; case 'p': ADD(ptr->entry[ptr->active].passwd); break; case 'B': ADD(ptr->entry[ptr->active].base); break; case 'a': if (ptr->entry[ptr->active].numattrs >= SCLDAP_MAX_ATTRIBUTES) { break; } ptr->entry[ptr->active].attributes = (char **) realloc(ptr->entry[ptr->active].attributes, (ptr->entry[ptr->active].numattrs + 2) * sizeof(char *)); if (!ptr->entry[ptr->active].attributes) break; memset(&ptr->entry[ptr->active].attributes[ptr->entry[ptr->active].numattrs], 0, sizeof(char *)); ADD(ptr->entry[ptr->active].attributes[ptr->entry[ptr->active].numattrs]); ptr->entry[ptr->active].numattrs++; ptr->entry[ptr->active].attributes[ptr->entry[ptr->active].numattrs] = NULL; break; case 'f': ADD(ptr->entry[ptr->active].filter);#undef ADD break; case 'L': { scldap_context *tmp = scldap_parse_parameters(optarg); if (tmp) { scldap_free_parameters(ptr); ptr = tmp; } } break; } } } *ctx = ptr;}const char *scldap_show_arguments(void){ static char buf[250]; memset(buf, 0, 250); snprintf(buf, 250, " -L ldap.conf Configuration file to load\n" " -A entry Add new entry\n" " -E entry Set current entry\n" " LDAP entry specific options:\n" " -H hostname\n" " -P port\n" " -S scope\n" " -b binddn\n" " -p passwd\n" " -B base\n" " -a attribute(s)\n" " -f filter\n"); return &buf[0];}int scldap_add_entry(scldap_context * ctx, const char *entry){ unsigned int i; if (!ctx) return 0; if (entry) { for (i = 0; i < ctx->entries; i++) { if (!ctx->entry[i].entry) { ctx->entry[i].entry = strdup(entry); ctx->active = i; return i; } } i = ctx->entries; ctx->entry = (scldap_param_entry *) realloc(ctx->entry, (i + 2) * sizeof(scldap_param_entry)); if (!ctx->entry) return 0; memset(&ctx->entry[i], 0, sizeof(scldap_param_entry)); ctx->entry[i].entry = strdup(entry); ctx->active = i; ctx->entries++; return i; } return 0;}int scldap_get_entry(scldap_context * ctx, const char *entry){ unsigned int i; if (!ctx) return 0; if (entry) { for (i = 0; i < ctx->entries; i++) { if (ctx->entry[i].entry) { if (!strcmp(ctx->entry[i].entry, entry)) { return i; } } } } return 0;}void scldap_set_entry(scldap_context * ctx, const char *entry){ unsigned int i; if (!ctx) return; if (entry) { for (i = 0; i < ctx->entries; i++) { if (ctx->entry[i].entry) { if (!strcmp(ctx->entry[i].entry, entry)) { ctx->active = i; break; } } } }}void scldap_remove_entry(scldap_context * ctx, const char *entry){ unsigned int i, j; if (!ctx) return; if (entry) { for (i = 0; i < ctx->entries; i++) { if (ctx->entry[i].entry) { if (!strcmp(ctx->entry[i].entry, entry)) { if (ctx->entry[i].entry) { free(ctx->entry[i].entry); } ctx->entry[i].entry = NULL; if (ctx->entry[i].ldaphost) { free(ctx->entry[i].ldaphost); } ctx->entry[i].ldaphost = NULL; ctx->entry[i].ldapport = 0; ctx->entry[i].scope = 0; if (ctx->entry[i].binddn) { free(ctx->entry[i].binddn); } ctx->entry[i].binddn = NULL; if (ctx->entry[i].passwd) { free(ctx->entry[i].passwd); } ctx->entry[i].passwd = NULL; if (ctx->entry[i].base) { free(ctx->entry[i].base); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -