2 * Copyright (c) 1991, 1992, 1993
3 * Regents of the University of Michigan. All rights reserved.
5 * Redistribution and use in source and binary forms are permitted
6 * provided that this notice is preserved and that due credit is given
7 * to the University of Michigan at Ann Arbor. The name of the University
8 * may not be used to endorse or promote products derived from this
9 * software without specific prior written permission. This software
10 * is provided ``as is'' without express or implied warranty.
18 #include <ac/string.h>
26 extern char *search_base; /* search base */
27 extern int verbose; /* verbose mode flag */
28 extern LDAP *ld; /* our ldap descriptor */
30 static int num_picked = 0; /* used when user picks entry at More prompt */
33 extern int debug; /* debug flag */
40 static char *attrs[2] = { "objectClass", NULL };
45 printf("->vrfy(%s)\n", dn);
47 /* verify that this DN exists in the directory */
48 (void) ldap_search_s(ld, dn, LDAP_SCOPE_BASE, "objectClass=*", attrs, TRUE, &results);
49 (void) ldap_msgfree(results);
51 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
53 if ((ld_errno == LDAP_NO_SUCH_OBJECT) || (ld_errno == LDAP_INVALID_DN_SYNTAX))
55 else if (ld_errno == LDAP_SUCCESS)
58 ldap_perror(ld, "ldap_search");
64 static LDAPMessage * disambiguate( result, matches, read_attrs, who )
70 int choice; /* entry that user chooses */
72 char *namelist[MAX_NUM_NAMES]; /* names found */
73 char response[SMALL_BUF_SIZE]; /* results from user */
74 char *name = NULL; /* DN to lookup */
81 printf("->disambiguate(%x, %d, %x, %s)\n", result, matches,
85 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
88 * If we are here, it means that we got back multiple answers.
90 if ((ld_errno == LDAP_TIMELIMIT_EXCEEDED)
91 || (ld_errno == LDAP_SIZELIMIT_EXCEEDED)) {
93 printf(" Your query was too general and a limit was exceeded. The results listed\n");
94 printf(" are not complete. You may want to try again with a more refined query.\n\n");
97 printf(" Time or size limit exceeded. Partial results follow.\n\n");
99 printf(" %1d names matched \"%s\".\n", matches, who);
101 printf(" Do you wish to see a list of names? ");
103 (void) memset(response, 0, sizeof(response));
104 fetch_buffer(response, sizeof(response), stdin);
105 switch (response[0]) {
114 print_list(result, namelist, &matches);
116 choice = pick_one(matches);
121 name = strdup(namelist[choice]);
123 * Now free up all of the pointers allocated in
124 * namelist. The print_list() routine that filled
125 * in this collection of strings starts at 1, not 0.
127 for (i = 1; namelist[i] != NULL; i++)
135 if (debug & D_FIND) {
136 printf(" Calling ldap_search_s()\n");
137 printf(" ld = 0x%x\n", ld);
138 printf(" search base = %s\n", name);
139 printf(" scope = LDAP_SCOPE_BASE\n");
140 printf(" filter = objectClass=*\n");
141 for (i = 0; read_attrs[i] != NULL; i++)
142 printf(" read_attrs[%d] = %s\n", i, read_attrs[i]);
143 printf(" read_attrs[%d] = NULL\n", i);
144 printf(" attrsonly = FALSE\n");
145 printf(" &mp = 0x%x\n", &mp);
148 if (ldap_search_s(ld, name, LDAP_SCOPE_BASE, "objectClass=*", read_attrs, FALSE, &mp) != LDAP_SUCCESS) {
149 ldap_perror(ld, "ldap_search_s");
158 printf(" Please enter 'y', 'n', or RETURN.\n");
164 LDAPMessage * find(who, quiet)
168 register int i, j, k; /* general ints */
169 int matches; /* from ldap_count_entries() */
170 int admonished = FALSE;
171 static int attrs_set = 0;
172 static char *read_attrs[MAX_ATTRS]; /* attrs to use in a read op */
173 static char *search_attrs[MAX_ATTRS]; /* attrs to use in a srch op */
174 static int rc; /* return from ldap_search */
175 LDAPMessage *ldtmp, *res; /* results returned from search */
176 char name[MED_BUF_SIZE];
177 char response[SMALL_BUF_SIZE];
178 char *cp, *dn, **rdns;
180 extern LDAPFiltDesc *lfdp; /* LDAP filter descriptor */
181 extern struct attribute attrlist[]; /* complete list of attrs */
186 fprintf(stderr, "->find(%s)\n", who);
188 /* did not specify a 'who' */
190 printf(" Locate whose entry? ");
192 fetch_buffer(name, sizeof(name), stdin);
198 if (attrs_set == 0) {
201 for (i = 0; attrlist[i].quipu_name != NULL; i++) {
202 if (attrlist[i].flags & ATTR_FLAG_READ)
203 read_attrs[j++] = attrlist[i].quipu_name;
204 if (attrlist[i].flags & ATTR_FLAG_SEARCH)
205 search_attrs[k++] = attrlist[i].quipu_name;
207 read_attrs[j] = NULL;
208 search_attrs[k] = NULL;
212 * If the user-supplied name has any commas in it, we
213 * assume that it is a UFN, and do everything right
214 * here. If we don't find it, treat it as NOT a UFN.
216 if (strchr(who, ',') != NULL) {
217 int savederef, deref;
220 printf("\"%s\" appears to be a UFN\n", who);
222 ldap_get_option(ld, LDAP_OPT_DEREF, &savederef);
223 deref = LDAP_DEREF_FINDING;
224 ldap_set_option(ld, LDAP_OPT_DEREF, &deref);
226 if ((rc = ldap_ufn_search_s(ld, who, search_attrs, FALSE, &res)) !=
227 LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED &&
228 rc != LDAP_TIMELIMIT_EXCEEDED) {
229 ldap_perror(ld, "ldap_ufn_search_s");
230 ldap_set_option(ld, LDAP_OPT_DEREF, &savederef);
233 if ((matches = ldap_count_entries(ld, res)) < 0) {
234 ldap_perror(ld, "ldap_count_entries");
235 ldap_set_option(ld, LDAP_OPT_DEREF, &savederef);
237 } else if (matches == 1) {
238 dn = ldap_get_dn(ld, ldap_first_entry(ld, res));
239 rc = ldap_search_s(ld, dn, LDAP_SCOPE_BASE, "objectClass=*", read_attrs, FALSE, &res);
241 if (rc != LDAP_SUCCESS) {
243 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
244 if (ld_errno == LDAP_UNAVAILABLE)
245 printf(" Could not contact the LDAP server to find \"%s\".\n", who);
247 ldap_perror(ld, "ldap_search_s");
250 ldap_set_option(ld, LDAP_OPT_DEREF, &savederef);
252 } else if (matches > 1 ) {
253 return disambiguate( res, matches, read_attrs, who );
255 ldap_set_option(ld, LDAP_OPT_DEREF, &savederef);
259 * Old users of the MTS *USERDIRECTORY will likely wrap the name
260 * in quotes. Not only is this unnecessary, but it also won't work.
262 for (cp = strchr(who, '"'); cp != NULL; cp = strchr(cp, '"')) {
264 printf(" You do not need to enclose names in quotes.\n");
273 * It wasn't a UFN, so look it up in the usual method.
275 for (fi = ldap_getfirstfilter(lfdp, "ud", who); fi != NULL;
276 fi = ldap_getnextfilter(lfdp)) {
279 printf("Searching, filter = %s\n", fi->lfi_filter);
282 if ((rc = ldap_search_s(ld, search_base, fi->lfi_scope,
283 fi->lfi_filter, search_attrs, FALSE, &res)) != LDAP_SUCCESS &&
284 rc != LDAP_SIZELIMIT_EXCEEDED && rc != LDAP_TIMELIMIT_EXCEEDED) {
285 ldap_perror(ld, "ldap_search_s");
289 if ((matches = ldap_count_entries(ld, res)) < 0) {
290 ldap_perror(ld, "ldap_count_entries");
294 else if (matches == 1) {
295 dn = ldap_get_dn(ld, ldap_first_entry(ld, res));
298 printf(" Found one %s match for \"%s\"\n",
300 if (!fi->lfi_isexact) {
301 rdns = ldap_explode_dn(dn, TRUE);
302 printf(" Do you mean %s? ", *rdns);
303 (void) ldap_value_free(rdns);
305 fetch_buffer(response, sizeof(response), stdin);
306 if ((response[0] == 'n') || (response[0] == 'N'))
313 if (debug & D_FIND) {
314 printf(" Calling ldap_search_s()\n");
315 printf(" ld = 0x%x\n", ld);
316 printf(" dn = %s\n", dn);
317 printf(" scope = LDAP_SCOPE_BASE\n");
318 printf(" filter = %s\n", "objectClass=*");
319 for (i = 0; read_attrs[i] != NULL; i++)
320 printf(" read_attrs[%d] = %s\n", i, read_attrs[i]);
321 printf(" read_attrs[%d] = NULL\n", i);
322 printf(" attrsonly = FALSE\n");
323 printf(" &results = 0x%x\n", &res);
326 if (ldap_search_s(ld, dn, LDAP_SCOPE_BASE, "objectClass=*", read_attrs, FALSE, &res) != LDAP_SUCCESS) {
327 ldap_perror(ld, "ldap_search_s");
334 else if (matches > 0) {
335 ldtmp = disambiguate(res, matches, read_attrs, who);
339 /* if we're here, there were zero matches */
349 char user_pick[SMALL_BUF_SIZE];
353 printf("->pick_one(%d)\n", i);
356 /* make the user pick an entry */
358 printf(" Enter the number of the name you want or Q to quit: ");
360 fetch_buffer(user_pick, sizeof(user_pick), stdin);
361 if (user_pick[0] == 'q' || user_pick[0] == 'Q')
364 if ((n > 0) && (n <= i))
366 printf(" Invalid response\n");
371 print_list(list, names, matches)
378 char resp[SMALL_BUF_SIZE];
379 register LDAPMessage *ep;
381 register int rest = 4; /* 4, not 1 */
385 printf("->print_list(%x, %x, %x)\n", list, names, matches);
387 /* print a list of names from which the user will select */
388 for (ep = ldap_first_entry(ld, list); ep != NULL; ep = ldap_next_entry(ld, ep)) {
390 names[i] = ldap_get_dn(ld, ep);
391 rdns = ldap_explode_dn(names[i], TRUE);
392 cpp = ldap_get_values(ld, ep, "title");
394 printf(" %3d. %s\n", i, *rdns);
396 printf(" %3d. %s, %s\n", i, *rdns, *cpp);
397 ldap_value_free(rdns);
398 ldap_value_free(cpp);
400 if ((rest++ > (lpp - 1)) && (i < *matches)) {
404 fetch_buffer(resp, sizeof(resp), stdin);
405 if ((resp[0] == 'n') || (resp[0] == 'N'))
407 else if ((num_picked = atoi(resp)) != 0) {
421 find_all_subscribers(sub, group)
427 static char *attributes[] = { "cn", NULL };
428 char filter[MED_BUF_SIZE];
429 register LDAPMessage *ep;
434 printf("->find_all_subscribers(%x, %s)\n", sub, group);
437 sprintf(filter, "%s=%s", "memberOfGroup", group);
438 if (ldap_search_s(ld, search_base, LDAP_SCOPE_SUBTREE, filter, attributes, FALSE, &result) != LDAP_SUCCESS) {
440 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
441 if (ld_errno == LDAP_NO_SUCH_ATTRIBUTE)
443 ldap_perror(ld, "ldap_search_s");
446 count = ldap_count_entries(ld, result);
448 ldap_msgfree(result);
451 if ( count > MAX_VALUES ) {
452 printf( " Only retrieving the first %d subscribers....\n",
456 for (ep = ldap_first_entry(ld, result); i < MAX_VALUES && ep != NULL; ep = ldap_next_entry(ld, ep)) {
457 sub[i++] = ldap_get_dn(ld, ep);
460 printf("sub[%d] = %s\n", i - 1, sub[i - 1]);
464 ldap_msgfree(result);
468 char * fetch_boolean_value(who, attr)
470 struct attribute attr;
472 LDAPMessage *result; /* from the search below */
473 register LDAPMessage *ep; /* entry pointer */
474 register char **vp; /* for parsing the result */
475 static char *attributes[] = { NULL, NULL };
479 printf("->fetch_boolean_value(%s, %s)\n", who, attr.quipu_name);
481 attributes[0] = attr.quipu_name;
482 if (ldap_search_s(ld, who, LDAP_SCOPE_BASE, "objectClass=*", attributes, FALSE, &result) != LDAP_SUCCESS) {
484 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
485 if (ld_errno == LDAP_NO_SUCH_ATTRIBUTE)
487 ldap_perror(ld, "ldap_search_s");
488 ldap_msgfree(result);
493 * We did a read on one name and only asked for one attribute.
494 * There's no reason to loop through any of these structures.
496 * If ldap_first_attribute() returns NULL, then this entry did
497 * not have this particular attribute.
499 ep = ldap_first_entry(ld, result);
500 if ((vp = (char **) ldap_get_values(ld, ep, attr.quipu_name)) == NULL) {
501 ldap_msgfree(result);
505 ldap_msgfree(result);
506 if (!strcasecmp(*vp, "TRUE")) {
510 else if (!strcasecmp(*vp, "FALSE")) {
515 fprintf(stderr, " Got garbage -> [%s]\n", *vp);