]> git.sur5r.net Git - openldap/blobdiff - clients/tools/common.c
Happy new year! (belated)
[openldap] / clients / tools / common.c
index ea7b6f7e553f52ded33f1f2c1286ba4b19c6b3eb..46358ebac05183b094c6686f5521f4c759bae6e5 100644 (file)
@@ -2,7 +2,7 @@
 /* $OpenLDAP$ */
 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
- * Copyright 1998-2007 The OpenLDAP Foundation.
+ * Copyright 1998-2008 The OpenLDAP Foundation.
  * Portions Copyright 2003 Kurt D. Zeilenga.
  * Portions Copyright 2003 IBM Corporation.
  * All rights reserved.
@@ -46,6 +46,8 @@
 #include "ldap_defaults.h"
 #include "ldap_pvt.h"
 #include "lber_pvt.h"
+#include "lutil.h"
+#include "ldif.h"
 
 #include "common.h"
 
@@ -87,6 +89,7 @@ char *pw_file = NULL;
 int   referrals = 0;
 int   protocol = -1;
 int   verbose = 0;
+int   ldif = 0;
 int   version = 0;
 
 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
@@ -155,7 +158,7 @@ N_("             [!]preread[=<attrs>]   (a comma-separated attribute list)\n"),
 N_("             abandon, cancel (SIGINT sends abandon/cancel; not really controls)\n")
 N_("  -f file    read operations from `file'\n"),
 N_("  -h host    LDAP server\n"),
-N_("  -H URI     LDAP Uniform Resource Indentifier(s)\n"),
+N_("  -H URI     LDAP Uniform Resource Identifier(s)\n"),
 N_("  -I         use SASL Interactive mode\n"),
 N_("  -k         use Kerberos authentication\n"),
 N_("  -K         like -k, but do only step 1 of the Kerberos bind\n"),
@@ -163,7 +166,7 @@ N_("  -M         enable Manage DSA IT control (-MM to make critical)\n"),
 N_("  -n         show what would be done but don't actually do it\n"),
 N_("  -O props   SASL security properties\n"),
 N_("  -p port    port on LDAP server\n"),
-N_("  -P version procotol version (default: 3)\n"),
+N_("  -P version protocol version (default: 3)\n"),
 N_("  -Q         use SASL Quiet mode\n"),
 N_("  -R realm   SASL realm\n"),
 N_("  -U authcid SASL authentication identity\n"),
@@ -1272,3 +1275,126 @@ tool_check_abandon( LDAP *ld, int msgid )
        return 0;
 }
 
+#ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
+static int
+print_ppolicy( LDAP *ld, LDAPControl *ctrl )
+{
+       int expire = 0, grace = 0, rc;
+       LDAPPasswordPolicyError pperr;
+
+       rc = ldap_parse_passwordpolicy_control( ld, ctrl,
+               &expire, &grace, &pperr );
+       if ( rc == LDAP_SUCCESS ) {
+               char    buf[ BUFSIZ ], *ptr = buf;
+
+               if ( expire != -1 ) {
+                       ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
+                               "expire=%d", expire );
+               }
+
+               if ( grace != -1 ) {
+                       ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
+                               "%sgrace=%d", ptr == buf ? "" : " ", grace );
+               }
+
+               if ( pperr != PP_noError ) {
+                       ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
+                               "%serror=%d (%s)", ptr == buf ? "" : " ",
+                               pperr,
+                               ldap_passwordpolicy_err2txt( pperr ) );
+               }
+
+               tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
+                       "ppolicy", buf, ptr - buf );
+       }
+
+       return rc;
+}
+#endif
+
+void tool_print_ctrls(
+       LDAP            *ld,
+       LDAPControl     **ctrls )
+{
+       int     i;
+       char    *ptr;
+
+       for ( i = 0; ctrls[i] != NULL; i++ ) {
+               /* control: OID criticality base64value */
+               struct berval b64 = BER_BVNULL;
+               ber_len_t len;
+               char *str;
+               int j;
+
+               len = ldif ? 2 : 0;
+               len += strlen( ctrls[i]->ldctl_oid );
+
+               /* add enough for space after OID and the critical value itself */
+               len += ctrls[i]->ldctl_iscritical
+                       ? sizeof("true") : sizeof("false");
+
+               /* convert to base64 */
+               if ( ctrls[i]->ldctl_value.bv_len ) {
+                       b64.bv_len = LUTIL_BASE64_ENCODE_LEN(
+                               ctrls[i]->ldctl_value.bv_len ) + 1;
+                       b64.bv_val = ber_memalloc( b64.bv_len + 1 );
+
+                       b64.bv_len = lutil_b64_ntop(
+                               (unsigned char *) ctrls[i]->ldctl_value.bv_val,
+                               ctrls[i]->ldctl_value.bv_len,
+                               b64.bv_val, b64.bv_len );
+               }
+
+               if ( b64.bv_len ) {
+                       len += 1 + b64.bv_len;
+               }
+
+               ptr = str = malloc( len + 1 );
+               if ( ldif ) {
+                       ptr = lutil_strcopy( ptr, ": " );
+               }
+               ptr = lutil_strcopy( ptr, ctrls[i]->ldctl_oid );
+               ptr = lutil_strcopy( ptr, ctrls[i]->ldctl_iscritical
+                       ? " true" : " false" );
+
+               if ( b64.bv_len ) {
+                       ptr = lutil_strcopy( ptr, " " );
+                       ptr = lutil_strcopy( ptr, b64.bv_val );
+               }
+
+               if ( ldif < 2 ) {
+                       tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
+                               "control", str, len );
+               }
+
+               free( str );
+               if ( b64.bv_len ) {
+                       ber_memfree( b64.bv_val );
+               }
+
+               /* known controls */
+               if ( 0 ) {
+                       /* dummy */ ;
+#ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
+               } else if ( strcmp( LDAP_CONTROL_PASSWORDPOLICYRESPONSE, ctrls[i]->ldctl_oid ) == 0 ) {
+                       (void)print_ppolicy( ld, ctrls[i] );
+#endif
+               }
+       }
+}
+
+int
+tool_write_ldif( int type, char *name, char *value, ber_len_t vallen )
+{
+       char    *ldif;
+
+       if (( ldif = ldif_put( type, name, value, vallen )) == NULL ) {
+               return( -1 );
+       }
+
+       fputs( ldif, stdout );
+       ber_memfree( ldif );
+
+       return( 0 );
+}
+