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/signal.h>
18 #include <ac/socket.h>
19 #include <ac/string.h>
20 #include <ac/syslog.h>
22 #include <ac/unistd.h>
25 #ifdef HAVE_SYS_RESOURCE_H
26 #include <sys/resource.h>
34 #include <ldapconfig.h>
36 #define DEFAULT_PORT 79
37 #define DEFAULT_SIZELIMIT 50
40 char *ldaphost = NULL;
42 int deref = LDAP_DEREF_ALWAYS;
43 int sizelimit = DEFAULT_SIZELIMIT;
46 static void print_entry(LDAP *ld, LDAPMessage *e);
51 fprintf( stderr, "usage: %s [-d debuglevel] [-x ldaphost] [-b searchbase] [-a] [-z sizelimit] [-f filterfile] searchstring\r\n", name );
56 main( int argc, char **argv )
59 char *filterfile = FILTERFILE;
63 LDAPMessage *result, *e;
66 static char *attrs[] = { "title", "o", "ou", "postalAddress",
67 "telephoneNumber", "mail",
68 "facsimileTelephoneNumber", NULL };
70 while ( (i = getopt( argc, argv, "ab:d:f:x:z:" )) != EOF ) {
72 case 'a': /* do not deref aliases when searching */
73 deref = LDAP_DEREF_FINDING;
76 case 'b': /* search base */
77 base = strdup( optarg );
80 case 'd': /* turn on debugging */
81 debug = atoi( optarg );
84 case 'f': /* ldap filter file */
85 filterfile = strdup( optarg );
88 case 'x': /* specify ldap host */
89 ldaphost = strdup( optarg );
92 case 'z': /* size limit */
93 sizelimit = atoi( optarg );
101 if ( optind == argc ) {
106 if ( (filtd = ldap_init_getfilter( filterfile )) == NULL ) {
107 fprintf( stderr, "Cannot open filter file (%s)\n", filterfile );
111 if ( (ld = ldap_open( ldaphost, LDAP_PORT )) == NULL ) {
112 perror( "ldap_open" );
116 ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &sizelimit);
117 ldap_set_option(ld, LDAP_OPT_DEREF, &deref);
119 if ( ldap_simple_bind_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
120 fprintf( stderr, "X.500 is temporarily unavailable.\n" );
121 ldap_perror( ld, "ldap_simple_bind_s" );
126 if ( strchr( key, ',' ) != NULL ) {
127 int ld_deref = LDAP_DEREF_FINDING;
128 ldap_set_option(ld, LDAP_OPT_DEREF, &ld_deref);
129 if ( (rc = ldap_ufn_search_s( ld, key, attrs, 0, &result ))
130 != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED &&
131 rc != LDAP_TIMELIMIT_EXCEEDED )
133 ldap_perror( ld, "ldap_ufn_search_s" );
136 matches = ldap_count_entries( ld, result );
138 for ( fi = ldap_getfirstfilter( filtd, "rp500", key );
139 fi != NULL; fi = ldap_getnextfilter( filtd ) ) {
140 if ( (rc = ldap_search_s( ld, base, LDAP_SCOPE_SUBTREE,
141 fi->lfi_filter, attrs, 0, &result ))
142 != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED
143 && rc != LDAP_TIMELIMIT_EXCEEDED )
145 ldap_perror( ld, "ldap_search" );
149 if ( (matches = ldap_count_entries( ld, result )) != 0
150 || rc != LDAP_SUCCESS ) {
156 if ( matches == 1 ) {
157 e = ldap_first_entry( ld, result );
159 print_entry( ld, e );
160 } else if ( matches > 1 ) {
161 fprintf( stderr, "%d %s matches for \"%s\":\r\n", matches,
164 for ( i = 1, e = ldap_first_entry( ld, result ); e != NULL;
165 i++, e = ldap_next_entry( ld, e ) ) {
170 dn = ldap_get_dn( ld, e );
172 if ( (p = strchr( dn, ',' )) != NULL )
174 while ( *rdn && *rdn != '=' )
178 if ( strcasecmp( rdn, buf ) == 0 ) {
182 cn = ldap_get_values( ld, e, "cn" );
183 for ( i = 0; cn[i] != NULL; i++ ) {
184 last = strlen( cn[i] ) - 1;
185 if ( isdigit( cn[i][last] ) ) {
186 rdn = strdup( cn[i] );
192 title = ldap_get_values( ld, e, "title" );
194 fprintf( stderr, " %d: %-20s %s\r\n", i, rdn,
195 title ? title[0] : "" );
196 if ( title != NULL ) {
197 for ( j = 1; title[j] != NULL; j++ )
198 fprintf( stderr, " %-20s %s\r\n",
202 ldap_value_free( title );
206 if ( rc == LDAP_SIZELIMIT_EXCEEDED
207 || rc == LDAP_TIMELIMIT_EXCEEDED ) {
208 fprintf( stderr, "(Size or time limit exceeded)\n" );
211 fprintf( stderr, "Enter the number of the person you want: ");
213 if ( fgets( buf, sizeof(buf), stdin ) == NULL
214 || buf[0] == '\n' ) {
218 e = ldap_first_entry( ld, result );
219 for ( ; i > 0 && e != NULL; i-- ) {
220 e = ldap_next_entry( ld, e );
223 fprintf( stderr, "Invalid choice!\n" );
227 print_entry( ld, e );
228 } else if ( matches == 0 ) {
229 fprintf( stderr, "No matches found for \"%s\"\n", key );
232 fprintf( stderr, "Error return from ldap_count_entries\n" );
241 print_entry( LDAP *ld, LDAPMessage *e )
246 char **title, **dept, **addr, **phone, **fax, **mail;
249 dn = ldap_get_dn( ld, e );
250 ufn = ldap_explode_dn( dn, 0 );
251 rdn = strchr( ufn[0], '=' ) + 1;
253 if ( (fax = ldap_get_values( ld, e, "facsimileTelephoneNumber" ))
255 fprintf( stderr, "Entry \"%s\" has no fax number.\n", dn );
258 faxmail = faxtotpc( fax[0], NULL );
259 title = ldap_get_values( ld, e, "title" );
260 phone = ldap_get_values( ld, e, "telephoneNumber" );
261 mail = ldap_get_values( ld, e, "mail" );
262 dept = ldap_get_values( ld, e, "ou" );
263 addr = ldap_get_values( ld, e, "postalAddress" );
265 for ( i = 0; ufn[i] != NULL; i++ ) {
266 if ( strncmp( "o=", ufn[i], 2 ) == 0 ) {
267 org = strdup( strchr( ufn[i], '=' ) + 1 );
272 printf( "To: %s\n", faxmail );
273 printf( "Subject:\n" );
274 printf( "--------\n" );
275 printf( "#<application/remote-printing\n" );
276 printf( "Recipient: %s\r\n", rdn );
277 printf( "Title: %s\r\n", title ? title[0] : "" );
278 printf( "Organization: %s\r\n", org );
279 printf( "Department: %s\r\n", dept ? dept[0] : "" );
280 printf( "Telephone: %s\r\n", phone ? phone[0] : "" );
281 printf( "Facsimile: %s\r\n", fax ? fax[0] : "" );
282 printf( "Email: %s\r\n", mail ? mail[0] : "" );