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/socket.h>
20 #include <ac/syslog.h>
24 #include <sys/resource.h>
29 #include <ldapconfig.h>
31 #define DEFAULT_PORT 79
32 #define DEFAULT_SIZELIMIT 50
35 char *ldaphost = NULL;
37 int deref = LDAP_DEREF_ALWAYS;
38 int sizelimit = DEFAULT_SIZELIMIT;
41 static void print_entry(LDAP *ld, LDAPMessage *e);
46 fprintf( stderr, "usage: %s [-d debuglevel] [-x ldaphost] [-b searchbase] [-a] [-z sizelimit] [-f filterfile] searchstring\r\n", name );
51 main( int argc, char **argv )
54 char *filterfile = FILTERFILE;
58 LDAPMessage *result, *e;
61 static char *attrs[] = { "title", "o", "ou", "postalAddress",
62 "telephoneNumber", "mail",
63 "facsimileTelephoneNumber", NULL };
67 while ( (i = getopt( argc, argv, "ab:d:f:x:z:" )) != EOF ) {
69 case 'a': /* do not deref aliases when searching */
70 deref = LDAP_DEREF_FINDING;
73 case 'b': /* search base */
74 base = strdup( optarg );
77 case 'd': /* turn on debugging */
78 debug = atoi( optarg );
81 case 'f': /* ldap filter file */
82 filterfile = strdup( optarg );
85 case 'x': /* specify ldap host */
86 ldaphost = strdup( optarg );
89 case 'z': /* size limit */
90 sizelimit = atoi( optarg );
98 if ( optind == argc ) {
103 if ( (filtd = ldap_init_getfilter( filterfile )) == NULL ) {
104 fprintf( stderr, "Cannot open filter file (%s)\n", filterfile );
108 if ( (ld = ldap_open( ldaphost, LDAP_PORT )) == NULL ) {
109 perror( "ldap_open" );
113 ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &sizelimit);
114 ldap_set_option(ld, LDAP_OPT_DEREF, &deref);
116 if ( ldap_simple_bind_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
117 fprintf( stderr, "X.500 is temporarily unavailable.\n" );
118 ldap_perror( ld, "ldap_simple_bind_s" );
123 if ( strchr( key, ',' ) != NULL ) {
124 int ld_deref = LDAP_DEREF_FINDING;
125 ldap_set_option(ld, LDAP_OPT_DEREF, &ld_deref);
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 ) {
179 cn = ldap_get_values( ld, e, "cn" );
180 for ( i = 0; cn[i] != NULL; i++ ) {
181 last = strlen( cn[i] ) - 1;
182 if ( isdigit( cn[i][last] ) ) {
183 rdn = strdup( cn[i] );
189 title = ldap_get_values( ld, e, "title" );
191 fprintf( stderr, " %d: %-20s %s\r\n", i, rdn,
192 title ? title[0] : "" );
193 if ( title != NULL ) {
194 for ( j = 1; title[j] != NULL; j++ )
195 fprintf( stderr, " %-20s %s\r\n",
199 ldap_value_free( title );
203 if ( rc == LDAP_SIZELIMIT_EXCEEDED
204 || rc == LDAP_TIMELIMIT_EXCEEDED ) {
205 fprintf( stderr, "(Size or time limit exceeded)\n" );
208 fprintf( stderr, "Enter the number of the person you want: ");
210 if ( fgets( buf, sizeof(buf), stdin ) == NULL
211 || buf[0] == '\n' ) {
215 e = ldap_first_entry( ld, result );
216 for ( ; i > 0 && e != NULL; i-- ) {
217 e = ldap_next_entry( ld, e );
220 fprintf( stderr, "Invalid choice!\n" );
224 print_entry( ld, e );
225 } else if ( matches == 0 ) {
226 fprintf( stderr, "No matches found for \"%s\"\n", key );
229 fprintf( stderr, "Error return from ldap_count_entries\n" );
238 print_entry( LDAP *ld, LDAPMessage *e )
243 char **title, **dept, **addr, **phone, **fax, **mail;
246 dn = ldap_get_dn( ld, e );
247 ufn = ldap_explode_dn( dn, 0 );
248 rdn = strchr( ufn[0], '=' ) + 1;
250 if ( (fax = ldap_get_values( ld, e, "facsimileTelephoneNumber" ))
252 fprintf( stderr, "Entry \"%s\" has no fax number.\n", dn );
255 faxmail = faxtotpc( fax[0], NULL );
256 title = ldap_get_values( ld, e, "title" );
257 phone = ldap_get_values( ld, e, "telephoneNumber" );
258 mail = ldap_get_values( ld, e, "mail" );
259 dept = ldap_get_values( ld, e, "ou" );
260 addr = ldap_get_values( ld, e, "postalAddress" );
262 for ( i = 0; ufn[i] != NULL; i++ ) {
263 if ( strncmp( "o=", ufn[i], 2 ) == 0 ) {
264 org = strdup( strchr( ufn[i], '=' ) + 1 );
269 printf( "To: %s\n", faxmail );
270 printf( "Subject:\n" );
271 printf( "--------\n" );
272 printf( "#<application/remote-printing\n" );
273 printf( "Recipient: %s\r\n", rdn );
274 printf( "Title: %s\r\n", title ? title[0] : "" );
275 printf( "Organization: %s\r\n", org );
276 printf( "Department: %s\r\n", dept ? dept[0] : "" );
277 printf( "Telephone: %s\r\n", phone ? phone[0] : "" );
278 printf( "Facsimile: %s\r\n", fax ? fax[0] : "" );
279 printf( "Email: %s\r\n", mail ? mail[0] : "" );