2 * Copyright (c) 1995 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>
27 #include "ldapsyntax.h"
30 short ldap_password_syntax;
31 short ldap_photo_syntax;
32 short ldap_jpeg_syntax;
33 short ldap_audio_syntax;
35 static int dn2ldif( PS ps, DN dn );
36 static int jpeg2ldif( PS ps, AttributeValue av );
37 static int audio2ldif( PS ps, AttributeValue av );
38 static int photo2ldif( PS ps, AttributeValue av );
39 static int fileattr2ldif( PS ps, AttributeValue av );
40 static void de_t61( char *s, int t61mark );
41 static void de_crypt( char *s );
43 extern char *progname;
45 #define SEPARATOR(c) (c == ',' || c == ';')
46 #define SPACE(c) (c == ' ' || c == '\n')
52 if (( ldap_dn_syntax = str2syntax( "DN" )) == 0 ) {
53 return( -1 ); /* must have this syntax handler */
55 ldap_password_syntax = str2syntax( "password" );
56 ldap_photo_syntax = str2syntax( "photo" );
57 ldap_jpeg_syntax = str2syntax( "jpeg" );
58 ldap_audio_syntax = str2syntax( "audio" );
65 * av2ldif: convert attribute value contained in "av" to ldif format
66 * and write to "outfp". If "dn" is not NULL, convert it instead of "av".
69 av2ldif( FILE *outfp, AV_Sequence av, DN dn, short syntax, char *attrname,
74 struct file_syntax *fsyntax;
77 fsyntax = (struct file_syntax *) av->avseq_av.av_struct;
80 rc = 0; /* optimistic */
81 str_ps->ps_ptr = str_ps->ps_base; /* reset string PS */
83 if ( dn != NULL || syntax == ldap_dn_syntax ) { /* DNs */
84 rc = dn2ldif( str_ps, ( dn != NULLDN ) ? dn :
85 (DN)(av->avseq_av.av_struct));
87 } else if ( syntax == ldap_jpeg_syntax || ( syntax > AV_WRITE_FILE &&
88 fsyntax->fs_real_syntax == ldap_jpeg_syntax )) {
89 rc = jpeg2ldif( str_ps, &av->avseq_av );
91 } else if ( syntax == ldap_photo_syntax || ( syntax > AV_WRITE_FILE &&
92 fsyntax->fs_real_syntax == ldap_photo_syntax )) {
93 rc = photo2ldif( str_ps, &av->avseq_av );
95 } else if ( syntax == ldap_audio_syntax || ( syntax > AV_WRITE_FILE &&
96 fsyntax->fs_real_syntax == ldap_audio_syntax )) {
97 rc = audio2ldif( str_ps, &av->avseq_av );
99 } else if ( syntax > AV_WRITE_FILE ) {
100 rc = fileattr2ldif( str_ps, &av->avseq_av );
103 AttrV_print( str_ps, &av->avseq_av, EDBOUT );
104 *str_ps->ps_ptr = '\0';
105 de_t61( str_ps->ps_base, 0 );
107 if ( syntax == ldap_password_syntax ) {
108 de_crypt( str_ps->ps_base );
111 str_ps->ps_ptr = str_ps->ps_base + strlen( str_ps->ps_base );
114 if ( rc == 0 && str_ps->ps_ptr > str_ps->ps_base ) {
115 *str_ps->ps_ptr = '\0';
116 if (( buf = ldif_type_and_value( attrname, str_ps->ps_base,
117 str_ps->ps_ptr - str_ps->ps_base )) == NULL ) {
120 if ( fputs( buf, outfp ) == EOF ) {
128 if ( syntax > AV_WRITE_FILE ) {
130 "%s: attribute file '%s' not found (skipping value)\n",
131 progname, fsyntax->fs_name );
133 rc = 0; /* treat as "soft" error -- keep going */
141 dn2ldif( PS ps, DN dn )
148 if ( dn == NULLDN ) {
152 if ( dn->dn_parent != NULLDN ) {
153 if (( rc = dn2ldif( ps, dn->dn_parent )) != 0 ) {
156 ps_print( ps, ", " );
159 if ( (rps = ps_alloc( str_open )) == NULLPS ||
160 str_setup( rps, NULLCP, 0, 0 ) == NOTOK ) {
165 for ( rdn = dn->dn_rdn; rdn != NULLRDN; rdn = rdn->rdn_next ) {
169 ps_print( ps, " + " );
172 AttrT_print( ps, rdn->rdn_at, EDBOUT );
175 if ( rdn->rdn_at->oa_syntax == ldap_dn_syntax ) {
176 if (( rc = dn2ldif( rps, (DN) rdn->rdn_av.av_struct )) != 0 ) {
180 value = rps->ps_base;
182 AttrV_print( rps, &rdn->rdn_av, EDBOUT );
184 value = rps->ps_base;
189 * ,+="\\\n all go in quotes. " and \\ need to
190 * be preceeded by \\.
193 if ( strpbrk( value, ",+=\"\\\n" ) != NULL || SPACE( value[0] )
194 || SPACE( value[max( strlen(value) - 1, 0 )] ) ) {
198 ps_print( ps, "\"" );
201 for ( p = value; *p != '\0'; p++ ) {
202 if ( *p == '"' || *p == '\\' ) {
206 if ( specialcount > 0 ) {
207 tmp = smalloc( strlen( value ) + specialcount + 1 );
208 for ( p = value, t = tmp; *p != '\0'; p++ ) {
222 ps_print( ps, value );
225 ps_print( ps, "\"" );
227 ps_print( ps, value );
230 rps->ps_ptr = rps->ps_base;
252 if ( strncasecmp( s, T61, T61LEN) == 0 ) {
271 else if ( c >= 'A' && c <= 'F' )
273 else if ( c >= 'a' && c <= 'f' )
283 else if ( c >= 'A' && c <= 'F' )
285 else if ( c >= 'a' && c <= 'f' )
306 #define CRYPT_MASK 0x23
313 if ( strncmp( s, "{CRYPT}", 7 ) == 0 ) {
314 strcpy( s, s + 7 ); /* strip off "{CRYPT}" */
316 for ( p = s; *p != '\0'; ++p) { /* "decrypt" each byte */
317 if ( *p != CRYPT_MASK ) {
326 jpeg2ldif( PS ps, AttributeValue av )
331 if (( pe = grab_pe( av )) == NULLPE || pe->pe_id == PE_PRIM_NULL ) {
332 return( -2 ); /* signal soft error */
335 if (( pe->pe_class != PE_CLASS_UNIV && pe->pe_class != PE_CLASS_CONT )
336 || pe->pe_form != PE_FORM_PRIM || pe->pe_id != PE_PRIM_OCTS ) {
340 if ( pe_pullup( pe ) == NOTOK ) {
344 len = ps_get_abs( pe );
346 if ( ps_write( ps, (PElementData)pe->pe_prim, len ) == NOTOK ) {
355 audio2ldif( PS ps, AttributeValue av )
362 return( 0 ); /* for now */
364 if (( pe = grab_pe( av )) == NULLPE || pe->pe_id == PE_PRIM_NULL ) {
365 return( -2 ); /* signal soft error */
368 qb = (struct qbuf *)pe;
371 for ( p = qb->qb_forw; p != qb; p = p->qb_forw ) {
375 if (( buf = (char *) malloc( len )) == NULL ) {
380 for ( p = qb->qb_forw; p != qb; p = p->qb_forw ) {
381 SAFEMEMCPY( buf + len, p->qb_data, p->qb_len );
385 if ( ps_write( ps, (PElementData)buf, len ) == NOTOK ) {
398 photo2ldif( PS ps, AttributeValue av )
402 char *faxparamset = "\000\300\000\000";
405 if (( pe = grab_pe( av )) == NULLPE || pe->pe_id == PE_PRIM_NULL ) {
406 return( -2 ); /* signal soft error */
409 /* old bit string-like format - only handle this for now */
410 if ( pe->pe_class == PE_CLASS_UNIV && pe->pe_form == PE_FORM_PRIM
411 && pe->pe_id == PE_PRIM_BITS ) {
412 len = ps_get_abs( pe );
413 if (( phber = der_alloc()) == NULLBER ) {
416 if ( ber_printf( phber, "t{[tB]{B}}", 0xA3, 0x81, faxparamset,
417 31, (char *)pe->pe_prim, len * 8 ) == -1 ) {
418 ber_free( phber, 1 );
421 if ( ps_write( ps, (PElementData)phber->ber_buf,
422 phber->ber_ptr - phber->ber_buf ) == NOTOK ) {
423 ber_free( phber, 1 );
426 ber_free( phber, 1 );
429 * try just writing this into a PS and sending it along
431 if ( pe2ps( ps, pe ) == NOTOK ) {
441 fileattr2ldif( PS ps, AttributeValue av )
445 if (( pe = grab_pe( av )) == NULLPE || pe->pe_id == PE_PRIM_NULL ) {
446 return( -2 ); /* signal soft error */
450 * try just writing this into a PS and sending it along
452 if ( pe2ps( ps, pe ) == NOTOK ) {