3 * query.c: for rcpt500 (X.500 email query responder)
5 * 18 June 1992 by Mark C Smith
6 * Copyright (c) 1992 The Regents of The University of Michigan
12 #include <ac/stdlib.h>
15 #include <ac/string.h>
16 #include <ac/syslog.h>
25 #include "ldap_defaults.h"
27 static char buf[ MAXSIZE ];
28 static char *errpreface = "Your query failed: ";
30 static void close_ldap(LDAP *ld);
31 static void append_entry_list(char *rep, char *qu, LDAP *ld, LDAPMessage *msg);
32 static int append_text(void *reply, char *text, ber_len_t len);
33 static int do_read (LDAP *ld, char *dn, char *rep, struct ldap_disptmpl *tmp);
34 static void report_ldap_err (LDAP *ldp, char *reply);
35 static void remove_trailing_space (char *s);
39 query_cmd( struct msginfo *msgp, char *reply )
42 LDAPMessage *ldmsgp, *entry;
44 int matches, rc, ld_errno;
47 struct ldap_disptmpl *tmpllist = NULL;
48 static char *attrs[] = { "cn", "title",
49 #ifdef RCPT500_SORT_ATTR
54 if ( msgp->msg_arg == NULL ) {
55 return( help_cmd( msgp, reply ));
58 remove_trailing_space( msgp->msg_arg );
59 if ( *msgp->msg_arg == '\0' ) {
60 return( help_cmd( msgp, reply ));
63 if (( lfdp = ldap_init_getfilter( filterfile )) == NULL ) {
64 strcat( reply, errpreface );
65 strcat( reply, "filter file configuration error. Try again later." );
70 * open connection to LDAP server and bind as dapuser
72 ldp = ldap_init( ldaphost, ldapport );
75 strcat( reply, errpreface );
76 strcat( reply, "X.500 service unavailable. Try again later." );
77 ldap_getfilter_free( lfdp );
81 if ( ldap_simple_bind_s( ldp, dapuser, NULL ) != LDAP_SUCCESS ) {
82 report_ldap_err( ldp, reply );
84 ldap_getfilter_free( lfdp );
89 * set options for search and build filter
91 ldap_set_option(ldp, LDAP_OPT_DEREF, &derefaliases);
92 ldap_set_option(ldp, LDAP_OPT_SIZELIMIT, &sizelimit);
97 for ( lfi = ldap_getfirstfilter( lfdp, "rcpt500", msgp->msg_arg );
98 lfi != NULL; lfi = ldap_getnextfilter( lfdp )) {
99 rc = ldap_search_s( ldp, searchbase, LDAP_SCOPE_SUBTREE,
100 lfi->lfi_filter, attrs, 0, &ldmsgp );
102 if ( rc != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED
103 && rc != LDAP_TIMELIMIT_EXCEEDED ) {
104 report_ldap_err( ldp, reply );
106 ldap_getfilter_free( lfdp );
110 if (( matches = ldap_count_entries( ldp, ldmsgp )) != 0 ) {
114 if ( ldmsgp != NULL ) {
115 ldap_msgfree( ldmsgp );
119 if ( matches == 0 ) {
120 sprintf( buf, "No matches were found for '%s'\n", msgp->msg_arg );
121 strcat( reply, buf );
123 ldap_getfilter_free( lfdp );
128 ldap_get_option(ldp, LDAP_OPT_ERROR_NUMBER, &ld_errno);
130 if ( ld_errno == LDAP_TIMELIMIT_EXCEEDED
131 || ld_errno == LDAP_SIZELIMIT_EXCEEDED ) {
132 strcat( reply, "(Partial results only - a limit was exceeded)\n" );
135 if ( matches <= RCPT500_LISTLIMIT ) {
136 sprintf( buf, "%d %s match%s found for '%s':\n\n", matches,
138 ( matches > 1 ) ? "es" : "", msgp->msg_arg );
139 strcat( reply, buf );
141 if (( rc = ldap_init_templates( templatefile, &tmpllist )) != 0 ) {
142 sprintf( buf, "%s ldap_init_templates( %s ) failed (error %d)\n",
143 errpreface, templatefile, rc );
144 strcat( reply, buf );
147 for ( entry = ldap_first_entry( ldp, ldmsgp ); entry != NULL; ) {
148 dn = ldap_get_dn( ldp, entry );
149 if ( do_read( ldp, dn, reply, tmpllist ) != LDAP_SUCCESS ) {
150 report_ldap_err( ldp, reply );
153 if (( entry = ldap_next_entry( ldp, entry )) != NULL ) {
154 strcat( reply, "\n-------\n\n" );
158 if ( tmpllist != NULL ) {
159 ldap_free_templates( tmpllist );
161 ldap_msgfree( ldmsgp );
164 sprintf( buf, "%d %s matches were found for '%s':\n",
165 matches, lfi->lfi_desc, msgp->msg_arg );
166 strcat( reply, buf );
167 append_entry_list( reply, msgp->msg_arg, ldp, ldmsgp );
168 ldap_msgfree( ldmsgp );
172 ldap_getfilter_free( lfdp );
178 close_ldap( LDAP *ld )
185 append_entry_list( char *reply, char *query, LDAP *ldp, LDAPMessage *ldmsgp )
188 char *dn, *rdn, *s, **title;
191 #ifdef RCPT500_SORT_ATTR
192 ldap_sort_entries( ldp, &ldmsgp, RCPT500_SORT_ATTR, strcasecmp );
195 for ( e = ldap_first_entry( ldp, ldmsgp ); e != NULL;
196 e = ldap_next_entry( ldp, e )) {
197 dn = ldap_get_dn( ldp, e );
198 if (( s = strchr( dn, ',' )) != NULL ) {
201 if (( s = strchr( dn, '=' )) == NULL ) {
209 * if this entry's rdn is an exact match for the thing looked up, we
210 * return the CN that has a digit after it, so that the user is
211 * returned something guaranteed to yield exactly one match if they
212 * pick it from the list and query it
215 if ( strcasecmp( rdn, query ) == 0 ) {
219 if (( cn = ldap_get_values( ldp, e, "cn" )) != NULL ) {
220 for ( i = 0; cn[i] != NULL; i++ ) {
221 if ( isdigit((unsigned char) cn[i][strlen( cn[i] ) - 1])) {
222 rdn = strdup( cn[i] );
227 ldap_value_free( cn );
232 title = ldap_get_values( ldp, e, "title" );
233 sprintf( buf, " %-20s %s\n", rdn, title ? title[0] : "" );
234 strcat( reply, buf );
235 if ( title != NULL ) {
236 ldap_value_free( title );
247 append_text( void *reply, char *text, ber_len_t len )
249 strcat( (char *) reply, text );
255 do_read( LDAP *ldp, char *dn, char *reply, struct ldap_disptmpl *tmpll )
258 static char *maildefvals[] = { "None registered in this service", NULL };
259 static char *defattrs[] = { "mail", NULL };
260 static char **defvals[] = { maildefvals, NULL };
263 rc = ldap_entry2text_search( ldp, dn, searchbase, NULL, tmpll,
264 defattrs, defvals, append_text, (void *)reply, "\n",
265 rdncount, LDAP_DISP_OPT_DOSEARCHACTIONS );
272 report_ldap_err( LDAP *ldp, char *reply )
275 ldap_get_option(ldp, LDAP_OPT_ERROR_NUMBER, &ld_errno);
277 strcat( reply, errpreface );
278 strcat( reply, ldap_err2string( ld_errno ));
279 strcat( reply, "\n" );
284 remove_trailing_space( char *s )
286 char *p = s + strlen( s ) - 1;
288 while ( isspace( (unsigned char) *p ) && p > s ) {