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
11 #include <ac/stdlib.h>
14 #include <ac/string.h>
15 #include <ac/syslog.h>
24 #include "ldap_defaults.h"
26 static char buf[ MAXSIZE ];
27 static char *errpreface = "Your query failed: ";
29 static void close_ldap(LDAP *ld);
30 static void append_entry_list(char *rep, char *qu, LDAP *ld, LDAPMessage *msg);
31 static int append_text(void *reply, char *text, ber_len_t len);
32 static int do_read (LDAP *ld, char *dn, char *rep, struct ldap_disptmpl *tmp);
33 static void report_ldap_err (LDAP *ldp, char *reply);
34 static void remove_trailing_space (char *s);
38 query_cmd( struct msginfo *msgp, char *reply )
41 LDAPMessage *ldmsgp, *entry;
43 int matches, rc, ld_errno, ufn;
46 struct ldap_disptmpl *tmpllist = NULL;
47 static char *attrs[] = { "cn", "title",
48 #ifdef RCPT500_SORT_ATTR
55 if ( msgp->msg_arg == NULL ) {
56 return( help_cmd( msgp, reply ));
59 remove_trailing_space( msgp->msg_arg );
60 if ( *msgp->msg_arg == '\0' ) {
61 return( help_cmd( msgp, reply ));
64 if (( lfdp = ldap_init_getfilter( filterfile )) == NULL ) {
65 strcat( reply, errpreface );
66 strcat( reply, "filter file configuration error. Try again later." );
71 * open connection to LDAP server and bind as dapuser
73 #ifdef LDAP_CONNECTIONLESS
75 ldp = cldap_open( ldaphost, ldapport );
77 #endif /* LDAP_CONNECTIONLESS */
78 ldp = ldap_init( ldaphost, ldapport );
81 strcat( reply, errpreface );
82 strcat( reply, "X.500 service unavailable. Try again later." );
83 ldap_getfilter_free( lfdp );
87 #ifdef LDAP_CONNECTIONLESS
89 #endif /* LDAP_CONNECTIONLESS */
90 if ( ldap_simple_bind_s( ldp, dapuser, NULL ) != LDAP_SUCCESS ) {
91 report_ldap_err( ldp, reply );
93 ldap_getfilter_free( lfdp );
98 * set options for search and build filter
100 ldap_set_option(ldp, LDAP_OPT_DEREF, &derefaliases);
101 ldap_set_option(ldp, LDAP_OPT_SIZELIMIT, &sizelimit);
106 #ifdef LDAP_CONNECTIONLESS
107 if ( !do_cldap && strchr( msgp->msg_arg, ',' ) != NULL ) {
108 #else /* LDAP_CONNECTIONLESS */
109 if ( strchr( msgp->msg_arg, ',' ) != NULL ) {
110 #endif /* LDAP_CONNECTIONLESS */
113 ldap_ufn_setprefix( ldp, searchbase );
114 if (( rc = ldap_ufn_search_s( ldp, msgp->msg_arg, attrs, 0, &ldmsgp ))
115 != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED
116 && rc != LDAP_TIMELIMIT_EXCEEDED ) {
117 report_ldap_err( ldp, reply );
119 ldap_getfilter_free( lfdp );
122 matches = ldap_count_entries( ldp, ldmsgp );
125 #endif /* RCPT500_UFN */
127 for ( lfi = ldap_getfirstfilter( lfdp, "rcpt500", msgp->msg_arg );
128 lfi != NULL; lfi = ldap_getnextfilter( lfdp )) {
129 #ifdef LDAP_CONNECTIONLESS
131 rc = cldap_search_s( ldp, searchbase, LDAP_SCOPE_SUBTREE,
132 lfi->lfi_filter, attrs, 0, &ldmsgp, dapuser );
134 #endif /* LDAP_CONNECTIONLESS */
135 rc = ldap_search_s( ldp, searchbase, LDAP_SCOPE_SUBTREE,
136 lfi->lfi_filter, attrs, 0, &ldmsgp );
138 if ( rc != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED
139 && rc != LDAP_TIMELIMIT_EXCEEDED ) {
140 report_ldap_err( ldp, reply );
142 ldap_getfilter_free( lfdp );
146 if (( matches = ldap_count_entries( ldp, ldmsgp )) != 0 ) {
150 if ( ldmsgp != NULL ) {
151 ldap_msgfree( ldmsgp );
156 #endif /* RCPT500_UFN */
158 if ( matches == 0 ) {
159 sprintf( buf, "No matches were found for '%s'\n", msgp->msg_arg );
160 strcat( reply, buf );
162 ldap_getfilter_free( lfdp );
167 ldap_get_option(ldp, LDAP_OPT_ERROR_NUMBER, &ld_errno);
169 if ( ld_errno == LDAP_TIMELIMIT_EXCEEDED
170 || ld_errno == LDAP_SIZELIMIT_EXCEEDED ) {
171 strcat( reply, "(Partial results only - a limit was exceeded)\n" );
174 if ( matches <= RCPT500_LISTLIMIT ) {
175 sprintf( buf, "%d %s match%s found for '%s':\n\n", matches,
176 ufn ? "UFN" : lfi->lfi_desc,
177 ( matches > 1 ) ? "es" : "", msgp->msg_arg );
178 strcat( reply, buf );
180 if (( rc = ldap_init_templates( templatefile, &tmpllist )) != 0 ) {
181 sprintf( buf, "%s ldap_init_templates( %s ) failed (error %d)\n",
182 errpreface, templatefile, rc );
183 strcat( reply, buf );
186 for ( entry = ldap_first_entry( ldp, ldmsgp ); entry != NULL; ) {
187 dn = ldap_get_dn( ldp, entry );
188 if ( do_read( ldp, dn, reply, tmpllist ) != LDAP_SUCCESS ) {
189 report_ldap_err( ldp, reply );
192 if (( entry = ldap_next_entry( ldp, entry )) != NULL ) {
193 strcat( reply, "\n-------\n\n" );
197 if ( tmpllist != NULL ) {
198 ldap_free_templates( tmpllist );
200 ldap_msgfree( ldmsgp );
203 sprintf( buf, "%d %s matches were found for '%s':\n",
204 matches, ufn ? "UFN" : lfi->lfi_desc, msgp->msg_arg );
205 strcat( reply, buf );
206 append_entry_list( reply, msgp->msg_arg, ldp, ldmsgp );
207 ldap_msgfree( ldmsgp );
211 ldap_getfilter_free( lfdp );
217 close_ldap( LDAP *ld )
219 #ifdef LDAP_CONNECTIONLESS
223 #endif /* LDAP_CONNECTIONLESS */
229 append_entry_list( char *reply, char *query, LDAP *ldp, LDAPMessage *ldmsgp )
232 char *dn, *rdn, *s, **title;
235 #ifdef RCPT500_SORT_ATTR
236 ldap_sort_entries( ldp, &ldmsgp, RCPT500_SORT_ATTR, strcasecmp );
239 for ( e = ldap_first_entry( ldp, ldmsgp ); e != NULL;
240 e = ldap_next_entry( ldp, e )) {
241 dn = ldap_get_dn( ldp, e );
242 if (( s = strchr( dn, ',' )) != NULL ) {
245 if (( s = strchr( dn, '=' )) == NULL ) {
253 * if this entry's rdn is an exact match for the thing looked up, we
254 * return the CN that has a digit after it, so that the user is
255 * returned something guaranteed to yield exactly one match if they
256 * pick it from the list and query it
259 if ( strcasecmp( rdn, query ) == 0 ) {
263 if (( cn = ldap_get_values( ldp, e, "cn" )) != NULL ) {
264 for ( i = 0; cn[i] != NULL; i++ ) {
265 if ( isdigit((unsigned char) cn[i][strlen( cn[i] ) - 1])) {
266 rdn = strdup( cn[i] );
271 ldap_value_free( cn );
276 title = ldap_get_values( ldp, e, "title" );
277 sprintf( buf, " %-20s %s\n", rdn, title ? title[0] : "" );
278 strcat( reply, buf );
279 if ( title != NULL ) {
280 ldap_value_free( title );
291 append_text( void *reply, char *text, ber_len_t len )
293 strcat( (char *) reply, text );
299 do_read( LDAP *ldp, char *dn, char *reply, struct ldap_disptmpl *tmpll )
302 static char *maildefvals[] = { "None registered in this service", NULL };
303 static char *defattrs[] = { "mail", NULL };
304 static char **defvals[] = { maildefvals, NULL };
307 rc = ldap_entry2text_search( ldp, dn, searchbase, NULL, tmpll,
308 defattrs, defvals, append_text, (void *)reply, "\n",
309 rdncount, LDAP_DISP_OPT_DOSEARCHACTIONS );
316 report_ldap_err( LDAP *ldp, char *reply )
319 ldap_get_option(ldp, LDAP_OPT_ERROR_NUMBER, &ld_errno);
321 strcat( reply, errpreface );
322 strcat( reply, ldap_err2string( ld_errno ));
323 strcat( reply, "\n" );
328 remove_trailing_space( char *s )
330 char *p = s + strlen( s ) - 1;
332 while ( isspace( (unsigned char) *p ) && p > s ) {