summaryrefslogtreecommitdiffstats
path: root/ldap/servers/slapd/attrsyntax.c
blob: 1afa3c3db619122256d16abf6f8258719f2fab0d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
/** BEGIN COPYRIGHT BLOCK
 * This Program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; version 2 of the License.
 * 
 * This Program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA.
 * 
 * In addition, as a special exception, Red Hat, Inc. gives You the additional
 * right to link the code of this Program with code not covered under the GNU
 * General Public License ("Non-GPL Code") and to distribute linked combinations
 * including the two, subject to the limitations in this paragraph. Non-GPL Code
 * permitted under this exception must only link to the code of this Program
 * through those well defined interfaces identified in the file named EXCEPTION
 * found in the source code files (the "Approved Interfaces"). The files of
 * Non-GPL Code may instantiate templates or use macros or inline functions from
 * the Approved Interfaces without causing the resulting work to be covered by
 * the GNU General Public License. Only Red Hat, Inc. may make changes or
 * additions to the list of Approved Interfaces. You must obey the GNU General
 * Public License in all respects for all of the Program code and other code used
 * in conjunction with the Program except the Non-GPL Code covered by this
 * exception. If you modify this file, you may extend this exception to your
 * version of the file, but you are not obligated to do so. If you do not wish to
 * provide this exception without modification, you must delete this exception
 * statement from your version and license this file solely under the GPL without
 * exception. 
 * 
 * 
 * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
 * Copyright (C) 2005 Red Hat, Inc.
 * All rights reserved.
 * END COPYRIGHT BLOCK **/

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

/* attrsyntax.c */

#include "slap.h"
#include <plhash.h>

/*
 * Note: if both the oid2asi and name2asi locks are acquired at the
 * same time, the old2asi one should be acquired first,
 */

/*
 * This hashtable maps the oid to the struct asyntaxinfo for that oid.
 */
static PLHashTable *oid2asi = NULL;
/* read/write lock to protect table */
static PRRWLock *oid2asi_lock = NULL;

/*
 * This hashtable maps the name or alias of the attribute to the
 * syntax info structure for that attribute.  An attribute type has as
 * many entries in the name2asi table as it has names and aliases, but
 * all entries point to the same struct asyntaxinfo.
 */
static PLHashTable *name2asi = NULL;
/* read/write lock to protect table */
static PRRWLock *name2asi_lock = NULL;

#define AS_LOCK_READ(l)		PR_RWLock_Rlock(l)
#define AS_LOCK_WRITE(l)	PR_RWLock_Wlock(l)
#define AS_UNLOCK_READ(l)	PR_RWLock_Unlock(l)
#define AS_UNLOCK_WRITE(l)	PR_RWLock_Unlock(l)



static void *attr_syntax_get_plugin_by_name_with_default( const char *type );
static void attr_syntax_delete_no_lock( struct asyntaxinfo *asip,
		PRBool remove_from_oid_table );
static struct asyntaxinfo *attr_syntax_get_by_oid_locking_optional( const
		char *oid, PRBool use_lock, PRBool ref_count);

#ifdef ATTR_LDAP_DEBUG
static void attr_syntax_print();
#endif
static int attr_syntax_init(void);

void
attr_syntax_read_lock(void)
{
	if (0 != attr_syntax_init()) return;

	AS_LOCK_READ(oid2asi_lock);
	AS_LOCK_READ(name2asi_lock);
}

void
attr_syntax_unlock_read(void)
{
	if(name2asi_lock) AS_UNLOCK_READ(name2asi_lock);
	if(oid2asi_lock) AS_UNLOCK_READ(oid2asi_lock);
}



#if 0
static int 
check_oid( const char *oid ) {

  int i = 0, length_oid = 0, rc = 0;

  if ( oid == NULL) {
	/* this is bad */
	LDAPDebug (LDAP_DEBUG_ANY, "NULL passed to check_oid\n",0,0,0);
	return 0;
  }
  
  length_oid = strlen(oid);
  if (length_oid < 4) {
	/* this is probably bad */
	LDAPDebug (LDAP_DEBUG_ANY, "Bad oid %s passed to check_oid\n",oid,0,0);
	return 0;
  }

  rc = strcasecmp(oid+(length_oid-4), "-oid");

  if ( 0 == rc ) {
	return 1;
  }

  /* If not, the OID must begin and end with a digit, and contain only 
	 digits and dots */
  
  if ( !isdigit(oid[0]) || 
	   !isdigit(oid[length_oid-1]) ) {
	LDAPDebug (LDAP_DEBUG_ANY, "Non numeric oid %s passed to check_oid\n",oid,0,0);
	return 0;
  }

  /* check to see that it contains only digits and dots */
  for ( i = 0; i < length_oid; i++ ) {
	if ( !isdigit(oid[i]) && oid[i] != '.'  ){
	  LDAPDebug (LDAP_DEBUG_ANY, "Non numeric oid %s passed to check_oid\n",oid,0,0);
	  return 0;
	}
  }

  /* The oid is OK if we're here */
  return 1;
  

}
#endif

#define NBUCKETS(ht)    (1 << (PL_HASH_BITS - (ht)->shift))

#if 0
static int
attr_syntax_check_oids()
{
	int ii = 0;
	int nbad = 0;
	AS_LOCK_READ(oid2asi_lock);
	ii = NBUCKETS(oid2asi);
    for (;ii;--ii) {
		PLHashEntry *he = oid2asi->buckets[ii-1];
		for (; he; he = he->next) {
			if (!check_oid(he->key)) {
				LDAPDebug(LDAP_DEBUG_ANY, "Error: bad oid %s in bucket %d\n",
						  he->key, ii-1, 0);
				nbad++;
			}
		}
    }

	AS_UNLOCK_READ(oid2asi_lock);
	return nbad;
}
#endif

void
attr_syntax_free( struct asyntaxinfo *a )
{
	PR_ASSERT( a->asi_refcnt == 0 );

	cool_charray_free( a->asi_aliases );
	slapi_ch_free( (void**)&a->asi_name );
	slapi_ch_free( (void **)&a->asi_desc );
	slapi_ch_free( (void **)&a->asi_oid );
	slapi_ch_free( (void **)&a->asi_superior );
	slapi_ch_free( (void **)&a->asi_mr_equality );
	slapi_ch_free( (void **)&a->asi_mr_ordering );
	slapi_ch_free( (void **)&a->asi_mr_substring );
	cool_charray_free( a->asi_origin );
	slapi_ch_free( (void **) &a );
}

static struct asyntaxinfo *
attr_syntax_new()
{
	return (struct asyntaxinfo *)slapi_ch_calloc(1, sizeof(struct asyntaxinfo));
}

/*
 * hashNocaseString - used for case insensitive hash lookups
 */
static PLHashNumber
hashNocaseString(const void *key)
{
    PLHashNumber h = 0;
    const unsigned char *s;
 
    for (s = key; *s; s++)
        h = (h >> 28) ^ (h << 4) ^ (tolower(*s));
    return h;
}

/*
 * hashNocaseCompare - used for case insensitive hash key comparisons
 */
static PRIntn
hashNocaseCompare(const void *v1, const void *v2)
{
	return (strcasecmp((char *)v1, (char *)v2) == 0);
}

/*
 * Given an OID, return the syntax info.  If there is more than one
 * attribute syntax with the same OID (i.e. aliases), the first one
 * will be returned.  This is usually the "canonical" one, but it may
 * not be.
 *
 * Note: once the caller is finished using it, the structure returned must
 * be returned by calling to attr_syntax_return().
 */
struct asyntaxinfo *
attr_syntax_get_by_oid(const char *oid)
{
	return attr_syntax_get_by_oid_locking_optional( oid, PR_TRUE, PR_TRUE);
}


static struct asyntaxinfo *
attr_syntax_get_by_oid_locking_optional( const char *oid, PRBool use_lock, PRBool ref_count )
{
	struct asyntaxinfo *asi = 0;
	if (oid2asi)
	{
		if ( use_lock ) AS_LOCK_READ(oid2asi_lock);
		asi = (struct asyntaxinfo *)PL_HashTableLookup_const(oid2asi, oid);
		if (asi)
		{
			if(ref_count) PR_AtomicIncrement( &asi->asi_refcnt );
		}
		if ( use_lock ) AS_UNLOCK_READ(oid2asi_lock);
	}

	return asi;
}

/*
 * Add the syntax info pointer to the look-up-by-oid table.
 * The lock parameter is used by the initialization code.  Normally, we want
 * to acquire a write lock before we modify the table, but during
 * initialization, we are running in single threaded mode, so we don't have
 * to worry about resource contention.
 */
static void
attr_syntax_add_by_oid(const char *oid, struct asyntaxinfo *a, int lock)
{
	if (0 != attr_syntax_init()) return;

	if (lock)
		AS_LOCK_WRITE(oid2asi_lock);

	PL_HashTableAdd(oid2asi, oid, a);

	if (lock)
		AS_UNLOCK_WRITE(oid2asi_lock);
}

/*
 * Return the syntax info given an attribute name.  The name may be the
 * "canonical" name, an alias, or an OID.  The given name need not be
 * normalized since the look up is done case insensitively.
 *
 * Note: once the caller is finished using it, the structure returned must
 * be returned by calling to attr_syntax_return().
 */
struct asyntaxinfo *
attr_syntax_get_by_name(const char *name)
{
	return attr_syntax_get_by_name_locking_optional(name, PR_TRUE, PR_TRUE);
}


struct asyntaxinfo *
attr_syntax_get_by_name_locking_optional(const char *name, PRBool use_lock, PRBool ref_count)
{
	struct asyntaxinfo *asi = 0;
	if (name2asi)
	{
		if ( use_lock ) AS_LOCK_READ(name2asi_lock);
		asi = (struct asyntaxinfo *)PL_HashTableLookup_const(name2asi, name);
		if ( NULL != asi ) {
			if(ref_count) PR_AtomicIncrement( &asi->asi_refcnt );
		}
		if ( use_lock ) AS_UNLOCK_READ(name2asi_lock);
	}
	if (!asi) /* given name may be an OID */
		asi = attr_syntax_get_by_oid_locking_optional(name, use_lock, ref_count);

	return asi;
}


/*
 * Give up a reference to an asi.
 * If the asi has been marked for delete, free it.  This would be a bit
 * easier if we could upgrade a read lock to a write one... but NSPR does
 * not support that paradigm.
 */
void
attr_syntax_return( struct asyntaxinfo *asi )
{
	attr_syntax_return_locking_optional( asi, PR_TRUE );
}

void
attr_syntax_return_locking_optional( struct asyntaxinfo *asi, PRBool use_lock )
{
	if ( NULL != asi ) {
		if ( 0 == PR_AtomicDecrement( &asi->asi_refcnt ))
		{
			PRBool		delete_it;

			if(use_lock) AS_LOCK_READ(name2asi_lock);
			delete_it = asi->asi_marked_for_delete;
			if(use_lock) AS_UNLOCK_READ(name2asi_lock);

			if ( delete_it )
			{
				AS_LOCK_WRITE(name2asi_lock);		/* get a write lock */
				if ( asi->asi_marked_for_delete )	/* one final check */
				{
					attr_syntax_free(asi);
				}
				AS_UNLOCK_WRITE(name2asi_lock);
			}
		}
	}
}

/*
 * Add the syntax info to the look-up-by-name table.  The asi_name and
 * elements of the asi_aliasses field of the syntax info are the keys.
 * These need not be normalized since the look up table is case insensitive.
 * The lock parameter is used by the initialization code.  Normally, we want
 * to acquire a write lock before we modify the table, but during
 * initialization, we are running in single threaded mode, so we don't have
 * to worry about resource contention.
 */
static void
attr_syntax_add_by_name(struct asyntaxinfo *a, int lock)
{
	if (0 != attr_syntax_init()) return;

	if (lock)
		AS_LOCK_WRITE(name2asi_lock);

	PL_HashTableAdd(name2asi, a->asi_name, a);
	if ( a->asi_aliases != NULL ) {
		int		i;

		for ( i = 0; a->asi_aliases[i] != NULL; ++i ) {
			PL_HashTableAdd(name2asi, a->asi_aliases[i], a);
		}
	}

	if (lock)
		AS_UNLOCK_WRITE(name2asi_lock);
}


/*
 * Delete the attribute syntax and all entries corresponding to aliases
 * and oids.
 */
void
attr_syntax_delete( struct asyntaxinfo *asi )
{
	PR_ASSERT( asi );

	if (oid2asi && name2asi) {
		AS_LOCK_WRITE(oid2asi_lock);
		AS_LOCK_WRITE(name2asi_lock);

		attr_syntax_delete_no_lock( asi, PR_TRUE );

		AS_UNLOCK_WRITE(name2asi_lock);
		AS_UNLOCK_WRITE(oid2asi_lock);
	}
}


/*
 * Dispose of a node.  The caller is responsible for locking.  See
 * attr_syntax_delete() for an example.
 */
static void
attr_syntax_delete_no_lock( struct asyntaxinfo *asi,
		PRBool remove_from_oidtable )
{
	int		i;

	if (oid2asi && remove_from_oidtable ) {
		PL_HashTableRemove(oid2asi, asi->asi_oid);
	}

	if(name2asi) {
		PL_HashTableRemove(name2asi, asi->asi_name);
		if ( asi->asi_aliases != NULL ) {
			for ( i = 0; asi->asi_aliases[i] != NULL; ++i ) {
				PL_HashTableRemove(name2asi, asi->asi_aliases[i]);
			}
		}
		if ( asi->asi_refcnt > 0 ) {
			asi->asi_marked_for_delete = PR_TRUE;
		} else {
			attr_syntax_free(asi);
		}
	}
}


/*
 * Look up the attribute type in the syntaxes and return a copy of the
 * normalised attribute type. If it's not there then return a normalised
 * copy of what the caller gave us.
 *
 * Warning: The caller must free the returned string.
 */



char *
slapi_attr_syntax_normalize( const char *s )
{
	struct asyntaxinfo *asi = NULL;
	char *r;
	

    if((asi=attr_syntax_get_by_name_locking_optional(s, PR_TRUE, PR_FALSE)) != NULL ) {
		r = slapi_ch_strdup(asi->asi_name);
	}
	if ( NULL == asi ) {
		r = attr_syntax_normalize_no_lookup( s );
	}
	return r;
}


/*
 * attr_syntax_exists: return 1 if attr_name exists, 0 otherwise
 *
 */
int
attr_syntax_exists(const char *attr_name)
{
	struct asyntaxinfo	*asi;

	asi = attr_syntax_get_by_name(attr_name);
	attr_syntax_return( asi );

	if ( asi != NULL )
	{
		return 1;
	}
	return 0;
}

/* check syntax without incrementing refcount -- handles locking itself */

static void *
attr_syntax_get_plugin_by_name_with_default( const char *type )
{
	struct asyntaxinfo	*asi;
	void				*plugin = NULL;

	/*
	 * first we look for this attribute type explictly
	 */
	if ( (asi = attr_syntax_get_by_name_locking_optional(type, PR_TRUE, PR_FALSE)) == NULL ) {
		/*
		 * no syntax for this type... return DirectoryString
		 * syntax.  we accomplish this by looking up a well known
		 * attribute type that has that syntax.
		 */
		asi = attr_syntax_get_by_name_locking_optional(
				ATTR_WITH_DIRSTRING_SYNTAX, PR_TRUE, PR_FALSE);
	}
	if ( NULL != asi ) {
		plugin = asi->asi_plugin;
	}
	return( plugin );
}


static struct asyntaxinfo *
attr_syntax_dup( struct asyntaxinfo *a )
{
	struct asyntaxinfo *newas = attr_syntax_new();

	newas->asi_aliases = cool_charray_dup( a->asi_aliases );
	newas->asi_name = slapi_ch_strdup( a->asi_name );
	newas->asi_desc = slapi_ch_strdup( a->asi_desc );
	newas->asi_superior = slapi_ch_strdup( a->asi_superior );
	newas->asi_mr_equality = slapi_ch_strdup( a->asi_mr_equality );
	newas->asi_mr_ordering = slapi_ch_strdup( a->asi_mr_ordering );
	newas->asi_mr_substring = slapi_ch_strdup( a->asi_mr_substring );
	newas->asi_origin = cool_charray_dup( a->asi_origin );
	newas->asi_plugin = a->asi_plugin;
	newas->asi_flags = a->asi_flags;
	newas->asi_oid = slapi_ch_strdup( a->asi_oid);
	newas->asi_syntaxlength = a->asi_syntaxlength;

	return( newas );
}


/*
 * Add a new attribute type to the schema.
 *
 * Returns an LDAP error code (LDAP_SUCCESS if all goes well).
 */
int 
attr_syntax_add( struct asyntaxinfo *asip )
{
	int i, rc = LDAP_SUCCESS;
	int nolock = asip->asi_flags & SLAPI_ATTR_FLAG_NOLOCKING;
	struct asyntaxinfo *oldas_from_oid = NULL, *oldas_from_name = NULL;
	/* attr names may have subtypes in them, and we may not want this
	   if strip_subtypes is true, the ; and anything after it in the
	   attr name or alias will be stripped */
	/*int strip_subtypes = 1;*/

	/* make sure the oid is unique */
	if ( NULL != ( oldas_from_oid = attr_syntax_get_by_oid_locking_optional(
					asip->asi_oid, !nolock, PR_TRUE))) {
		if ( 0 == (asip->asi_flags & SLAPI_ATTR_FLAG_OVERRIDE)) {
			/* failure - OID is in use; no override flag */
			rc = LDAP_TYPE_OR_VALUE_EXISTS;
			goto cleanup_and_return;
		}
	}

	/* make sure the primary name is unique OR, if override is allowed, that
     * the primary name and OID point to the same schema definition.
	 */
	if ( NULL != ( oldas_from_name = attr_syntax_get_by_name_locking_optional(
					asip->asi_name, !nolock, PR_TRUE))) {
		if ( 0 == (asip->asi_flags & SLAPI_ATTR_FLAG_OVERRIDE)
					|| ( oldas_from_oid != oldas_from_name )) {
			/* failure; no override flag OR OID and name don't match */
			rc = LDAP_TYPE_OR_VALUE_EXISTS;
			goto cleanup_and_return;
		}
		attr_syntax_delete(oldas_from_name);
	} else if ( NULL != oldas_from_oid ) {
		/* failure - OID is in use but name does not exist */
		rc = LDAP_TYPE_OR_VALUE_EXISTS;
		goto cleanup_and_return;
	}

	if ( NULL != asip->asi_aliases ) {
		/* make sure the aliases are unique */
		for (i = 0; asip->asi_aliases[i] != NULL; ++i) {
			struct asyntaxinfo	*tmpasi;

			if ( NULL != ( tmpasi =
							attr_syntax_get_by_name_locking_optional(
							asip->asi_aliases[i], !nolock,PR_TRUE))) {
				if (asip->asi_flags & SLAPI_ATTR_FLAG_OVERRIDE) {
					attr_syntax_delete(tmpasi);
				} else {
					/* failure - one of the aliases is already in use */
					rc = LDAP_TYPE_OR_VALUE_EXISTS;
				}

				attr_syntax_return( tmpasi );
				if ( LDAP_SUCCESS != rc ) {
					goto cleanup_and_return;
				}
			}
		}
	}

	/* the no lock flag is not worth keeping around */
	asip->asi_flags &= ~SLAPI_ATTR_FLAG_NOLOCKING;
	/* ditto for the override one */
	asip->asi_flags &= ~SLAPI_ATTR_FLAG_OVERRIDE;
	
	attr_syntax_add_by_oid( asip->asi_oid, asip, !nolock);
	attr_syntax_add_by_name( asip, !nolock);

cleanup_and_return:
	attr_syntax_return( oldas_from_oid );
	attr_syntax_return( oldas_from_name );
	return rc;
}


/*
 * Returns an LDAP result code.
 */
int
attr_syntax_create(
	const char			*attr_oid,
	char *const			*attr_names,
	int					num_names,
	const char			*attr_desc,
	const char			*attr_superior,
	const char			*mr_equality,
	const char			*mr_ordering,
	const char			*mr_substring,
	char *const			*attr_origins,
	const char			*attr_syntax,
	int					syntaxlength,
	unsigned long		flags,
	struct asyntaxinfo	**asip
)
{
	char					*s;
	struct asyntaxinfo		a;

	/* XXXmcs: had to cast away const in many places below */
	memset(&a, 0, sizeof(a));
	a.asi_name = slapi_ch_strdup(attr_names[0]);
	if ( NULL != attr_names[1] ) {
		a.asi_aliases = (char **)&attr_names[1]; /* all but the zero'th element */
	}
	a.asi_desc = (char*)attr_desc;
	a.asi_oid = (char*)attr_oid;
	a.asi_superior = (char*)attr_superior;
	a.asi_mr_equality = (char*)mr_equality;
	a.asi_mr_ordering = (char*)mr_ordering;
	a.asi_mr_substring = (char*)mr_substring;
	a.asi_origin = (char **)attr_origins;
	a.asi_plugin = plugin_syntax_find( attr_syntax );
	a.asi_syntaxlength = syntaxlength;
	a.asi_flags = flags;

	/*
	 * If the 'return exact case' option is on (the default), we store the
	 * first name (the canonical one) unchanged so that attribute names are
	 * returned exactly as they appear in the schema configuration files.
	 * But if 'return exact case' has been turned off, we convert the name
	 * to lowercase.  In Netscape Directory Server 4.x and earlier versions,
	 * the default was to convert to lowercase.
	 */
	if (!config_get_return_exact_case()) {
		for (s = a.asi_name; *s; ++s) {
			*s = TOLOWER(*s);
		}
	}

	*asip = attr_syntax_dup(&a);
	slapi_ch_free((void **)&a.asi_name);

	return LDAP_SUCCESS;
}


/*
 * slapi_attr_type2plugin - return the plugin handling the attribute type
 * if type is unknown, we return the caseIgnoreString plugin used by the
 *     objectClass attribute type.
 */

int
slapi_attr_type2plugin( const char *type, void **pi )
{
	char			buf[SLAPD_TYPICAL_ATTRIBUTE_NAME_MAX_LENGTH];
	char			*tmp, *basetype;
	int				rc;

	basetype = buf;
	if ( (tmp = slapi_attr_basetype( type, buf, sizeof(buf) )) != NULL ) {
		basetype = tmp;
	}
	rc = -1;
	*pi = attr_syntax_get_plugin_by_name_with_default( basetype );
	if ( NULL != *pi ) {
		rc = 0;
	}
	if ( tmp != NULL ) {
		free( tmp );
	}

	return( rc );
}

/* deprecated -- not MT safe (pointer into asi is returned!) */
int
slapi_attr_get_oid( const Slapi_Attr *a, char **oid )
{
	struct asyntaxinfo *asi = attr_syntax_get_by_name(a->a_type);
	if (asi) {
		*oid = asi->asi_oid;
		attr_syntax_return(asi);
		return( 0 );
	} else {
		*oid = NULL;
		return( -1 );
	}
}


/* The caller must dispose of oid by calling slapi_ch_free(). */
int
slapi_attr_get_oid_copy( const Slapi_Attr *a, char **oidp )
{
	struct asyntaxinfo *asi = attr_syntax_get_by_name(a->a_type);
	if (asi) {
		*oidp = slapi_ch_strdup( asi->asi_oid );
		attr_syntax_return(asi);
		return( 0 );
	} else {
		*oidp = NULL;
		return( -1 );
	}
}

/* Returns the oid of the syntax of the Slapi_Attr that's passed in.
 * The caller must dispose of oid by calling slapi_ch_free_string(). */
int
slapi_attr_get_syntax_oid_copy( const Slapi_Attr *a, char **oidp )
{
	void *pi = NULL;

	if (a && (slapi_attr_type2plugin(a->a_type, &pi) == 0)) {
		*oidp = slapi_ch_strdup(plugin_syntax2oid(pi));
		return( 0 );
	} else {
		*oidp = NULL;
		return( -1 );
	}
}

#ifdef ATTR_LDAP_DEBUG

PRIntn
attr_syntax_printnode(PLHashEntry *he, PRIntn i, void *arg)
{
	char *alias = (char *)he->key;
	struct asyntaxinfo *a = (struct asyntaxinfo *)he->value;
	printf( "  name: %s\n", a->asi_name );
	printf( "\t flags       : 0x%x\n", a->asi_flags );
	printf( "\t alias       : %s\n", alias );
	printf( "\t desc        : %s\n", a->asi_desc );
	printf( "\t oid         : %s\n", a->asi_oid );
	printf( "\t superior    : %s\n", a->asi_superior );
	printf( "\t mr_equality : %s\n", a->asi_mr_equality );
	printf( "\t mr_ordering : %s\n", a->asi_mr_ordering );
	printf( "\t mr_substring: %s\n", a->asi_mr_substring );
	if ( NULL != a->asi_origin ) {
		for ( i = 0; NULL != a->asi_origin[i]; ++i ) {
			printf( "\t origin      : %s\n", a->asi_origin[i] );
		}
	}
	printf( "\tplugin: %p\n", a->asi_plugin );
	printf( "--------------\n" );

	return HT_ENUMERATE_NEXT;
}

void
attr_syntax_print()
{
	printf( "*** attr_syntax_print ***\n" );
	PL_HashTableEnumerateEntries(name2asi, attr_syntax_printnode, 0);
}

#endif


/* lowercase the attr name and chop trailing spaces */
/* note that s may contain options also, e.g., userCertificate;binary */
char *
attr_syntax_normalize_no_lookup( const char *s )
{
	char	*save, *tmps;

    tmps = slapi_ch_strdup(s);
    for ( save = tmps; (*tmps != '\0') && (*tmps != ' '); tmps++ )
	{
	  *tmps = TOLOWER( *tmps );
	}
	*tmps = '\0';

	return save;
}

struct enum_arg_wrapper {
	AttrEnumFunc aef;
	void *arg;
};

PRIntn
attr_syntax_enumerate_internal(PLHashEntry *he, PRIntn i, void *arg)
{
	struct enum_arg_wrapper *eaw = (struct enum_arg_wrapper *)arg;
	int	rc;

	rc = (eaw->aef)((struct asyntaxinfo *)he->value, eaw->arg);
	if ( ATTR_SYNTAX_ENUM_STOP == rc ) {
		rc = HT_ENUMERATE_STOP;
	} else if ( ATTR_SYNTAX_ENUM_REMOVE == rc ) {
		rc = HT_ENUMERATE_REMOVE;
	} else {
		rc = HT_ENUMERATE_NEXT;
	}

	return rc;
}

void
attr_syntax_enumerate_attrs(AttrEnumFunc aef, void *arg, PRBool writelock )
{
	struct enum_arg_wrapper eaw;
	eaw.aef = aef;
	eaw.arg = arg;

	if (!oid2asi)
		return;

	if ( writelock ) {
		AS_LOCK_WRITE(oid2asi_lock);
		AS_LOCK_WRITE(name2asi_lock);
	} else {
		AS_LOCK_READ(oid2asi_lock);
		AS_LOCK_READ(name2asi_lock);
	}

	PL_HashTableEnumerateEntries(oid2asi, attr_syntax_enumerate_internal, &eaw);

	if ( writelock ) {
		AS_UNLOCK_WRITE(oid2asi_lock);
		AS_UNLOCK_WRITE(name2asi_lock);
	} else {
		AS_UNLOCK_READ(oid2asi_lock);
		AS_UNLOCK_READ(name2asi_lock);
	}
}


struct attr_syntax_enum_flaginfo {
	unsigned long	asef_flag;
};

static int
attr_syntax_clear_flag_callback(struct asyntaxinfo *asip, void *arg)
{
	struct attr_syntax_enum_flaginfo	*fi;

	PR_ASSERT( asip != NULL );
	fi = (struct attr_syntax_enum_flaginfo *)arg;
	PR_ASSERT( fi != NULL );

	asip->asi_flags &= ~(fi->asef_flag);

	return ATTR_SYNTAX_ENUM_NEXT;
}


static int
attr_syntax_delete_if_not_flagged(struct asyntaxinfo *asip, void *arg)
{
	struct attr_syntax_enum_flaginfo	*fi;

	PR_ASSERT( asip != NULL );
	fi = (struct attr_syntax_enum_flaginfo *)arg;
	PR_ASSERT( fi != NULL );

	if ( 0 == ( asip->asi_flags & fi->asef_flag )) {
		attr_syntax_delete_no_lock( asip, PR_FALSE );
		return ATTR_SYNTAX_ENUM_REMOVE;
	} else {
		return ATTR_SYNTAX_ENUM_NEXT;
	}
}


/*
 * Clear 'flag' within all attribute definitions.
 */
void
attr_syntax_all_clear_flag( unsigned long flag )
{
	struct attr_syntax_enum_flaginfo fi;

	memset( &fi, 0, sizeof(fi));
	fi.asef_flag = flag;
	attr_syntax_enumerate_attrs( attr_syntax_clear_flag_callback,
				(void *)&fi, PR_TRUE );
}


/*
 * Delete all attribute definitions that do not contain any bits of 'flag'
 * in their flags.
 */
void
attr_syntax_delete_all_not_flagged( unsigned long flag )
{
	struct attr_syntax_enum_flaginfo fi;

	memset( &fi, 0, sizeof(fi));
	fi.asef_flag = flag;
	attr_syntax_enumerate_attrs( attr_syntax_delete_if_not_flagged,
				(void *)&fi, PR_TRUE );
}

static int
attr_syntax_init(void)
{
	if (!oid2asi)
	{
		oid2asi = PL_NewHashTable(2047, hashNocaseString,
								  hashNocaseCompare,
								  PL_CompareValues, 0, 0);
		if ( NULL == ( oid2asi_lock = PR_NewRWLock( PR_RWLOCK_RANK_NONE,
				"attrsyntax oid rwlock" ))) {
			if(oid2asi) PL_HashTableDestroy(oid2asi);
			oid2asi = NULL;

			slapi_log_error( SLAPI_LOG_FATAL, "attr_syntax_init",
					"PR_NewRWLock() for oid2asi lock failed\n" );
			return 1;
		}
	}

	if (!name2asi)
	{
		name2asi = PL_NewHashTable(2047, hashNocaseString,
								   hashNocaseCompare,
								   PL_CompareValues, 0, 0);
		if ( NULL == ( name2asi_lock = PR_NewRWLock( PR_RWLOCK_RANK_NONE,
				"attrsyntax name2asi rwlock"))) {
			if(name2asi) PL_HashTableDestroy(name2asi);
			name2asi = NULL;

			slapi_log_error( SLAPI_LOG_FATAL, "attr_syntax_init",
					"PR_NewRWLock() for oid2asi lock failed\n" );
			return 1;
		}
	}
	return 0;
}