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.
16 #include <quipu/commonarg.h>
17 #include <quipu/attrvalue.h>
18 #include <quipu/ds_error.h>
19 #include <quipu/ds_search.h>
20 #include <quipu/dap2.h>
21 #include <quipu/dua.h>
22 #include <sys/types.h>
23 #include <sys/socket.h>
28 short ldap_photo_syntax;
29 short ldap_jpeg_syntax;
30 short ldap_jpeg_nonfile_syntax;
31 short ldap_audio_syntax;
33 short ldap_postaladdress_syntax;
34 short ldap_acl_syntax;
35 short ldap_mtai_syntax;
36 short ldap_rts_cred_syntax;
37 short ldap_rtl_syntax;
38 short ldap_mailbox_syntax;
39 short ldap_caseignorelist_syntax;
40 short ldap_caseexactstring_syntax;
41 short ldap_certif_syntax;
42 short ldap_iattr_syntax;
43 short ldap_telex_syntax;
44 short ldap_octetstring_syntax;
45 short ldap_deliverymethod_syntax;
46 short ldap_facsimileTelephoneNumber_syntax;
47 short ldap_presentationAddress_syntax;
48 short ldap_teletexTerminalIdentifier_syntax;
49 short ldap_searchGuide_syntax;
50 short ldap_dLSubmitPermission_syntax;
52 static void de_t61( char *s, int t61mark );
53 static int syntax_is_string( short syntax );
55 static get_one_syntax( char *attrib, int required )
57 oid_table_attr *p, *name2attr();
59 if ( (p = name2attr( attrib )) != (oid_table_attr *) 0 )
60 return( p->oa_syntax );
65 Debug( LDAP_DEBUG_ANY, "name2attr (%s) failed - exiting\n", attrib,
74 oid_table_attr *name2attr();
76 Debug( LDAP_DEBUG_TRACE, "get_syntaxes\n", 0, 0, 0 );
78 ldap_photo_syntax = get_one_syntax( "photo", 0 );
79 ldap_jpeg_syntax = get_one_syntax( "jpegPhoto", 0 );
80 ldap_jpeg_nonfile_syntax = str2syntax( "jpeg" );
81 ldap_audio_syntax = get_one_syntax( "audio", 0 );
82 ldap_postaladdress_syntax = get_one_syntax( "postaladdress", 0 );
83 ldap_dn_syntax = get_one_syntax( "aliasedObjectName", 1 );
84 ldap_acl_syntax = get_one_syntax( "acl", 0 );
85 ldap_mtai_syntax = get_one_syntax( "mTAInfo", 0 );
86 ldap_rts_cred_syntax= get_one_syntax( "initiatingRTSCredentials", 0 );
87 ldap_rtl_syntax= get_one_syntax( "routingTreeList", 0 );
88 ldap_mailbox_syntax = get_one_syntax( "otherMailbox", 0 );
89 ldap_caseignorelist_syntax = str2syntax( "CaseIgnoreList" );
90 ldap_caseexactstring_syntax = str2syntax( "caseexactstring" );
91 ldap_octetstring_syntax = str2syntax( "OctetString" );
92 ldap_deliverymethod_syntax = str2syntax( "DeliveryMethod" );
93 ldap_iattr_syntax = get_one_syntax( "inheritedAttribute", 0 );
94 ldap_certif_syntax = get_one_syntax( "userCertificate", 0 );
95 ldap_telex_syntax = get_one_syntax( "telexNumber", 0 );
96 ldap_facsimileTelephoneNumber_syntax =
97 get_one_syntax( "facsimileTelephoneNumber", 0 );
98 ldap_presentationAddress_syntax =
99 get_one_syntax( "presentationAddress", 0 );
100 ldap_teletexTerminalIdentifier_syntax =
101 get_one_syntax( "teletexTerminalIdentifier", 0 );
102 ldap_searchGuide_syntax = get_one_syntax( "searchGuide", 0 );
103 ldap_dLSubmitPermission_syntax =
104 get_one_syntax( "mhsDLSubmitPermissions", 0 );
106 certif_init(); /* initialize certificate syntax handler */
110 * From RFC 1779 "A String Representation of Distinguished Names"
112 * Key Attribute (X.520 keys)
113 * ------------------------------
116 * ST StateOrProvinceName
118 * OU OrganizationalUnitName
120 * STREET StreetAddress
123 * Table 1: Standardised Keywords
125 * There is an escape mechanism from the normal user oriented form, so
126 * that this syntax may be used to print any valid distinguished name.
128 * 1. Attributes types are represented in a (big-endian) dotted
129 * notation. (e.g., OID.2.6.53).
132 static void attr_key_rfc1779 (
134 char *key /* return key, caller allocated */
139 x = attr2name_aux ( at );
143 } else if ( isdigit ( *x ) ) {
144 sprintf ( key, "OID.%s", x );
146 } else if (strcasecmp(x,"commonName")==0) {
148 } else if (strcasecmp(x,"localityName")==0) {
150 } else if (strcasecmp(x,"stateOrProvinceName")==0) {
152 } else if (strcasecmp(x,"organizationName")==0) {
154 } else if (strcasecmp(x,"organizationalUnitName")==0) {
156 } else if (strcasecmp(x,"countryName")==0) {
158 } else if (strcasecmp(x,"streetAddress")==0) {
165 #define SEPARATOR(c) (c == ',' || c == ';')
166 #define SPACE(c) (c == ' ' || c == '\n')
179 void ldap_dn_print();
185 if ( dn->dn_parent != NULLDN ) {
186 dn_print_real( ps, dn->dn_parent, format );
187 ps_print( ps, ", " );
190 if ( (rps = ps_alloc( str_open )) == NULLPS )
192 if ( str_setup( rps, NULLCP, 0, 0 ) == NOTOK )
196 for ( rdn = dn->dn_rdn; rdn != NULLRDN; rdn = rdn->rdn_next ) {
200 ps_print( ps, " + " );
202 attr_key_rfc1779 ( rdn->rdn_at, key );
204 ps_print ( ps, key );
207 if ( rdn->rdn_at->oa_syntax == ldap_dn_syntax ) {
208 dn_print_real( rps, (DN) rdn->rdn_av.av_struct,
211 value = rps->ps_base;
213 AttrV_print( rps, &rdn->rdn_av, EDBOUT );
215 if ( rps->ps_ptr - rps->ps_base >= 5 &&
216 strncmp( rps->ps_base, "{ASN}", 5 ) == 0 ) {
218 SAFEMEMCPY( rps->ps_base + 1, rps->ps_base + 5,
219 rps->ps_ptr - rps->ps_base - 4 );
221 value = rps->ps_base;
226 * ,+="\\\n all go in quotes. " and \\ need to
227 * be preceeded by \\.
230 if ( strpbrk( value, ",+=\"\\\n" ) != NULL || SPACE( value[0] )
231 || SPACE( value[max( strlen(value) - 1, (size_t) 0 )] ) ) {
235 ps_print( ps, "\"" );
238 for ( p = value; *p != '\0'; p++ ) {
239 if ( *p == '"' || *p == '\\' ) {
243 if ( specialcount > 0 ) {
244 tmp = smalloc( strlen( value ) + specialcount
246 for ( p = value, t = tmp; *p != '\0'; p++ ) {
260 ps_print( ps, value );
263 ps_print( ps, "\"" );
265 ps_print( ps, value );
268 rps->ps_ptr = rps->ps_base;
280 DN base, /* if non-NULL, subsitute '*' for base (for CLDAP) */
287 Debug( LDAP_DEBUG_TRACE, "ldap_dn_print\n", 0, 0, 0 );
290 if ( base != NULLDN && dn != NULL ) {
291 for ( tmpdn = dn; base != NULLDN && tmpdn != NULLDN;
292 base = base->dn_parent, tmpdn = tmpdn->dn_parent ) {
293 if ( dn_comp_cmp( base, tmpdn ) == NOTOK ) {
297 if (( addstar = ( base == NULLDN && tmpdn != NULL ))) {
302 dn_print_real( ps, dn, format );
304 ps_print( ps, ", *" );
312 DN base /* if non-NULL, subsitute '*' for base (for CLDAP) */
318 Debug( LDAP_DEBUG_TRACE, "encode_dn\n", 0, 0, 0 );
320 if ( (ps = ps_alloc( str_open )) == NULLPS )
322 if ( str_setup( ps, NULLCP, 0, 0 ) == NOTOK )
325 ldap_dn_print( ps, dn, base, EDBOUT );
328 rc = ber_printf( ber, "s", ps->ps_base );
335 static put_jpeg_value( BerElement *ber, AttributeValue av )
340 Debug( LDAP_DEBUG_TRACE, "put_jpeg_value\n", 0, 0, 0 );
342 if (av->av_syntax == AV_FILE)
343 pe = (PE) (((struct file_syntax *) av->av_struct)->
346 pe = (PE) av->av_struct;
348 Debug( LDAP_DEBUG_ARGS,
349 "put_jpeg_value: pe_class %x, pe_form %x, pe_id %x\n",
350 pe->pe_class, pe->pe_form, pe->pe_id );
352 if ( (pe->pe_class != PE_CLASS_UNIV && pe->pe_class != PE_CLASS_CONT)
353 || pe->pe_form != PE_FORM_PRIM || pe->pe_id != PE_PRIM_OCTS ) {
354 Debug( LDAP_DEBUG_ANY, "put_jpeg_value: unknown type\n", 0,
359 if ( pe_pullup( pe ) == NOTOK ) {
360 Debug( LDAP_DEBUG_ANY, "put_jpeg_value: cannot pullup\n", 0,
365 len = ps_get_abs( pe );
367 Debug( LDAP_DEBUG_ARGS, "put_jeg_value: ber_printf %d bytes\n",
369 if ( ber_printf( ber, "o", (char *) pe->pe_prim, len ) == -1 ) {
370 Debug( LDAP_DEBUG_ANY, "put_jpeg_value: ber_printf failed\n",
378 static put_audio_value( BerElement *ber, AttributeValue av )
384 Debug( LDAP_DEBUG_TRACE, "put_audio_value\n", 0, 0, 0 );
386 qb = (struct qbuf *) (((struct file_syntax *)
387 av->av_struct)->fs_attr->av_struct);
390 for ( p = qb->qb_forw; p != qb; p = p->qb_forw ) {
394 if ( (buf = (char *) malloc( len )) == NULL )
398 for ( p = qb->qb_forw; p != qb; p = p->qb_forw ) {
399 SAFEMEMCPY( buf + len, p->qb_data, p->qb_len );
403 Debug( LDAP_DEBUG_ARGS, "put_audio_value: ber_printf %d bytes\n",
406 if ( (rc = ber_printf( ber, "o", buf, len )) == -1 )
407 Debug( LDAP_DEBUG_ANY, "put_audio_value: ber_printf failed\n",
415 static put_photo_value( BerElement *ber, AttributeValue av )
420 char *faxparamset = "\000\300\000\000";
423 Debug( LDAP_DEBUG_TRACE, "put_photo_value\n", 0, 0, 0 );
425 pe = (PE) (((struct file_syntax *) av->av_struct)->fs_attr->av_struct);
427 /* old bit string-like format - only handle this for now */
428 if ( pe->pe_class == PE_CLASS_UNIV && pe->pe_form == PE_FORM_PRIM
429 && pe->pe_id == PE_PRIM_BITS ) {
430 len = ps_get_abs( pe );
431 Debug( LDAP_DEBUG_ARGS, "put_photo_val: ber_printf %d bytes\n",
433 if (( phber = der_alloc()) == NULLBER ) {
434 Debug( LDAP_DEBUG_ANY, "der_alloc failed\n", 0, 0, 0 );
437 if ( ber_printf( phber, "t{[tB]{B}}", 0xA3, 0x81, faxparamset,
438 31, (char *)pe->pe_prim, len * 8 ) == -1 ) {
439 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
440 ber_free( phber, 1 );
443 if ( ber_printf( ber, "o", phber->ber_buf, phber->ber_ptr
444 - phber->ber_buf ) == -1 ) {
445 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
446 ber_free( phber, 1 );
449 ber_free( phber, 1 );
452 * try just writing this into a PS and sending it along
454 ps_len_strategy = PS_LEN_LONG;
455 if ( (ps = ps_alloc( str_open )) == NULLPS )
457 if ( str_setup( ps, NULLCP, 0, 0 ) == NOTOK ||
458 pe2ps( ps, pe ) == NOTOK ) {
463 len = ps->ps_ptr - ps->ps_base;
464 Debug( LDAP_DEBUG_ARGS, "put_photo_val: ber_printf %d bytes\n",
466 if ( ber_printf( ber, "o", (char *) ps->ps_base, len ) == -1 ) {
467 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
487 Debug( LDAP_DEBUG_TRACE, "put_values\n", 0, 0, 0 );
489 for ( av = vals; av != NULLAV; av = av->avseq_next ) {
490 if ( syntax == ldap_jpeg_syntax ||
491 syntax == ldap_jpeg_nonfile_syntax ) {
492 if ( put_jpeg_value( ber, &av->avseq_av ) == -1 )
494 } else if ( syntax == ldap_photo_syntax ) {
495 if ( put_photo_value( ber, &av->avseq_av ) == -1 )
497 } else if ( syntax == ldap_audio_syntax ) {
498 if ( put_audio_value( ber, &av->avseq_av ) == -1 )
500 } else if ( syntax == ldap_dn_syntax ) {
501 if ( encode_dn( ber, (DN) av->avseq_av.av_struct,
504 } else if ( syntax > AV_WRITE_FILE ) {
505 struct file_syntax *fsyntax;
507 fsyntax = (struct file_syntax *) av->avseq_av.av_struct;
509 ps->ps_ptr = ps->ps_base;
510 AttrV_print( ps, fsyntax->fs_attr, EDBOUT );
513 if ( ber_printf( ber, "o", ps->ps_base,
514 ps->ps_ptr - ps->ps_base ) == -1 )
517 ps->ps_ptr = ps->ps_base;
518 AttrV_print( ps, &av->avseq_av, EDBOUT );
520 de_t61( ps->ps_base, 0 );
522 if ( syntax_is_string( av->avseq_av.av_syntax ) &&
523 *ps->ps_base == '\0' ) {
525 * If this is a zero-length string, make it
526 * a single blank (this is gross, but it works
527 * around a dsap library bug).
529 Debug( LDAP_DEBUG_ANY,
530 "put_values: replaced zero-length string with single blank\n", 0, 0, 0 );
533 strvalue = ps->ps_base;
535 if ( ber_printf( ber, "s", strvalue ) == -1 )
544 encode_attrs( BerElement *ber, Attr_Sequence as )
548 extern int ldap_compat;
551 Debug( LDAP_DEBUG_TRACE, "encode_attrs\n", 0, 0, 0 );
553 if ( (ps = ps_alloc( str_open )) == NULLPS )
555 if ( str_setup( ps, NULLCP, 0, 0 ) == NOTOK )
559 if ( ber_printf( ber, "t{", ldap_compat == 20 ? OLD_LBER_SEQUENCE :
560 LBER_SEQUENCE ) == -1 ) {
562 if ( ber_printf( ber, "{" ) == -1 ) {
568 while ( as != NULLATTR ) {
569 ps->ps_ptr = ps->ps_base;
570 AttrT_print( ps, as->attr_type, EDBOUT );
574 if ( ber_printf( ber, "t{st[", ldap_compat == 20 ?
575 OLD_LBER_SEQUENCE : LBER_SEQUENCE, ps->ps_base,
576 ldap_compat == 20 ? OLD_LBER_SET : LBER_SET ) == -1 ) {
578 if ( ber_printf( ber, "{s[", ps->ps_base ) == -1 ) {
584 put_values( ber, ps, as->attr_type->oa_syntax, as->attr_value );
586 if ( ber_printf( ber, "]}" ) == -1 ) {
595 if ( ber_printf( ber, "}" ) == -1 )
602 trim_trailing_spaces( char *s )
616 DN ldap_str2dn( char *str )
619 RDN rdn, newrdn, tmprdn;
622 char *type, *value, *savestr;
625 Debug( LDAP_DEBUG_TRACE, "ldap_str2dn\n", 0, 0, 0 );
627 savestr = str = strdup( str );
637 while ( *str == ' ' || *str == '\n' )
640 while ( *str != '\0' && *str != '=' )
642 if ( *str == '\0' ) {
644 Debug( LDAP_DEBUG_ARGS, "no =\n", 0, 0, 0 );
648 if ( strncmp( type, "OID.", 4 ) == 0 )
660 /* break or return out */
661 while ( state != ENDVALUE ) {
664 if ( state == BEGINVALUE ) {
667 } else if ( state == INQUOTE ) {
672 Debug( LDAP_DEBUG_ARGS,
673 "quote state %d\n", state,
682 if ( state == INVALUE ) {
684 } else if ( state == INQUOTE ) {
688 Debug( LDAP_DEBUG_ARGS,
689 "comma state %d\n", state,
697 if ( state == BEGINVALUE ) {
714 if ( state == BEGINVALUE )
721 while ( SPACE( *str ) )
728 if ( SEPARATOR( *str ) )
734 trim_trailing_spaces( type );
735 if ( (at = str2AttrT( type )) == NULLAttrT ) {
738 Debug( LDAP_DEBUG_ARGS, "bad type (%s)\n",
740 return( NULLDN ); /* LDAP_UNDEFINED_TYPE */
743 if ( (av = ldap_str2AttrV( value, at->oa_syntax ))
747 Debug( LDAP_DEBUG_ARGS, "bad val\n", 0, 0, 0 );
748 return( NULLDN ); /* LDAP_INVALID_SYNTAX */
751 newrdn = rdn_comp_new( at, av );
753 /* add it to the list */
754 for ( tmprdn = rdn; tmprdn != NULLRDN &&
755 tmprdn->rdn_next != NULLRDN;
756 tmprdn = tmprdn->rdn_next )
758 if ( tmprdn != NULLRDN )
759 tmprdn->rdn_next = newrdn;
764 } while ( morerdncomps );
767 dn = dn_comp_new( rdn );
768 dn->dn_parent = save;
769 } while ( str != NULL && *str != '\0' );
772 Debug( LDAP_DEBUG_TRACE, "ldap_str2dn OK\n", 0, 0, 0 );
779 static void de_t61( char *s, int t61mark )
787 if ( strncasecmp( s, T61, T61LEN) == 0 ) {
805 /* reverse solidus character itself */
812 else if ( c >= 'A' && c <= 'F' )
814 else if ( c >= 'a' && c <= 'f' )
824 else if ( c >= 'A' && c <= 'F' )
826 else if ( c >= 'a' && c <= 'f' )
848 bv_asn2pe( struct berval *bv )
853 if (( ps = ps_alloc(str_open)) == NULLPS || str_setup( ps, bv->bv_val,
854 bv->bv_len, 0 ) == NOTOK ) {
855 Debug( LDAP_DEBUG_TRACE, "bv_asn2pe: ps_alloc failed\n",
861 if ( ps->ps_errno != PS_ERR_NONE ) {
862 Debug( LDAP_DEBUG_TRACE, "bv_asn2pe: ps2pe failed %s\n",
863 ps_error(ps->ps_errno), 0, 0 );
864 if ( pe != NULLPE ) {
875 bv_octet2AttrV( struct berval *bv )
880 if ( av == NULLAttrV ) {
884 if (( av->av_struct = (caddr_t) str2prim( bv->bv_val, bv->bv_len,
885 PE_CLASS_UNIV, PE_PRIM_OCTS )) == NULL ) {
896 bv_asn2AttrV( struct berval *bv )
901 if ( av == NULLAttrV ) {
905 if (( av->av_struct = (caddr_t) bv_asn2pe( bv )) == NULL ) {
916 ldap_strdn2AttrV( char *dnstr )
921 if (( dn = ldap_str2dn( dnstr )) == NULL ) {
926 if ( av == NULLAttrV ) {
931 av->av_struct = (caddr_t)dn;
932 av->av_syntax = ldap_dn_syntax;
937 ldap_str2rdn( char *rdnstr )
942 if ( (dn = ldap_str2dn( rdnstr )) == NULL ) {
946 if ( (rdn = rdn_cpy( dn->dn_rdn )) == NULL ) {
956 ldap_str_at2AttrV( char *str, AttributeType type )
959 AttributeValue str_at2AttrV();
961 Debug( LDAP_DEBUG_TRACE, "ldap_str_at2AttrV str (%s) type (%s)\n", str,
962 type->oa_ot.ot_name, 0 );
964 if ( type->oa_syntax == ldap_rts_cred_syntax ||
965 type->oa_syntax == ldap_mtai_syntax ||
966 type->oa_syntax == ldap_acl_syntax ||
967 type->oa_syntax == ldap_mailbox_syntax ||
968 type->oa_syntax == ldap_caseignorelist_syntax ||
969 type->oa_syntax == ldap_certif_syntax ||
970 type->oa_syntax == ldap_iattr_syntax ||
971 type->oa_syntax == ldap_telex_syntax ||
972 type->oa_syntax == ldap_deliverymethod_syntax ||
973 type->oa_syntax == ldap_facsimileTelephoneNumber_syntax ||
974 type->oa_syntax == ldap_presentationAddress_syntax ||
975 type->oa_syntax == ldap_teletexTerminalIdentifier_syntax ||
976 type->oa_syntax == ldap_searchGuide_syntax ||
977 type->oa_syntax == ldap_dLSubmitPermission_syntax ||
978 type->oa_syntax == ldap_rtl_syntax ) {
981 res = (char *) malloc( max( 2 * strlen( str ), (size_t) 10 ) );
984 for ( s = str; *s; s++ ) {
992 sprintf( r, "\\%02x", *s & 0xff );
1003 Debug( LDAP_DEBUG_TRACE, "ldap_str_at2AttrV returning (%s)\n", res,
1006 return( str_at2AttrV( res, type ) );
1010 ldap_str2AttrV( char *value, short syntax )
1012 if ( syntax == ldap_dn_syntax ) {
1013 return( ldap_strdn2AttrV( value ) );
1015 return( str2AttrV( value, syntax ) );
1021 syntax_is_string( short syntax )
1024 * this code depends on the order and nunber of strings that are in
1025 * the ISODE file lib/syntax/x500/string.c
1027 return ( syntax >= ldap_caseexactstring_syntax &&
1028 syntax <= ldap_caseexactstring_syntax + 8 );