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.
19 #include <ac/signal.h>
20 #include <ac/socket.h>
21 #include <ac/string.h>
22 #include <ac/syslog.h>
24 #include <ac/unistd.h>
27 #ifdef HAVE_SYS_RESOURCE_H
28 #include <sys/resource.h>
36 #include <ldapconfig.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 );
113 if ( (ld = ldap_open( ldaphost, LDAP_PORT )) == NULL ) {
114 perror( "ldap_open" );
118 ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &sizelimit);
119 ldap_set_option(ld, LDAP_OPT_DEREF, &deref);
121 if ( ldap_simple_bind_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
122 fprintf( stderr, "X.500 is temporarily unavailable.\n" );
123 ldap_perror( ld, "ldap_simple_bind_s" );
128 if ( strchr( key, ',' ) != NULL ) {
129 int ld_deref = LDAP_DEREF_FINDING;
130 ldap_set_option(ld, LDAP_OPT_DEREF, &ld_deref);
131 if ( (rc = ldap_ufn_search_s( ld, key, attrs, 0, &result ))
132 != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED &&
133 rc != LDAP_TIMELIMIT_EXCEEDED )
135 ldap_perror( ld, "ldap_ufn_search_s" );
138 matches = ldap_count_entries( ld, result );
140 for ( fi = ldap_getfirstfilter( filtd, "rp500", key );
141 fi != NULL; fi = ldap_getnextfilter( filtd ) ) {
142 if ( (rc = ldap_search_s( ld, base, LDAP_SCOPE_SUBTREE,
143 fi->lfi_filter, attrs, 0, &result ))
144 != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED
145 && rc != LDAP_TIMELIMIT_EXCEEDED )
147 ldap_perror( ld, "ldap_search" );
151 if ( (matches = ldap_count_entries( ld, result )) != 0
152 || rc != LDAP_SUCCESS ) {
158 if ( matches == 1 ) {
159 e = ldap_first_entry( ld, result );
161 print_entry( ld, e );
162 } else if ( matches > 1 ) {
163 fprintf( stderr, "%d %s matches for \"%s\":\r\n", matches,
166 for ( i = 1, e = ldap_first_entry( ld, result ); e != NULL;
167 i++, e = ldap_next_entry( ld, e ) ) {
172 dn = ldap_get_dn( ld, e );
174 if ( (p = strchr( dn, ',' )) != NULL )
176 while ( *rdn && *rdn != '=' )
180 if ( strcasecmp( rdn, buf ) == 0 ) {
184 cn = ldap_get_values( ld, e, "cn" );
185 for ( i = 0; cn[i] != NULL; i++ ) {
186 last = strlen( cn[i] ) - 1;
187 if ( isdigit( cn[i][last] ) ) {
188 rdn = strdup( cn[i] );
194 title = ldap_get_values( ld, e, "title" );
196 fprintf( stderr, " %d: %-20s %s\r\n", i, rdn,
197 title ? title[0] : "" );
198 if ( title != NULL ) {
199 for ( j = 1; title[j] != NULL; j++ )
200 fprintf( stderr, " %-20s %s\r\n",
204 ldap_value_free( title );
208 if ( rc == LDAP_SIZELIMIT_EXCEEDED
209 || rc == LDAP_TIMELIMIT_EXCEEDED ) {
210 fprintf( stderr, "(Size or time limit exceeded)\n" );
213 fprintf( stderr, "Enter the number of the person you want: ");
215 if ( fgets( buf, sizeof(buf), stdin ) == NULL
216 || buf[0] == '\n' ) {
220 e = ldap_first_entry( ld, result );
221 for ( ; i > 0 && e != NULL; i-- ) {
222 e = ldap_next_entry( ld, e );
225 fprintf( stderr, "Invalid choice!\n" );
229 print_entry( ld, e );
230 } else if ( matches == 0 ) {
231 fprintf( stderr, "No matches found for \"%s\"\n", key );
234 fprintf( stderr, "Error return from ldap_count_entries\n" );
243 print_entry( LDAP *ld, LDAPMessage *e )
248 char **title, **dept, **addr, **phone, **fax, **mail;
251 dn = ldap_get_dn( ld, e );
252 ufn = ldap_explode_dn( dn, 0 );
253 rdn = strchr( ufn[0], '=' ) + 1;
255 if ( (fax = ldap_get_values( ld, e, "facsimileTelephoneNumber" ))
257 fprintf( stderr, "Entry \"%s\" has no fax number.\n", dn );
260 faxmail = faxtotpc( fax[0], NULL );
261 title = ldap_get_values( ld, e, "title" );
262 phone = ldap_get_values( ld, e, "telephoneNumber" );
263 mail = ldap_get_values( ld, e, "mail" );
264 dept = ldap_get_values( ld, e, "ou" );
265 addr = ldap_get_values( ld, e, "postalAddress" );
267 for ( i = 0; ufn[i] != NULL; i++ ) {
268 if ( strncmp( "o=", ufn[i], 2 ) == 0 ) {
269 org = strdup( strchr( ufn[i], '=' ) + 1 );
274 printf( "To: %s\n", faxmail );
275 printf( "Subject:\n" );
276 printf( "--------\n" );
277 printf( "#<application/remote-printing\n" );
278 printf( "Recipient: %s\r\n", rdn );
279 printf( "Title: %s\r\n", title ? title[0] : "" );
280 printf( "Organization: %s\r\n", org );
281 printf( "Department: %s\r\n", dept ? dept[0] : "" );
282 printf( "Telephone: %s\r\n", phone ? phone[0] : "" );
283 printf( "Facsimile: %s\r\n", fax ? fax[0] : "" );
284 printf( "Email: %s\r\n", mail ? mail[0] : "" );