]> git.sur5r.net Git - openldap/blob - clients/fax500/rp500.c
Removed use of paths not defined in ldapconfig.h.edit.
[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 <stdio.h>
14 #include <string.h>
15 #include <sys/types.h>
16 #include <sys/time.h>
17 #include <sys/socket.h>
18 #include <netinet/in.h>
19 #include <arpa/inet.h>
20 #include <netdb.h>
21 #include <syslog.h>
22 #include <sys/resource.h>
23 #include <sys/wait.h>
24 #include <signal.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;
38 int             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         deref = LDAP_DEREF_ALWAYS;
71         while ( (i = getopt( argc, argv, "ab:d:f:x:z:" )) != EOF ) {
72                 switch( i ) {
73                 case 'a':       /* do not deref aliases when searching */
74                         deref = LDAP_DEREF_FINDING;
75                         break;
76
77                 case 'b':       /* search base */
78                         base = strdup( optarg );
79                         break;
80
81                 case 'd':       /* turn on debugging */
82                         debug = atoi( optarg );
83                         break;
84
85                 case 'f':       /* ldap filter file */
86                         filterfile = strdup( optarg );
87                         break;
88
89                 case 'x':       /* specify ldap host */
90                         ldaphost = strdup( optarg );
91                         break;
92
93                 case 'z':       /* size limit */
94                         sizelimit = atoi( optarg );
95                         break;
96
97                 default:
98                         usage( argv[0] );
99                 }
100         }
101
102         if ( optind == argc ) {
103                 usage( argv[0] );
104         }
105         key = argv[optind];
106
107         if ( (filtd = ldap_init_getfilter( filterfile )) == NULL ) {
108                 fprintf( stderr, "Cannot open filter file (%s)\n", filterfile );
109                 exit( -1 );
110         }
111
112         if ( (ld = ldap_open( ldaphost, LDAP_PORT )) == NULL ) {
113                 perror( "ldap_open" );
114                 exit( -1 );
115         }
116         ld->ld_sizelimit = sizelimit ? sizelimit : DEFAULT_SIZELIMIT;
117         ld->ld_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                 ld->ld_deref = LDAP_DEREF_FINDING;
128                 if ( (rc = ldap_ufn_search_s( ld, key, attrs, 0, &result ))
129                     != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED &&
130                     rc != LDAP_TIMELIMIT_EXCEEDED )
131                 {
132                         ldap_perror( ld, "ldap_ufn_search_s" );
133                         exit( -1 );
134                 }
135                 matches = ldap_count_entries( ld, result );
136         } else {
137                 for ( fi = ldap_getfirstfilter( filtd, "rp500", key );
138                     fi != NULL; fi = ldap_getnextfilter( filtd ) ) {
139                         if ( (rc = ldap_search_s( ld, base, LDAP_SCOPE_SUBTREE,
140                             fi->lfi_filter, attrs, 0, &result ))
141                             != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED
142                             && rc != LDAP_TIMELIMIT_EXCEEDED )
143                         {
144                                 ldap_perror( ld, "ldap_search" );
145                                 exit( -1 );
146                         }
147
148                         if ( (matches = ldap_count_entries( ld, result )) != 0
149                             || rc != LDAP_SUCCESS ) {
150                                 break;
151                         }
152                 }
153         }
154
155         if ( matches == 1 ) {
156                 e = ldap_first_entry( ld, result );
157
158                 print_entry( ld, e );
159         } else if ( matches > 1 ) {
160                 fprintf( stderr, "%d %s matches for \"%s\":\r\n", matches,
161                     fi->lfi_desc, key );
162
163                 for ( i = 1, e = ldap_first_entry( ld, result ); e != NULL;
164                     i++, e = ldap_next_entry( ld, e ) ) {
165                         int     j;
166                         char    *p, *dn, *rdn;
167                         char    **title;
168
169                         dn = ldap_get_dn( ld, e );
170                         rdn = dn;
171                         if ( (p = strchr( dn, ',' )) != NULL )
172                                 *p = '\0';
173                         while ( *rdn && *rdn != '=' )
174                                 rdn++;
175                         if ( *rdn )
176                                 rdn++;
177                         if ( strcasecmp( rdn, buf ) == 0 ) {
178                                 char    **cn;
179                                 char    *s;
180                                 int     i, last;
181
182                                 cn = ldap_get_values( ld, e, "cn" );
183                                 for ( i = 0; cn[i] != NULL; i++ ) {
184                                         last = strlen( cn[i] ) - 1;
185                                         if ( isdigit( cn[i][last] ) ) {
186                                                 rdn = strdup( cn[i] );
187                                                 break;
188                                         }
189                                 }
190                         }
191                                         
192                         title = ldap_get_values( ld, e, "title" );
193
194                         fprintf( stderr, "  %d: %-20s    %s\r\n", i, rdn,
195                             title ? title[0] : "" );
196                         if ( title != NULL ) {
197                                 for ( j = 1; title[j] != NULL; j++ )
198                                         fprintf( stderr, "  %-20s    %s\r\n",
199                                             "", title[j] );
200                         }
201                         if ( title != NULL )
202                                 ldap_value_free( title );
203
204                         free( dn );
205                 }
206                 if ( rc == LDAP_SIZELIMIT_EXCEEDED
207                     || rc == LDAP_TIMELIMIT_EXCEEDED ) {
208                         fprintf( stderr, "(Size or time limit exceeded)\n" );
209                 }
210
211                 fprintf( stderr, "Enter the number of the person you want: ");
212
213                 if ( fgets( buf, sizeof(buf), stdin ) == NULL
214                     || buf[0] == '\n' ) {
215                         exit( 1 );
216                 }
217                 i = atoi( buf ) - 1;
218                 e = ldap_first_entry( ld, result );
219                 for ( ; i > 0 && e != NULL; i-- ) {
220                         e = ldap_next_entry( ld, e );
221                 }
222                 if ( e == NULL ) {
223                         fprintf( stderr, "Invalid choice!\n" );
224                         exit( 1 );
225                 }
226
227                 print_entry( ld, e );
228         } else if ( matches == 0 ) {
229                 fprintf( stderr, "No matches found for \"%s\"\n", key );
230                 exit( 1 );
231         } else {
232                 fprintf( stderr, "Error return from ldap_count_entries\n" );
233                 exit( -1 );
234         }
235
236         ldap_unbind( ld );
237         return( 0 );
238 }
239
240 static
241 print_entry( ld, e )
242     LDAP        *ld;
243     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, *faxtotpc();
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] );
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 }