1 /* common.c - common routines for the ldap client tools */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2006 The OpenLDAP Foundation.
6 * Portions Copyright 2003 Kurt D. Zeilenga.
7 * Portions Copyright 2003 IBM Corporation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted only as authorized by the OpenLDAP
14 * A copy of this license is available in the file LICENSE in the
15 * top-level directory of the distribution or, alternatively, at
16 * <http://www.OpenLDAP.org/license.html>.
19 * This file was initially created by Hallvard B. Furuseth based (in
20 * part) upon argument parsing code for individual tools located in
21 * this directory. Additional contributors include:
22 * Kurt D. Zeilenga (additional common argument and control support)
29 #include <ac/stdlib.h>
30 #include <ac/signal.h>
31 #include <ac/string.h>
33 #include <ac/unistd.h>
36 #ifdef HAVE_CYRUS_SASL
37 #ifdef HAVE_SASL_SASL_H
38 #include <sasl/sasl.h>
48 #include "lutil_ldap.h"
49 #include "ldap_defaults.h"
55 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
57 /* Necessary for old LDAPv2 Kerberos Bind methods */
59 ldap_bind LDAP_P(( /* deprecated */
62 LDAP_CONST char *passwd,
67 /* input-related vars */
69 /* misc. parameters */
70 tool_type_t tool_type;
82 char *ldaphost = NULL;
92 struct berval passwd = { 0, NULL };
94 #ifdef HAVE_CYRUS_SASL
95 unsigned sasl_flags = LDAP_SASL_AUTOMATIC;
96 char *sasl_realm = NULL;
97 char *sasl_authc_id = NULL;
98 char *sasl_authz_id = NULL;
99 char *sasl_mech = NULL;
100 char *sasl_secprops = NULL;
105 char *assertion = NULL;
106 char *authzid = NULL;
107 /* support deprecated early version of proxyAuthz */
108 #define LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ "2.16.840.1.113730.3.4.12"
109 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
110 char *proxydn = NULL;
111 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
117 static char *preread_attrs = NULL;
119 static char *postread_attrs = NULL;
120 ber_int_t pr_morePagedResults = 1;
121 struct berval pr_cookie = { 0, NULL };
122 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
124 static int chainingResolve = -1;
125 static int chainingContinuation = -1;
126 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
128 typedef int (*print_ctrl_fn)( LDAP *ld, LDAPControl *ctrl );
130 static int print_preread( LDAP *ld, LDAPControl *ctrl );
131 static int print_postread( LDAP *ld, LDAPControl *ctrl );
132 static int print_paged_results( LDAP *ld, LDAPControl *ctrl );
133 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
134 static int print_ppolicy( LDAP *ld, LDAPControl *ctrl );
137 static struct tool_ctrls_t {
141 } tool_ctrl_response[] = {
142 { LDAP_CONTROL_PRE_READ, TOOL_ALL, print_preread },
143 { LDAP_CONTROL_POST_READ, TOOL_ALL, print_postread },
144 { LDAP_CONTROL_PAGEDRESULTS, TOOL_SEARCH, print_paged_results },
145 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
146 { LDAP_CONTROL_PASSWORDPOLICYRESPONSE, TOOL_ALL, print_ppolicy },
162 tool_init( tool_type_t type )
165 ldap_pvt_setlocale(LC_MESSAGES, "");
166 ldap_pvt_bindtextdomain(OPENLDAP_PACKAGE, LDAP_LOCALEDIR);
167 ldap_pvt_textdomain(OPENLDAP_PACKAGE);
173 #ifdef HAVE_CYRUS_SASL
177 ldap_pvt_tls_destroy();
182 tool_common_usage( void )
184 static const char *const descriptions[] = {
185 N_(" -c continuous operation mode (do not stop on errors)\n"),
186 N_(" -C chase referrals (anonymously)\n"),
187 N_(" -d level set LDAP debugging level to `level'\n"),
188 N_(" -D binddn bind DN\n"),
189 N_(" -e [!]<ext>[=<extparam>] general extensions (! indicates criticality)\n")
190 N_(" [!]assert=<filter> (a RFC 4515 Filter string)\n")
191 N_(" [!]authzid=<authzid> (\"dn:<dn>\" or \"u:<user>\")\n")
192 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
194 /* non-advertized support for proxyDN */
195 N_(" [!]proxydn=<dn> (a RFC 4514 DN string)\n")
198 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
199 N_(" [!]chaining[=<resolveBehavior>[/<continuationBehavior>]]\n")
200 N_(" one of \"chainingPreferred\", \"chainingRequired\",\n")
201 N_(" \"referralsPreferred\", \"referralsRequired\"\n")
202 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
203 N_(" [!]manageDSAit\n")
205 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
208 N_(" [!]postread[=<attrs>] (a comma-separated attribute list)\n")
209 N_(" [!]preread[=<attrs>] (a comma-separated attribute list)\n")
213 N_(" abandon, cancel (SIGINT sends abandon/cancel; not really controls)\n"),
214 N_(" -f file read operations from `file'\n"),
215 N_(" -h host LDAP server\n"),
216 N_(" -H URI LDAP Uniform Resource Indentifier(s)\n"),
217 N_(" -I use SASL Interactive mode\n"),
218 N_(" -k use Kerberos authentication\n"),
219 N_(" -K like -k, but do only step 1 of the Kerberos bind\n"),
220 N_(" -M enable Manage DSA IT control (-MM to make critical)\n"),
221 N_(" -n show what would be done but don't actually do it\n"),
222 N_(" -O props SASL security properties\n"),
223 N_(" -p port port on LDAP server\n"),
224 N_(" -P version procotol version (default: 3)\n"),
225 N_(" -Q use SASL Quiet mode\n"),
226 N_(" -R realm SASL realm\n"),
227 N_(" -U authcid SASL authentication identity\n"),
228 N_(" -v run in verbose mode (diagnostics to standard output)\n"),
229 N_(" -V print version info (-VV only)\n"),
230 N_(" -w passwd bind password (for simple authentication)\n"),
231 N_(" -W prompt for bind password\n"),
232 N_(" -x Simple authentication\n"),
233 N_(" -X authzid SASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"),
234 N_(" -y file Read password from file\n"),
235 N_(" -Y mech SASL mechanism\n"),
236 N_(" -Z Start TLS request (-ZZ to require successful response)\n"),
239 const char *const *cpp;
241 fputs( _("Common options:\n"), stderr );
242 for( cpp = descriptions; *cpp != NULL; cpp++ ) {
243 if( strchr( options, (*cpp)[3] ) || (*cpp)[3] == ' ' ) {
244 fputs( _(*cpp), stderr );
257 fprintf( stderr, "%s: %s (%d)%s\n",
258 func, ldap_err2string( err ), err, extra ? extra : "" );
260 if ( matched && *matched ) {
261 fprintf( stderr, _("\tmatched DN: %s\n"), matched );
264 if ( info && *info ) {
265 fprintf( stderr, _("\tadditional info: %s\n"), info );
268 if ( refs && *refs ) {
270 fprintf( stderr, _("\treferrals:\n") );
271 for( i=0; refs[i]; i++ ) {
272 fprintf( stderr, "\t\t%s\n", refs[i] );
279 tool_args( int argc, char **argv )
283 while (( i = getopt( argc, argv, options )) != EOF ) {
285 char *control, *cvalue, *next;
287 case 'c': /* continuous operation mode */
294 ival = strtol( optarg, &next, 10 );
295 if (next == NULL || next[0] != '\0') {
296 fprintf( stderr, "%s: unable to parse debug value \"%s\"\n", prog, optarg);
301 case 'D': /* bind DN */
302 if( binddn != NULL ) {
303 fprintf( stderr, "%s: -D previously specified\n", prog );
304 exit( EXIT_FAILURE );
306 binddn = ber_strdup( optarg );
308 case 'e': /* general extensions (controls and such) */
309 /* should be extended to support comma separated list of
310 * [!]key[=value] parameters, e.g. -e !foo,bar=567
315 if( optarg[0] == '!' ) {
320 control = ber_strdup( optarg );
321 if ( (cvalue = strchr( control, '=' )) != NULL ) {
325 if ( strcasecmp( control, "assert" ) == 0 ) {
327 fprintf( stderr, "assert control previously specified\n");
328 exit( EXIT_FAILURE );
330 if( cvalue == NULL ) {
331 fprintf( stderr, "assert: control value expected\n" );
335 assertctl = 1 + crit;
337 assert( assertion == NULL );
340 } else if ( strcasecmp( control, "authzid" ) == 0 ) {
341 if( authzid != NULL ) {
342 fprintf( stderr, "authzid control previously specified\n");
343 exit( EXIT_FAILURE );
345 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
346 if( proxydn != NULL ) {
347 fprintf( stderr, "authzid control incompatible with proxydn\n");
348 exit( EXIT_FAILURE );
350 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
351 if( cvalue == NULL ) {
352 fprintf( stderr, "authzid: control value expected\n" );
356 fprintf( stderr, "authzid: must be marked critical\n" );
360 assert( authzid == NULL );
363 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
364 } else if ( strcasecmp( control, "proxydn" ) == 0 ) {
365 if( proxydn != NULL ) {
366 fprintf( stderr, "proxydn control previously specified\n");
367 exit( EXIT_FAILURE );
369 if( authzid != NULL ) {
370 fprintf( stderr, "proxydn control incompatible with authzid\n");
371 exit( EXIT_FAILURE );
373 if( cvalue == NULL ) {
374 fprintf( stderr, "proxydn: control value expected\n" );
378 fprintf( stderr, "proxydn: must be marked critical\n" );
382 assert( proxydn == NULL );
384 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
386 } else if ( ( strcasecmp( control, "relax" ) == 0 ) ||
387 ( strcasecmp( control, "manageDIT" ) == 0 ) )
391 "relax control previously specified\n");
392 exit( EXIT_FAILURE );
394 if( cvalue != NULL ) {
396 "relax: no control value expected\n" );
400 manageDIT = 1 + crit;
402 } else if ( strcasecmp( control, "manageDSAit" ) == 0 ) {
405 "manageDSAit control previously specified\n");
406 exit( EXIT_FAILURE );
408 if( cvalue != NULL ) {
410 "manageDSAit: no control value expected\n" );
414 manageDSAit = 1 + crit;
416 } else if ( strcasecmp( control, "noop" ) == 0 ) {
418 fprintf( stderr, "noop control previously specified\n");
419 exit( EXIT_FAILURE );
421 if( cvalue != NULL ) {
422 fprintf( stderr, "noop: no control value expected\n" );
428 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
429 } else if ( strcasecmp( control, "ppolicy" ) == 0 ) {
431 fprintf( stderr, "ppolicy control previously specified\n");
432 exit( EXIT_FAILURE );
434 if( cvalue != NULL ) {
435 fprintf( stderr, "ppolicy: no control value expected\n" );
439 fprintf( stderr, "ppolicy: critical flag not allowed\n" );
446 } else if ( strcasecmp( control, "preread" ) == 0 ) {
448 fprintf( stderr, "preread control previously specified\n");
449 exit( EXIT_FAILURE );
453 preread_attrs = cvalue;
455 } else if ( strcasecmp( control, "postread" ) == 0 ) {
457 fprintf( stderr, "postread control previously specified\n");
458 exit( EXIT_FAILURE );
462 postread_attrs = cvalue;
464 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
465 } else if ( strcasecmp( control, "chaining" ) == 0 ) {
468 if ( cvalue != NULL ) {
471 continuation = strchr( cvalue, '/' );
472 if ( continuation ) {
473 /* FIXME: this makes sense only in searches */
474 *continuation++ = '\0';
475 if ( strcasecmp( continuation, "chainingPreferred" ) == 0 ) {
476 chainingContinuation = LDAP_CHAINING_PREFERRED;
477 } else if ( strcasecmp( continuation, "chainingRequired" ) == 0 ) {
478 chainingContinuation = LDAP_CHAINING_REQUIRED;
479 } else if ( strcasecmp( continuation, "referralsPreferred" ) == 0 ) {
480 chainingContinuation = LDAP_REFERRALS_PREFERRED;
481 } else if ( strcasecmp( continuation, "referralsRequired" ) == 0 ) {
482 chainingContinuation = LDAP_REFERRALS_REQUIRED;
485 "chaining behavior control "
486 "continuation value \"%s\" invalid\n",
488 exit( EXIT_FAILURE );
492 if ( strcasecmp( cvalue, "chainingPreferred" ) == 0 ) {
493 chainingResolve = LDAP_CHAINING_PREFERRED;
494 } else if ( strcasecmp( cvalue, "chainingRequired" ) == 0 ) {
495 chainingResolve = LDAP_CHAINING_REQUIRED;
496 } else if ( strcasecmp( cvalue, "referralsPreferred" ) == 0 ) {
497 chainingResolve = LDAP_REFERRALS_PREFERRED;
498 } else if ( strcasecmp( cvalue, "referralsRequired" ) == 0 ) {
499 chainingResolve = LDAP_REFERRALS_REQUIRED;
502 "chaining behavior control "
503 "resolve value \"%s\" invalid\n",
505 exit( EXIT_FAILURE );
508 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
510 /* this shouldn't go here, really; but it's a feature... */
511 } else if ( strcasecmp( control, "abandon" ) == 0 ) {
512 abcan = LDAP_REQ_ABANDON;
514 } else if ( strcasecmp( control, "cancel" ) == 0 ) {
515 abcan = LDAP_REQ_EXTENDED;
518 fprintf( stderr, "Invalid general control name: %s\n",
523 case 'f': /* read from file */
524 if( infile != NULL ) {
525 fprintf( stderr, "%s: -f previously specified\n", prog );
526 exit( EXIT_FAILURE );
528 infile = ber_strdup( optarg );
530 case 'h': /* ldap host */
531 if( ldaphost != NULL ) {
532 fprintf( stderr, "%s: -h previously specified\n", prog );
533 exit( EXIT_FAILURE );
535 ldaphost = ber_strdup( optarg );
537 case 'H': /* ldap URI */
538 if( ldapuri != NULL ) {
539 fprintf( stderr, "%s: -H previously specified\n", prog );
540 exit( EXIT_FAILURE );
542 ldapuri = ber_strdup( optarg );
545 #ifdef HAVE_CYRUS_SASL
546 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
547 fprintf( stderr, "%s: incompatible previous "
548 "authentication choice\n",
550 exit( EXIT_FAILURE );
552 authmethod = LDAP_AUTH_SASL;
553 sasl_flags = LDAP_SASL_INTERACTIVE;
556 fprintf( stderr, "%s: was not compiled with SASL support\n",
558 exit( EXIT_FAILURE );
560 case 'k': /* kerberos bind */
561 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
562 if( authmethod != -1 ) {
563 fprintf( stderr, "%s: -k incompatible with previous "
564 "authentication choice\n", prog );
565 exit( EXIT_FAILURE );
567 authmethod = LDAP_AUTH_KRBV4;
569 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
570 exit( EXIT_FAILURE );
573 case 'K': /* kerberos bind, part one only */
574 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
575 if( authmethod != -1 ) {
576 fprintf( stderr, "%s: incompatible with previous "
577 "authentication choice\n", prog );
578 exit( EXIT_FAILURE );
580 authmethod = LDAP_AUTH_KRBV41;
582 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
583 exit( EXIT_FAILURE );
587 /* enable Manage DSA IT */
590 case 'n': /* print operations, don't actually do them */
594 #ifdef HAVE_CYRUS_SASL
595 if( sasl_secprops != NULL ) {
596 fprintf( stderr, "%s: -O previously specified\n", prog );
597 exit( EXIT_FAILURE );
599 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
600 fprintf( stderr, "%s: incompatible previous "
601 "authentication choice\n", prog );
602 exit( EXIT_FAILURE );
604 authmethod = LDAP_AUTH_SASL;
605 sasl_secprops = ber_strdup( optarg );
607 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
608 exit( EXIT_FAILURE );
613 fprintf( stderr, "%s: -p previously specified\n", prog );
614 exit( EXIT_FAILURE );
616 ival = strtol( optarg, &next, 10 );
617 if ( next == NULL || next[0] != '\0' ) {
618 fprintf( stderr, "%s: unable to parse port number \"%s\"\n", prog, optarg );
619 exit( EXIT_FAILURE );
624 ival = strtol( optarg, &next, 10 );
625 if ( next == NULL || next[0] != '\0' ) {
626 fprintf( stderr, "%s: unabel to parse protocol version \"%s\"\n", prog, optarg );
627 exit( EXIT_FAILURE );
631 if( protocol == LDAP_VERSION3 ) {
632 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
634 exit( EXIT_FAILURE );
636 protocol = LDAP_VERSION2;
639 if( protocol == LDAP_VERSION2 ) {
640 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
642 exit( EXIT_FAILURE );
644 protocol = LDAP_VERSION3;
647 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
653 #ifdef HAVE_CYRUS_SASL
654 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
655 fprintf( stderr, "%s: incompatible previous "
656 "authentication choice\n",
658 exit( EXIT_FAILURE );
660 authmethod = LDAP_AUTH_SASL;
661 sasl_flags = LDAP_SASL_QUIET;
664 fprintf( stderr, "%s: not compiled with SASL support\n",
666 exit( EXIT_FAILURE );
669 #ifdef HAVE_CYRUS_SASL
670 if( sasl_realm != NULL ) {
671 fprintf( stderr, "%s: -R previously specified\n", prog );
672 exit( EXIT_FAILURE );
674 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
675 fprintf( stderr, "%s: incompatible previous "
676 "authentication choice\n",
678 exit( EXIT_FAILURE );
680 authmethod = LDAP_AUTH_SASL;
681 sasl_realm = ber_strdup( optarg );
683 fprintf( stderr, "%s: not compiled with SASL support\n",
685 exit( EXIT_FAILURE );
689 #ifdef HAVE_CYRUS_SASL
690 if( sasl_authc_id != NULL ) {
691 fprintf( stderr, "%s: -U previously specified\n", prog );
692 exit( EXIT_FAILURE );
694 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
695 fprintf( stderr, "%s: incompatible previous "
696 "authentication choice\n",
698 exit( EXIT_FAILURE );
700 authmethod = LDAP_AUTH_SASL;
701 sasl_authc_id = ber_strdup( optarg );
703 fprintf( stderr, "%s: not compiled with SASL support\n",
705 exit( EXIT_FAILURE );
708 case 'v': /* verbose mode */
711 case 'V': /* version */
714 case 'w': /* password */
715 passwd.bv_val = ber_strdup( optarg );
719 for( p = optarg; *p != '\0'; p++ ) {
723 passwd.bv_len = strlen( passwd.bv_val );
732 #ifdef HAVE_CYRUS_SASL
733 if( sasl_mech != NULL ) {
734 fprintf( stderr, "%s: -Y previously specified\n", prog );
735 exit( EXIT_FAILURE );
737 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
739 "%s: incompatible with authentication choice\n", prog );
740 exit( EXIT_FAILURE );
742 authmethod = LDAP_AUTH_SASL;
743 sasl_mech = ber_strdup( optarg );
745 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
746 exit( EXIT_FAILURE );
750 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
751 fprintf( stderr, "%s: incompatible with previous "
752 "authentication choice\n", prog );
753 exit( EXIT_FAILURE );
755 authmethod = LDAP_AUTH_SIMPLE;
758 #ifdef HAVE_CYRUS_SASL
759 if( sasl_authz_id != NULL ) {
760 fprintf( stderr, "%s: -X previously specified\n", prog );
761 exit( EXIT_FAILURE );
763 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
764 fprintf( stderr, "%s: -X incompatible with "
765 "authentication choice\n", prog );
766 exit( EXIT_FAILURE );
768 authmethod = LDAP_AUTH_SASL;
769 sasl_authz_id = ber_strdup( optarg );
771 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
772 exit( EXIT_FAILURE );
779 fprintf( stderr, "%s: not compiled with TLS support\n", prog );
780 exit( EXIT_FAILURE );
784 if( handle_private_option( i ) ) break;
785 fprintf( stderr, "%s: unrecognized option -%c\n",
792 /* prevent bad linking */
794 api.ldapai_info_version = LDAP_API_INFO_VERSION;
796 if ( ldap_get_option(NULL, LDAP_OPT_API_INFO, &api)
797 != LDAP_OPT_SUCCESS )
799 fprintf( stderr, "%s: ldap_get_option(API_INFO) failed\n", prog );
800 exit( EXIT_FAILURE );
803 if (api.ldapai_info_version != LDAP_API_INFO_VERSION) {
804 fprintf( stderr, "LDAP APIInfo version mismatch: "
805 "library %d, header %d\n",
806 api.ldapai_info_version, LDAP_API_INFO_VERSION );
807 exit( EXIT_FAILURE );
810 if( api.ldapai_api_version != LDAP_API_VERSION ) {
811 fprintf( stderr, "LDAP API version mismatch: "
812 "library %d, header %d\n",
813 api.ldapai_api_version, LDAP_API_VERSION );
814 exit( EXIT_FAILURE );
817 if( strcmp(api.ldapai_vendor_name, LDAP_VENDOR_NAME ) != 0 ) {
818 fprintf( stderr, "LDAP vendor name mismatch: "
819 "library %s, header %s\n",
820 api.ldapai_vendor_name, LDAP_VENDOR_NAME );
821 exit( EXIT_FAILURE );
824 if( api.ldapai_vendor_version != LDAP_VENDOR_VERSION ) {
825 fprintf( stderr, "LDAP vendor version mismatch: "
826 "library %d, header %d\n",
827 api.ldapai_vendor_version, LDAP_VENDOR_VERSION );
828 exit( EXIT_FAILURE );
832 fprintf( stderr, "%s: %s\t(LDAP library: %s %d)\n",
834 LDAP_VENDOR_NAME, LDAP_VENDOR_VERSION );
835 if (version > 1) exit( EXIT_SUCCESS );
838 ldap_memfree( api.ldapai_vendor_name );
839 ber_memvfree( (void **)api.ldapai_extensions );
843 protocol = LDAP_VERSION3;
845 if (authmethod == -1 && protocol > LDAP_VERSION2) {
846 #ifdef HAVE_CYRUS_SASL
847 authmethod = LDAP_AUTH_SASL;
849 authmethod = LDAP_AUTH_SIMPLE;
853 if( ldapuri == NULL ) {
854 if( ldapport && ( ldaphost == NULL )) {
855 fprintf( stderr, "%s: -p without -h is invalid.\n", prog );
856 exit( EXIT_FAILURE );
859 if( ldaphost != NULL ) {
860 fprintf( stderr, "%s: -H incompatible with -h\n", prog );
861 exit( EXIT_FAILURE );
864 fprintf( stderr, "%s: -H incompatible with -p\n", prog );
865 exit( EXIT_FAILURE );
869 if( protocol == LDAP_VERSION2 ) {
870 if( assertctl || authzid || manageDIT || manageDSAit ||
871 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
873 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
874 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
877 noop || ppolicy || preread || postread )
879 fprintf( stderr, "%s: -e/-M incompatible with LDAPv2\n", prog );
880 exit( EXIT_FAILURE );
884 fprintf( stderr, "%s: -Z incompatible with LDAPv2\n", prog );
885 exit( EXIT_FAILURE );
888 #ifdef HAVE_CYRUS_SASL
889 if( authmethod == LDAP_AUTH_SASL ) {
890 fprintf( stderr, "%s: -[IOQRUXY] incompatible with LDAPv2\n",
892 exit( EXIT_FAILURE );
896 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
898 if ( authmethod == LDAP_AUTH_KRBV4 || authmethod == LDAP_AUTH_KRBV41 ) {
899 fprintf( stderr, "%s: -k/-K incompatible with LDAPv%d\n",
901 exit( EXIT_FAILURE );
909 tool_conn_setup( int dont, void (*private_setup)( LDAP * ) )
914 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug )
915 != LBER_OPT_SUCCESS )
918 "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
920 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug )
921 != LDAP_OPT_SUCCESS )
924 "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
929 (void) SIGNAL( SIGPIPE, SIG_IGN );
933 SIGNAL( SIGINT, do_sig );
939 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
942 memset( &url, 0, sizeof(url));
944 url.lud_scheme = "ldap";
945 url.lud_host = ldaphost;
946 url.lud_port = ldapport;
947 url.lud_scope = LDAP_SCOPE_DEFAULT;
949 ldapuri = ldap_url_desc2str( &url );
953 fprintf( stderr, "ldap_initialize( %s )\n",
954 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
956 rc = ldap_initialize( &ld, ldapuri );
957 if( rc != LDAP_SUCCESS ) {
959 "Could not create LDAP session handle for URI=%s (%d): %s\n",
960 ldapuri, rc, ldap_err2string(rc) );
961 exit( EXIT_FAILURE );
964 if( private_setup ) private_setup( ld );
967 if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
968 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
970 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
971 referrals ? "on" : "off" );
972 exit( EXIT_FAILURE );
975 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &protocol )
976 != LDAP_OPT_SUCCESS )
978 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
980 exit( EXIT_FAILURE );
984 rc = ldap_start_tls_s( ld, NULL, NULL );
985 if ( rc != LDAP_SUCCESS ) {
986 tool_perror( "ldap_start_tls", rc, NULL, NULL, NULL, NULL );
988 exit( EXIT_FAILURE );
999 tool_bind( LDAP *ld )
1001 LDAPControl **sctrlsp = NULL;
1002 LDAPControl *sctrls[2];
1005 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1008 c.ldctl_oid = LDAP_CONTROL_PASSWORDPOLICYREQUEST;
1009 c.ldctl_value.bv_val = NULL;
1010 c.ldctl_value.bv_len = 0;
1011 c.ldctl_iscritical = 0;
1012 sctrls[nsctrls] = &c;
1013 sctrls[++nsctrls] = NULL;
1021 assert( nsctrls < sizeof(sctrls)/sizeof(sctrls[0]) );
1023 if ( authmethod == LDAP_AUTH_SASL ) {
1024 #ifdef HAVE_CYRUS_SASL
1028 if( sasl_secprops != NULL ) {
1029 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
1030 (void *) sasl_secprops );
1032 if( rc != LDAP_OPT_SUCCESS ) {
1034 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
1036 exit( LDAP_LOCAL_ERROR );
1040 defaults = lutil_sasl_defaults( ld,
1047 rc = ldap_sasl_interactive_bind_s( ld, binddn, sasl_mech,
1049 NULL, sasl_flags, lutil_sasl_interact, defaults );
1051 lutil_sasl_freedefs( defaults );
1052 if( rc != LDAP_SUCCESS ) {
1053 tool_perror( "ldap_sasl_interactive_bind_s",
1054 rc, NULL, NULL, NULL, NULL );
1058 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
1059 exit( LDAP_NOT_SUPPORTED );
1063 LDAPMessage *result;
1064 LDAPControl **ctrls;
1066 char *matched = NULL;
1072 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
1073 if ( authmethod == LDAP_AUTH_KRBV4 || authmethod == LDAP_AUTH_KRBV41 ) {
1074 msgid = ldap_bind( ld, binddn, passwd.bv_val, authmethod );
1075 if ( msgid == -1 ) {
1076 tool_perror( "ldap_bind", -1, NULL, NULL, NULL, NULL );
1077 exit( LDAP_LOCAL_ERROR );
1083 rc = ldap_sasl_bind( ld, binddn, LDAP_SASL_SIMPLE, &passwd,
1084 sctrlsp, NULL, &msgid );
1085 if ( msgid == -1 ) {
1086 tool_perror( "ldap_sasl_bind(SIMPLE)", rc,
1087 NULL, NULL, NULL, NULL );
1092 if ( ldap_result( ld, msgid, LDAP_MSG_ALL, NULL, &result ) == -1 ) {
1093 tool_perror( "ldap_result", -1, NULL, NULL, NULL, NULL );
1094 exit( LDAP_LOCAL_ERROR );
1097 rc = ldap_parse_result( ld, result, &err, &matched, &info, &refs,
1099 if ( rc != LDAP_SUCCESS ) {
1100 tool_perror( "ldap_bind parse result", rc, NULL, NULL, NULL, NULL );
1101 exit( LDAP_LOCAL_ERROR );
1104 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1105 if ( ctrls && ppolicy ) {
1107 int expire, grace, len = 0;
1108 LDAPPasswordPolicyError pErr = -1;
1110 ctrl = ldap_find_control( LDAP_CONTROL_PASSWORDPOLICYRESPONSE,
1113 if ( ctrl && ldap_parse_passwordpolicy_control( ld, ctrl,
1114 &expire, &grace, &pErr ) == LDAP_SUCCESS )
1116 if ( pErr != PP_noError ){
1119 strcpy( msgbuf+2, ldap_passwordpolicy_err2txt( pErr ));
1120 len = strlen( msgbuf );
1122 if ( expire >= 0 ) {
1123 sprintf( msgbuf+len,
1124 " (Password expires in %d seconds)",
1126 } else if ( grace >= 0 ) {
1127 sprintf( msgbuf+len,
1128 " (Password expired, %d grace logins remain)",
1136 ldap_controls_free( ctrls );
1139 if ( err != LDAP_SUCCESS
1141 || ( matched && matched[ 0 ] )
1142 || ( info && info[ 0 ] )
1145 tool_perror( "ldap_bind", err, msgbuf, matched, info, refs );
1147 if( matched ) ber_memfree( matched );
1148 if( info ) ber_memfree( info );
1149 if( refs ) ber_memvfree( (void **)refs );
1151 if ( err != LDAP_SUCCESS ) exit( err );
1157 tool_unbind( LDAP *ld )
1159 int err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, NULL );
1161 if ( err != LDAP_OPT_SUCCESS ) {
1162 fprintf( stderr, "Could not unset controls\n");
1165 (void) ldap_unbind_ext( ld, NULL, NULL );
1169 /* Set server controls. Add controls extra_c[0..count-1], if set. */
1171 tool_server_controls( LDAP *ld, LDAPControl *extra_c, int count )
1173 int i = 0, j, crit = 0, err;
1174 LDAPControl c[10], **ctrls;
1178 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
1180 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
1186 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
1188 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
1194 ctrls = (LDAPControl**) malloc(sizeof(c) + (count+1)*sizeof(LDAPControl*));
1195 if ( ctrls == NULL ) {
1196 fprintf( stderr, "No memory\n" );
1197 exit( EXIT_FAILURE );
1201 BerElementBuffer berbuf;
1202 BerElement *ber = (BerElement *)&berbuf;
1204 if( assertion == NULL || *assertion == '\0' ) {
1205 fprintf( stderr, "Assertion=<empty>\n" );
1206 exit( EXIT_FAILURE );
1209 ber_init2( ber, NULL, LBER_USE_DER );
1211 err = ldap_pvt_put_filter( ber, assertion );
1213 fprintf( stderr, "assertion encode failed (%d)\n", err );
1214 exit( EXIT_FAILURE );
1217 err = ber_flatten2( ber, &c[i].ldctl_value, 0 );
1219 fprintf( stderr, "assertion flatten failed (%d)\n", err );
1220 exit( EXIT_FAILURE );
1223 c[i].ldctl_oid = LDAP_CONTROL_ASSERT;
1224 c[i].ldctl_iscritical = assertctl > 1;
1230 c[i].ldctl_value.bv_val = authzid;
1231 c[i].ldctl_value.bv_len = strlen( authzid );
1232 c[i].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
1233 c[i].ldctl_iscritical = 1;
1238 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
1239 /* NOTE: doesn't need an extra count because it's incompatible
1242 BerElementBuffer berbuf;
1243 BerElement *ber = (BerElement *)&berbuf;
1245 ber_init2( ber, NULL, LBER_USE_DER );
1247 if ( ber_printf( ber, "s", proxydn ) == LBER_ERROR ) {
1248 exit( EXIT_FAILURE );
1251 if ( ber_flatten2( ber, &c[i].ldctl_value, 0 ) == -1 ) {
1252 exit( EXIT_FAILURE );
1255 c[i].ldctl_oid = LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ;
1256 c[i].ldctl_iscritical = 1;
1260 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
1263 c[i].ldctl_oid = LDAP_CONTROL_MANAGEDIT;
1264 BER_BVZERO( &c[i].ldctl_value );
1265 c[i].ldctl_iscritical = manageDIT > 1;
1270 if ( manageDSAit ) {
1271 c[i].ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
1272 BER_BVZERO( &c[i].ldctl_value );
1273 c[i].ldctl_iscritical = manageDSAit > 1;
1279 c[i].ldctl_oid = LDAP_CONTROL_NOOP;
1280 BER_BVZERO( &c[i].ldctl_value );
1281 c[i].ldctl_iscritical = noop > 1;
1287 char berbuf[LBER_ELEMENT_SIZEOF];
1288 BerElement *ber = (BerElement *)berbuf;
1289 char **attrs = NULL;
1291 if( preread_attrs ) {
1292 attrs = ldap_str2charray( preread_attrs, "," );
1295 ber_init2( ber, NULL, LBER_USE_DER );
1297 if( ber_printf( ber, "{v}", attrs ) == -1 ) {
1298 fprintf( stderr, "preread attrs encode failed.\n" );
1299 exit( EXIT_FAILURE );
1302 err = ber_flatten2( ber, &c[i].ldctl_value, 0 );
1304 fprintf( stderr, "preread flatten failed (%d)\n", err );
1305 exit( EXIT_FAILURE );
1308 c[i].ldctl_oid = LDAP_CONTROL_PRE_READ;
1309 c[i].ldctl_iscritical = preread > 1;
1313 if( attrs ) ldap_charray_free( attrs );
1317 char berbuf[LBER_ELEMENT_SIZEOF];
1318 BerElement *ber = (BerElement *)berbuf;
1319 char **attrs = NULL;
1321 if( postread_attrs ) {
1322 attrs = ldap_str2charray( postread_attrs, "," );
1325 ber_init2( ber, NULL, LBER_USE_DER );
1327 if( ber_printf( ber, "{v}", attrs ) == -1 ) {
1328 fprintf( stderr, "postread attrs encode failed.\n" );
1329 exit( EXIT_FAILURE );
1332 err = ber_flatten2( ber, &c[i].ldctl_value, 0 );
1334 fprintf( stderr, "postread flatten failed (%d)\n", err );
1335 exit( EXIT_FAILURE );
1338 c[i].ldctl_oid = LDAP_CONTROL_POST_READ;
1339 c[i].ldctl_iscritical = postread > 1;
1343 if( attrs ) ldap_charray_free( attrs );
1346 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
1348 if ( chainingResolve > -1 ) {
1349 BerElementBuffer berbuf;
1350 BerElement *ber = (BerElement *)&berbuf;
1352 ber_init2( ber, NULL, LBER_USE_DER );
1354 err = ber_printf( ber, "{e" /* } */, chainingResolve );
1357 fprintf( stderr, _("Chaining behavior control encoding error!\n") );
1358 exit( EXIT_FAILURE );
1361 if ( chainingContinuation > -1 ) {
1362 err = ber_printf( ber, "e", chainingContinuation );
1365 fprintf( stderr, _("Chaining behavior control encoding error!\n") );
1366 exit( EXIT_FAILURE );
1370 err = ber_printf( ber, /* { */ "N}" );
1373 fprintf( stderr, _("Chaining behavior control encoding error!\n") );
1374 exit( EXIT_FAILURE );
1377 if ( ber_flatten2( ber, &c[i].ldctl_value, 0 ) == -1 ) {
1378 exit( EXIT_FAILURE );
1382 BER_BVZERO( &c[i].ldctl_value );
1385 c[i].ldctl_oid = LDAP_CONTROL_X_CHAINING_BEHAVIOR;
1386 c[i].ldctl_iscritical = chaining > 1;
1390 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
1393 ctrls[i++] = extra_c++;
1397 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
1399 if ( err != LDAP_OPT_SUCCESS ) {
1400 for ( j = 0; j < i; j++ ) {
1401 if ( ctrls[j]->ldctl_iscritical ) crit = 1;
1403 fprintf( stderr, "Could not set %scontrols\n",
1404 crit ? "critical " : "" );
1409 exit( EXIT_FAILURE );
1414 tool_check_abandon( LDAP *ld, int msgid )
1418 switch ( gotintr ) {
1419 case LDAP_REQ_EXTENDED:
1420 rc = ldap_cancel_s( ld, msgid, NULL, NULL );
1421 fprintf( stderr, "got interrupt, cancel got %d: %s\n",
1422 rc, ldap_err2string( rc ) );
1425 case LDAP_REQ_ABANDON:
1426 rc = ldap_abandon_ext( ld, msgid, NULL, NULL );
1427 fprintf( stderr, "got interrupt, abandon got %d: %s\n",
1428 rc, ldap_err2string( rc ) );
1436 print_prepostread( LDAP *ld, LDAPControl *ctrl, struct berval *what)
1441 tool_write_ldif( LDIF_PUT_COMMENT, "==> ",
1442 what->bv_val, what->bv_len );
1443 ber = ber_init( &ctrl->ldctl_value );
1444 if ( ber == NULL ) {
1448 } else if ( ber_scanf( ber, "{m{" /*}}*/, &bv ) == LBER_ERROR ) {
1453 tool_write_ldif( LDIF_PUT_VALUE, "dn", bv.bv_val, bv.bv_len );
1455 while ( ber_scanf( ber, "{m" /*}*/, &bv ) != LBER_ERROR ) {
1457 BerVarray vals = NULL;
1459 if ( ber_scanf( ber, "[W]", &vals ) == LBER_ERROR ||
1466 for ( i = 0; vals[ i ].bv_val != NULL; i++ ) {
1468 ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1469 bv.bv_val, vals[ i ].bv_val, vals[ i ].bv_len );
1472 ber_bvarray_free( vals );
1476 if ( ber != NULL ) {
1480 tool_write_ldif( LDIF_PUT_COMMENT, "<== ",
1481 what->bv_val, what->bv_len );
1487 print_preread( LDAP *ld, LDAPControl *ctrl )
1489 static struct berval what = BER_BVC( "preread" );
1491 return print_prepostread( ld, ctrl, &what );
1495 print_postread( LDAP *ld, LDAPControl *ctrl )
1497 static struct berval what = BER_BVC( "postread" );
1499 return print_prepostread( ld, ctrl, &what );
1503 print_paged_results( LDAP *ld, LDAPControl *ctrl )
1507 /* note: pr_cookie is being malloced; it's freed
1508 * the next time the control is sent, but the last
1509 * time it's not; we don't care too much, because
1510 * the last time an empty value is returned... */
1511 if ( ldap_parse_pageresponse_control( ld, ctrl, &estimate, &pr_cookie )
1518 /* FIXME: check buffer overflow */
1519 char buf[ BUFSIZ ], *ptr = buf;
1521 if ( estimate > 0 ) {
1522 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1523 "estimate=%d", estimate );
1526 if ( pr_cookie.bv_len > 0 ) {
1529 bv.bv_len = LUTIL_BASE64_ENCODE_LEN(
1530 pr_cookie.bv_len ) + 1;
1531 bv.bv_val = ber_memalloc( bv.bv_len + 1 );
1533 bv.bv_len = lutil_b64_ntop(
1534 (unsigned char *) pr_cookie.bv_val,
1536 bv.bv_val, bv.bv_len );
1538 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1539 "%scookie=%s", ptr == buf ? "" : " ",
1542 ber_memfree( bv.bv_val );
1544 pr_morePagedResults = 1;
1547 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1548 "%scookie=", ptr == buf ? "" : " " );
1551 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1552 "pagedresults", buf, ptr - buf );
1558 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1560 print_ppolicy( LDAP *ld, LDAPControl *ctrl )
1562 int expire = 0, grace = 0, rc;
1563 LDAPPasswordPolicyError pperr;
1565 rc = ldap_parse_passwordpolicy_control( ld, ctrl,
1566 &expire, &grace, &pperr );
1567 if ( rc == LDAP_SUCCESS ) {
1568 char buf[ BUFSIZ ], *ptr = buf;
1570 if ( expire != -1 ) {
1571 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1572 "expire=%d", expire );
1575 if ( grace != -1 ) {
1576 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1577 "%sgrace=%d", ptr == buf ? "" : " ", grace );
1580 if ( pperr != PP_noError ) {
1581 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1582 "%serror=%s", ptr == buf ? "" : " ",
1583 ldap_passwordpolicy_err2txt( pperr ) );
1586 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1587 "ppolicy", buf, ptr - buf );
1594 void tool_print_ctrls(
1596 LDAPControl **ctrls )
1601 for ( i = 0; ctrls[i] != NULL; i++ ) {
1602 /* control: OID criticality base64value */
1603 struct berval b64 = BER_BVNULL;
1609 len += strlen( ctrls[i]->ldctl_oid );
1611 /* add enough for space after OID and the critical value itself */
1612 len += ctrls[i]->ldctl_iscritical
1613 ? sizeof("true") : sizeof("false");
1615 /* convert to base64 */
1616 if ( ctrls[i]->ldctl_value.bv_len ) {
1617 b64.bv_len = LUTIL_BASE64_ENCODE_LEN(
1618 ctrls[i]->ldctl_value.bv_len ) + 1;
1619 b64.bv_val = ber_memalloc( b64.bv_len + 1 );
1621 b64.bv_len = lutil_b64_ntop(
1622 (unsigned char *) ctrls[i]->ldctl_value.bv_val,
1623 ctrls[i]->ldctl_value.bv_len,
1624 b64.bv_val, b64.bv_len );
1628 len += 1 + b64.bv_len;
1631 ptr = str = malloc( len + 1 );
1633 ptr = lutil_strcopy( ptr, ": " );
1635 ptr = lutil_strcopy( ptr, ctrls[i]->ldctl_oid );
1636 ptr = lutil_strcopy( ptr, ctrls[i]->ldctl_iscritical
1637 ? " true" : " false" );
1640 ptr = lutil_strcopy( ptr, " " );
1641 ptr = lutil_strcopy( ptr, b64.bv_val );
1645 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1646 "control", str, len );
1651 ber_memfree( b64.bv_val );
1654 /* known controls */
1655 for ( j = 0; tool_ctrl_response[j].oid != NULL; j++ ) {
1656 if ( strcmp( tool_ctrl_response[j].oid, ctrls[i]->ldctl_oid ) == 0 ) {
1657 if ( !tool_ctrl_response[j].mask & tool_type ) {
1658 /* this control should not appear
1659 * with this tool; warning? */
1665 if ( tool_ctrl_response[j].oid != NULL && tool_ctrl_response[j].func ) {
1666 (void)tool_ctrl_response[j].func( ld, ctrls[i] );
1672 tool_write_ldif( int type, char *name, char *value, ber_len_t vallen )
1676 if (( ldif = ldif_put( type, name, value, vallen )) == NULL ) {
1680 fputs( ldif, stdout );
1681 ber_memfree( ldif );
1687 tool_is_oid( const char *s )
1691 if ( !isdigit( (unsigned char) s[ 0 ] ) ) {
1695 for ( ; s[ 0 ]; s++ ) {
1696 if ( s[ 0 ] == '.' ) {
1697 if ( s[ 1 ] == '\0' ) {
1704 if ( !isdigit( (unsigned char) s[ 0 ] ) ) {
1708 if ( first == 1 && s[ 0 ] == '0' && s[ 1 ] != '.' ) {