]> git.sur5r.net Git - openldap/blob - clients/fax500/rp500.c
missing 2nd argument to faxtotpc()
[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 #include <ldapconfig.h>
30
31 #define DEFAULT_PORT            79
32 #define DEFAULT_SIZELIMIT       50
33
34 int             debug;
35 char    *ldaphost = LDAPHOST;
36 char    *base = RP_BASE;
37 int             deref = LDAP_DEREF_ALWAYS;
38 int             sizelimit = DEFAULT_SIZELIMIT;
39 LDAPFiltDesc    *filtd;
40
41 static print_entry();
42
43 static
44 usage( name )
45     char        *name;
46 {
47         fprintf( stderr, "usage: %s [-d debuglevel] [-x ldaphost] [-b searchbase] [-a] [-z sizelimit] [-f filterfile] searchstring\r\n", name );
48         exit( -1 );
49 }
50
51 main (argc, argv)
52     int         argc;
53     char        **argv;
54 {
55         int             i, rc, matches;
56         char            *filterfile = FILTERFILE;
57         struct timeval  timeout;
58         char            buf[10];
59         char            *key;
60         LDAP            *ld;
61         LDAPMessage     *result, *e;
62         LDAPFiltDesc    *filtd;
63         LDAPFiltInfo    *fi;
64         static char     *attrs[] = { "title", "o", "ou", "postalAddress",
65                                         "telephoneNumber", "mail",
66                                         "facsimileTelephoneNumber", NULL };
67         extern char     *optarg;
68         extern int      optind;
69
70         while ( (i = getopt( argc, argv, "ab:d:f:x:z:" )) != EOF ) {
71                 switch( i ) {
72                 case 'a':       /* do not deref aliases when searching */
73                         deref = LDAP_DEREF_FINDING;
74                         break;
75
76                 case 'b':       /* search base */
77                         base = strdup( optarg );
78                         break;
79
80                 case 'd':       /* turn on debugging */
81                         debug = atoi( optarg );
82                         break;
83
84                 case 'f':       /* ldap filter file */
85                         filterfile = strdup( optarg );
86                         break;
87
88                 case 'x':       /* specify ldap host */
89                         ldaphost = strdup( optarg );
90                         break;
91
92                 case 'z':       /* size limit */
93                         sizelimit = atoi( optarg );
94                         break;
95
96                 default:
97                         usage( argv[0] );
98                 }
99         }
100
101         if ( optind == argc ) {
102                 usage( argv[0] );
103         }
104         key = argv[optind];
105
106         if ( (filtd = ldap_init_getfilter( filterfile )) == NULL ) {
107                 fprintf( stderr, "Cannot open filter file (%s)\n", filterfile );
108                 exit( -1 );
109         }
110
111         if ( (ld = ldap_open( ldaphost, LDAP_PORT )) == NULL ) {
112                 perror( "ldap_open" );
113                 exit( -1 );
114         }
115
116         ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &sizelimit);
117         ldap_set_option(ld, LDAP_OPT_DEREF, &deref);
118
119         if ( ldap_simple_bind_s( ld, RP_BINDDN, RP_BIND_CRED ) != LDAP_SUCCESS ) {
120                 fprintf( stderr, "X.500 is temporarily unavailable.\n" );
121                 ldap_perror( ld, "ldap_simple_bind_s" );
122                 exit( -1 );
123         }
124
125         result = NULL;
126         if ( strchr( key, ',' ) != NULL ) {
127                 int ld_deref = LDAP_DEREF_FINDING;
128                 ldap_set_option(ld, LDAP_OPT_DEREF, &ld_deref);
129                 if ( (rc = ldap_ufn_search_s( ld, key, attrs, 0, &result ))
130                     != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED &&
131                     rc != LDAP_TIMELIMIT_EXCEEDED )
132                 {
133                         ldap_perror( ld, "ldap_ufn_search_s" );
134                         exit( -1 );
135                 }
136                 matches = ldap_count_entries( ld, result );
137         } else {
138                 for ( fi = ldap_getfirstfilter( filtd, "rp500", key );
139                     fi != NULL; fi = ldap_getnextfilter( filtd ) ) {
140                         if ( (rc = ldap_search_s( ld, base, LDAP_SCOPE_SUBTREE,
141                             fi->lfi_filter, attrs, 0, &result ))
142                             != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED
143                             && rc != LDAP_TIMELIMIT_EXCEEDED )
144                         {
145                                 ldap_perror( ld, "ldap_search" );
146                                 exit( -1 );
147                         }
148
149                         if ( (matches = ldap_count_entries( ld, result )) != 0
150                             || rc != LDAP_SUCCESS ) {
151                                 break;
152                         }
153                 }
154         }
155
156         if ( matches == 1 ) {
157                 e = ldap_first_entry( ld, result );
158
159                 print_entry( ld, e );
160         } else if ( matches > 1 ) {
161                 fprintf( stderr, "%d %s matches for \"%s\":\r\n", matches,
162                     fi->lfi_desc, key );
163
164                 for ( i = 1, e = ldap_first_entry( ld, result ); e != NULL;
165                     i++, e = ldap_next_entry( ld, e ) ) {
166                         int     j;
167                         char    *p, *dn, *rdn;
168                         char    **title;
169
170                         dn = ldap_get_dn( ld, e );
171                         rdn = dn;
172                         if ( (p = strchr( dn, ',' )) != NULL )
173                                 *p = '\0';
174                         while ( *rdn && *rdn != '=' )
175                                 rdn++;
176                         if ( *rdn )
177                                 rdn++;
178                         if ( strcasecmp( rdn, buf ) == 0 ) {
179                                 char    **cn;
180                                 char    *s;
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
242 print_entry( ld, e )
243     LDAP        *ld;
244     LDAPMessage *e;
245 {
246         int     i;
247         char    *dn, *rdn;
248         char    **ufn;
249         char    **title, **dept, **addr, **phone, **fax, **mail;
250         char    *faxmail, *org, *faxtotpc();
251
252         dn = ldap_get_dn( ld, e );
253         ufn = ldap_explode_dn( dn, 0 );
254         rdn = strchr( ufn[0], '=' ) + 1;
255
256         if ( (fax = ldap_get_values( ld, e, "facsimileTelephoneNumber" ))
257             == NULL ) {
258                 fprintf( stderr, "Entry \"%s\" has no fax number.\n", dn );
259                 exit( 1 );
260         }
261         faxmail = faxtotpc( fax[0], NULL );
262         title = ldap_get_values( ld, e, "title" );
263         phone = ldap_get_values( ld, e, "telephoneNumber" );
264         mail = ldap_get_values( ld, e, "mail" );
265         dept = ldap_get_values( ld, e, "ou" );
266         addr = ldap_get_values( ld, e, "postalAddress" );
267         org = "";
268         for ( i = 0; ufn[i] != NULL; i++ ) {
269                 if ( strncmp( "o=", ufn[i], 2 ) == 0 ) {
270                         org = strdup( strchr( ufn[i], '=' ) + 1 );
271                         break;
272                 }
273         }
274
275         printf( "To: %s\n", faxmail );
276         printf( "Subject:\n" );
277         printf( "--------\n" );
278         printf( "#<application/remote-printing\n" );
279         printf( "Recipient:      %s\r\n", rdn );
280         printf( "Title:          %s\r\n", title ? title[0] : "" );
281         printf( "Organization:   %s\r\n", org );
282         printf( "Department:     %s\r\n", dept ? dept[0] : "" );
283         printf( "Telephone:      %s\r\n", phone ? phone[0] : "" );
284         printf( "Facsimile:      %s\r\n", fax ? fax[0] : "" );
285         printf( "Email:          %s\r\n", mail ? mail[0] : "" );
286 }