]> git.sur5r.net Git - openldap/blob - clients/tools/ldapurl.c
5a356f2398e75b896f9a3f5a5937c2769069f32b
[openldap] / clients / tools / ldapurl.c
1 /* ldapurl -- a tool for generating LDAP URLs */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2008-2017 The OpenLDAP Foundation.
6  * Portions Copyright 2008 Pierangelo Masarati, SysNet
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted only as authorized by the OpenLDAP
11  * Public License.
12  *
13  * A copy of this license is available in the file LICENSE in the
14  * top-level directory of the distribution or, alternatively, at
15  * <http://www.OpenLDAP.org/license.html>.
16  */
17 /* Portions Copyright (c) 1992-1996 Regents of the University of Michigan.
18  * All rights reserved.
19  *
20  * Redistribution and use in source and binary forms are permitted
21  * provided that this notice is preserved and that due credit is given
22  * to the University of Michigan at Ann Arbor.  The name of the
23  * University may not be used to endorse or promote products derived
24  * from this software without specific prior written permission.  This
25  * software is provided ``as is'' without express or implied warranty.
26  */
27 /* ACKNOWLEDGEMENTS:
28  * This work was originally developed by Pierangelo Masarati
29  * for inclusion in OpenLDAP software.
30  */
31
32 #include "portable.h"
33
34 #include <ac/stdlib.h>
35 #include <stdio.h>
36 #include <ac/unistd.h>
37
38 #include "ldap.h"
39 #include "ldap_pvt.h"
40 #include "lutil.h"
41
42 static int
43 usage(void)
44 {
45         fprintf( stderr, _("usage: %s [options]\n\n"), "ldapurl" );
46         fprintf( stderr, _("generates RFC 4516 LDAP URL with extensions\n\n" ) );
47         fprintf( stderr, _("URL options:\n"));
48         fprintf( stderr, _("  -a attrs   comma separated list of attributes\n" ) );
49         fprintf( stderr, _("  -b base    (RFC 4514 LDAP DN)\n" ) );
50         fprintf( stderr, _("  -E ext     (format: \"ext=value\"; multiple occurrences allowed)\n" ) );
51         fprintf( stderr, _("  -f filter  (RFC 4515 LDAP filter)\n" ) );
52         fprintf( stderr, _("  -h host    \n" ) );
53         fprintf( stderr, _("  -p port    (default: 389 for ldap, 636 for ldaps)\n" ) );
54         fprintf( stderr, _("  -s scope   (RFC 4511 searchScope and extensions)\n" ) );
55         fprintf( stderr, _("  -S scheme  (RFC 4516 LDAP URL scheme and extensions)\n" ) );
56         exit( EXIT_FAILURE );
57 }
58
59 static int
60 do_uri_create( LDAPURLDesc *lud )
61 {
62         char    *uri;
63
64         if ( lud->lud_scheme == NULL ) {
65                 lud->lud_scheme = "ldap";
66         }
67
68         if ( lud->lud_port == -1 ) {
69                 if ( strcasecmp( lud->lud_scheme, "ldap" ) == 0 ) {
70                         lud->lud_port = LDAP_PORT;
71
72                 } else if ( strcasecmp( lud->lud_scheme, "ldaps" ) == 0 ) {
73                         lud->lud_port = LDAPS_PORT;
74
75                 } else if ( strcasecmp( lud->lud_scheme, "ldapi" ) == 0 ) {
76                         lud->lud_port = 0;
77
78                 } else {
79                         /* forgiving... */
80                         lud->lud_port = 0;
81                 }
82         }
83
84         if ( lud->lud_scope == -1 ) {
85                 lud->lud_scope = LDAP_SCOPE_DEFAULT;
86         }
87
88         uri = ldap_url_desc2str( lud );
89
90         if ( lud->lud_attrs != NULL ) {
91                 ldap_charray_free( lud->lud_attrs );
92                 lud->lud_attrs = NULL;
93         }
94
95         if ( lud->lud_exts != NULL ) {
96                 free( lud->lud_exts );
97                 lud->lud_exts = NULL;
98         }
99
100         if ( uri == NULL ) {
101                 fprintf( stderr, "unable to generate URI\n" );
102                 exit( EXIT_FAILURE );
103         }
104
105         printf( "%s\n", uri );
106         free( uri );
107
108         return 0;
109 }
110
111 static int
112 do_uri_explode( const char *uri )
113 {
114         LDAPURLDesc     *lud;
115         int             rc;
116
117         rc = ldap_url_parse( uri, &lud );
118         if ( rc != LDAP_URL_SUCCESS ) {
119                 fprintf( stderr, "unable to parse URI \"%s\"\n", uri );
120                 return 1;
121         }
122
123         if ( lud->lud_scheme != NULL && lud->lud_scheme[0] != '\0' ) {
124                 printf( "scheme: %s\n", lud->lud_scheme );
125         }
126
127         if ( lud->lud_host != NULL && lud->lud_host[0] != '\0' ) {
128                 printf( "host: %s\n", lud->lud_host );
129         }
130
131         if ( lud->lud_port != 0 ) {
132                 printf( "port: %d\n", lud->lud_port );
133         }
134
135         if ( lud->lud_dn != NULL && lud->lud_dn[0] != '\0' ) {
136                 printf( "dn: %s\n", lud->lud_dn );
137         }
138
139         if ( lud->lud_attrs != NULL ) {
140                 int     i;
141
142                 for ( i = 0; lud->lud_attrs[i] != NULL; i++ ) {
143                         printf( "selector: %s\n", lud->lud_attrs[i] );
144                 }
145         }
146
147         if ( lud->lud_scope != LDAP_SCOPE_DEFAULT ) {
148                 printf( "scope: %s\n", ldap_pvt_scope2str( lud->lud_scope ) );
149         }
150
151         if ( lud->lud_filter != NULL && lud->lud_filter[0] != '\0' ) {
152                 printf( "filter: %s\n", lud->lud_filter );
153         }
154
155         if ( lud->lud_exts != NULL ) {
156                 int     i;
157
158                 for ( i = 0; lud->lud_exts[i] != NULL; i++ ) {
159                         printf( "extension: %s\n", lud->lud_exts[i] );
160                 }
161         }
162         ldap_free_urldesc( lud );
163
164         return 0;
165 }
166
167 int
168 main( int argc, char *argv[])
169 {
170         LDAPURLDesc     lud = { 0 };
171         char            *uri = NULL;
172         int             gotlud = 0;
173         int             nexts = 0;
174
175         lud.lud_port = -1;
176         lud.lud_scope = -1;
177
178         while ( 1 ) {
179                 int opt = getopt( argc, argv, "S:h:p:b:a:s:f:E:H:" );
180
181                 if ( opt == EOF ) {
182                         break;
183                 }
184
185                 if ( opt == 'H' ) {
186                         if ( gotlud ) {
187                                 fprintf( stderr, "option -H incompatible with previous options\n" );
188                                 usage();
189                         }
190
191                         if ( uri != NULL ) {
192                                 fprintf( stderr, "URI already provided\n" );
193                                 usage();
194                         }
195
196                         uri = optarg;
197                         continue;
198                 }
199
200                 switch ( opt ) {
201                 case 'S':
202                 case 'h':
203                 case 'p':
204                 case 'b':
205                 case 'a':
206                 case 's':
207                 case 'f':
208                 case 'E':
209                         if ( uri != NULL ) {
210                                 fprintf( stderr, "option -%c incompatible with -H\n", opt );
211                                 usage();
212                         }
213                         gotlud++;
214                 }
215
216                 switch ( opt ) {
217                 case 'S':
218                         if ( lud.lud_scheme != NULL ) {
219                                 fprintf( stderr, "scheme already provided\n" );
220                                 usage();
221                         }
222                         lud.lud_scheme = optarg;
223                         break;
224
225                 case 'h':
226                         if ( lud.lud_host != NULL ) {
227                                 fprintf( stderr, "host already provided\n" );
228                                 usage();
229                         }
230                         lud.lud_host = optarg;
231                         break;
232
233                 case 'p':
234                         if ( lud.lud_port != -1 ) {
235                                 fprintf( stderr, "port already provided\n" );
236                                 usage();
237                         }
238
239                         if ( lutil_atoi( &lud.lud_port, optarg ) ) {
240                                 fprintf( stderr, "unable to parse port \"%s\"\n", optarg );
241                                 usage();
242                         }
243                         break;
244
245                 case 'b':
246                         if ( lud.lud_dn != NULL ) {
247                                 fprintf( stderr, "base already provided\n" );
248                                 usage();
249                         }
250                         lud.lud_dn = optarg;
251                         break;
252
253                 case 'a':
254                         if ( lud.lud_attrs != NULL ) {
255                                 fprintf( stderr, "attrs already provided\n" );
256                                 usage();
257                         }
258                         lud.lud_attrs = ldap_str2charray( optarg, "," );
259                         if ( lud.lud_attrs == NULL ) {
260                                 fprintf( stderr, "unable to parse attrs list \"%s\"\n", optarg );
261                                 usage();
262                         }
263                         break;
264
265                 case 's':
266                         if ( lud.lud_scope != -1 ) {
267                                 fprintf( stderr, "scope already provided\n" );
268                                 usage();
269                         }
270
271                         lud.lud_scope = ldap_pvt_str2scope( optarg );
272                         if ( lud.lud_scope == -1 ) {
273                                 fprintf( stderr, "unable to parse scope \"%s\"\n", optarg );
274                                 usage();
275                         }
276                         break;
277
278                 case 'f':
279                         if ( lud.lud_filter != NULL ) {
280                                 fprintf( stderr, "filter already provided\n" );
281                                 usage();
282                         }
283                         lud.lud_filter = optarg;
284                         break;
285
286                 case 'E':
287                         lud.lud_exts = (char **)realloc( lud.lud_exts,
288                                 sizeof( char * ) * ( nexts + 2 ) );
289                         lud.lud_exts[ nexts++ ] = optarg;
290                         lud.lud_exts[ nexts ] = NULL;
291                         break;
292
293                 default:
294                         assert( opt != 'H' );
295                         usage();
296                 }
297         }
298
299         if ( uri != NULL ) {
300                 return do_uri_explode( uri );
301
302         }
303
304         return do_uri_create( &lud );
305 }