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