3 * Copyright (c) 1990 Regents of the University of Michigan.
6 * Redistribution and use in source and binary forms are permitted
7 * provided that this notice is preserved and that due credit is given
8 * to the University of Michigan at Ann Arbor. The name of the University
9 * may not be used to endorse or promote products derived from this
10 * software without specific prior written permission. This software
11 * is provided ``as is'' without express or implied warranty.
18 #include <ac/stdlib.h>
21 #include <ac/signal.h>
22 #include <ac/socket.h>
23 #include <ac/string.h>
24 #include <ac/syslog.h>
26 #include <ac/unistd.h>
29 #ifdef HAVE_SYS_RESOURCE_H
30 #include <sys/resource.h>
36 #include "ldap_defaults.h"
38 #define DEFAULT_PORT 79
39 #define DEFAULT_SIZELIMIT 50
42 char *ldaphost = NULL;
44 int deref = LDAP_DEREF_ALWAYS;
45 int sizelimit = DEFAULT_SIZELIMIT;
48 static void print_entry(LDAP *ld, LDAPMessage *e);
53 fprintf( stderr, "usage: %s [-d debuglevel] [-x ldaphost] [-b searchbase] [-a] [-z sizelimit] [-f filterfile] searchstring\r\n", name );
58 main( int argc, char **argv )
61 char *filterfile = FILTERFILE;
65 LDAPMessage *result, *e;
68 static char *attrs[] = { "title", "o", "ou", "postalAddress",
69 "telephoneNumber", "mail",
70 "facsimileTelephoneNumber", NULL };
72 while ( (i = getopt( argc, argv, "ab:d:f:x:z:" )) != EOF ) {
74 case 'a': /* do not deref aliases when searching */
75 deref = LDAP_DEREF_FINDING;
78 case 'b': /* search base */
79 base = strdup( optarg );
82 case 'd': /* turn on debugging */
83 debug = atoi( optarg );
86 case 'f': /* ldap filter file */
87 filterfile = strdup( optarg );
90 case 'x': /* specify ldap host */
91 ldaphost = strdup( optarg );
94 case 'z': /* size limit */
95 sizelimit = atoi( optarg );
103 if ( optind == argc ) {
108 if ( (filtd = ldap_init_getfilter( filterfile )) == NULL ) {
109 fprintf( stderr, "Cannot open filter file (%s)\n", filterfile );
114 (void) SIGNAL( SIGPIPE, SIG_IGN );
117 if ( (ld = ldap_init( ldaphost, 0 )) == NULL ) {
118 perror( "ldap_init" );
122 ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &sizelimit);
123 ldap_set_option(ld, LDAP_OPT_DEREF, &deref);
125 if ( ldap_simple_bind_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
126 fprintf( stderr, "X.500 is temporarily unavailable.\n" );
127 ldap_perror( ld, "ldap_simple_bind_s" );
132 if ( strchr( key, ',' ) != NULL ) {
133 int ld_deref = LDAP_DEREF_FINDING;
134 ldap_set_option(ld, LDAP_OPT_DEREF, &ld_deref);
135 if ( (rc = ldap_ufn_search_s( ld, key, attrs, 0, &result ))
136 != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED &&
137 rc != LDAP_TIMELIMIT_EXCEEDED )
139 ldap_perror( ld, "ldap_ufn_search_s" );
142 matches = ldap_count_entries( ld, result );
144 for ( fi = ldap_getfirstfilter( filtd, "rp500", key );
145 fi != NULL; fi = ldap_getnextfilter( filtd ) ) {
146 if ( (rc = ldap_search_s( ld, base, LDAP_SCOPE_SUBTREE,
147 fi->lfi_filter, attrs, 0, &result ))
148 != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED
149 && rc != LDAP_TIMELIMIT_EXCEEDED )
151 ldap_perror( ld, "ldap_search" );
155 if ( (matches = ldap_count_entries( ld, result )) != 0
156 || rc != LDAP_SUCCESS ) {
162 if ( matches == 1 ) {
163 e = ldap_first_entry( ld, result );
165 print_entry( ld, e );
166 } else if ( matches > 1 ) {
167 fprintf( stderr, "%d %s matches for \"%s\":\r\n", matches,
170 for ( i = 1, e = ldap_first_entry( ld, result ); e != NULL;
171 i++, e = ldap_next_entry( ld, e ) ) {
176 dn = ldap_get_dn( ld, e );
178 if ( (p = strchr( dn, ',' )) != NULL )
180 while ( *rdn && *rdn != '=' )
184 if ( strcasecmp( rdn, buf ) == 0 ) {
188 cn = ldap_get_values( ld, e, "cn" );
189 for ( i = 0; cn[i] != NULL; i++ ) {
190 last = strlen( cn[i] ) - 1;
191 if ( isdigit((unsigned char) cn[i][last]) ) {
192 rdn = strdup( cn[i] );
198 title = ldap_get_values( ld, e, "title" );
200 fprintf( stderr, " %d: %-20s %s\r\n", i, rdn,
201 title ? title[0] : "" );
202 if ( title != NULL ) {
203 for ( j = 1; title[j] != NULL; j++ )
204 fprintf( stderr, " %-20s %s\r\n",
208 ldap_value_free( title );
212 if ( rc == LDAP_SIZELIMIT_EXCEEDED
213 || rc == LDAP_TIMELIMIT_EXCEEDED ) {
214 fprintf( stderr, "(Size or time limit exceeded)\n" );
217 fprintf( stderr, "Enter the number of the person you want: ");
219 if ( fgets( buf, sizeof(buf), stdin ) == NULL
220 || buf[0] == '\n' ) {
221 exit( EXIT_FAILURE );
224 e = ldap_first_entry( ld, result );
225 for ( ; i > 0 && e != NULL; i-- ) {
226 e = ldap_next_entry( ld, e );
229 fprintf( stderr, "Invalid choice!\n" );
230 exit( EXIT_FAILURE );
233 print_entry( ld, e );
234 } else if ( matches == 0 ) {
235 fprintf( stderr, "No matches found for \"%s\"\n", key );
236 exit( EXIT_FAILURE );
238 fprintf( stderr, "Error return from ldap_count_entries\n" );
247 print_entry( LDAP *ld, LDAPMessage *e )
252 char **title, **dept, **addr, **phone, **fax, **mail;
255 dn = ldap_get_dn( ld, e );
256 ufn = ldap_explode_dn( dn, 0 );
257 rdn = strchr( ufn[0], '=' ) + 1;
259 if ( (fax = ldap_get_values( ld, e, "facsimileTelephoneNumber" ))
261 fprintf( stderr, "Entry \"%s\" has no fax number.\n", dn );
262 exit( EXIT_FAILURE );
264 faxmail = faxtotpc( fax[0], NULL );
265 title = ldap_get_values( ld, e, "title" );
266 phone = ldap_get_values( ld, e, "telephoneNumber" );
267 mail = ldap_get_values( ld, e, "mail" );
268 dept = ldap_get_values( ld, e, "ou" );
269 addr = ldap_get_values( ld, e, "postalAddress" );
271 for ( i = 0; ufn[i] != NULL; i++ ) {
272 if ( strncmp( "o=", ufn[i], 2 ) == 0 ) {
273 org = strdup( strchr( ufn[i], '=' ) + 1 );
278 printf( "To: %s\n", faxmail );
279 printf( "Subject:\n" );
280 printf( "--------\n" );
281 printf( "#<application/remote-printing\n" );
282 printf( "Recipient: %s\r\n", rdn );
283 printf( "Title: %s\r\n", title ? title[0] : "" );
284 printf( "Organization: %s\r\n", org );
285 printf( "Department: %s\r\n", dept ? dept[0] : "" );
286 printf( "Telephone: %s\r\n", phone ? phone[0] : "" );
287 printf( "Facsimile: %s\r\n", fax ? fax[0] : "" );
288 printf( "Email: %s\r\n", mail ? mail[0] : "" );