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.
15 #include <sys/types.h>
17 #include <sys/socket.h>
18 #include <netinet/in.h>
19 #include <arpa/inet.h>
22 #include <sys/resource.h>
29 #include <ldapconfig.h>
31 #define DEFAULT_PORT 79
32 #define DEFAULT_SIZELIMIT 50
35 char *ldaphost = LDAPHOST;
47 fprintf( stderr, "usage: %s [-d debuglevel] [-x ldaphost] [-b searchbase] [-a] [-z sizelimit] [-f filterfile] searchstring\r\n", name );
56 char *filterfile = FILTERFILE;
57 struct timeval timeout;
61 LDAPMessage *result, *e;
64 static char *attrs[] = { "title", "o", "ou", "postalAddress",
65 "telephoneNumber", "mail",
66 "facsimileTelephoneNumber", NULL };
70 deref = LDAP_DEREF_ALWAYS;
71 while ( (i = getopt( argc, argv, "ab:d:f:x:z:" )) != EOF ) {
73 case 'a': /* do not deref aliases when searching */
74 deref = LDAP_DEREF_FINDING;
77 case 'b': /* search base */
78 base = strdup( optarg );
81 case 'd': /* turn on debugging */
82 debug = atoi( optarg );
85 case 'f': /* ldap filter file */
86 filterfile = strdup( optarg );
89 case 'x': /* specify ldap host */
90 ldaphost = strdup( optarg );
93 case 'z': /* size limit */
94 sizelimit = atoi( optarg );
102 if ( optind == argc ) {
107 if ( (filtd = ldap_init_getfilter( filterfile )) == NULL ) {
108 fprintf( stderr, "Cannot open filter file (%s)\n", filterfile );
112 if ( (ld = ldap_open( ldaphost, LDAP_PORT )) == NULL ) {
113 perror( "ldap_open" );
116 ld->ld_sizelimit = sizelimit ? sizelimit : DEFAULT_SIZELIMIT;
117 ld->ld_deref = deref;
119 if ( ldap_simple_bind_s( ld, RP_BINDDN, RP_BIND_CRED ) != 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 ld->ld_deref = LDAP_DEREF_FINDING;
128 if ( (rc = ldap_ufn_search_s( ld, key, attrs, 0, &result ))
129 != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED &&
130 rc != LDAP_TIMELIMIT_EXCEEDED )
132 ldap_perror( ld, "ldap_ufn_search_s" );
135 matches = ldap_count_entries( ld, result );
137 for ( fi = ldap_getfirstfilter( filtd, "rp500", key );
138 fi != NULL; fi = ldap_getnextfilter( filtd ) ) {
139 if ( (rc = ldap_search_s( ld, base, LDAP_SCOPE_SUBTREE,
140 fi->lfi_filter, attrs, 0, &result ))
141 != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED
142 && rc != LDAP_TIMELIMIT_EXCEEDED )
144 ldap_perror( ld, "ldap_search" );
148 if ( (matches = ldap_count_entries( ld, result )) != 0
149 || rc != LDAP_SUCCESS ) {
155 if ( matches == 1 ) {
156 e = ldap_first_entry( ld, result );
158 print_entry( ld, e );
159 } else if ( matches > 1 ) {
160 fprintf( stderr, "%d %s matches for \"%s\":\r\n", matches,
163 for ( i = 1, e = ldap_first_entry( ld, result ); e != NULL;
164 i++, e = ldap_next_entry( ld, e ) ) {
169 dn = ldap_get_dn( ld, e );
171 if ( (p = strchr( dn, ',' )) != NULL )
173 while ( *rdn && *rdn != '=' )
177 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" );
248 char **title, **dept, **addr, **phone, **fax, **mail;
249 char *faxmail, *org, *faxtotpc();
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] );
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] : "" );