2 * query.c: for rcpt500 (X.500 email query responder)
4 * 18 June 1992 by Mark C Smith
5 * Copyright (c) 1992 The Regents of The University of Michigan
13 #include <ac/stdlib.h>
16 #include <ac/string.h>
17 #include <ac/syslog.h>
26 #include "ldapconfig.h"
28 static char buf[ MAXSIZE ];
29 static char *errpreface = "Your query failed: ";
31 static void close_ldap(LDAP *ld);
32 static void append_entry_list(char *rep, char *qu, LDAP *ld, LDAPMessage *msg);
33 static int append_text(void *reply, char *text, int len);
34 static int do_read (LDAP *ld, char *dn, char *rep, struct ldap_disptmpl *tmp);
35 static void report_ldap_err (LDAP *ldp, char *reply);
36 static void remove_trailing_space (char *s);
40 query_cmd( struct msginfo *msgp, char *reply )
43 LDAPMessage *ldmsgp, *entry;
45 int matches, rc, ld_errno, ufn;
48 struct ldap_disptmpl *tmpllist = NULL;
49 static char *attrs[] = { "cn", "title",
50 #ifdef RCPT500_SORT_ATTR
57 if ( msgp->msg_arg == NULL ) {
58 return( help_cmd( msgp, reply ));
61 remove_trailing_space( msgp->msg_arg );
62 if ( *msgp->msg_arg == '\0' ) {
63 return( help_cmd( msgp, reply ));
66 if (( lfdp = ldap_init_getfilter( filterfile )) == NULL ) {
67 strcat( reply, errpreface );
68 strcat( reply, "filter file configuration error. Try again later." );
73 * open connection to LDAP server and bind as dapuser
75 #ifdef LDAP_CONNECTIONLESS
77 ldp = cldap_open( ldaphost, ldapport );
79 #endif /* LDAP_CONNECTIONLESS */
80 ldp = ldap_init( ldaphost, ldapport );
83 strcat( reply, errpreface );
84 strcat( reply, "X.500 service unavailable. Try again later." );
85 ldap_getfilter_free( lfdp );
89 #ifdef LDAP_CONNECTIONLESS
91 #endif /* LDAP_CONNECTIONLESS */
92 if ( ldap_simple_bind_s( ldp, dapuser, NULL ) != LDAP_SUCCESS ) {
93 report_ldap_err( ldp, reply );
95 ldap_getfilter_free( lfdp );
100 * set options for search and build filter
102 ldap_set_option(ldp, LDAP_OPT_DEREF, &derefaliases);
103 ldap_set_option(ldp, LDAP_OPT_SIZELIMIT, &sizelimit);
108 #ifdef LDAP_CONNECTIONLESS
109 if ( !do_cldap && strchr( msgp->msg_arg, ',' ) != NULL ) {
110 #else /* LDAP_CONNECTIONLESS */
111 if ( strchr( msgp->msg_arg, ',' ) != NULL ) {
112 #endif /* LDAP_CONNECTIONLESS */
115 ldap_ufn_setprefix( ldp, searchbase );
116 if (( rc = ldap_ufn_search_s( ldp, msgp->msg_arg, attrs, 0, &ldmsgp ))
117 != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED
118 && rc != LDAP_TIMELIMIT_EXCEEDED ) {
119 report_ldap_err( ldp, reply );
121 ldap_getfilter_free( lfdp );
124 matches = ldap_count_entries( ldp, ldmsgp );
127 #endif /* RCPT500_UFN */
129 for ( lfi = ldap_getfirstfilter( lfdp, "rcpt500", msgp->msg_arg );
130 lfi != NULL; lfi = ldap_getnextfilter( lfdp )) {
131 #ifdef LDAP_CONNECTIONLESS
133 rc = cldap_search_s( ldp, searchbase, LDAP_SCOPE_SUBTREE,
134 lfi->lfi_filter, attrs, 0, &ldmsgp, dapuser );
136 #endif /* LDAP_CONNECTIONLESS */
137 rc = ldap_search_s( ldp, searchbase, LDAP_SCOPE_SUBTREE,
138 lfi->lfi_filter, attrs, 0, &ldmsgp );
140 if ( rc != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED
141 && rc != LDAP_TIMELIMIT_EXCEEDED ) {
142 report_ldap_err( ldp, reply );
144 ldap_getfilter_free( lfdp );
148 if (( matches = ldap_count_entries( ldp, ldmsgp )) != 0 ) {
152 if ( ldmsgp != NULL ) {
153 ldap_msgfree( ldmsgp );
158 #endif /* RCPT500_UFN */
160 if ( matches == 0 ) {
161 sprintf( buf, "No matches were found for '%s'\n", msgp->msg_arg );
162 strcat( reply, buf );
164 ldap_getfilter_free( lfdp );
169 ldap_get_option(ldp, LDAP_OPT_ERROR_NUMBER, &ld_errno);
171 if ( ld_errno == LDAP_TIMELIMIT_EXCEEDED
172 || ld_errno == LDAP_SIZELIMIT_EXCEEDED ) {
173 strcat( reply, "(Partial results only - a limit was exceeded)\n" );
176 if ( matches <= RCPT500_LISTLIMIT ) {
177 sprintf( buf, "%d %s match%s found for '%s':\n\n", matches,
178 ufn ? "UFN" : lfi->lfi_desc,
179 ( matches > 1 ) ? "es" : "", msgp->msg_arg );
180 strcat( reply, buf );
182 if (( rc = ldap_init_templates( templatefile, &tmpllist )) != 0 ) {
183 sprintf( buf, "%s ldap_init_templates( %s ) failed (error %d)\n",
184 errpreface, templatefile, rc );
185 strcat( reply, buf );
188 for ( entry = ldap_first_entry( ldp, ldmsgp ); entry != NULL; ) {
189 dn = ldap_get_dn( ldp, entry );
190 if ( do_read( ldp, dn, reply, tmpllist ) != LDAP_SUCCESS ) {
191 report_ldap_err( ldp, reply );
194 if (( entry = ldap_next_entry( ldp, entry )) != NULL ) {
195 strcat( reply, "\n-------\n\n" );
199 if ( tmpllist != NULL ) {
200 ldap_free_templates( tmpllist );
202 ldap_msgfree( ldmsgp );
205 sprintf( buf, "%d %s matches were found for '%s':\n",
206 matches, ufn ? "UFN" : lfi->lfi_desc, msgp->msg_arg );
207 strcat( reply, buf );
208 append_entry_list( reply, msgp->msg_arg, ldp, ldmsgp );
209 ldap_msgfree( ldmsgp );
213 ldap_getfilter_free( lfdp );
219 close_ldap( LDAP *ld )
221 #ifdef LDAP_CONNECTIONLESS
225 #endif /* LDAP_CONNECTIONLESS */
231 append_entry_list( char *reply, char *query, LDAP *ldp, LDAPMessage *ldmsgp )
234 char *dn, *rdn, *s, **title;
237 #ifdef RCPT500_SORT_ATTR
238 ldap_sort_entries( ldp, &ldmsgp, RCPT500_SORT_ATTR, strcasecmp );
241 for ( e = ldap_first_entry( ldp, ldmsgp ); e != NULL;
242 e = ldap_next_entry( ldp, e )) {
243 dn = ldap_get_dn( ldp, e );
244 if (( s = strchr( dn, ',' )) != NULL ) {
247 if (( s = strchr( dn, '=' )) == NULL ) {
255 * if this entry's rdn is an exact match for the thing looked up, we
256 * return the CN that has a digit after it, so that the user is
257 * returned something guaranteed to yield exactly one match if they
258 * pick it from the list and query it
261 if ( strcasecmp( rdn, query ) == 0 ) {
265 if (( cn = ldap_get_values( ldp, e, "cn" )) != NULL ) {
266 for ( i = 0; cn[i] != NULL; i++ ) {
267 if ( isdigit((unsigned char) cn[i][strlen( cn[i] ) - 1])) {
268 rdn = strdup( cn[i] );
273 ldap_value_free( cn );
278 title = ldap_get_values( ldp, e, "title" );
279 sprintf( buf, " %-20s %s\n", rdn, title ? title[0] : "" );
280 strcat( reply, buf );
281 if ( title != NULL ) {
282 ldap_value_free( title );
293 append_text( void *reply, char *text, int len )
295 strcat( (char *) reply, text );
301 do_read( LDAP *ldp, char *dn, char *reply, struct ldap_disptmpl *tmpll )
304 static char *maildefvals[] = { "None registered in this service", NULL };
305 static char *defattrs[] = { "mail", NULL };
306 static char **defvals[] = { maildefvals, NULL };
309 rc = ldap_entry2text_search( ldp, dn, searchbase, NULL, tmpll,
310 defattrs, defvals, append_text, (void *)reply, "\n",
311 rdncount, LDAP_DISP_OPT_DOSEARCHACTIONS );
318 report_ldap_err( LDAP *ldp, char *reply )
321 ldap_get_option(ldp, LDAP_OPT_ERROR_NUMBER, &ld_errno);
323 strcat( reply, errpreface );
324 strcat( reply, ldap_err2string( ld_errno ));
325 strcat( reply, "\n" );
330 remove_trailing_space( char *s )
332 char *p = s + strlen( s ) - 1;
334 while ( isspace( (unsigned char) *p ) && p > s ) {