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.
17 #include <ac/stdlib.h>
20 #include <ac/signal.h>
21 #include <ac/socket.h>
22 #include <ac/string.h>
23 #include <ac/syslog.h>
25 #include <ac/unistd.h>
28 #ifdef HAVE_SYS_RESOURCE_H
29 #include <sys/resource.h>
37 #include <ldap_defaults.h>
39 #define DEFAULT_PORT 79
40 #define DEFAULT_SIZELIMIT 50
43 char *ldaphost = NULL;
45 int deref = LDAP_DEREF_ALWAYS;
46 int sizelimit = DEFAULT_SIZELIMIT;
49 static void print_entry(LDAP *ld, LDAPMessage *e);
54 fprintf( stderr, "usage: %s [-d debuglevel] [-x ldaphost] [-b searchbase] [-a] [-z sizelimit] [-f filterfile] searchstring\r\n", name );
59 main( int argc, char **argv )
62 char *filterfile = FILTERFILE;
66 LDAPMessage *result, *e;
69 static char *attrs[] = { "title", "o", "ou", "postalAddress",
70 "telephoneNumber", "mail",
71 "facsimileTelephoneNumber", NULL };
73 while ( (i = getopt( argc, argv, "ab:d:f:x:z:" )) != EOF ) {
75 case 'a': /* do not deref aliases when searching */
76 deref = LDAP_DEREF_FINDING;
79 case 'b': /* search base */
80 base = strdup( optarg );
83 case 'd': /* turn on debugging */
84 debug = atoi( optarg );
87 case 'f': /* ldap filter file */
88 filterfile = strdup( optarg );
91 case 'x': /* specify ldap host */
92 ldaphost = strdup( optarg );
95 case 'z': /* size limit */
96 sizelimit = atoi( optarg );
104 if ( optind == argc ) {
109 if ( (filtd = ldap_init_getfilter( filterfile )) == NULL ) {
110 fprintf( stderr, "Cannot open filter file (%s)\n", filterfile );
115 (void) SIGNAL( SIGPIPE, SIG_IGN );
118 if ( (ld = ldap_init( ldaphost, 0 )) == NULL ) {
119 perror( "ldap_init" );
123 ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &sizelimit);
124 ldap_set_option(ld, LDAP_OPT_DEREF, &deref);
126 if ( ldap_simple_bind_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
127 fprintf( stderr, "X.500 is temporarily unavailable.\n" );
128 ldap_perror( ld, "ldap_simple_bind_s" );
133 if ( strchr( key, ',' ) != NULL ) {
134 int ld_deref = LDAP_DEREF_FINDING;
135 ldap_set_option(ld, LDAP_OPT_DEREF, &ld_deref);
136 if ( (rc = ldap_ufn_search_s( ld, key, attrs, 0, &result ))
137 != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED &&
138 rc != LDAP_TIMELIMIT_EXCEEDED )
140 ldap_perror( ld, "ldap_ufn_search_s" );
143 matches = ldap_count_entries( ld, result );
145 for ( fi = ldap_getfirstfilter( filtd, "rp500", key );
146 fi != NULL; fi = ldap_getnextfilter( filtd ) ) {
147 if ( (rc = ldap_search_s( ld, base, LDAP_SCOPE_SUBTREE,
148 fi->lfi_filter, attrs, 0, &result ))
149 != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED
150 && rc != LDAP_TIMELIMIT_EXCEEDED )
152 ldap_perror( ld, "ldap_search" );
156 if ( (matches = ldap_count_entries( ld, result )) != 0
157 || rc != LDAP_SUCCESS ) {
163 if ( matches == 1 ) {
164 e = ldap_first_entry( ld, result );
166 print_entry( ld, e );
167 } else if ( matches > 1 ) {
168 fprintf( stderr, "%d %s matches for \"%s\":\r\n", matches,
171 for ( i = 1, e = ldap_first_entry( ld, result ); e != NULL;
172 i++, e = ldap_next_entry( ld, e ) ) {
177 dn = ldap_get_dn( ld, e );
179 if ( (p = strchr( dn, ',' )) != NULL )
181 while ( *rdn && *rdn != '=' )
185 if ( strcasecmp( rdn, buf ) == 0 ) {
189 cn = ldap_get_values( ld, e, "cn" );
190 for ( i = 0; cn[i] != NULL; i++ ) {
191 last = strlen( cn[i] ) - 1;
192 if ( isdigit((unsigned char) cn[i][last]) ) {
193 rdn = strdup( cn[i] );
199 title = ldap_get_values( ld, e, "title" );
201 fprintf( stderr, " %d: %-20s %s\r\n", i, rdn,
202 title ? title[0] : "" );
203 if ( title != NULL ) {
204 for ( j = 1; title[j] != NULL; j++ )
205 fprintf( stderr, " %-20s %s\r\n",
209 ldap_value_free( title );
213 if ( rc == LDAP_SIZELIMIT_EXCEEDED
214 || rc == LDAP_TIMELIMIT_EXCEEDED ) {
215 fprintf( stderr, "(Size or time limit exceeded)\n" );
218 fprintf( stderr, "Enter the number of the person you want: ");
220 if ( fgets( buf, sizeof(buf), stdin ) == NULL
221 || buf[0] == '\n' ) {
225 e = ldap_first_entry( ld, result );
226 for ( ; i > 0 && e != NULL; i-- ) {
227 e = ldap_next_entry( ld, e );
230 fprintf( stderr, "Invalid choice!\n" );
234 print_entry( ld, e );
235 } else if ( matches == 0 ) {
236 fprintf( stderr, "No matches found for \"%s\"\n", key );
239 fprintf( stderr, "Error return from ldap_count_entries\n" );
248 print_entry( LDAP *ld, LDAPMessage *e )
253 char **title, **dept, **addr, **phone, **fax, **mail;
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], NULL );
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] : "" );