diff options
| -rw-r--r-- | include/ldaputils.h | 9 | ||||
| -rw-r--r-- | lib/libldaputils/lconfig.c | 16 | ||||
| -rw-r--r-- | lib/libldaputils/lconfig_opts.c | 26 | ||||
| -rw-r--r-- | lib/libldaputils/lmemory.c | 41 | ||||
| -rw-r--r-- | lib/libldaputils/lmemory.h | 2 | ||||
| -rw-r--r-- | src/ldap2csv.c | 26 | ||||
| -rw-r--r-- | tests/ldaptest.c | 47 | ||||
| -rw-r--r-- | tests/readline.c | 30 |
8 files changed, 140 insertions, 57 deletions
diff --git a/include/ldaputils.h b/include/ldaputils.h index 7dbed22..92fe64e 100644 --- a/include/ldaputils.h +++ b/include/ldaputils.h | |||
| @@ -118,7 +118,7 @@ typedef struct ldaputils_config_struct LDAPUtils; | |||
| 118 | struct ldaputils_config_struct | 118 | struct ldaputils_config_struct |
| 119 | { | 119 | { |
| 120 | LDAP * ld; ///< LDAP descriptor | 120 | LDAP * ld; ///< LDAP descriptor |
| 121 | LDAPURLDesc * ludp; ///< LDAP URL descriptor | 121 | char * prog_name; ///< program name |
| 122 | int continuous; // -c continuous operation mode | 122 | int continuous; // -c continuous operation mode |
| 123 | long debug; // -d debug level | 123 | long debug; // -d debug level |
| 124 | int dryrun; // -n dry run mode | 124 | int dryrun; // -n dry run mode |
| @@ -139,7 +139,6 @@ struct ldaputils_config_struct | |||
| 139 | const char * passfile; // -y password file | 139 | const char * passfile; // -y password file |
| 140 | const char * sortattr; // -S sort by attribute | 140 | const char * sortattr; // -S sort by attribute |
| 141 | const char * uri; // -H LDAP URI | 141 | const char * uri; // -H LDAP URI |
| 142 | const char * prog_name; // name of calling program | ||
| 143 | }; | 142 | }; |
| 144 | 143 | ||
| 145 | 144 | ||
| @@ -159,9 +158,6 @@ int ldaputils_cmdargs(LDAPUtils * cnf, int c, const char * arg); | |||
| 159 | // frees common config | 158 | // frees common config |
| 160 | void ldaputils_config_free(LDAPUtils * cnf); | 159 | void ldaputils_config_free(LDAPUtils * cnf); |
| 161 | 160 | ||
| 162 | // initializes the common config | ||
| 163 | void ldaputils_config_init(LDAPUtils * cnf, const char * prog_name); | ||
| 164 | |||
| 165 | // prints configuration to stdout | 161 | // prints configuration to stdout |
| 166 | void ldaputils_config_print(LDAPUtils * cnf); | 162 | void ldaputils_config_print(LDAPUtils * cnf); |
| 167 | 163 | ||
| @@ -262,6 +258,9 @@ char * ldaputils_get_vals(LDAPUtils * cnf, LDAPUtilsEntry * entry, | |||
| 262 | const char * attr); | 258 | const char * attr); |
| 263 | 259 | ||
| 264 | // connects and binds to LDAP server | 260 | // connects and binds to LDAP server |
| 261 | int ldaputils_initialize(LDAPUtils ** lup, const char * prog_name); | ||
| 262 | |||
| 263 | // connects and binds to LDAP server | ||
| 265 | LDAP * ldaputils_initialize_conn(LDAPUtils * cnf); | 264 | LDAP * ldaputils_initialize_conn(LDAPUtils * cnf); |
| 266 | 265 | ||
| 267 | // connects and binds to LDAP server | 266 | // connects and binds to LDAP server |
diff --git a/lib/libldaputils/lconfig.c b/lib/libldaputils/lconfig.c index be683f7..30c10a5 100644 --- a/lib/libldaputils/lconfig.c +++ b/lib/libldaputils/lconfig.c | |||
| @@ -177,22 +177,6 @@ void ldaputils_config_free(LDAPUtils * cnf) | |||
| 177 | if (!(cnf)) | 177 | if (!(cnf)) |
| 178 | return; | 178 | return; |
| 179 | 179 | ||
| 180 | if (cnf->ludp) | ||
| 181 | ldap_free_urldesc(cnf->ludp); | ||
| 182 | cnf->ludp = NULL; | ||
| 183 | |||
| 184 | return; | ||
| 185 | } | ||
| 186 | |||
| 187 | |||
| 188 | /// initializes the common config | ||
| 189 | /// @param[in] cnf reference to common configuration struct | ||
| 190 | void ldaputils_config_init(LDAPUtils * cnf, const char * prog_name) | ||
| 191 | { | ||
| 192 | memset(cnf, 0, sizeof(LDAPUtils)); | ||
| 193 | cnf->referrals = 0; | ||
| 194 | cnf->scope = LDAP_SCOPE_SUBTREE; | ||
| 195 | cnf->prog_name = prog_name; | ||
| 196 | return; | 180 | return; |
| 197 | } | 181 | } |
| 198 | 182 | ||
diff --git a/lib/libldaputils/lconfig_opts.c b/lib/libldaputils/lconfig_opts.c index 91cf362..5fc6583 100644 --- a/lib/libldaputils/lconfig_opts.c +++ b/lib/libldaputils/lconfig_opts.c | |||
| @@ -53,6 +53,7 @@ | |||
| 53 | #include <sys/types.h> | 53 | #include <sys/types.h> |
| 54 | #include <unistd.h> | 54 | #include <unistd.h> |
| 55 | #include <string.h> | 55 | #include <string.h> |
| 56 | #include <assert.h> | ||
| 56 | 57 | ||
| 57 | 58 | ||
| 58 | ///////////////// | 59 | ///////////////// |
| @@ -239,28 +240,9 @@ int ldaputils_config_set_timelimit(LDAPUtils * cnf, const char * arg) | |||
| 239 | /// @param[in] arg value of the command line argument | 240 | /// @param[in] arg value of the command line argument |
| 240 | int ldaputils_config_set_uri(LDAPUtils * cnf, const char * arg) | 241 | int ldaputils_config_set_uri(LDAPUtils * cnf, const char * arg) |
| 241 | { | 242 | { |
| 242 | if ((cnf->ludp)) | 243 | |
| 243 | { | 244 | assert(cnf != NULL); |
| 244 | if (cnf->host == cnf->ludp->lud_host) | 245 | assert(arg != NULL); |
| 245 | cnf->host = NULL; | ||
| 246 | ldap_free_urldesc(cnf->ludp); | ||
| 247 | }; | ||
| 248 | cnf->ludp = NULL; | ||
| 249 | |||
| 250 | if (!(cnf->uri = arg)) | ||
| 251 | return(0); | ||
| 252 | |||
| 253 | if ((ldap_url_parse(arg, &cnf->ludp))) | ||
| 254 | { | ||
| 255 | // TRANSLATORS: The following strings provide an error message if the | ||
| 256 | // URI provided on the command line is an invalid LDAP URI. | ||
| 257 | fprintf(stderr, "%s: invalid LDAP URI\n", cnf->prog_name); | ||
| 258 | fprintf(stderr, "Try `%s --help' for more information.\n", cnf->prog_name); | ||
| 259 | return(1); | ||
| 260 | }; | ||
| 261 | |||
| 262 | cnf->host = cnf->ludp->lud_host; | ||
| 263 | cnf->port = cnf->ludp->lud_port; | ||
| 264 | 246 | ||
| 265 | return(0); | 247 | return(0); |
| 266 | } | 248 | } |
diff --git a/lib/libldaputils/lmemory.c b/lib/libldaputils/lmemory.c index 9e0cb90..78b68f5 100644 --- a/lib/libldaputils/lmemory.c +++ b/lib/libldaputils/lmemory.c | |||
| @@ -48,8 +48,10 @@ | |||
| 48 | #include <errno.h> | 48 | #include <errno.h> |
| 49 | #include <stdio.h> | 49 | #include <stdio.h> |
| 50 | #include <string.h> | 50 | #include <string.h> |
| 51 | #include <strings.h> | ||
| 51 | #include <ldap.h> | 52 | #include <ldap.h> |
| 52 | #include <stdlib.h> | 53 | #include <stdlib.h> |
| 54 | #include <assert.h> | ||
| 53 | 55 | ||
| 54 | 56 | ||
| 55 | ///////////////// | 57 | ///////////////// |
| @@ -58,5 +60,44 @@ | |||
| 58 | // // | 60 | // // |
| 59 | ///////////////// | 61 | ///////////////// |
| 60 | 62 | ||
| 63 | // connects and binds to LDAP server | ||
| 64 | int ldaputils_initialize(LDAPUtils ** lup, const char * prog_name) | ||
| 65 | { | ||
| 66 | int rc; | ||
| 67 | char * idx; | ||
| 68 | LDAPUtils * lu; | ||
| 69 | |||
| 70 | assert(lup != NULL); | ||
| 71 | assert(prog_name != NULL); | ||
| 72 | |||
| 73 | |||
| 74 | // allocate initial memory for base struct | ||
| 75 | if ((lu = malloc(sizeof(LDAPUtils))) == NULL) | ||
| 76 | return(LDAP_NO_MEMORY); | ||
| 77 | bzero(lu, sizeof(LDAPUtils)); | ||
| 78 | |||
| 79 | |||
| 80 | // save program name | ||
| 81 | if ((idx = rindex(prog_name, '/')) != NULL) | ||
| 82 | if (idx[1] != '\0') | ||
| 83 | prog_name = &idx[1]; | ||
| 84 | if ((lu->prog_name = strdup(prog_name)) == NULL) | ||
| 85 | { | ||
| 86 | free(lu); | ||
| 87 | return(LDAP_NO_MEMORY); | ||
| 88 | }; | ||
| 89 | |||
| 90 | |||
| 91 | // initialize LDAP library | ||
| 92 | if ((rc = ldap_initialize(&lu->ld, NULL)) != LDAP_SUCCESS) | ||
| 93 | { | ||
| 94 | free(lu->prog_name); | ||
| 95 | free(lu); | ||
| 96 | return(rc); | ||
| 97 | }; | ||
| 98 | |||
| 99 | |||
| 100 | return(LDAP_SUCCESS); | ||
| 101 | } | ||
| 61 | 102 | ||
| 62 | /* end of source file */ | 103 | /* end of source file */ |
diff --git a/lib/libldaputils/lmemory.h b/lib/libldaputils/lmemory.h index 7f7e2bb..ac232f5 100644 --- a/lib/libldaputils/lmemory.h +++ b/lib/libldaputils/lmemory.h | |||
| @@ -46,7 +46,7 @@ | |||
| 46 | // // | 46 | // // |
| 47 | /////////////// | 47 | /////////////// |
| 48 | 48 | ||
| 49 | #include <ldaputils.h> | 49 | #include "libldaputils.h" |
| 50 | 50 | ||
| 51 | 51 | ||
| 52 | /////////////////// | 52 | /////////////////// |
diff --git a/src/ldap2csv.c b/src/ldap2csv.c index 5a1f8dc..b779cfc 100644 --- a/src/ldap2csv.c +++ b/src/ldap2csv.c | |||
| @@ -102,7 +102,7 @@ | |||
| 102 | typedef struct my_config MyConfig; | 102 | typedef struct my_config MyConfig; |
| 103 | struct my_config | 103 | struct my_config |
| 104 | { | 104 | { |
| 105 | LDAPUtils common; | 105 | LDAPUtils * common; |
| 106 | char output[LDAPUTILS_OPT_LEN]; | 106 | char output[LDAPUTILS_OPT_LEN]; |
| 107 | }; | 107 | }; |
| 108 | 108 | ||
| @@ -157,7 +157,7 @@ int main(int argc, char * argv[]) | |||
| 157 | if (!(cnf)) | 157 | if (!(cnf)) |
| 158 | return(0); | 158 | return(0); |
| 159 | 159 | ||
| 160 | if (!(ld = ldaputils_initialize_conn(&cnf->common))) | 160 | if (!(ld = ldaputils_initialize_conn(cnf->common))) |
| 161 | { | 161 | { |
| 162 | ldaputils_config_free((LDAPUtils *)cnf); | 162 | ldaputils_config_free((LDAPUtils *)cnf); |
| 163 | free(cnf); | 163 | free(cnf); |
| @@ -173,12 +173,12 @@ int main(int argc, char * argv[]) | |||
| 173 | }; | 173 | }; |
| 174 | 174 | ||
| 175 | // prints attribute names | 175 | // prints attribute names |
| 176 | printf("\"%s\"", cnf->common.attrs[0]); | 176 | printf("\"%s\"", cnf->common->attrs[0]); |
| 177 | for(x = 1; cnf->common.attrs[x]; x++) | 177 | for(x = 1; cnf->common->attrs[x]; x++) |
| 178 | printf(",\"%s\"", cnf->common.attrs[x]); | 178 | printf(",\"%s\"", cnf->common->attrs[x]); |
| 179 | printf("\n"); | 179 | printf("\n"); |
| 180 | 180 | ||
| 181 | if (!(entries = ldaputils_get_entries(&cnf->common, ld, res, ((LDAPUtils *)cnf)->sortattr))) | 181 | if (!(entries = ldaputils_get_entries(cnf->common, ld, res, ((LDAPUtils *)cnf)->sortattr))) |
| 182 | { | 182 | { |
| 183 | ldap_msgfree(res); | 183 | ldap_msgfree(res); |
| 184 | ldap_unbind_ext_s(ld, NULL, NULL); | 184 | ldap_unbind_ext_s(ld, NULL, NULL); |
| @@ -192,9 +192,9 @@ int main(int argc, char * argv[]) | |||
| 192 | 192 | ||
| 193 | for(x = 0; entries[x]; x++) | 193 | for(x = 0; entries[x]; x++) |
| 194 | { | 194 | { |
| 195 | for(y = 0; cnf->common.attrs[y]; y++) | 195 | for(y = 0; cnf->common->attrs[y]; y++) |
| 196 | { | 196 | { |
| 197 | if (!(val = ldaputils_get_vals(&cnf->common, entries[x], cnf->common.attrs[y]))) | 197 | if (!(val = ldaputils_get_vals(cnf->common, entries[x], cnf->common->attrs[y]))) |
| 198 | { | 198 | { |
| 199 | ldap_msgfree(res); | 199 | ldap_msgfree(res); |
| 200 | ldap_unbind_ext_s(ld, NULL, NULL); | 200 | ldap_unbind_ext_s(ld, NULL, NULL); |
| @@ -252,7 +252,7 @@ int my_config(int argc, char * argv[], MyConfig ** cnfp) | |||
| 252 | }; | 252 | }; |
| 253 | memset(cnf, 0, sizeof(MyConfig)); | 253 | memset(cnf, 0, sizeof(MyConfig)); |
| 254 | 254 | ||
| 255 | ldaputils_config_init((LDAPUtils *) cnf, PROGRAM_NAME); | 255 | ldaputils_initialize(&cnf->common, PROGRAM_NAME); |
| 256 | 256 | ||
| 257 | // loops through args | 257 | // loops through args |
| 258 | while((c = getopt_long(argc, argv, short_options, long_options, &option_index)) != -1) | 258 | while((c = getopt_long(argc, argv, short_options, long_options, &option_index)) != -1) |
| @@ -280,17 +280,17 @@ int my_config(int argc, char * argv[], MyConfig ** cnfp) | |||
| 280 | return(1); | 280 | return(1); |
| 281 | }; | 281 | }; |
| 282 | 282 | ||
| 283 | cnf->common.filter = argv[optind]; | 283 | cnf->common->filter = argv[optind]; |
| 284 | 284 | ||
| 285 | // configures LDAP attributes to return in results | 285 | // configures LDAP attributes to return in results |
| 286 | if (!(cnf->common.attrs = (char **) malloc(sizeof(char *) * (size_t)(argc-optind)))) | 286 | if (!(cnf->common->attrs = (char **) malloc(sizeof(char *) * (size_t)(argc-optind)))) |
| 287 | { | 287 | { |
| 288 | fprintf(stderr, "%s: out of virtual memory\n", PROGRAM_NAME); | 288 | fprintf(stderr, "%s: out of virtual memory\n", PROGRAM_NAME); |
| 289 | return(1); | 289 | return(1); |
| 290 | }; | 290 | }; |
| 291 | for(c = 0; c < (argc-optind-1); c++) | 291 | for(c = 0; c < (argc-optind-1); c++) |
| 292 | cnf->common.attrs[c] = argv[optind+1+c]; | 292 | cnf->common->attrs[c] = argv[optind+1+c]; |
| 293 | cnf->common.attrs[c] = 0; | 293 | cnf->common->attrs[c] = 0; |
| 294 | 294 | ||
| 295 | *cnfp = cnf; | 295 | *cnfp = cnf; |
| 296 | 296 | ||
diff --git a/tests/ldaptest.c b/tests/ldaptest.c new file mode 100644 index 0000000..70d9723 --- /dev/null +++ b/tests/ldaptest.c | |||
| @@ -0,0 +1,47 @@ | |||
| 1 | |||
| 2 | #include <stdio.h> | ||
| 3 | #include <stdio.h> | ||
| 4 | #include <ldap.h> | ||
| 5 | |||
| 6 | int main(void); | ||
| 7 | int main(void) | ||
| 8 | { | ||
| 9 | int opt; | ||
| 10 | int lerr; | ||
| 11 | int count; | ||
| 12 | LDAP * ld; | ||
| 13 | LDAPMessage * res; | ||
| 14 | //LDAPMessage * entry; | ||
| 15 | |||
| 16 | printf("running: ldap_initialize()\n"); | ||
| 17 | if (ldap_initialize(&ld, NULL)) | ||
| 18 | { | ||
| 19 | perror("ldap_initialize()"); | ||
| 20 | return(1); | ||
| 21 | }; | ||
| 22 | |||
| 23 | opt = 0xffff; | ||
| 24 | ldap_set_option(ld, LDAP_OPT_DEBUG_LEVEL, &opt); | ||
| 25 | |||
| 26 | printf("running: ldap_search_ext_s()\n"); | ||
| 27 | if ((lerr = ldap_search_ext_s(ld, NULL, LDAP_SCOPE_SUB, "(mail=syzdek@mosquitonet.com)", NULL, 0, NULL, NULL, NULL, -1, &res))) | ||
| 28 | { | ||
| 29 | fprintf(stderr, "ldap_search_ext_s(): %s\n", ldap_err2string(lerr)); | ||
| 30 | ldap_unbind_ext_s(ld, NULL, NULL); | ||
| 31 | return(1); | ||
| 32 | }; | ||
| 33 | |||
| 34 | printf("running: ldap_count_entries()\n"); | ||
| 35 | if (!(count = ldap_count_entries(ld, res))) | ||
| 36 | { | ||
| 37 | ldap_msgfree(res); | ||
| 38 | ldap_unbind_ext_s(ld, NULL, NULL); | ||
| 39 | return(1); | ||
| 40 | }; | ||
| 41 | printf("# %i entries found\n", count); | ||
| 42 | |||
| 43 | printf("running: ldap_unbind_ext_s()\n"); | ||
| 44 | ldap_unbind_ext_s(ld, NULL, NULL); | ||
| 45 | |||
| 46 | return(0); | ||
| 47 | } | ||
diff --git a/tests/readline.c b/tests/readline.c new file mode 100644 index 0000000..a27de67 --- /dev/null +++ b/tests/readline.c | |||
| @@ -0,0 +1,30 @@ | |||
| 1 | |||
| 2 | #include <stdio.h> | ||
| 3 | #include <string.h> | ||
| 4 | #include <readline/readline.h> | ||
| 5 | #include <readline/history.h> | ||
| 6 | |||
| 7 | int main(void); | ||
| 8 | |||
| 9 | int main(void) | ||
| 10 | { | ||
| 11 | char * data; | ||
| 12 | char * old; | ||
| 13 | |||
| 14 | data = strdup(""); | ||
| 15 | |||
| 16 | while(strcmp(data, "exit")) | ||
| 17 | { | ||
| 18 | old = data; | ||
| 19 | printf("Enter text [%s]: ", data); | ||
| 20 | data = readline(NULL); | ||
| 21 | if(!(strlen(data))) | ||
| 22 | data = old; | ||
| 23 | else | ||
| 24 | free(old); | ||
| 25 | printf("you typed: %s\n", data); | ||
| 26 | old = data; | ||
| 27 | }; | ||
| 28 | |||
| 29 | return(0); | ||
| 30 | } | ||
