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 <ac/string.h>
20 #include <quipu/commonarg.h>
21 #include <quipu/attrvalue.h>
22 #include <quipu/ds_error.h>
23 #include <quipu/ds_search.h>
24 /* #include <quipu/dap2.h> */
25 #include <quipu/dua.h>
26 #include <sys/types.h>
31 #include "ldapsyntax.h"
34 short ldap_password_syntax;
35 short ldap_photo_syntax;
36 short ldap_jpeg_syntax;
37 short ldap_audio_syntax;
39 static int dn2ldif( PS ps, DN dn );
40 static int jpeg2ldif( PS ps, AttributeValue av );
41 static int audio2ldif( PS ps, AttributeValue av );
42 static int photo2ldif( PS ps, AttributeValue av );
43 static int fileattr2ldif( PS ps, AttributeValue av );
44 static void de_t61( char *s, int t61mark );
45 static void de_crypt( char *s );
47 extern char *progname;
49 #define SEPARATOR(c) (c == ',' || c == ';')
50 #define SPACE(c) (c == ' ' || c == '\n')
56 if (( ldap_dn_syntax = str2syntax( "DN" )) == 0 ) {
57 return( -1 ); /* must have this syntax handler */
59 ldap_password_syntax = str2syntax( "password" );
60 ldap_photo_syntax = str2syntax( "photo" );
61 ldap_jpeg_syntax = str2syntax( "jpeg" );
62 ldap_audio_syntax = str2syntax( "audio" );
69 * av2ldif: convert attribute value contained in "av" to ldif format
70 * and write to "outfp". If "dn" is not NULL, convert it instead of "av".
73 av2ldif( FILE *outfp, AV_Sequence av, DN dn, short syntax, char *attrname,
78 struct file_syntax *fsyntax;
81 fsyntax = (struct file_syntax *) av->avseq_av.av_struct;
84 rc = 0; /* optimistic */
85 str_ps->ps_ptr = str_ps->ps_base; /* reset string PS */
87 if ( dn != NULL || syntax == ldap_dn_syntax ) { /* DNs */
88 rc = dn2ldif( str_ps, ( dn != NULLDN ) ? dn :
89 (DN)(av->avseq_av.av_struct));
91 } else if ( syntax == ldap_jpeg_syntax || ( syntax > AV_WRITE_FILE &&
92 fsyntax->fs_real_syntax == ldap_jpeg_syntax )) {
93 rc = jpeg2ldif( str_ps, &av->avseq_av );
95 } else if ( syntax == ldap_photo_syntax || ( syntax > AV_WRITE_FILE &&
96 fsyntax->fs_real_syntax == ldap_photo_syntax )) {
97 rc = photo2ldif( str_ps, &av->avseq_av );
99 } else if ( syntax == ldap_audio_syntax || ( syntax > AV_WRITE_FILE &&
100 fsyntax->fs_real_syntax == ldap_audio_syntax )) {
101 rc = audio2ldif( str_ps, &av->avseq_av );
103 } else if ( syntax > AV_WRITE_FILE ) {
104 rc = fileattr2ldif( str_ps, &av->avseq_av );
107 AttrV_print( str_ps, &av->avseq_av, EDBOUT );
108 *str_ps->ps_ptr = '\0';
109 de_t61( str_ps->ps_base, 0 );
111 if ( syntax == ldap_password_syntax ) {
112 de_crypt( str_ps->ps_base );
115 str_ps->ps_ptr = str_ps->ps_base + strlen( str_ps->ps_base );
118 if ( rc == 0 && str_ps->ps_ptr > str_ps->ps_base ) {
119 *str_ps->ps_ptr = '\0';
120 if (( buf = ldif_type_and_value( attrname, str_ps->ps_base,
121 str_ps->ps_ptr - str_ps->ps_base )) == NULL ) {
124 if ( fputs( buf, outfp ) == EOF ) {
132 if ( syntax > AV_WRITE_FILE ) {
134 "%s: attribute file '%s' not found (skipping value)\n",
135 progname, fsyntax->fs_name );
137 rc = 0; /* treat as "soft" error -- keep going */
145 dn2ldif( PS ps, DN dn )
152 if ( dn == NULLDN ) {
156 if ( dn->dn_parent != NULLDN ) {
157 if (( rc = dn2ldif( ps, dn->dn_parent )) != 0 ) {
160 ps_print( ps, ", " );
163 if ( (rps = ps_alloc( str_open )) == NULLPS ||
164 str_setup( rps, NULLCP, 0, 0 ) == NOTOK ) {
169 for ( rdn = dn->dn_rdn; rdn != NULLRDN; rdn = rdn->rdn_next ) {
173 ps_print( ps, " + " );
176 AttrT_print( ps, rdn->rdn_at, EDBOUT );
179 if ( rdn->rdn_at->oa_syntax == ldap_dn_syntax ) {
180 if (( rc = dn2ldif( rps, (DN) rdn->rdn_av.av_struct )) != 0 ) {
184 value = rps->ps_base;
186 AttrV_print( rps, &rdn->rdn_av, EDBOUT );
188 value = rps->ps_base;
193 * ,+="\\\n all go in quotes. " and \\ need to
194 * be preceeded by \\.
197 if ( strpbrk( value, ",+=\"\\\n" ) != NULL || SPACE( value[0] )
198 || SPACE( value[max( strlen(value) - 1, 0 )] ) ) {
202 ps_print( ps, "\"" );
205 for ( p = value; *p != '\0'; p++ ) {
206 if ( *p == '"' || *p == '\\' ) {
210 if ( specialcount > 0 ) {
211 tmp = smalloc( strlen( value ) + specialcount + 1 );
212 for ( p = value, t = tmp; *p != '\0'; p++ ) {
226 ps_print( ps, value );
229 ps_print( ps, "\"" );
231 ps_print( ps, value );
234 rps->ps_ptr = rps->ps_base;
256 if ( strncasecmp( s, T61, T61LEN) == 0 ) {
275 else if ( c >= 'A' && c <= 'F' )
277 else if ( c >= 'a' && c <= 'f' )
287 else if ( c >= 'A' && c <= 'F' )
289 else if ( c >= 'a' && c <= 'f' )
310 #define CRYPT_MASK 0x23
317 if ( strncmp( s, "{CRYPT}", 7 ) == 0 ) {
318 SAFEMEMCPY( s, s + 7, strlen( s + 7 ) + 1 ); /* strip off "{CRYPT}" */
320 for ( p = s; *p != '\0'; ++p) { /* "decrypt" each byte */
321 if ( *p != CRYPT_MASK ) {
330 jpeg2ldif( PS ps, AttributeValue av )
335 if (( pe = grab_pe( av )) == NULLPE || pe->pe_id == PE_PRIM_NULL ) {
336 return( -2 ); /* signal soft error */
339 if (( pe->pe_class != PE_CLASS_UNIV && pe->pe_class != PE_CLASS_CONT )
340 || pe->pe_form != PE_FORM_PRIM || pe->pe_id != PE_PRIM_OCTS ) {
344 if ( pe_pullup( pe ) == NOTOK ) {
348 len = ps_get_abs( pe );
350 if ( ps_write( ps, (PElementData)pe->pe_prim, len ) == NOTOK ) {
359 audio2ldif( PS ps, AttributeValue av )
366 return( 0 ); /* for now */
368 if (( pe = grab_pe( av )) == NULLPE || pe->pe_id == PE_PRIM_NULL ) {
369 return( -2 ); /* signal soft error */
372 qb = (struct qbuf *)pe;
375 for ( p = qb->qb_forw; p != qb; p = p->qb_forw ) {
379 if (( buf = (char *) malloc( len )) == NULL ) {
384 for ( p = qb->qb_forw; p != qb; p = p->qb_forw ) {
385 SAFEMEMCPY( buf + len, p->qb_data, p->qb_len );
389 if ( ps_write( ps, (PElementData)buf, len ) == NOTOK ) {
402 photo2ldif( PS ps, AttributeValue av )
406 char *faxparamset = "\000\300\000\000";
409 if (( pe = grab_pe( av )) == NULLPE || pe->pe_id == PE_PRIM_NULL ) {
410 return( -2 ); /* signal soft error */
413 /* old bit string-like format - only handle this for now */
414 if ( pe->pe_class == PE_CLASS_UNIV && pe->pe_form == PE_FORM_PRIM
415 && pe->pe_id == PE_PRIM_BITS ) {
416 len = ps_get_abs( pe );
417 if (( phber = der_alloc()) == NULLBER ) {
420 if ( ber_printf( phber, "t{[tB]{B}}", 0xA3, 0x81, faxparamset,
421 31, (char *)pe->pe_prim, len * 8 ) == -1 ) {
422 ber_free( phber, 1 );
425 if ( ps_write( ps, (PElementData)phber->ber_buf,
426 phber->ber_ptr - phber->ber_buf ) == NOTOK ) {
427 ber_free( phber, 1 );
430 ber_free( phber, 1 );
433 * try just writing this into a PS and sending it along
435 if ( pe2ps( ps, pe ) == NOTOK ) {
445 fileattr2ldif( PS ps, AttributeValue av )
449 if (( pe = grab_pe( av )) == NULLPE || pe->pe_id == PE_PRIM_NULL ) {
450 return( -2 ); /* signal soft error */
454 * try just writing this into a PS and sending it along
456 if ( pe2ps( ps, pe ) == NOTOK ) {