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