]> git.sur5r.net Git - openldap/blob - clients/fax500/rp500.c
Don't automatically index objectlclass eq, ineffective more than not
[openldap] / clients / fax500 / rp500.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright (c) 1990 Regents of the University of Michigan.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms are permitted
7  * provided that this notice is preserved and that due credit is given
8  * to the University of Michigan at Ann Arbor. The name of the University
9  * may not be used to endorse or promote products derived from this
10  * software without specific prior written permission. This software
11  * is provided ``as is'' without express or implied warranty.
12  */
13
14 #include "portable.h"
15
16 #include <stdio.h>
17
18 #include <ac/stdlib.h>
19
20 #include <ac/ctype.h>
21 #include <ac/signal.h>
22 #include <ac/socket.h>
23 #include <ac/string.h>
24 #include <ac/syslog.h>
25 #include <ac/time.h>
26 #include <ac/unistd.h>
27 #include <ac/wait.h>
28
29 #ifdef HAVE_SYS_RESOURCE_H
30 #include <sys/resource.h>
31 #endif
32
33 #include <lber.h>
34 #include <ldap.h>
35
36 #include "fax500.h"
37 #include "ldap_defaults.h"
38
39 #define DEFAULT_PORT            79
40 #define DEFAULT_SIZELIMIT       50
41
42 int             debug;
43 char    *ldaphost = NULL;
44 char    *base = NULL;
45 int             deref = LDAP_DEREF_ALWAYS;
46 int             sizelimit = DEFAULT_SIZELIMIT;
47 LDAPFiltDesc    *filtd;
48
49 static void     print_entry(LDAP *ld, LDAPMessage *e);
50
51 static void
52 usage( char *name )
53 {
54         fprintf( stderr, "usage: %s [-d debuglevel] [-x ldaphost] [-b searchbase] [-a] [-z sizelimit] [-f filterfile] searchstring\r\n", name );
55         exit( -1 );
56 }
57
58 int
59 main( int argc, char **argv )
60 {
61         int             i, rc, matches;
62         char            *filterfile = FILTERFILE;
63         char            buf[10];
64         char            *key;
65         LDAP            *ld;
66         LDAPMessage     *result, *e;
67         LDAPFiltDesc    *filtd;
68         LDAPFiltInfo    *fi;
69         static char     *attrs[] = { "title", "o", "ou", "postalAddress",
70                                         "telephoneNumber", "mail",
71                                         "facsimileTelephoneNumber", NULL };
72
73         while ( (i = getopt( argc, argv, "ab:d:f:x:z:" )) != EOF ) {
74                 switch( i ) {
75                 case 'a':       /* do not deref aliases when searching */
76                         deref = LDAP_DEREF_FINDING;
77                         break;
78
79                 case 'b':       /* search base */
80                         base = strdup( optarg );
81                         break;
82
83                 case 'd':       /* turn on debugging */
84                         debug = atoi( optarg );
85                         break;
86
87                 case 'f':       /* ldap filter file */
88                         filterfile = strdup( optarg );
89                         break;
90
91                 case 'x':       /* specify ldap host */
92                         ldaphost = strdup( optarg );
93                         break;
94
95                 case 'z':       /* size limit */
96                         sizelimit = atoi( optarg );
97                         break;
98
99                 default:
100                         usage( argv[0] );
101                 }
102         }
103
104         if ( optind == argc ) {
105                 usage( argv[0] );
106         }
107         key = argv[optind];
108
109         if ( (filtd = ldap_init_getfilter( filterfile )) == NULL ) {
110                 fprintf( stderr, "Cannot open filter file (%s)\n", filterfile );
111                 exit( -1 );
112         }
113
114 #ifdef SIGPIPE
115         (void) SIGNAL( SIGPIPE, SIG_IGN );
116 #endif
117
118         if ( (ld = ldap_init( ldaphost, 0 )) == NULL ) {
119                 perror( "ldap_init" );
120                 exit( -1 );
121         }
122
123         ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &sizelimit);
124         ldap_set_option(ld, LDAP_OPT_DEREF, &deref);
125
126         if ( ldap_simple_bind_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
127                 fprintf( stderr, "X.500 is temporarily unavailable.\n" );
128                 ldap_perror( ld, "ldap_simple_bind_s" );
129                 exit( -1 );
130         }
131
132         result = NULL;
133         if ( strchr( key, ',' ) != NULL ) {
134                 int ld_deref = LDAP_DEREF_FINDING;
135                 ldap_set_option(ld, LDAP_OPT_DEREF, &ld_deref);
136                 if ( (rc = ldap_ufn_search_s( ld, key, attrs, 0, &result ))
137                     != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED &&
138                     rc != LDAP_TIMELIMIT_EXCEEDED )
139                 {
140                         ldap_perror( ld, "ldap_ufn_search_s" );
141                         exit( -1 );
142                 }
143                 matches = ldap_count_entries( ld, result );
144         } else {
145                 for ( fi = ldap_getfirstfilter( filtd, "rp500", key );
146                     fi != NULL; fi = ldap_getnextfilter( filtd ) ) {
147                         if ( (rc = ldap_search_s( ld, base, LDAP_SCOPE_SUBTREE,
148                             fi->lfi_filter, attrs, 0, &result ))
149                             != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED
150                             && rc != LDAP_TIMELIMIT_EXCEEDED )
151                         {
152                                 ldap_perror( ld, "ldap_search" );
153                                 exit( -1 );
154                         }
155
156                         if ( (matches = ldap_count_entries( ld, result )) != 0
157                             || rc != LDAP_SUCCESS ) {
158                                 break;
159                         }
160                 }
161         }
162
163         if ( matches == 1 ) {
164                 e = ldap_first_entry( ld, result );
165
166                 print_entry( ld, e );
167         } else if ( matches > 1 ) {
168                 fprintf( stderr, "%d %s matches for \"%s\":\r\n", matches,
169                     fi->lfi_desc, key );
170
171                 for ( i = 1, e = ldap_first_entry( ld, result ); e != NULL;
172                     i++, e = ldap_next_entry( ld, e ) ) {
173                         int     j;
174                         char    *p, *dn, *rdn;
175                         char    **title;
176
177                         dn = ldap_get_dn( ld, e );
178                         rdn = dn;
179                         if ( (p = strchr( dn, ',' )) != NULL )
180                                 *p = '\0';
181                         while ( *rdn && *rdn != '=' )
182                                 rdn++;
183                         if ( *rdn )
184                                 rdn++;
185                         if ( strcasecmp( rdn, buf ) == 0 ) {
186                                 char    **cn;
187                                 int     i, last;
188
189                                 cn = ldap_get_values( ld, e, "cn" );
190                                 for ( i = 0; cn[i] != NULL; i++ ) {
191                                         last = strlen( cn[i] ) - 1;
192                                         if ( isdigit((unsigned char) cn[i][last]) ) {
193                                                 rdn = strdup( cn[i] );
194                                                 break;
195                                         }
196                                 }
197                         }
198                                         
199                         title = ldap_get_values( ld, e, "title" );
200
201                         fprintf( stderr, "  %d: %-20s    %s\r\n", i, rdn,
202                             title ? title[0] : "" );
203                         if ( title != NULL ) {
204                                 for ( j = 1; title[j] != NULL; j++ )
205                                         fprintf( stderr, "  %-20s    %s\r\n",
206                                             "", title[j] );
207                         }
208                         if ( title != NULL )
209                                 ldap_value_free( title );
210
211                         free( dn );
212                 }
213                 if ( rc == LDAP_SIZELIMIT_EXCEEDED
214                     || rc == LDAP_TIMELIMIT_EXCEEDED ) {
215                         fprintf( stderr, "(Size or time limit exceeded)\n" );
216                 }
217
218                 fprintf( stderr, "Enter the number of the person you want: ");
219
220                 if ( fgets( buf, sizeof(buf), stdin ) == NULL
221                     || buf[0] == '\n' ) {
222                         exit( EXIT_FAILURE );
223                 }
224                 i = atoi( buf ) - 1;
225                 e = ldap_first_entry( ld, result );
226                 for ( ; i > 0 && e != NULL; i-- ) {
227                         e = ldap_next_entry( ld, e );
228                 }
229                 if ( e == NULL ) {
230                         fprintf( stderr, "Invalid choice!\n" );
231                         exit( EXIT_FAILURE );
232                 }
233
234                 print_entry( ld, e );
235         } else if ( matches == 0 ) {
236                 fprintf( stderr, "No matches found for \"%s\"\n", key );
237                 exit( EXIT_FAILURE );
238         } else {
239                 fprintf( stderr, "Error return from ldap_count_entries\n" );
240                 exit( -1 );
241         }
242
243         ldap_unbind( ld );
244         return( 0 );
245 }
246
247 static void
248 print_entry( LDAP *ld, LDAPMessage *e )
249 {
250         int     i;
251         char    *dn, *rdn;
252         char    **ufn;
253         char    **title, **dept, **addr, **phone, **fax, **mail;
254         char    *faxmail, *org;
255
256         dn = ldap_get_dn( ld, e );
257         ufn = ldap_explode_dn( dn, 0 );
258         rdn = strchr( ufn[0], '=' ) + 1;
259
260         if ( (fax = ldap_get_values( ld, e, "facsimileTelephoneNumber" ))
261             == NULL ) {
262                 fprintf( stderr, "Entry \"%s\" has no fax number.\n", dn );
263                 exit( EXIT_FAILURE );
264         }
265         faxmail = faxtotpc( fax[0], NULL );
266         title = ldap_get_values( ld, e, "title" );
267         phone = ldap_get_values( ld, e, "telephoneNumber" );
268         mail = ldap_get_values( ld, e, "mail" );
269         dept = ldap_get_values( ld, e, "ou" );
270         addr = ldap_get_values( ld, e, "postalAddress" );
271         org = "";
272         for ( i = 0; ufn[i] != NULL; i++ ) {
273                 if ( strncmp( "o=", ufn[i], 2 ) == 0 ) {
274                         org = strdup( strchr( ufn[i], '=' ) + 1 );
275                         break;
276                 }
277         }
278
279         printf( "To: %s\n", faxmail );
280         printf( "Subject:\n" );
281         printf( "--------\n" );
282         printf( "#<application/remote-printing\n" );
283         printf( "Recipient:      %s\r\n", rdn );
284         printf( "Title:          %s\r\n", title ? title[0] : "" );
285         printf( "Organization:   %s\r\n", org );
286         printf( "Department:     %s\r\n", dept ? dept[0] : "" );
287         printf( "Telephone:      %s\r\n", phone ? phone[0] : "" );
288         printf( "Facsimile:      %s\r\n", fax ? fax[0] : "" );
289         printf( "Email:          %s\r\n", mail ? mail[0] : "" );
290 }