]> git.sur5r.net Git - openldap/blob - clients/fax500/rp500.c
Protoized, moved extern definitions to .h files, fixed related bugs.
[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 #include "fax500.h"
15
16 #include <stdlib.h>
17 #include <signal.h>
18
19 #include <ac/socket.h>
20 #include <ac/syslog.h>
21 #include <ac/time.h>
22 #include <ac/wait.h>
23
24 #include <sys/resource.h>
25
26 #include <lber.h>
27 #include <ldap.h>
28
29 #include <ldapconfig.h>
30
31 #define DEFAULT_PORT            79
32 #define DEFAULT_SIZELIMIT       50
33
34 int             debug;
35 char    *ldaphost = NULL;
36 char    *base = NULL;
37 int             deref = LDAP_DEREF_ALWAYS;
38 int             sizelimit = DEFAULT_SIZELIMIT;
39 LDAPFiltDesc    *filtd;
40
41 static void     print_entry(LDAP *ld, LDAPMessage *e);
42
43 static void
44 usage( char *name )
45 {
46         fprintf( stderr, "usage: %s [-d debuglevel] [-x ldaphost] [-b searchbase] [-a] [-z sizelimit] [-f filterfile] searchstring\r\n", name );
47         exit( -1 );
48 }
49
50 int
51 main( int argc, char **argv )
52 {
53         int             i, rc, matches;
54         char            *filterfile = FILTERFILE;
55         char            buf[10];
56         char            *key;
57         LDAP            *ld;
58         LDAPMessage     *result, *e;
59         LDAPFiltDesc    *filtd;
60         LDAPFiltInfo    *fi;
61         static char     *attrs[] = { "title", "o", "ou", "postalAddress",
62                                         "telephoneNumber", "mail",
63                                         "facsimileTelephoneNumber", NULL };
64         extern char     *optarg;
65         extern int      optind;
66
67         while ( (i = getopt( argc, argv, "ab:d:f:x:z:" )) != EOF ) {
68                 switch( i ) {
69                 case 'a':       /* do not deref aliases when searching */
70                         deref = LDAP_DEREF_FINDING;
71                         break;
72
73                 case 'b':       /* search base */
74                         base = strdup( optarg );
75                         break;
76
77                 case 'd':       /* turn on debugging */
78                         debug = atoi( optarg );
79                         break;
80
81                 case 'f':       /* ldap filter file */
82                         filterfile = strdup( optarg );
83                         break;
84
85                 case 'x':       /* specify ldap host */
86                         ldaphost = strdup( optarg );
87                         break;
88
89                 case 'z':       /* size limit */
90                         sizelimit = atoi( optarg );
91                         break;
92
93                 default:
94                         usage( argv[0] );
95                 }
96         }
97
98         if ( optind == argc ) {
99                 usage( argv[0] );
100         }
101         key = argv[optind];
102
103         if ( (filtd = ldap_init_getfilter( filterfile )) == NULL ) {
104                 fprintf( stderr, "Cannot open filter file (%s)\n", filterfile );
105                 exit( -1 );
106         }
107
108         if ( (ld = ldap_open( ldaphost, LDAP_PORT )) == NULL ) {
109                 perror( "ldap_open" );
110                 exit( -1 );
111         }
112
113         ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &sizelimit);
114         ldap_set_option(ld, LDAP_OPT_DEREF, &deref);
115
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" );
119                 exit( -1 );
120         }
121
122         result = NULL;
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 )
129                 {
130                         ldap_perror( ld, "ldap_ufn_search_s" );
131                         exit( -1 );
132                 }
133                 matches = ldap_count_entries( ld, result );
134         } else {
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 )
141                         {
142                                 ldap_perror( ld, "ldap_search" );
143                                 exit( -1 );
144                         }
145
146                         if ( (matches = ldap_count_entries( ld, result )) != 0
147                             || rc != LDAP_SUCCESS ) {
148                                 break;
149                         }
150                 }
151         }
152
153         if ( matches == 1 ) {
154                 e = ldap_first_entry( ld, result );
155
156                 print_entry( ld, e );
157         } else if ( matches > 1 ) {
158                 fprintf( stderr, "%d %s matches for \"%s\":\r\n", matches,
159                     fi->lfi_desc, key );
160
161                 for ( i = 1, e = ldap_first_entry( ld, result ); e != NULL;
162                     i++, e = ldap_next_entry( ld, e ) ) {
163                         int     j;
164                         char    *p, *dn, *rdn;
165                         char    **title;
166
167                         dn = ldap_get_dn( ld, e );
168                         rdn = dn;
169                         if ( (p = strchr( dn, ',' )) != NULL )
170                                 *p = '\0';
171                         while ( *rdn && *rdn != '=' )
172                                 rdn++;
173                         if ( *rdn )
174                                 rdn++;
175                         if ( strcasecmp( rdn, buf ) == 0 ) {
176                                 char    **cn;
177                                 int     i, last;
178
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] );
184                                                 break;
185                                         }
186                                 }
187                         }
188                                         
189                         title = ldap_get_values( ld, e, "title" );
190
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",
196                                             "", title[j] );
197                         }
198                         if ( title != NULL )
199                                 ldap_value_free( title );
200
201                         free( dn );
202                 }
203                 if ( rc == LDAP_SIZELIMIT_EXCEEDED
204                     || rc == LDAP_TIMELIMIT_EXCEEDED ) {
205                         fprintf( stderr, "(Size or time limit exceeded)\n" );
206                 }
207
208                 fprintf( stderr, "Enter the number of the person you want: ");
209
210                 if ( fgets( buf, sizeof(buf), stdin ) == NULL
211                     || buf[0] == '\n' ) {
212                         exit( 1 );
213                 }
214                 i = atoi( buf ) - 1;
215                 e = ldap_first_entry( ld, result );
216                 for ( ; i > 0 && e != NULL; i-- ) {
217                         e = ldap_next_entry( ld, e );
218                 }
219                 if ( e == NULL ) {
220                         fprintf( stderr, "Invalid choice!\n" );
221                         exit( 1 );
222                 }
223
224                 print_entry( ld, e );
225         } else if ( matches == 0 ) {
226                 fprintf( stderr, "No matches found for \"%s\"\n", key );
227                 exit( 1 );
228         } else {
229                 fprintf( stderr, "Error return from ldap_count_entries\n" );
230                 exit( -1 );
231         }
232
233         ldap_unbind( ld );
234         return( 0 );
235 }
236
237 static void
238 print_entry( LDAP *ld, LDAPMessage *e )
239 {
240         int     i;
241         char    *dn, *rdn;
242         char    **ufn;
243         char    **title, **dept, **addr, **phone, **fax, **mail;
244         char    *faxmail, *org;
245
246         dn = ldap_get_dn( ld, e );
247         ufn = ldap_explode_dn( dn, 0 );
248         rdn = strchr( ufn[0], '=' ) + 1;
249
250         if ( (fax = ldap_get_values( ld, e, "facsimileTelephoneNumber" ))
251             == NULL ) {
252                 fprintf( stderr, "Entry \"%s\" has no fax number.\n", dn );
253                 exit( 1 );
254         }
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" );
261         org = "";
262         for ( i = 0; ufn[i] != NULL; i++ ) {
263                 if ( strncmp( "o=", ufn[i], 2 ) == 0 ) {
264                         org = strdup( strchr( ufn[i], '=' ) + 1 );
265                         break;
266                 }
267         }
268
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] : "" );
280 }