2 * Copyright (c) 1990 Regents of the University of Michigan.
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.
18 #include <ac/socket.h>
19 #include <ac/string.h>
21 #include <quipu/commonarg.h>
22 #include <quipu/attrvalue.h>
23 #include <quipu/ds_error.h>
24 #include <quipu/ds_search.h>
25 #include <quipu/dap2.h>
26 #include <quipu/dua.h>
27 extern oid_table_attr *name2attr( char * );
28 /*extern AttributeValue str_at2AttrV( char *, IF_AttributeType * );*/
31 #include "../../libraries/liblber/lber-int.h" /* get struct berelement */
35 short ldap_photo_syntax;
36 short ldap_jpeg_syntax;
37 short ldap_jpeg_nonfile_syntax;
38 short ldap_audio_syntax;
40 short ldap_postaladdress_syntax;
41 short ldap_acl_syntax;
42 short ldap_mtai_syntax;
43 short ldap_rts_cred_syntax;
44 short ldap_rtl_syntax;
45 short ldap_mailbox_syntax;
46 short ldap_caseignorelist_syntax;
47 short ldap_caseexactstring_syntax;
48 short ldap_certif_syntax;
49 short ldap_iattr_syntax;
50 short ldap_telex_syntax;
51 short ldap_octetstring_syntax;
52 short ldap_deliverymethod_syntax;
53 short ldap_facsimileTelephoneNumber_syntax;
54 short ldap_presentationAddress_syntax;
55 short ldap_teletexTerminalIdentifier_syntax;
56 short ldap_searchGuide_syntax;
57 short ldap_dLSubmitPermission_syntax;
59 static void de_t61( char *s, int t61mark );
60 static int syntax_is_string( short syntax );
63 get_one_syntax( char *attrib, int required )
67 if ( (p = name2attr( attrib )) != (oid_table_attr *) 0 )
68 return( p->oa_syntax );
73 Debug( LDAP_DEBUG_ANY, "name2attr (%s) failed - exiting\n", attrib,
82 Debug( LDAP_DEBUG_TRACE, "get_syntaxes\n", 0, 0, 0 );
84 ldap_photo_syntax = get_one_syntax( "photo", 0 );
85 ldap_jpeg_syntax = get_one_syntax( "jpegPhoto", 0 );
86 ldap_jpeg_nonfile_syntax = str2syntax( "jpeg" );
87 ldap_audio_syntax = get_one_syntax( "audio", 0 );
88 ldap_postaladdress_syntax = get_one_syntax( "postaladdress", 0 );
89 ldap_dn_syntax = get_one_syntax( "aliasedObjectName", 1 );
90 ldap_acl_syntax = get_one_syntax( "acl", 0 );
91 ldap_mtai_syntax = get_one_syntax( "mTAInfo", 0 );
92 ldap_rts_cred_syntax= get_one_syntax( "initiatingRTSCredentials", 0 );
93 ldap_rtl_syntax= get_one_syntax( "routingTreeList", 0 );
94 ldap_mailbox_syntax = get_one_syntax( "otherMailbox", 0 );
95 ldap_caseignorelist_syntax = str2syntax( "CaseIgnoreList" );
96 ldap_caseexactstring_syntax = str2syntax( "caseexactstring" );
97 ldap_octetstring_syntax = str2syntax( "OctetString" );
98 ldap_deliverymethod_syntax = str2syntax( "DeliveryMethod" );
99 ldap_iattr_syntax = get_one_syntax( "inheritedAttribute", 0 );
100 ldap_certif_syntax = get_one_syntax( "userCertificate", 0 );
101 ldap_telex_syntax = get_one_syntax( "telexNumber", 0 );
102 ldap_facsimileTelephoneNumber_syntax =
103 get_one_syntax( "facsimileTelephoneNumber", 0 );
104 ldap_presentationAddress_syntax =
105 get_one_syntax( "presentationAddress", 0 );
106 ldap_teletexTerminalIdentifier_syntax =
107 get_one_syntax( "teletexTerminalIdentifier", 0 );
108 ldap_searchGuide_syntax = get_one_syntax( "searchGuide", 0 );
109 ldap_dLSubmitPermission_syntax =
110 get_one_syntax( "mhsDLSubmitPermissions", 0 );
112 certif_init(); /* initialize certificate syntax handler */
116 * From RFC 1779 "A String Representation of Distinguished Names"
118 * Key Attribute (X.520 keys)
119 * ------------------------------
122 * ST StateOrProvinceName
124 * OU OrganizationalUnitName
126 * STREET StreetAddress
129 * Table 1: Standardised Keywords
131 * There is an escape mechanism from the normal user oriented form, so
132 * that this syntax may be used to print any valid distinguished name.
134 * 1. Attributes types are represented in a (big-endian) dotted
135 * notation. (e.g., OID.2.6.53).
141 char *key /* return key, caller allocated */
146 x = attr2name_aux ( at );
150 } else if ( isdigit ( (unsigned char) *x ) ) {
151 sprintf ( key, "OID.%s", x );
153 } else if (strcasecmp(x,"commonName")==0) {
155 } else if (strcasecmp(x,"localityName")==0) {
157 } else if (strcasecmp(x,"stateOrProvinceName")==0) {
159 } else if (strcasecmp(x,"organizationName")==0) {
161 } else if (strcasecmp(x,"organizationalUnitName")==0) {
163 } else if (strcasecmp(x,"countryName")==0) {
165 } else if (strcasecmp(x,"streetAddress")==0) {
172 #define SEPARATOR(c) ((c) == ',' || (c) == ';')
173 #define SPACE(c) ((c) == ' ' || (c) == '\n')
191 if ( dn->dn_parent != NULLDN ) {
192 dn_print_real( ps, dn->dn_parent, format );
193 ps_print( ps, ", " );
196 if ( (rps = ps_alloc( str_open )) == NULLPS )
198 if ( str_setup( rps, NULLCP, 0, 0 ) == NOTOK )
202 for ( rdn = dn->dn_rdn; rdn != NULLRDN; rdn = rdn->rdn_next ) {
206 ps_print( ps, " + " );
208 attr_key_rfc1779 ( rdn->rdn_at, key );
210 ps_print ( ps, key );
213 if ( rdn->rdn_at->oa_syntax == ldap_dn_syntax ) {
214 dn_print_real( rps, (DN) rdn->rdn_av.av_struct,
217 value = rps->ps_base;
219 AttrV_print( rps, &rdn->rdn_av, EDBOUT );
221 if ( rps->ps_ptr - rps->ps_base >= 5 &&
222 strncmp( rps->ps_base, "{ASN}", 5 ) == 0 ) {
224 SAFEMEMCPY( rps->ps_base + 1, rps->ps_base + 5,
225 rps->ps_ptr - rps->ps_base - 4 );
227 value = rps->ps_base;
232 * ,+="\\\n all go in quotes. " and \\ need to
233 * be preceeded by \\.
236 if ( strpbrk( value, ",+=\"\\\n" ) != NULL || SPACE( value[0] )
237 || SPACE( value[max( strlen(value) - 1, (size_t) 0 )] ) ) {
241 ps_print( ps, "\"" );
244 for ( p = value; *p != '\0'; p++ ) {
245 if ( *p == '"' || *p == '\\' ) {
249 if ( specialcount > 0 ) {
250 tmp = smalloc( strlen( value ) + specialcount
252 for ( p = value, t = tmp; *p != '\0'; p++ ) {
266 ps_print( ps, value );
269 ps_print( ps, "\"" );
271 ps_print( ps, value );
274 rps->ps_ptr = rps->ps_base;
286 DN base, /* if non-NULL, subsitute '*' for base (for CLDAP) */
293 Debug( LDAP_DEBUG_TRACE, "ldap_dn_print\n", 0, 0, 0 );
296 if ( base != NULLDN && dn != NULL ) {
297 for ( tmpdn = dn; base != NULLDN && tmpdn != NULLDN;
298 base = base->dn_parent, tmpdn = tmpdn->dn_parent ) {
299 if ( dn_comp_cmp( base, tmpdn ) == NOTOK ) {
303 if (( addstar = ( base == NULLDN && tmpdn != NULL ))) {
308 dn_print_real( ps, dn, format );
310 ps_print( ps, ", *" );
318 DN base /* if non-NULL, subsitute '*' for base (for CLDAP) */
324 Debug( LDAP_DEBUG_TRACE, "encode_dn\n", 0, 0, 0 );
326 if ( (ps = ps_alloc( str_open )) == NULLPS )
328 if ( str_setup( ps, NULLCP, 0, 0 ) == NOTOK )
331 ldap_dn_print( ps, dn, base, EDBOUT );
334 rc = ber_printf( ber, "s", ps->ps_base );
342 put_jpeg_value( BerElement *ber, AttributeValue av )
347 Debug( LDAP_DEBUG_TRACE, "put_jpeg_value\n", 0, 0, 0 );
349 if (av->av_syntax == AV_FILE)
350 pe = (PE) (((struct file_syntax *) av->av_struct)->
353 pe = (PE) av->av_struct;
355 Debug( LDAP_DEBUG_ARGS,
356 "put_jpeg_value: pe_class %x, pe_form %x, pe_id %x\n",
357 pe->pe_class, pe->pe_form, pe->pe_id );
359 if ( (pe->pe_class != PE_CLASS_UNIV && pe->pe_class != PE_CLASS_CONT)
360 || pe->pe_form != PE_FORM_PRIM || pe->pe_id != PE_PRIM_OCTS ) {
361 Debug( LDAP_DEBUG_ANY, "put_jpeg_value: unknown type\n", 0,
366 if ( pe_pullup( pe ) == NOTOK ) {
367 Debug( LDAP_DEBUG_ANY, "put_jpeg_value: cannot pullup\n", 0,
372 len = ps_get_abs( pe );
374 Debug( LDAP_DEBUG_ARGS, "put_jeg_value: ber_printf %d bytes\n",
376 if ( ber_printf( ber, "o", (char *) pe->pe_prim, len ) == -1 ) {
377 Debug( LDAP_DEBUG_ANY, "put_jpeg_value: ber_printf failed\n",
386 put_audio_value( BerElement *ber, AttributeValue av )
392 Debug( LDAP_DEBUG_TRACE, "put_audio_value\n", 0, 0, 0 );
394 qb = (struct qbuf *) (((struct file_syntax *)
395 av->av_struct)->fs_attr->av_struct);
398 for ( p = qb->qb_forw; p != qb; p = p->qb_forw ) {
402 if ( (buf = (char *) malloc( len )) == NULL )
406 for ( p = qb->qb_forw; p != qb; p = p->qb_forw ) {
407 SAFEMEMCPY( buf + len, p->qb_data, p->qb_len );
411 Debug( LDAP_DEBUG_ARGS, "put_audio_value: ber_printf %d bytes\n",
414 if ( (rc = ber_printf( ber, "o", buf, len )) == -1 )
415 Debug( LDAP_DEBUG_ANY, "put_audio_value: ber_printf failed\n",
424 put_photo_value( BerElement *ber, AttributeValue av )
429 char *faxparamset = "\000\300\000\000";
432 Debug( LDAP_DEBUG_TRACE, "put_photo_value\n", 0, 0, 0 );
434 pe = (PE) (((struct file_syntax *) av->av_struct)->fs_attr->av_struct);
436 /* old bit string-like format - only handle this for now */
437 if ( pe->pe_class == PE_CLASS_UNIV && pe->pe_form == PE_FORM_PRIM
438 && pe->pe_id == PE_PRIM_BITS ) {
439 len = ps_get_abs( pe );
440 Debug( LDAP_DEBUG_ARGS, "put_photo_val: ber_printf %d bytes\n",
442 if (( phber = der_alloc()) == NULL ) {
443 Debug( LDAP_DEBUG_ANY, "der_alloc failed\n", 0, 0, 0 );
446 if ( ber_printf( phber, "t{[tB]{B}}", 0xA3, 0x81, faxparamset,
447 31, (char *)pe->pe_prim, len * 8 ) == -1 ) {
448 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
449 ber_free( phber, 1 );
452 if ( ber_printf( ber, "o", phber->ber_buf, phber->ber_ptr
453 - phber->ber_buf ) == -1 ) {
454 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
455 ber_free( phber, 1 );
458 ber_free( phber, 1 );
461 * try just writing this into a PS and sending it along
463 ps_len_strategy = PS_LEN_LONG;
464 if ( (ps = ps_alloc( str_open )) == NULLPS )
466 if ( str_setup( ps, NULLCP, 0, 0 ) == NOTOK ||
467 pe2ps( ps, pe ) == NOTOK ) {
472 len = ps->ps_ptr - ps->ps_base;
473 Debug( LDAP_DEBUG_ARGS, "put_photo_val: ber_printf %d bytes\n",
475 if ( ber_printf( ber, "o", (char *) ps->ps_base, len ) == -1 ) {
476 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
497 Debug( LDAP_DEBUG_TRACE, "put_values\n", 0, 0, 0 );
499 for ( av = vals; av != NULLAV; av = av->avseq_next ) {
500 if ( syntax == ldap_jpeg_syntax ||
501 syntax == ldap_jpeg_nonfile_syntax ) {
502 if ( put_jpeg_value( ber, &av->avseq_av ) == -1 )
504 } else if ( syntax == ldap_photo_syntax ) {
505 if ( put_photo_value( ber, &av->avseq_av ) == -1 )
507 } else if ( syntax == ldap_audio_syntax ) {
508 if ( put_audio_value( ber, &av->avseq_av ) == -1 )
510 } else if ( syntax == ldap_dn_syntax ) {
511 if ( encode_dn( ber, (DN) av->avseq_av.av_struct,
514 } else if ( syntax > AV_WRITE_FILE ) {
515 struct file_syntax *fsyntax;
517 fsyntax = (struct file_syntax *) av->avseq_av.av_struct;
519 ps->ps_ptr = ps->ps_base;
520 AttrV_print( ps, fsyntax->fs_attr, EDBOUT );
523 if ( ber_printf( ber, "o", ps->ps_base,
524 ps->ps_ptr - ps->ps_base ) == -1 )
527 ps->ps_ptr = ps->ps_base;
528 AttrV_print( ps, &av->avseq_av, EDBOUT );
530 de_t61( ps->ps_base, 0 );
532 if ( syntax_is_string( av->avseq_av.av_syntax ) &&
533 *ps->ps_base == '\0' ) {
535 * If this is a zero-length string, make it
536 * a single blank (this is gross, but it works
537 * around a dsap library bug).
539 Debug( LDAP_DEBUG_ANY,
540 "put_values: replaced zero-length string with single blank\n", 0, 0, 0 );
543 strvalue = ps->ps_base;
545 if ( ber_printf( ber, "s", strvalue ) == -1 )
554 encode_attrs( BerElement *ber, Attr_Sequence as )
558 Debug( LDAP_DEBUG_TRACE, "encode_attrs\n", 0, 0, 0 );
560 if ( (ps = ps_alloc( str_open )) == NULLPS )
562 if ( str_setup( ps, NULLCP, 0, 0 ) == NOTOK )
566 if ( ber_printf( ber, "t{", ldap_compat == 20 ? OLD_LBER_SEQUENCE :
567 LBER_SEQUENCE ) == -1 ) {
569 if ( ber_printf( ber, "{" ) == -1 ) {
575 while ( as != NULLATTR ) {
576 ps->ps_ptr = ps->ps_base;
577 AttrT_print( ps, as->attr_type, EDBOUT );
581 if ( ber_printf( ber, "t{st[", ldap_compat == 20 ?
582 OLD_LBER_SEQUENCE : LBER_SEQUENCE, ps->ps_base,
583 ldap_compat == 20 ? OLD_LBER_SET : LBER_SET ) == -1 ) {
585 if ( ber_printf( ber, "{s[", ps->ps_base ) == -1 ) {
591 put_values( ber, ps, as->attr_type->oa_syntax, as->attr_value );
593 if ( ber_printf( ber, "]}" ) == -1 ) {
602 if ( ber_printf( ber, "}" ) == -1 )
609 trim_trailing_spaces( char *s )
624 ldap_str2dn( char *str )
627 RDN rdn, newrdn, tmprdn;
630 char *type, *value, *savestr;
633 Debug( LDAP_DEBUG_TRACE, "ldap_str2dn\n", 0, 0, 0 );
635 savestr = str = strdup( str );
645 while ( *str == ' ' || *str == '\n' )
648 while ( *str != '\0' && *str != '=' )
650 if ( *str == '\0' ) {
652 Debug( LDAP_DEBUG_ARGS, "no =\n", 0, 0, 0 );
656 if ( strncmp( type, "OID.", 4 ) == 0 )
668 /* break or return out */
669 while ( state != ENDVALUE ) {
672 if ( state == BEGINVALUE ) {
675 } else if ( state == INQUOTE ) {
680 Debug( LDAP_DEBUG_ARGS,
681 "quote state %d\n", state,
690 if ( state == INVALUE ) {
692 } else if ( state == INQUOTE ) {
696 Debug( LDAP_DEBUG_ARGS,
697 "comma state %d\n", state,
705 if ( state == BEGINVALUE ) {
722 if ( state == BEGINVALUE )
729 while ( SPACE( *str ) )
736 if ( SEPARATOR( *str ) )
742 trim_trailing_spaces( type );
743 if ( (at = str2AttrT( type )) == NULLAttrT ) {
746 Debug( LDAP_DEBUG_ARGS, "bad type (%s)\n",
748 return( NULLDN ); /* LDAP_UNDEFINED_TYPE */
751 if ( (av = ldap_str2AttrV( value, at->oa_syntax ))
755 Debug( LDAP_DEBUG_ARGS, "bad val\n", 0, 0, 0 );
756 return( NULLDN ); /* LDAP_INVALID_SYNTAX */
759 newrdn = rdn_comp_new( at, av );
761 /* add it to the list */
762 for ( tmprdn = rdn; tmprdn != NULLRDN &&
763 tmprdn->rdn_next != NULLRDN;
764 tmprdn = tmprdn->rdn_next )
766 if ( tmprdn != NULLRDN )
767 tmprdn->rdn_next = newrdn;
772 } while ( morerdncomps );
775 dn = dn_comp_new( rdn );
776 dn->dn_parent = save;
777 } while ( str != NULL && *str != '\0' );
780 Debug( LDAP_DEBUG_TRACE, "ldap_str2dn OK\n", 0, 0, 0 );
788 de_t61( char *s, int t61mark )
797 if ( strncasecmp( s, T61, T61LEN) == 0 ) {
815 /* reverse solidus character itself */
822 else if ( c >= 'A' && c <= 'F' )
824 else if ( c >= 'a' && c <= 'f' )
834 else if ( c >= 'A' && c <= 'F' )
836 else if ( c >= 'a' && c <= 'f' )
858 bv_asn2pe( struct berval *bv )
863 if (( ps = ps_alloc(str_open)) == NULLPS || str_setup( ps, bv->bv_val,
864 bv->bv_len, 0 ) == NOTOK ) {
865 Debug( LDAP_DEBUG_TRACE, "bv_asn2pe: ps_alloc failed\n",
871 if ( ps->ps_errno != PS_ERR_NONE ) {
872 Debug( LDAP_DEBUG_TRACE, "bv_asn2pe: ps2pe failed %s\n",
873 ps_error(ps->ps_errno), 0, 0 );
874 if ( pe != NULLPE ) {
885 bv_octet2AttrV( struct berval *bv )
890 if ( av == NULLAttrV ) {
894 if (( av->av_struct = (caddr_t) str2prim( bv->bv_val, bv->bv_len,
895 PE_CLASS_UNIV, PE_PRIM_OCTS )) == NULL ) {
906 bv_asn2AttrV( struct berval *bv )
911 if ( av == NULLAttrV ) {
915 if (( av->av_struct = (caddr_t) bv_asn2pe( bv )) == NULL ) {
926 ldap_strdn2AttrV( char *dnstr )
931 if (( dn = ldap_str2dn( dnstr )) == NULL ) {
936 if ( av == NULLAttrV ) {
941 av->av_struct = (caddr_t)dn;
942 av->av_syntax = ldap_dn_syntax;
947 ldap_str2rdn( char *rdnstr )
952 if ( (dn = ldap_str2dn( rdnstr )) == NULL ) {
956 if ( (rdn = rdn_cpy( dn->dn_rdn )) == NULL ) {
966 ldap_str_at2AttrV( char *str, AttributeType type )
970 Debug( LDAP_DEBUG_TRACE, "ldap_str_at2AttrV str (%s) type (%s)\n", str,
971 type->oa_ot.ot_name, 0 );
973 if ( type->oa_syntax == ldap_rts_cred_syntax ||
974 type->oa_syntax == ldap_mtai_syntax ||
975 type->oa_syntax == ldap_acl_syntax ||
976 type->oa_syntax == ldap_mailbox_syntax ||
977 type->oa_syntax == ldap_caseignorelist_syntax ||
978 type->oa_syntax == ldap_certif_syntax ||
979 type->oa_syntax == ldap_iattr_syntax ||
980 type->oa_syntax == ldap_telex_syntax ||
981 type->oa_syntax == ldap_deliverymethod_syntax ||
982 type->oa_syntax == ldap_facsimileTelephoneNumber_syntax ||
983 type->oa_syntax == ldap_presentationAddress_syntax ||
984 type->oa_syntax == ldap_teletexTerminalIdentifier_syntax ||
985 type->oa_syntax == ldap_searchGuide_syntax ||
986 type->oa_syntax == ldap_dLSubmitPermission_syntax ||
987 type->oa_syntax == ldap_rtl_syntax ) {
990 res = (char *) malloc( max( 2 * strlen( str ), (size_t) 10 ) );
993 for ( s = str; *s; s++ ) {
1001 sprintf( r, "\\%02x", *s & 0xff );
1012 Debug( LDAP_DEBUG_TRACE, "ldap_str_at2AttrV returning (%s)\n", res,
1015 return( str_at2AttrV( res, type ) );
1019 ldap_str2AttrV( char *value, short syntax )
1021 if ( syntax == ldap_dn_syntax ) {
1022 return( ldap_strdn2AttrV( value ) );
1024 return( str2AttrV( value, syntax ) );
1030 syntax_is_string( short syntax )
1033 * this code depends on the order and nunber of strings that are in
1034 * the ISODE file lib/syntax/x500/string.c
1036 return ( syntax >= ldap_caseexactstring_syntax &&
1037 syntax <= ldap_caseexactstring_syntax + 8 );