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>
25 #include <ldapconfig.h>
29 #define DEFAULT_PORT 79
30 #define DEFAULT_SIZELIMIT 50
33 char *ldaphost = LDAPHOST;
34 char *base = DEFAULT_BASE;
45 fprintf( stderr, "usage: %s [-d debuglevel] [-x ldaphost] [-b searchbase] [-a] [-z sizelimit] [-f filterfile] searchstring\r\n", name );
54 char *filterfile = FILTERFILE;
55 struct timeval timeout;
59 LDAPMessage *result, *e;
62 static char *attrs[] = { "title", "o", "ou", "postalAddress",
63 "telephoneNumber", "mail",
64 "facsimileTelephoneNumber", NULL };
68 deref = LDAP_DEREF_ALWAYS;
69 while ( (i = getopt( argc, argv, "ab:d:f:x:z:" )) != EOF ) {
71 case 'a': /* do not deref aliases when searching */
72 deref = LDAP_DEREF_FINDING;
75 case 'b': /* search base */
76 base = strdup( optarg );
79 case 'd': /* turn on debugging */
80 debug = atoi( optarg );
83 case 'f': /* ldap filter file */
84 filterfile = strdup( optarg );
87 case 'x': /* specify ldap host */
88 ldaphost = strdup( optarg );
91 case 'z': /* size limit */
92 sizelimit = atoi( optarg );
100 if ( optind == argc ) {
105 if ( (filtd = ldap_init_getfilter( filterfile )) == NULL ) {
106 fprintf( stderr, "Cannot open filter file (%s)\n", filterfile );
110 if ( (ld = ldap_open( ldaphost, LDAP_PORT )) == NULL ) {
111 perror( "ldap_open" );
114 ld->ld_sizelimit = sizelimit ? sizelimit : DEFAULT_SIZELIMIT;
115 ld->ld_deref = deref;
117 if ( ldap_simple_bind_s( ld, RP_BINDDN, NULL ) != LDAP_SUCCESS ) {
118 fprintf( stderr, "X.500 is temporarily unavailable.\n" );
119 ldap_perror( ld, "ldap_simple_bind_s" );
124 if ( strchr( key, ',' ) != NULL ) {
125 ld->ld_deref = LDAP_DEREF_FINDING;
126 if ( (rc = ldap_ufn_search_s( ld, key, attrs, 0, &result ))
127 != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED &&
128 rc != LDAP_TIMELIMIT_EXCEEDED )
130 ldap_perror( ld, "ldap_ufn_search_s" );
133 matches = ldap_count_entries( ld, result );
135 for ( fi = ldap_getfirstfilter( filtd, "rp500", key );
136 fi != NULL; fi = ldap_getnextfilter( filtd ) ) {
137 if ( (rc = ldap_search_s( ld, base, LDAP_SCOPE_SUBTREE,
138 fi->lfi_filter, attrs, 0, &result ))
139 != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED
140 && rc != LDAP_TIMELIMIT_EXCEEDED )
142 ldap_perror( ld, "ldap_search" );
146 if ( (matches = ldap_count_entries( ld, result )) != 0
147 || rc != LDAP_SUCCESS ) {
153 if ( matches == 1 ) {
154 e = ldap_first_entry( ld, result );
156 print_entry( ld, e );
157 } else if ( matches > 1 ) {
158 fprintf( stderr, "%d %s matches for \"%s\":\r\n", matches,
161 for ( i = 1, e = ldap_first_entry( ld, result ); e != NULL;
162 i++, e = ldap_next_entry( ld, e ) ) {
167 dn = ldap_get_dn( ld, e );
169 if ( (p = strchr( dn, ',' )) != NULL )
171 while ( *rdn && *rdn != '=' )
175 if ( strcasecmp( rdn, buf ) == 0 ) {
180 cn = ldap_get_values( ld, e, "cn" );
181 for ( i = 0; cn[i] != NULL; i++ ) {
182 last = strlen( cn[i] ) - 1;
183 if ( isdigit( cn[i][last] ) ) {
184 rdn = strdup( cn[i] );
190 title = ldap_get_values( ld, e, "title" );
192 fprintf( stderr, " %d: %-20s %s\r\n", i, rdn,
193 title ? title[0] : "" );
194 if ( title != NULL ) {
195 for ( j = 1; title[j] != NULL; j++ )
196 fprintf( stderr, " %-20s %s\r\n",
200 ldap_value_free( title );
204 if ( rc == LDAP_SIZELIMIT_EXCEEDED
205 || rc == LDAP_TIMELIMIT_EXCEEDED ) {
206 fprintf( stderr, "(Size or time limit exceeded)\n" );
209 fprintf( stderr, "Enter the number of the person you want: ");
211 if ( fgets( buf, sizeof(buf), stdin ) == NULL
212 || buf[0] == '\n' ) {
216 e = ldap_first_entry( ld, result );
217 for ( ; i > 0 && e != NULL; i-- ) {
218 e = ldap_next_entry( ld, e );
221 fprintf( stderr, "Invalid choice!\n" );
225 print_entry( ld, e );
226 } else if ( matches == 0 ) {
227 fprintf( stderr, "No matches found for \"%s\"\n", key );
230 fprintf( stderr, "Error return from ldap_count_entries\n" );
246 char **title, **dept, **addr, **phone, **fax, **mail;
247 char *faxmail, *org, *faxtotpc();
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] );
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] : "" );