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, ufn;
47 struct ldap_disptmpl *tmpllist = NULL;
48 static char *attrs[] = { "cn", "title",
49 #ifdef RCPT500_SORT_ATTR
56 if ( msgp->msg_arg == NULL ) {
57 return( help_cmd( msgp, reply ));
60 remove_trailing_space( msgp->msg_arg );
61 if ( *msgp->msg_arg == '\0' ) {
62 return( help_cmd( msgp, reply ));
65 if (( lfdp = ldap_init_getfilter( filterfile )) == NULL ) {
66 strcat( reply, errpreface );
67 strcat( reply, "filter file configuration error. Try again later." );
72 * open connection to LDAP server and bind as dapuser
74 ldp = ldap_init( ldaphost, ldapport );
77 strcat( reply, errpreface );
78 strcat( reply, "X.500 service unavailable. Try again later." );
79 ldap_getfilter_free( lfdp );
83 if ( ldap_simple_bind_s( ldp, dapuser, NULL ) != LDAP_SUCCESS ) {
84 report_ldap_err( ldp, reply );
86 ldap_getfilter_free( lfdp );
91 * set options for search and build filter
93 ldap_set_option(ldp, LDAP_OPT_DEREF, &derefaliases);
94 ldap_set_option(ldp, LDAP_OPT_SIZELIMIT, &sizelimit);
99 if ( strchr( msgp->msg_arg, ',' ) != NULL ) {
102 ldap_ufn_setprefix( ldp, searchbase );
103 if (( rc = ldap_ufn_search_s( ldp, msgp->msg_arg, attrs, 0, &ldmsgp ))
104 != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED
105 && rc != LDAP_TIMELIMIT_EXCEEDED ) {
106 report_ldap_err( ldp, reply );
108 ldap_getfilter_free( lfdp );
111 matches = ldap_count_entries( ldp, ldmsgp );
114 #endif /* RCPT500_UFN */
116 for ( lfi = ldap_getfirstfilter( lfdp, "rcpt500", msgp->msg_arg );
117 lfi != NULL; lfi = ldap_getnextfilter( lfdp )) {
118 rc = ldap_search_s( ldp, searchbase, LDAP_SCOPE_SUBTREE,
119 lfi->lfi_filter, attrs, 0, &ldmsgp );
121 if ( rc != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED
122 && rc != LDAP_TIMELIMIT_EXCEEDED ) {
123 report_ldap_err( ldp, reply );
125 ldap_getfilter_free( lfdp );
129 if (( matches = ldap_count_entries( ldp, ldmsgp )) != 0 ) {
133 if ( ldmsgp != NULL ) {
134 ldap_msgfree( ldmsgp );
139 #endif /* RCPT500_UFN */
141 if ( matches == 0 ) {
142 sprintf( buf, "No matches were found for '%s'\n", msgp->msg_arg );
143 strcat( reply, buf );
145 ldap_getfilter_free( lfdp );
150 ldap_get_option(ldp, LDAP_OPT_ERROR_NUMBER, &ld_errno);
152 if ( ld_errno == LDAP_TIMELIMIT_EXCEEDED
153 || ld_errno == LDAP_SIZELIMIT_EXCEEDED ) {
154 strcat( reply, "(Partial results only - a limit was exceeded)\n" );
157 if ( matches <= RCPT500_LISTLIMIT ) {
158 sprintf( buf, "%d %s match%s found for '%s':\n\n", matches,
159 ufn ? "UFN" : lfi->lfi_desc,
160 ( matches > 1 ) ? "es" : "", msgp->msg_arg );
161 strcat( reply, buf );
163 if (( rc = ldap_init_templates( templatefile, &tmpllist )) != 0 ) {
164 sprintf( buf, "%s ldap_init_templates( %s ) failed (error %d)\n",
165 errpreface, templatefile, rc );
166 strcat( reply, buf );
169 for ( entry = ldap_first_entry( ldp, ldmsgp ); entry != NULL; ) {
170 dn = ldap_get_dn( ldp, entry );
171 if ( do_read( ldp, dn, reply, tmpllist ) != LDAP_SUCCESS ) {
172 report_ldap_err( ldp, reply );
175 if (( entry = ldap_next_entry( ldp, entry )) != NULL ) {
176 strcat( reply, "\n-------\n\n" );
180 if ( tmpllist != NULL ) {
181 ldap_free_templates( tmpllist );
183 ldap_msgfree( ldmsgp );
186 sprintf( buf, "%d %s matches were found for '%s':\n",
187 matches, ufn ? "UFN" : lfi->lfi_desc, msgp->msg_arg );
188 strcat( reply, buf );
189 append_entry_list( reply, msgp->msg_arg, ldp, ldmsgp );
190 ldap_msgfree( ldmsgp );
194 ldap_getfilter_free( lfdp );
200 close_ldap( LDAP *ld )
207 append_entry_list( char *reply, char *query, LDAP *ldp, LDAPMessage *ldmsgp )
210 char *dn, *rdn, *s, **title;
213 #ifdef RCPT500_SORT_ATTR
214 ldap_sort_entries( ldp, &ldmsgp, RCPT500_SORT_ATTR, strcasecmp );
217 for ( e = ldap_first_entry( ldp, ldmsgp ); e != NULL;
218 e = ldap_next_entry( ldp, e )) {
219 dn = ldap_get_dn( ldp, e );
220 if (( s = strchr( dn, ',' )) != NULL ) {
223 if (( s = strchr( dn, '=' )) == NULL ) {
231 * if this entry's rdn is an exact match for the thing looked up, we
232 * return the CN that has a digit after it, so that the user is
233 * returned something guaranteed to yield exactly one match if they
234 * pick it from the list and query it
237 if ( strcasecmp( rdn, query ) == 0 ) {
241 if (( cn = ldap_get_values( ldp, e, "cn" )) != NULL ) {
242 for ( i = 0; cn[i] != NULL; i++ ) {
243 if ( isdigit((unsigned char) cn[i][strlen( cn[i] ) - 1])) {
244 rdn = strdup( cn[i] );
249 ldap_value_free( cn );
254 title = ldap_get_values( ldp, e, "title" );
255 sprintf( buf, " %-20s %s\n", rdn, title ? title[0] : "" );
256 strcat( reply, buf );
257 if ( title != NULL ) {
258 ldap_value_free( title );
269 append_text( void *reply, char *text, ber_len_t len )
271 strcat( (char *) reply, text );
277 do_read( LDAP *ldp, char *dn, char *reply, struct ldap_disptmpl *tmpll )
280 static char *maildefvals[] = { "None registered in this service", NULL };
281 static char *defattrs[] = { "mail", NULL };
282 static char **defvals[] = { maildefvals, NULL };
285 rc = ldap_entry2text_search( ldp, dn, searchbase, NULL, tmpll,
286 defattrs, defvals, append_text, (void *)reply, "\n",
287 rdncount, LDAP_DISP_OPT_DOSEARCHACTIONS );
294 report_ldap_err( LDAP *ldp, char *reply )
297 ldap_get_option(ldp, LDAP_OPT_ERROR_NUMBER, &ld_errno);
299 strcat( reply, errpreface );
300 strcat( reply, ldap_err2string( ld_errno ));
301 strcat( reply, "\n" );
306 remove_trailing_space( char *s )
308 char *p = s + strlen( s ) - 1;
310 while ( isspace( (unsigned char) *p ) && p > s ) {