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