diff options
| -rw-r--r-- | doc/references/libldap/.gitignore | 2 | ||||
| -rw-r--r-- | doc/references/libldap/Makefile | 13 | ||||
| -rw-r--r-- | doc/references/libldap/README.md | 6 | ||||
| -rw-r--r-- | doc/references/libldap/ldap_search_ext.c | 213 |
4 files changed, 234 insertions, 0 deletions
diff --git a/doc/references/libldap/.gitignore b/doc/references/libldap/.gitignore new file mode 100644 index 0000000..55cd29e --- /dev/null +++ b/doc/references/libldap/.gitignore | |||
| @@ -0,0 +1,2 @@ | |||
| 1 | /*.o | ||
| 2 | /ldap_search_ext | ||
diff --git a/doc/references/libldap/Makefile b/doc/references/libldap/Makefile new file mode 100644 index 0000000..d0ec39a --- /dev/null +++ b/doc/references/libldap/Makefile | |||
| @@ -0,0 +1,13 @@ | |||
| 1 | |||
| 2 | CFLAGS += -Wall | ||
| 3 | LDFLAGS += -lldap -llber | ||
| 4 | |||
| 5 | all: ldap_search_ext | ||
| 6 | |||
| 7 | |||
| 8 | ldap_search_ext: ldap_search_ext.o | ||
| 9 | |||
| 10 | |||
| 11 | clean: | ||
| 12 | rm -f *.o | ||
| 13 | rm -f ldap_search_ext | ||
diff --git a/doc/references/libldap/README.md b/doc/references/libldap/README.md new file mode 100644 index 0000000..b153629 --- /dev/null +++ b/doc/references/libldap/README.md | |||
| @@ -0,0 +1,6 @@ | |||
| 1 | |||
| 2 | LDAP Utilities: libldap: Reference | ||
| 3 | ================================== | ||
| 4 | |||
| 5 | This directory contains references examples of the OpenLDAP libldap API. | ||
| 6 | |||
diff --git a/doc/references/libldap/ldap_search_ext.c b/doc/references/libldap/ldap_search_ext.c new file mode 100644 index 0000000..e68de6c --- /dev/null +++ b/doc/references/libldap/ldap_search_ext.c | |||
| @@ -0,0 +1,213 @@ | |||
| 1 | /* | ||
| 2 | * LDAP Utilities | ||
| 3 | * Copyright (C) 2019 David M. Syzdek <david@syzdek.net> | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions are | ||
| 7 | * met: | ||
| 8 | * | ||
| 9 | * 1. Redistributions of source code must retain the above copyright | ||
| 10 | * notice, this list of conditions and the following disclaimer. | ||
| 11 | * | ||
| 12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 13 | * notice, this list of conditions and the following disclaimer in the | ||
| 14 | * documentation and/or other materials provided with the distribution. | ||
| 15 | * | ||
| 16 | * 3. Neither the name of the copyright holder nor the names of its | ||
| 17 | * contributors may be used to endorse or promote products derived from | ||
| 18 | * this software without specific prior written permission. | ||
| 19 | * | ||
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS | ||
| 21 | * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, | ||
| 22 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
| 23 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR | ||
| 24 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | ||
| 25 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | ||
| 26 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | ||
| 27 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | ||
| 28 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | ||
| 29 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
| 30 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| 31 | * | ||
| 32 | */ | ||
| 33 | /* | ||
| 34 | * Simple example of LDAP search using asynchronous functions. | ||
| 35 | */ | ||
| 36 | /* | ||
| 37 | * Simple Build: | ||
| 38 | * gcc -Wall -c -o ldap_search_ext.o ldap_search_ext.c | ||
| 39 | * gcc -lldap -llber -o ldap_search_ext ldap_search_ext.o | ||
| 40 | */ | ||
| 41 | |||
| 42 | #include <stdio.h> | ||
| 43 | #include <ldap.h> | ||
| 44 | |||
| 45 | int main(void); | ||
| 46 | |||
| 47 | int main(void) | ||
| 48 | { | ||
| 49 | int i; | ||
| 50 | int rc; | ||
| 51 | int err; | ||
| 52 | int msgid; | ||
| 53 | int opt_i; | ||
| 54 | const char * opt_s; | ||
| 55 | char * dn; | ||
| 56 | char * name; | ||
| 57 | char * str; | ||
| 58 | LDAP * ld; | ||
| 59 | LDAPMessage * res; | ||
| 60 | LDAPMessage * msg; | ||
| 61 | BerValue cred; | ||
| 62 | BerElement * ber; | ||
| 63 | BerValue * servercredp; | ||
| 64 | struct berval ** vals; | ||
| 65 | char * attrs[32]; | ||
| 66 | |||
| 67 | |||
| 68 | printf("# ldap_initialize ...\n"); | ||
| 69 | if ((rc = ldap_initialize(&ld, NULL)) != LDAP_SUCCESS) | ||
| 70 | { | ||
| 71 | perror("ldap_initialize()"); | ||
| 72 | ldap_unbind_ext_s(ld, NULL, NULL); | ||
| 73 | return(1); | ||
| 74 | }; | ||
| 75 | |||
| 76 | |||
| 77 | printf("# ldap_set_option(LDAP_OPT_PROTOCOL_VERSION) ...\n"); | ||
| 78 | opt_i = 3; | ||
| 79 | if ((rc = ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, &opt_i)) != LDAP_OPT_SUCCESS) | ||
| 80 | { | ||
| 81 | fprintf(stderr, "LDAP_OPT_PROTOCOL_VERSION error\n"); | ||
| 82 | ldap_unbind_ext_s(ld, NULL, NULL); | ||
| 83 | return(1); | ||
| 84 | }; | ||
| 85 | |||
| 86 | |||
| 87 | printf("# ldap_set_option(LDAP_OPT_TIMELIMIT) ...\n"); | ||
| 88 | opt_i = 5; | ||
| 89 | if ((rc = ldap_set_option(ld, LDAP_OPT_TIMELIMIT, &opt_i)) != LDAP_OPT_SUCCESS) | ||
| 90 | { | ||
| 91 | fprintf(stderr, "LDAP_OPT_PROTOCOL_VERSION error\n"); | ||
| 92 | ldap_unbind_ext_s(ld, NULL, NULL); | ||
| 93 | return(1); | ||
| 94 | }; | ||
| 95 | |||
| 96 | |||
| 97 | if ((opt_s = getenv("DEMO_LDAP_URI")) != NULL) | ||
| 98 | { | ||
| 99 | printf("# ldap_set_option(LDAP_OPT_URI) ...\n"); | ||
| 100 | if ((rc = ldap_set_option(ld, LDAP_OPT_URI, opt_s)) != LDAP_OPT_SUCCESS) | ||
| 101 | { | ||
| 102 | fprintf(stderr, "LDAP_OPT_URI error\n"); | ||
| 103 | ldap_unbind_ext_s(ld, NULL, NULL); | ||
| 104 | return(1); | ||
| 105 | }; | ||
| 106 | }; | ||
| 107 | if ((rc = ldap_get_option(ld, LDAP_OPT_URI, &str)) == LDAP_OPT_SUCCESS) | ||
| 108 | { | ||
| 109 | printf("# URI: %s\n", str); | ||
| 110 | ldap_memfree(str); | ||
| 111 | }; | ||
| 112 | |||
| 113 | |||
| 114 | printf("# ldap_sasl_bind_s ...\n"); | ||
| 115 | bzero(&cred, sizeof(cred)); | ||
| 116 | servercredp = NULL; | ||
| 117 | if ((rc = ldap_sasl_bind_s(ld, NULL, (const char *)LDAP_SASL_SIMPLE, &cred, NULL, NULL, &servercredp)) != LDAP_SUCCESS) | ||
| 118 | { | ||
| 119 | fprintf(stderr, "ldap_sasl_bind_s(): %s\n", ldap_err2string(rc)); | ||
| 120 | |||
| 121 | ldap_unbind_ext_s(ld, NULL, NULL); | ||
| 122 | return(1); | ||
| 123 | }; | ||
| 124 | |||
| 125 | |||
| 126 | attrs[0] = "gn"; | ||
| 127 | attrs[1] = "sn"; | ||
| 128 | attrs[2] = "uid"; | ||
| 129 | attrs[3] = "title"; | ||
| 130 | attrs[4] = "objectClass"; | ||
| 131 | attrs[5] = "o"; | ||
| 132 | attrs[6] = NULL; | ||
| 133 | |||
| 134 | |||
| 135 | printf("# ldap_search_ext ...\n"); | ||
| 136 | //if ((err = ldap_search_ext(ld, "dc=example,dc=com", LDAP_SCOPE_SUBTREE, "(cn=*)", attrs, 0, NULL, NULL, NULL, -1, &msgid))) | ||
| 137 | if ((err = ldap_search_ext( ld, NULL, LDAP_SCOPE_SUBTREE, NULL, attrs, 0, NULL, NULL, NULL, -1, &msgid))) | ||
| 138 | { | ||
| 139 | fprintf(stderr, "ldap_search_ext_s(): %s\n", ldap_err2string(err)); | ||
| 140 | ldap_unbind_ext_s(ld, NULL, NULL); | ||
| 141 | return(1); | ||
| 142 | }; | ||
| 143 | |||
| 144 | |||
| 145 | printf("# ldap_result ...\n"); | ||
| 146 | switch((err = ldap_result(ld, msgid, LDAP_MSG_ALL, NULL, &res))) | ||
| 147 | { | ||
| 148 | case 0: | ||
| 149 | break; | ||
| 150 | |||
| 151 | case -1: | ||
| 152 | fprintf(stderr, "ldap_result(): %s\n", ldap_err2string(err)); | ||
| 153 | ldap_unbind_ext_s(ld, NULL, NULL); | ||
| 154 | return(-1); | ||
| 155 | |||
| 156 | default: | ||
| 157 | break; | ||
| 158 | }; | ||
| 159 | |||
| 160 | |||
| 161 | printf("# ldap_parse_result ...\n"); | ||
| 162 | if ((rc = ldap_parse_result(ld, res, &err, NULL, NULL, NULL, NULL, 0)) != LDAP_SUCCESS) | ||
| 163 | { | ||
| 164 | fprintf(stderr, "ldap_parse_result(): %s\n", ldap_err2string(rc)); | ||
| 165 | ldap_unbind_ext_s(ld, NULL, NULL); | ||
| 166 | return(1); | ||
| 167 | }; | ||
| 168 | if (err != LDAP_SUCCESS) | ||
| 169 | { | ||
| 170 | fprintf(stderr, "ldap_parse_result(): %s\n", ldap_err2string(err)); | ||
| 171 | ldap_unbind_ext_s(ld, NULL, NULL); | ||
| 172 | return(1); | ||
| 173 | }; | ||
| 174 | |||
| 175 | |||
| 176 | printf("# Entry count: %i\n", ldap_count_entries(ld, res)); | ||
| 177 | |||
| 178 | |||
| 179 | printf("# ldap_first_entry ...\n\n\n"); | ||
| 180 | msg = ldap_first_entry(ld, res); | ||
| 181 | while ((msg)) | ||
| 182 | { | ||
| 183 | dn = ldap_get_dn(ld, msg); | ||
| 184 | printf("dn: %s\n", dn); | ||
| 185 | ldap_dnfree(dn); | ||
| 186 | |||
| 187 | name = ldap_first_attribute(ld, msg, &ber); | ||
| 188 | while((name)) | ||
| 189 | { | ||
| 190 | if ((vals = ldap_get_values_len(ld, msg, name))) | ||
| 191 | { | ||
| 192 | for(i = 0; (i < ldap_count_values_len(vals)); i++) | ||
| 193 | //printf("%s: %s (%lu)\n", name, vals[i]->bv_val, vals[i]->bv_len); | ||
| 194 | printf("%s: %s\n", name, vals[i]->bv_val); | ||
| 195 | ldap_value_free_len(vals); | ||
| 196 | }; | ||
| 197 | |||
| 198 | name = ldap_next_attribute(ld, msg, ber); | ||
| 199 | }; | ||
| 200 | ber_free(ber, 0); | ||
| 201 | printf("\n"); | ||
| 202 | |||
| 203 | msg = ldap_next_entry(ld, msg); | ||
| 204 | }; | ||
| 205 | |||
| 206 | ldap_msgfree(res); | ||
| 207 | |||
| 208 | |||
| 209 | ldap_unbind_ext_s(ld, NULL, NULL); | ||
| 210 | |||
| 211 | |||
| 212 | return(0); | ||
| 213 | } | ||
