2 * Copyright (c) 1990 Regents of the University of Michigan.
5 * Redistribution and use in source and binary forms are permitted
6 * provided that this notice is preserved and that due credit is given
7 * to the University of Michigan at Ann Arbor. The name of the University
8 * may not be used to endorse or promote products derived from this
9 * software without specific prior written permission. This software
10 * is provided ``as is'' without express or implied warranty.
18 #include <ac/socket.h>
19 #include <ac/string.h>
20 #include <ac/syslog.h>
24 #include <sys/resource.h>
29 #if LDAP_VERSION < LDAP_VERSION3
30 /* quick fix until we have ldap_set_options */
31 #include "../libraries/libldap/ldap-int.h"
34 #include <ldapconfig.h>
36 #define DEFAULT_PORT 79
37 #define DEFAULT_SIZELIMIT 50
40 char *ldaphost = LDAPHOST;
52 fprintf( stderr, "usage: %s [-d debuglevel] [-x ldaphost] [-b searchbase] [-a] [-z sizelimit] [-f filterfile] searchstring\r\n", name );
61 char *filterfile = FILTERFILE;
62 struct timeval timeout;
66 LDAPMessage *result, *e;
69 static char *attrs[] = { "title", "o", "ou", "postalAddress",
70 "telephoneNumber", "mail",
71 "facsimileTelephoneNumber", NULL };
75 deref = LDAP_DEREF_ALWAYS;
76 while ( (i = getopt( argc, argv, "ab:d:f:x:z:" )) != EOF ) {
78 case 'a': /* do not deref aliases when searching */
79 deref = LDAP_DEREF_FINDING;
82 case 'b': /* search base */
83 base = strdup( optarg );
86 case 'd': /* turn on debugging */
87 debug = atoi( optarg );
90 case 'f': /* ldap filter file */
91 filterfile = strdup( optarg );
94 case 'x': /* specify ldap host */
95 ldaphost = strdup( optarg );
98 case 'z': /* size limit */
99 sizelimit = atoi( optarg );
107 if ( optind == argc ) {
112 if ( (filtd = ldap_init_getfilter( filterfile )) == NULL ) {
113 fprintf( stderr, "Cannot open filter file (%s)\n", filterfile );
117 if ( (ld = ldap_open( ldaphost, LDAP_PORT )) == NULL ) {
118 perror( "ldap_open" );
121 ld->ld_sizelimit = sizelimit ? sizelimit : DEFAULT_SIZELIMIT;
122 ld->ld_deref = deref;
124 if ( ldap_simple_bind_s( ld, RP_BINDDN, RP_BIND_CRED ) != LDAP_SUCCESS ) {
125 fprintf( stderr, "X.500 is temporarily unavailable.\n" );
126 ldap_perror( ld, "ldap_simple_bind_s" );
131 if ( strchr( key, ',' ) != NULL ) {
132 ld->ld_deref = LDAP_DEREF_FINDING;
133 if ( (rc = ldap_ufn_search_s( ld, key, attrs, 0, &result ))
134 != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED &&
135 rc != LDAP_TIMELIMIT_EXCEEDED )
137 ldap_perror( ld, "ldap_ufn_search_s" );
140 matches = ldap_count_entries( ld, result );
142 for ( fi = ldap_getfirstfilter( filtd, "rp500", key );
143 fi != NULL; fi = ldap_getnextfilter( filtd ) ) {
144 if ( (rc = ldap_search_s( ld, base, LDAP_SCOPE_SUBTREE,
145 fi->lfi_filter, attrs, 0, &result ))
146 != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED
147 && rc != LDAP_TIMELIMIT_EXCEEDED )
149 ldap_perror( ld, "ldap_search" );
153 if ( (matches = ldap_count_entries( ld, result )) != 0
154 || rc != LDAP_SUCCESS ) {
160 if ( matches == 1 ) {
161 e = ldap_first_entry( ld, result );
163 print_entry( ld, e );
164 } else if ( matches > 1 ) {
165 fprintf( stderr, "%d %s matches for \"%s\":\r\n", matches,
168 for ( i = 1, e = ldap_first_entry( ld, result ); e != NULL;
169 i++, e = ldap_next_entry( ld, e ) ) {
174 dn = ldap_get_dn( ld, e );
176 if ( (p = strchr( dn, ',' )) != NULL )
178 while ( *rdn && *rdn != '=' )
182 if ( strcasecmp( rdn, buf ) == 0 ) {
187 cn = ldap_get_values( ld, e, "cn" );
188 for ( i = 0; cn[i] != NULL; i++ ) {
189 last = strlen( cn[i] ) - 1;
190 if ( isdigit( cn[i][last] ) ) {
191 rdn = strdup( cn[i] );
197 title = ldap_get_values( ld, e, "title" );
199 fprintf( stderr, " %d: %-20s %s\r\n", i, rdn,
200 title ? title[0] : "" );
201 if ( title != NULL ) {
202 for ( j = 1; title[j] != NULL; j++ )
203 fprintf( stderr, " %-20s %s\r\n",
207 ldap_value_free( title );
211 if ( rc == LDAP_SIZELIMIT_EXCEEDED
212 || rc == LDAP_TIMELIMIT_EXCEEDED ) {
213 fprintf( stderr, "(Size or time limit exceeded)\n" );
216 fprintf( stderr, "Enter the number of the person you want: ");
218 if ( fgets( buf, sizeof(buf), stdin ) == NULL
219 || buf[0] == '\n' ) {
223 e = ldap_first_entry( ld, result );
224 for ( ; i > 0 && e != NULL; i-- ) {
225 e = ldap_next_entry( ld, e );
228 fprintf( stderr, "Invalid choice!\n" );
232 print_entry( ld, e );
233 } else if ( matches == 0 ) {
234 fprintf( stderr, "No matches found for \"%s\"\n", key );
237 fprintf( stderr, "Error return from ldap_count_entries\n" );
253 char **title, **dept, **addr, **phone, **fax, **mail;
254 char *faxmail, *org, *faxtotpc();
256 dn = ldap_get_dn( ld, e );
257 ufn = ldap_explode_dn( dn, 0 );
258 rdn = strchr( ufn[0], '=' ) + 1;
260 if ( (fax = ldap_get_values( ld, e, "facsimileTelephoneNumber" ))
262 fprintf( stderr, "Entry \"%s\" has no fax number.\n", dn );
265 faxmail = faxtotpc( fax[0] );
266 title = ldap_get_values( ld, e, "title" );
267 phone = ldap_get_values( ld, e, "telephoneNumber" );
268 mail = ldap_get_values( ld, e, "mail" );
269 dept = ldap_get_values( ld, e, "ou" );
270 addr = ldap_get_values( ld, e, "postalAddress" );
272 for ( i = 0; ufn[i] != NULL; i++ ) {
273 if ( strncmp( "o=", ufn[i], 2 ) == 0 ) {
274 org = strdup( strchr( ufn[i], '=' ) + 1 );
279 printf( "To: %s\n", faxmail );
280 printf( "Subject:\n" );
281 printf( "--------\n" );
282 printf( "#<application/remote-printing\n" );
283 printf( "Recipient: %s\r\n", rdn );
284 printf( "Title: %s\r\n", title ? title[0] : "" );
285 printf( "Organization: %s\r\n", org );
286 printf( "Department: %s\r\n", dept ? dept[0] : "" );
287 printf( "Telephone: %s\r\n", phone ? phone[0] : "" );
288 printf( "Facsimile: %s\r\n", fax ? fax[0] : "" );
289 printf( "Email: %s\r\n", mail ? mail[0] : "" );