]> git.sur5r.net Git - openldap/blob - clients/fax500/rp500.c
bc8032530575d8ef452d56413400ace44a332d1d
[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
17 #include <ac/stdlib.h>
18
19 #include <ac/ctype.h>
20 #include <ac/signal.h>
21 #include <ac/socket.h>
22 #include <ac/string.h>
23 #include <ac/syslog.h>
24 #include <ac/time.h>
25 #include <ac/unistd.h>
26 #include <ac/wait.h>
27
28 #ifdef HAVE_SYS_RESOURCE_H
29 #include <sys/resource.h>
30 #endif
31
32 #include <lber.h>
33 #include <ldap.h>
34
35 #include "fax500.h"
36 #include "ldap_defaults.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 #ifdef SIGPIPE
114         (void) SIGNAL( SIGPIPE, SIG_IGN );
115 #endif
116
117         if ( (ld = ldap_init( ldaphost, 0 )) == NULL ) {
118                 perror( "ldap_init" );
119                 exit( -1 );
120         }
121
122         ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &sizelimit);
123         ldap_set_option(ld, LDAP_OPT_DEREF, &deref);
124
125         if ( ldap_simple_bind_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
126                 fprintf( stderr, "X.500 is temporarily unavailable.\n" );
127                 ldap_perror( ld, "ldap_simple_bind_s" );
128                 exit( -1 );
129         }
130
131         result = NULL;
132         if ( strchr( key, ',' ) != NULL ) {
133                 int ld_deref = LDAP_DEREF_FINDING;
134                 ldap_set_option(ld, LDAP_OPT_DEREF, &ld_deref);
135                 if ( (rc = ldap_ufn_search_s( ld, key, attrs, 0, &result ))
136                     != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED &&
137                     rc != LDAP_TIMELIMIT_EXCEEDED )
138                 {
139                         ldap_perror( ld, "ldap_ufn_search_s" );
140                         exit( -1 );
141                 }
142                 matches = ldap_count_entries( ld, result );
143         } else {
144                 for ( fi = ldap_getfirstfilter( filtd, "rp500", key );
145                     fi != NULL; fi = ldap_getnextfilter( filtd ) ) {
146                         if ( (rc = ldap_search_s( ld, base, LDAP_SCOPE_SUBTREE,
147                             fi->lfi_filter, attrs, 0, &result ))
148                             != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED
149                             && rc != LDAP_TIMELIMIT_EXCEEDED )
150                         {
151                                 ldap_perror( ld, "ldap_search" );
152                                 exit( -1 );
153                         }
154
155                         if ( (matches = ldap_count_entries( ld, result )) != 0
156                             || rc != LDAP_SUCCESS ) {
157                                 break;
158                         }
159                 }
160         }
161
162         if ( matches == 1 ) {
163                 e = ldap_first_entry( ld, result );
164
165                 print_entry( ld, e );
166         } else if ( matches > 1 ) {
167                 fprintf( stderr, "%d %s matches for \"%s\":\r\n", matches,
168                     fi->lfi_desc, key );
169
170                 for ( i = 1, e = ldap_first_entry( ld, result ); e != NULL;
171                     i++, e = ldap_next_entry( ld, e ) ) {
172                         int     j;
173                         char    *p, *dn, *rdn;
174                         char    **title;
175
176                         dn = ldap_get_dn( ld, e );
177                         rdn = dn;
178                         if ( (p = strchr( dn, ',' )) != NULL )
179                                 *p = '\0';
180                         while ( *rdn && *rdn != '=' )
181                                 rdn++;
182                         if ( *rdn )
183                                 rdn++;
184                         if ( strcasecmp( rdn, buf ) == 0 ) {
185                                 char    **cn;
186                                 int     i, last;
187
188                                 cn = ldap_get_values( ld, e, "cn" );
189                                 for ( i = 0; cn[i] != NULL; i++ ) {
190                                         last = strlen( cn[i] ) - 1;
191                                         if ( isdigit((unsigned char) cn[i][last]) ) {
192                                                 rdn = strdup( cn[i] );
193                                                 break;
194                                         }
195                                 }
196                         }
197                                         
198                         title = ldap_get_values( ld, e, "title" );
199
200                         fprintf( stderr, "  %d: %-20s    %s\r\n", i, rdn,
201                             title ? title[0] : "" );
202                         if ( title != NULL ) {
203                                 for ( j = 1; title[j] != NULL; j++ )
204                                         fprintf( stderr, "  %-20s    %s\r\n",
205                                             "", title[j] );
206                         }
207                         if ( title != NULL )
208                                 ldap_value_free( title );
209
210                         free( dn );
211                 }
212                 if ( rc == LDAP_SIZELIMIT_EXCEEDED
213                     || rc == LDAP_TIMELIMIT_EXCEEDED ) {
214                         fprintf( stderr, "(Size or time limit exceeded)\n" );
215                 }
216
217                 fprintf( stderr, "Enter the number of the person you want: ");
218
219                 if ( fgets( buf, sizeof(buf), stdin ) == NULL
220                     || buf[0] == '\n' ) {
221                         exit( EXIT_FAILURE );
222                 }
223                 i = atoi( buf ) - 1;
224                 e = ldap_first_entry( ld, result );
225                 for ( ; i > 0 && e != NULL; i-- ) {
226                         e = ldap_next_entry( ld, e );
227                 }
228                 if ( e == NULL ) {
229                         fprintf( stderr, "Invalid choice!\n" );
230                         exit( EXIT_FAILURE );
231                 }
232
233                 print_entry( ld, e );
234         } else if ( matches == 0 ) {
235                 fprintf( stderr, "No matches found for \"%s\"\n", key );
236                 exit( EXIT_FAILURE );
237         } else {
238                 fprintf( stderr, "Error return from ldap_count_entries\n" );
239                 exit( -1 );
240         }
241
242         ldap_unbind( ld );
243         return( 0 );
244 }
245
246 static void
247 print_entry( LDAP *ld, LDAPMessage *e )
248 {
249         int     i;
250         char    *dn, *rdn;
251         char    **ufn;
252         char    **title, **dept, **addr, **phone, **fax, **mail;
253         char    *faxmail, *org;
254
255         dn = ldap_get_dn( ld, e );
256         ufn = ldap_explode_dn( dn, 0 );
257         rdn = strchr( ufn[0], '=' ) + 1;
258
259         if ( (fax = ldap_get_values( ld, e, "facsimileTelephoneNumber" ))
260             == NULL ) {
261                 fprintf( stderr, "Entry \"%s\" has no fax number.\n", dn );
262                 exit( EXIT_FAILURE );
263         }
264         faxmail = faxtotpc( fax[0], NULL );
265         title = ldap_get_values( ld, e, "title" );
266         phone = ldap_get_values( ld, e, "telephoneNumber" );
267         mail = ldap_get_values( ld, e, "mail" );
268         dept = ldap_get_values( ld, e, "ou" );
269         addr = ldap_get_values( ld, e, "postalAddress" );
270         org = "";
271         for ( i = 0; ufn[i] != NULL; i++ ) {
272                 if ( strncmp( "o=", ufn[i], 2 ) == 0 ) {
273                         org = strdup( strchr( ufn[i], '=' ) + 1 );
274                         break;
275                 }
276         }
277
278         printf( "To: %s\n", faxmail );
279         printf( "Subject:\n" );
280         printf( "--------\n" );
281         printf( "#<application/remote-printing\n" );
282         printf( "Recipient:      %s\r\n", rdn );
283         printf( "Title:          %s\r\n", title ? title[0] : "" );
284         printf( "Organization:   %s\r\n", org );
285         printf( "Department:     %s\r\n", dept ? dept[0] : "" );
286         printf( "Telephone:      %s\r\n", phone ? phone[0] : "" );
287         printf( "Facsimile:      %s\r\n", fax ? fax[0] : "" );
288         printf( "Email:          %s\r\n", mail ? mail[0] : "" );
289 }