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