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.
18 #include <quipu/commonarg.h>
19 #include <quipu/attrvalue.h>
20 #include <quipu/ds_error.h>
21 #include <quipu/ds_search.h>
22 /* #include <quipu/dap2.h> */
23 #include <quipu/dua.h>
24 #include <sys/types.h>
29 #include "ldapsyntax.h"
32 short ldap_password_syntax;
33 short ldap_photo_syntax;
34 short ldap_jpeg_syntax;
35 short ldap_audio_syntax;
37 static int dn2ldif( PS ps, DN dn );
38 static int jpeg2ldif( PS ps, AttributeValue av );
39 static int audio2ldif( PS ps, AttributeValue av );
40 static int photo2ldif( PS ps, AttributeValue av );
41 static int fileattr2ldif( PS ps, AttributeValue av );
42 static void de_t61( char *s, int t61mark );
43 static void de_crypt( char *s );
45 extern char *progname;
47 #define SEPARATOR(c) (c == ',' || c == ';')
48 #define SPACE(c) (c == ' ' || c == '\n')
54 if (( ldap_dn_syntax = str2syntax( "DN" )) == 0 ) {
55 return( -1 ); /* must have this syntax handler */
57 ldap_password_syntax = str2syntax( "password" );
58 ldap_photo_syntax = str2syntax( "photo" );
59 ldap_jpeg_syntax = str2syntax( "jpeg" );
60 ldap_audio_syntax = str2syntax( "audio" );
67 * av2ldif: convert attribute value contained in "av" to ldif format
68 * and write to "outfp". If "dn" is not NULL, convert it instead of "av".
71 av2ldif( FILE *outfp, AV_Sequence av, DN dn, short syntax, char *attrname,
76 struct file_syntax *fsyntax;
79 fsyntax = (struct file_syntax *) av->avseq_av.av_struct;
82 rc = 0; /* optimistic */
83 str_ps->ps_ptr = str_ps->ps_base; /* reset string PS */
85 if ( dn != NULL || syntax == ldap_dn_syntax ) { /* DNs */
86 rc = dn2ldif( str_ps, ( dn != NULLDN ) ? dn :
87 (DN)(av->avseq_av.av_struct));
89 } else if ( syntax == ldap_jpeg_syntax || ( syntax > AV_WRITE_FILE &&
90 fsyntax->fs_real_syntax == ldap_jpeg_syntax )) {
91 rc = jpeg2ldif( str_ps, &av->avseq_av );
93 } else if ( syntax == ldap_photo_syntax || ( syntax > AV_WRITE_FILE &&
94 fsyntax->fs_real_syntax == ldap_photo_syntax )) {
95 rc = photo2ldif( str_ps, &av->avseq_av );
97 } else if ( syntax == ldap_audio_syntax || ( syntax > AV_WRITE_FILE &&
98 fsyntax->fs_real_syntax == ldap_audio_syntax )) {
99 rc = audio2ldif( str_ps, &av->avseq_av );
101 } else if ( syntax > AV_WRITE_FILE ) {
102 rc = fileattr2ldif( str_ps, &av->avseq_av );
105 AttrV_print( str_ps, &av->avseq_av, EDBOUT );
106 *str_ps->ps_ptr = '\0';
107 de_t61( str_ps->ps_base, 0 );
109 if ( syntax == ldap_password_syntax ) {
110 de_crypt( str_ps->ps_base );
113 str_ps->ps_ptr = str_ps->ps_base + strlen( str_ps->ps_base );
116 if ( rc == 0 && str_ps->ps_ptr > str_ps->ps_base ) {
117 *str_ps->ps_ptr = '\0';
118 if (( buf = ldif_type_and_value( attrname, str_ps->ps_base,
119 str_ps->ps_ptr - str_ps->ps_base )) == NULL ) {
122 if ( fputs( buf, outfp ) == EOF ) {
130 if ( syntax > AV_WRITE_FILE ) {
132 "%s: attribute file '%s' not found (skipping value)\n",
133 progname, fsyntax->fs_name );
135 rc = 0; /* treat as "soft" error -- keep going */
143 dn2ldif( PS ps, DN dn )
150 if ( dn == NULLDN ) {
154 if ( dn->dn_parent != NULLDN ) {
155 if (( rc = dn2ldif( ps, dn->dn_parent )) != 0 ) {
158 ps_print( ps, ", " );
161 if ( (rps = ps_alloc( str_open )) == NULLPS ||
162 str_setup( rps, NULLCP, 0, 0 ) == NOTOK ) {
167 for ( rdn = dn->dn_rdn; rdn != NULLRDN; rdn = rdn->rdn_next ) {
171 ps_print( ps, " + " );
174 AttrT_print( ps, rdn->rdn_at, EDBOUT );
177 if ( rdn->rdn_at->oa_syntax == ldap_dn_syntax ) {
178 if (( rc = dn2ldif( rps, (DN) rdn->rdn_av.av_struct )) != 0 ) {
182 value = rps->ps_base;
184 AttrV_print( rps, &rdn->rdn_av, EDBOUT );
186 value = rps->ps_base;
191 * ,+="\\\n all go in quotes. " and \\ need to
192 * be preceeded by \\.
195 if ( strpbrk( value, ",+=\"\\\n" ) != NULL || SPACE( value[0] )
196 || SPACE( value[max( strlen(value) - 1, 0 )] ) ) {
200 ps_print( ps, "\"" );
203 for ( p = value; *p != '\0'; p++ ) {
204 if ( *p == '"' || *p == '\\' ) {
208 if ( specialcount > 0 ) {
209 tmp = smalloc( strlen( value ) + specialcount + 1 );
210 for ( p = value, t = tmp; *p != '\0'; p++ ) {
224 ps_print( ps, value );
227 ps_print( ps, "\"" );
229 ps_print( ps, value );
232 rps->ps_ptr = rps->ps_base;
254 if ( strncasecmp( s, T61, T61LEN) == 0 ) {
273 else if ( c >= 'A' && c <= 'F' )
275 else if ( c >= 'a' && c <= 'f' )
285 else if ( c >= 'A' && c <= 'F' )
287 else if ( c >= 'a' && c <= 'f' )
308 #define CRYPT_MASK 0x23
315 if ( strncmp( s, "{CRYPT}", 7 ) == 0 ) {
316 strcpy( s, s + 7 ); /* strip off "{CRYPT}" */
318 for ( p = s; *p != '\0'; ++p) { /* "decrypt" each byte */
319 if ( *p != CRYPT_MASK ) {
328 jpeg2ldif( PS ps, AttributeValue av )
333 if (( pe = grab_pe( av )) == NULLPE || pe->pe_id == PE_PRIM_NULL ) {
334 return( -2 ); /* signal soft error */
337 if (( pe->pe_class != PE_CLASS_UNIV && pe->pe_class != PE_CLASS_CONT )
338 || pe->pe_form != PE_FORM_PRIM || pe->pe_id != PE_PRIM_OCTS ) {
342 if ( pe_pullup( pe ) == NOTOK ) {
346 len = ps_get_abs( pe );
348 if ( ps_write( ps, (PElementData)pe->pe_prim, len ) == NOTOK ) {
357 audio2ldif( PS ps, AttributeValue av )
364 return( 0 ); /* for now */
366 if (( pe = grab_pe( av )) == NULLPE || pe->pe_id == PE_PRIM_NULL ) {
367 return( -2 ); /* signal soft error */
370 qb = (struct qbuf *)pe;
373 for ( p = qb->qb_forw; p != qb; p = p->qb_forw ) {
377 if (( buf = (char *) malloc( len )) == NULL ) {
382 for ( p = qb->qb_forw; p != qb; p = p->qb_forw ) {
383 SAFEMEMCPY( buf + len, p->qb_data, p->qb_len );
387 if ( ps_write( ps, (PElementData)buf, len ) == NOTOK ) {
400 photo2ldif( PS ps, AttributeValue av )
404 char *faxparamset = "\000\300\000\000";
407 if (( pe = grab_pe( av )) == NULLPE || pe->pe_id == PE_PRIM_NULL ) {
408 return( -2 ); /* signal soft error */
411 /* old bit string-like format - only handle this for now */
412 if ( pe->pe_class == PE_CLASS_UNIV && pe->pe_form == PE_FORM_PRIM
413 && pe->pe_id == PE_PRIM_BITS ) {
414 len = ps_get_abs( pe );
415 if (( phber = der_alloc()) == NULLBER ) {
418 if ( ber_printf( phber, "t{[tB]{B}}", 0xA3, 0x81, faxparamset,
419 31, (char *)pe->pe_prim, len * 8 ) == -1 ) {
420 ber_free( phber, 1 );
423 if ( ps_write( ps, (PElementData)phber->ber_buf,
424 phber->ber_ptr - phber->ber_buf ) == NOTOK ) {
425 ber_free( phber, 1 );
428 ber_free( phber, 1 );
431 * try just writing this into a PS and sending it along
433 if ( pe2ps( ps, pe ) == NOTOK ) {
443 fileattr2ldif( PS ps, AttributeValue av )
447 if (( pe = grab_pe( av )) == NULLPE || pe->pe_id == PE_PRIM_NULL ) {
448 return( -2 ); /* signal soft error */
452 * try just writing this into a PS and sending it along
454 if ( pe2ps( ps, pe ) == NOTOK ) {