]> git.sur5r.net Git - openldap/blob - clients/fax500/rp500.c
More header work toward draft-ietf-ldapext-ldap-c-api-01.
[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 <signal.h>
17
18 #include <ac/socket.h>
19 #include <ac/string.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 #if LDAP_VERSION < LDAP_VERSION3
30 /* quick fix until we have ldap_set_options */
31 #include "../libraries/libldap/ldap-int.h"
32 #endif
33
34 #include <ldapconfig.h>
35
36 #define DEFAULT_PORT            79
37 #define DEFAULT_SIZELIMIT       50
38
39 int             debug;
40 char    *ldaphost = LDAPHOST;
41 char    *base = RP_BASE;
42 int             deref;
43 int             sizelimit;
44 LDAPFiltDesc    *filtd;
45
46 static print_entry();
47
48 static
49 usage( name )
50     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 main (argc, argv)
57     int         argc;
58     char        **argv;
59 {
60         int             i, rc, matches;
61         char            *filterfile = FILTERFILE;
62         struct timeval  timeout;
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         extern char     *optarg;
73         extern int      optind;
74
75         deref = LDAP_DEREF_ALWAYS;
76         while ( (i = getopt( argc, argv, "ab:d:f:x:z:" )) != EOF ) {
77                 switch( i ) {
78                 case 'a':       /* do not deref aliases when searching */
79                         deref = LDAP_DEREF_FINDING;
80                         break;
81
82                 case 'b':       /* search base */
83                         base = strdup( optarg );
84                         break;
85
86                 case 'd':       /* turn on debugging */
87                         debug = atoi( optarg );
88                         break;
89
90                 case 'f':       /* ldap filter file */
91                         filterfile = strdup( optarg );
92                         break;
93
94                 case 'x':       /* specify ldap host */
95                         ldaphost = strdup( optarg );
96                         break;
97
98                 case 'z':       /* size limit */
99                         sizelimit = atoi( optarg );
100                         break;
101
102                 default:
103                         usage( argv[0] );
104                 }
105         }
106
107         if ( optind == argc ) {
108                 usage( argv[0] );
109         }
110         key = argv[optind];
111
112         if ( (filtd = ldap_init_getfilter( filterfile )) == NULL ) {
113                 fprintf( stderr, "Cannot open filter file (%s)\n", filterfile );
114                 exit( -1 );
115         }
116
117         if ( (ld = ldap_open( ldaphost, LDAP_PORT )) == NULL ) {
118                 perror( "ldap_open" );
119                 exit( -1 );
120         }
121         ld->ld_sizelimit = sizelimit ? sizelimit : DEFAULT_SIZELIMIT;
122         ld->ld_deref = deref;
123
124         if ( ldap_simple_bind_s( ld, RP_BINDDN, RP_BIND_CRED ) != LDAP_SUCCESS ) {
125                 fprintf( stderr, "X.500 is temporarily unavailable.\n" );
126                 ldap_perror( ld, "ldap_simple_bind_s" );
127                 exit( -1 );
128         }
129
130         result = NULL;
131         if ( strchr( key, ',' ) != NULL ) {
132                 ld->ld_deref = LDAP_DEREF_FINDING;
133                 if ( (rc = ldap_ufn_search_s( ld, key, attrs, 0, &result ))
134                     != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED &&
135                     rc != LDAP_TIMELIMIT_EXCEEDED )
136                 {
137                         ldap_perror( ld, "ldap_ufn_search_s" );
138                         exit( -1 );
139                 }
140                 matches = ldap_count_entries( ld, result );
141         } else {
142                 for ( fi = ldap_getfirstfilter( filtd, "rp500", key );
143                     fi != NULL; fi = ldap_getnextfilter( filtd ) ) {
144                         if ( (rc = ldap_search_s( ld, base, LDAP_SCOPE_SUBTREE,
145                             fi->lfi_filter, attrs, 0, &result ))
146                             != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED
147                             && rc != LDAP_TIMELIMIT_EXCEEDED )
148                         {
149                                 ldap_perror( ld, "ldap_search" );
150                                 exit( -1 );
151                         }
152
153                         if ( (matches = ldap_count_entries( ld, result )) != 0
154                             || rc != LDAP_SUCCESS ) {
155                                 break;
156                         }
157                 }
158         }
159
160         if ( matches == 1 ) {
161                 e = ldap_first_entry( ld, result );
162
163                 print_entry( ld, e );
164         } else if ( matches > 1 ) {
165                 fprintf( stderr, "%d %s matches for \"%s\":\r\n", matches,
166                     fi->lfi_desc, key );
167
168                 for ( i = 1, e = ldap_first_entry( ld, result ); e != NULL;
169                     i++, e = ldap_next_entry( ld, e ) ) {
170                         int     j;
171                         char    *p, *dn, *rdn;
172                         char    **title;
173
174                         dn = ldap_get_dn( ld, e );
175                         rdn = dn;
176                         if ( (p = strchr( dn, ',' )) != NULL )
177                                 *p = '\0';
178                         while ( *rdn && *rdn != '=' )
179                                 rdn++;
180                         if ( *rdn )
181                                 rdn++;
182                         if ( strcasecmp( rdn, buf ) == 0 ) {
183                                 char    **cn;
184                                 char    *s;
185                                 int     i, last;
186
187                                 cn = ldap_get_values( ld, e, "cn" );
188                                 for ( i = 0; cn[i] != NULL; i++ ) {
189                                         last = strlen( cn[i] ) - 1;
190                                         if ( isdigit( cn[i][last] ) ) {
191                                                 rdn = strdup( cn[i] );
192                                                 break;
193                                         }
194                                 }
195                         }
196                                         
197                         title = ldap_get_values( ld, e, "title" );
198
199                         fprintf( stderr, "  %d: %-20s    %s\r\n", i, rdn,
200                             title ? title[0] : "" );
201                         if ( title != NULL ) {
202                                 for ( j = 1; title[j] != NULL; j++ )
203                                         fprintf( stderr, "  %-20s    %s\r\n",
204                                             "", title[j] );
205                         }
206                         if ( title != NULL )
207                                 ldap_value_free( title );
208
209                         free( dn );
210                 }
211                 if ( rc == LDAP_SIZELIMIT_EXCEEDED
212                     || rc == LDAP_TIMELIMIT_EXCEEDED ) {
213                         fprintf( stderr, "(Size or time limit exceeded)\n" );
214                 }
215
216                 fprintf( stderr, "Enter the number of the person you want: ");
217
218                 if ( fgets( buf, sizeof(buf), stdin ) == NULL
219                     || buf[0] == '\n' ) {
220                         exit( 1 );
221                 }
222                 i = atoi( buf ) - 1;
223                 e = ldap_first_entry( ld, result );
224                 for ( ; i > 0 && e != NULL; i-- ) {
225                         e = ldap_next_entry( ld, e );
226                 }
227                 if ( e == NULL ) {
228                         fprintf( stderr, "Invalid choice!\n" );
229                         exit( 1 );
230                 }
231
232                 print_entry( ld, e );
233         } else if ( matches == 0 ) {
234                 fprintf( stderr, "No matches found for \"%s\"\n", key );
235                 exit( 1 );
236         } else {
237                 fprintf( stderr, "Error return from ldap_count_entries\n" );
238                 exit( -1 );
239         }
240
241         ldap_unbind( ld );
242         return( 0 );
243 }
244
245 static
246 print_entry( ld, e )
247     LDAP        *ld;
248     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, *faxtotpc();
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( 1 );
264         }
265         faxmail = faxtotpc( fax[0] );
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 }