attrsyntax.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. /* attrsyntax.c */
  42. #include "slap.h"
  43. #include <plhash.h>
  44. /*
  45. * Note: if both the oid2asi and name2asi locks are acquired at the
  46. * same time, the old2asi one should be acquired first,
  47. */
  48. /*
  49. * This hashtable maps the oid to the struct asyntaxinfo for that oid.
  50. */
  51. static PLHashTable *oid2asi = NULL;
  52. /* read/write lock to protect table */
  53. static Slapi_RWLock *oid2asi_lock = NULL;
  54. static PLHashTable *internalasi = NULL;
  55. /*
  56. * This hashtable maps the name or alias of the attribute to the
  57. * syntax info structure for that attribute. An attribute type has as
  58. * many entries in the name2asi table as it has names and aliases, but
  59. * all entries point to the same struct asyntaxinfo.
  60. */
  61. static PLHashTable *name2asi = NULL;
  62. /* read/write lock to protect table */
  63. static Slapi_RWLock *name2asi_lock = NULL;
  64. static int asi_locking = 1;
  65. #define AS_LOCK_READ(l) if (asi_locking) { slapi_rwlock_rdlock(l); }
  66. #define AS_LOCK_WRITE(l) if (asi_locking) { slapi_rwlock_wrlock(l); }
  67. #define AS_UNLOCK_READ(l) if (asi_locking) { slapi_rwlock_unlock(l); }
  68. #define AS_UNLOCK_WRITE(l) if (asi_locking) { slapi_rwlock_unlock(l); }
  69. static struct asyntaxinfo *default_asi = NULL;
  70. static void *attr_syntax_get_plugin_by_name_with_default( const char *type );
  71. static void attr_syntax_delete_no_lock( struct asyntaxinfo *asip,
  72. PRBool remove_from_oid_table );
  73. static struct asyntaxinfo *attr_syntax_get_by_oid_locking_optional( const
  74. char *oid, PRBool use_lock);
  75. #ifdef ATTR_LDAP_DEBUG
  76. static void attr_syntax_print();
  77. #endif
  78. static int attr_syntax_init(void);
  79. void
  80. attr_syntax_read_lock(void)
  81. {
  82. if (0 != attr_syntax_init()) return;
  83. AS_LOCK_READ(oid2asi_lock);
  84. AS_LOCK_READ(name2asi_lock);
  85. }
  86. void
  87. attr_syntax_write_lock(void)
  88. {
  89. if (0 != attr_syntax_init()) return;
  90. AS_LOCK_WRITE(oid2asi_lock);
  91. AS_LOCK_WRITE(name2asi_lock);
  92. }
  93. void
  94. attr_syntax_unlock_read(void)
  95. {
  96. AS_UNLOCK_READ(name2asi_lock);
  97. AS_UNLOCK_READ(oid2asi_lock);
  98. }
  99. void
  100. attr_syntax_unlock_write(void)
  101. {
  102. AS_UNLOCK_WRITE(name2asi_lock);
  103. AS_UNLOCK_WRITE(oid2asi_lock);
  104. }
  105. #if 0
  106. static int
  107. check_oid( const char *oid ) {
  108. int i = 0, length_oid = 0, rc = 0;
  109. if ( oid == NULL) {
  110. /* this is bad */
  111. LDAPDebug (LDAP_DEBUG_ANY, "NULL passed to check_oid\n",0,0,0);
  112. return 0;
  113. }
  114. length_oid = strlen(oid);
  115. if (length_oid < 4) {
  116. /* this is probably bad */
  117. LDAPDebug (LDAP_DEBUG_ANY, "Bad oid %s passed to check_oid\n",oid,0,0);
  118. return 0;
  119. }
  120. rc = strcasecmp(oid+(length_oid-4), "-oid");
  121. if ( 0 == rc ) {
  122. return 1;
  123. }
  124. /* If not, the OID must begin and end with a digit, and contain only
  125. digits and dots */
  126. if ( !isdigit(oid[0]) ||
  127. !isdigit(oid[length_oid-1]) ) {
  128. LDAPDebug (LDAP_DEBUG_ANY, "Non numeric oid %s passed to check_oid\n",oid,0,0);
  129. return 0;
  130. }
  131. /* check to see that it contains only digits and dots */
  132. for ( i = 0; i < length_oid; i++ ) {
  133. if ( !isdigit(oid[i]) && oid[i] != '.' ){
  134. LDAPDebug (LDAP_DEBUG_ANY, "Non numeric oid %s passed to check_oid\n",oid,0,0);
  135. return 0;
  136. }
  137. }
  138. /* The oid is OK if we're here */
  139. return 1;
  140. }
  141. #endif
  142. #define NBUCKETS(ht) (1 << (PL_HASH_BITS - (ht)->shift))
  143. #if 0
  144. static int
  145. attr_syntax_check_oids()
  146. {
  147. int ii = 0;
  148. int nbad = 0;
  149. AS_LOCK_READ(oid2asi_lock);
  150. ii = NBUCKETS(oid2asi);
  151. for (;ii;--ii) {
  152. PLHashEntry *he = oid2asi->buckets[ii-1];
  153. for (; he; he = he->next) {
  154. if (!check_oid(he->key)) {
  155. LDAPDebug(LDAP_DEBUG_ANY, "Error: bad oid %s in bucket %d\n",
  156. he->key, ii-1, 0);
  157. nbad++;
  158. }
  159. }
  160. }
  161. AS_UNLOCK_READ(oid2asi_lock);
  162. return nbad;
  163. }
  164. #endif
  165. void
  166. attr_syntax_free( struct asyntaxinfo *a )
  167. {
  168. PR_ASSERT( a->asi_refcnt == 0 );
  169. cool_charray_free( a->asi_aliases );
  170. slapi_ch_free( (void**)&a->asi_name );
  171. slapi_ch_free( (void **)&a->asi_desc );
  172. slapi_ch_free( (void **)&a->asi_oid );
  173. slapi_ch_free( (void **)&a->asi_superior );
  174. slapi_ch_free( (void **)&a->asi_mr_equality );
  175. slapi_ch_free( (void **)&a->asi_mr_ordering );
  176. slapi_ch_free( (void **)&a->asi_mr_substring );
  177. schema_free_extensions(a->asi_extensions);
  178. slapi_ch_free( (void **) &a );
  179. }
  180. static struct asyntaxinfo *
  181. attr_syntax_new()
  182. {
  183. return (struct asyntaxinfo *)slapi_ch_calloc(1, sizeof(struct asyntaxinfo));
  184. }
  185. /*
  186. * Given an OID, return the syntax info. If there is more than one
  187. * attribute syntax with the same OID (i.e. aliases), the first one
  188. * will be returned. This is usually the "canonical" one, but it may
  189. * not be.
  190. *
  191. * Note: once the caller is finished using it, the structure returned must
  192. * be returned by calling to attr_syntax_return().
  193. */
  194. struct asyntaxinfo *
  195. attr_syntax_get_by_oid(const char *oid)
  196. {
  197. return attr_syntax_get_by_oid_locking_optional( oid, PR_TRUE);
  198. }
  199. /*
  200. * A version of attr_syntax_get_by_oid() that allows you to bypass using
  201. * a lock to access the global oid hash table.
  202. *
  203. * Note: once the caller is finished using it, the structure must be
  204. * returned by calling attr_syntax_return_locking_optional() with the
  205. * same use_lock parameter.
  206. */
  207. static struct asyntaxinfo *
  208. attr_syntax_get_by_oid_locking_optional( const char *oid, PRBool use_lock )
  209. {
  210. struct asyntaxinfo *asi = 0;
  211. if (oid2asi)
  212. {
  213. if ( use_lock ) {
  214. AS_LOCK_READ(oid2asi_lock);
  215. }
  216. asi = (struct asyntaxinfo *)PL_HashTableLookup_const(oid2asi, oid);
  217. if (asi)
  218. {
  219. PR_AtomicIncrement( &asi->asi_refcnt );
  220. }
  221. if ( use_lock ) {
  222. AS_UNLOCK_READ(oid2asi_lock);
  223. }
  224. }
  225. return asi;
  226. }
  227. /*
  228. * Add the syntax info pointer to the look-up-by-oid table.
  229. * The lock parameter is used by the initialization code. Normally, we want
  230. * to acquire a write lock before we modify the table, but during
  231. * initialization, we are running in single threaded mode, so we don't have
  232. * to worry about resource contention.
  233. */
  234. static void
  235. attr_syntax_add_by_oid(const char *oid, struct asyntaxinfo *a, int lock)
  236. {
  237. if (0 != attr_syntax_init()) return;
  238. if (lock) {
  239. AS_LOCK_WRITE(oid2asi_lock);
  240. }
  241. PL_HashTableAdd(oid2asi, oid, a);
  242. if (lock) {
  243. AS_UNLOCK_WRITE(oid2asi_lock);
  244. }
  245. }
  246. /*
  247. * Return the syntax info given an attribute name. The name may be the
  248. * "canonical" name, an alias, or an OID. The given name need not be
  249. * normalized since the look up is done case insensitively.
  250. *
  251. * Note: once the caller is finished using it, the structure returned must
  252. * be returned by calling to attr_syntax_return().
  253. */
  254. struct asyntaxinfo *
  255. attr_syntax_get_by_name(const char *name)
  256. {
  257. return attr_syntax_get_by_name_locking_optional(name, PR_TRUE);
  258. }
  259. struct asyntaxinfo *
  260. attr_syntax_get_by_name_with_default(const char *name)
  261. {
  262. struct asyntaxinfo *asi = NULL;
  263. asi = attr_syntax_get_by_name_locking_optional(name, PR_TRUE);
  264. if (asi == NULL)
  265. asi = attr_syntax_get_by_name(ATTR_WITH_OCTETSTRING_SYNTAX);
  266. if ( asi == NULL )
  267. asi = default_asi;
  268. return asi;
  269. }
  270. /*
  271. * A version of attr_syntax_get_by_name() that allows you to bypass using
  272. * a lock around the global name hashtable.
  273. *
  274. * Note: once the caller is finished using it, the structure must be
  275. * returned by calling attr_syntax_return_locking_optional() with the
  276. * same use_lock parameter.
  277. */
  278. struct asyntaxinfo *
  279. attr_syntax_get_by_name_locking_optional(const char *name, PRBool use_lock)
  280. {
  281. struct asyntaxinfo *asi = 0;
  282. if (name2asi)
  283. {
  284. if ( use_lock ) {
  285. AS_LOCK_READ(name2asi_lock);
  286. }
  287. asi = (struct asyntaxinfo *)PL_HashTableLookup_const(name2asi, name);
  288. if ( NULL != asi ) {
  289. PR_AtomicIncrement( &asi->asi_refcnt );
  290. }
  291. if ( use_lock ) {
  292. AS_UNLOCK_READ(name2asi_lock);
  293. }
  294. }
  295. if (!asi) /* given name may be an OID */
  296. asi = attr_syntax_get_by_oid_locking_optional(name, use_lock);
  297. return asi;
  298. }
  299. /*
  300. * Give up a reference to an asi.
  301. * If the asi has been marked for delete, free it. This would be a bit
  302. * easier if we could upgrade a read lock to a write one... but NSPR does
  303. * not support that paradigm.
  304. */
  305. void
  306. attr_syntax_return( struct asyntaxinfo *asi )
  307. {
  308. attr_syntax_return_locking_optional( asi, PR_TRUE );
  309. }
  310. void
  311. attr_syntax_return_locking_optional(struct asyntaxinfo *asi, PRBool use_lock)
  312. {
  313. int locked = 0;
  314. if(use_lock) {
  315. AS_LOCK_READ(name2asi_lock);
  316. locked = 1;
  317. }
  318. if ( NULL != asi ) {
  319. PRBool delete_it = PR_FALSE;
  320. if ( 0 == PR_AtomicDecrement( &asi->asi_refcnt )) {
  321. delete_it = asi->asi_marked_for_delete;
  322. }
  323. if (delete_it) {
  324. if ( asi->asi_marked_for_delete ) { /* one final check */
  325. if(use_lock) {
  326. AS_UNLOCK_READ(name2asi_lock);
  327. AS_LOCK_WRITE(name2asi_lock);
  328. }
  329. /* ref count is 0 and it's flagged for
  330. * deletion, so it's safe to free now */
  331. attr_syntax_free(asi);
  332. if(use_lock) {
  333. AS_UNLOCK_WRITE(name2asi_lock);
  334. locked = 0;
  335. }
  336. }
  337. }
  338. }
  339. if(locked) {
  340. AS_UNLOCK_READ(name2asi_lock);
  341. }
  342. }
  343. /*
  344. * Add the syntax info to the look-up-by-name table. The asi_name and
  345. * elements of the asi_aliasses field of the syntax info are the keys.
  346. * These need not be normalized since the look up table is case insensitive.
  347. * The lock parameter is used by the initialization code. Normally, we want
  348. * to acquire a write lock before we modify the table, but during
  349. * initialization, we are running in single threaded mode, so we don't have
  350. * to worry about resource contention.
  351. */
  352. static void
  353. attr_syntax_add_by_name(struct asyntaxinfo *a, int lock)
  354. {
  355. if (0 != attr_syntax_init()) return;
  356. if (lock) {
  357. AS_LOCK_WRITE(name2asi_lock);
  358. }
  359. PL_HashTableAdd(name2asi, a->asi_name, a);
  360. if ( a->asi_aliases != NULL ) {
  361. int i;
  362. for ( i = 0; a->asi_aliases[i] != NULL; ++i ) {
  363. PL_HashTableAdd(name2asi, a->asi_aliases[i], a);
  364. }
  365. }
  366. if (lock) {
  367. AS_UNLOCK_WRITE(name2asi_lock);
  368. }
  369. }
  370. /*
  371. * Delete the attribute syntax and all entries corresponding to aliases
  372. * and oids.
  373. */
  374. void
  375. attr_syntax_delete( struct asyntaxinfo *asi )
  376. {
  377. PR_ASSERT( asi );
  378. if (oid2asi && name2asi) {
  379. AS_LOCK_WRITE(oid2asi_lock);
  380. AS_LOCK_WRITE(name2asi_lock);
  381. attr_syntax_delete_no_lock( asi, PR_TRUE );
  382. AS_UNLOCK_WRITE(name2asi_lock);
  383. AS_UNLOCK_WRITE(oid2asi_lock);
  384. }
  385. }
  386. /*
  387. * Dispose of a node. The caller is responsible for locking. See
  388. * attr_syntax_delete() for an example.
  389. */
  390. static void
  391. attr_syntax_delete_no_lock( struct asyntaxinfo *asi,
  392. PRBool remove_from_oidtable )
  393. {
  394. int i;
  395. if (oid2asi && remove_from_oidtable ) {
  396. PL_HashTableRemove(oid2asi, asi->asi_oid);
  397. }
  398. if(name2asi) {
  399. PL_HashTableRemove(name2asi, asi->asi_name);
  400. if ( asi->asi_aliases != NULL ) {
  401. for ( i = 0; asi->asi_aliases[i] != NULL; ++i ) {
  402. PL_HashTableRemove(name2asi, asi->asi_aliases[i]);
  403. }
  404. }
  405. if ( asi->asi_refcnt > 0 ) {
  406. asi->asi_marked_for_delete = PR_TRUE;
  407. } else {
  408. /* This is ok, but the correct thing is to call delete first,
  409. * then to call return. The last return will then take care of
  410. * the free. The only way this free would happen here is if
  411. * you return the syntax before calling delete. */
  412. attr_syntax_free(asi);
  413. }
  414. }
  415. }
  416. /*
  417. * Look up the attribute type in the syntaxes and return a copy of the
  418. * normalised attribute type. If it's not there then return a normalised
  419. * copy of what the caller gave us.
  420. *
  421. * Warning: The caller must free the returned string.
  422. */
  423. char *
  424. slapi_attr_syntax_normalize( const char *s )
  425. {
  426. struct asyntaxinfo *asi = NULL;
  427. char *r = NULL;
  428. if((asi=attr_syntax_get_by_name(s)) != NULL ) {
  429. r = slapi_ch_strdup(asi->asi_name);
  430. attr_syntax_return( asi );
  431. }
  432. if ( NULL == asi ) {
  433. slapi_ch_free_string( &r );
  434. r = attr_syntax_normalize_no_lookup( s );
  435. }
  436. return r;
  437. }
  438. /*
  439. * flags:
  440. * 0 -- same as slapi_attr_syntax_normalize
  441. * ATTR_SYNTAX_NORM_ORIG_ATTR -- In addition to slapi_attr_syntax_normalize,
  442. * a space and following characters are removed
  443. * from the given string 's'.
  444. */
  445. char *
  446. slapi_attr_syntax_normalize_ext( char *s, int flags )
  447. {
  448. struct asyntaxinfo *asi = NULL;
  449. char *r = NULL;
  450. if((asi=attr_syntax_get_by_name(s)) != NULL ) {
  451. r = slapi_ch_strdup(asi->asi_name);
  452. attr_syntax_return( asi );
  453. }
  454. if ( NULL == asi ) {
  455. slapi_ch_free_string( &r );
  456. r = attr_syntax_normalize_no_lookup_ext( s, flags );
  457. }
  458. return r;
  459. }
  460. /*
  461. * attr_syntax_exists: return 1 if attr_name exists, 0 otherwise
  462. *
  463. */
  464. int
  465. attr_syntax_exists(const char *attr_name)
  466. {
  467. struct asyntaxinfo *asi;
  468. asi = attr_syntax_get_by_name(attr_name);
  469. attr_syntax_return( asi );
  470. if ( asi != NULL )
  471. {
  472. return 1;
  473. }
  474. return 0;
  475. }
  476. static void default_dirstring_normalize_int(char *s, int trim_spaces);
  477. static
  478. int default_dirstring_filter_ava( struct berval *bvfilter, Slapi_Value **bvals,int ftype, Slapi_Value **retVal )
  479. {
  480. return(0);
  481. }
  482. static
  483. int default_dirstring_values2keys( Slapi_PBlock *pb, Slapi_Value **bvals,Slapi_Value ***ivals, int ftype )
  484. {
  485. int numbvals = 0;
  486. Slapi_Value **nbvals, **nbvlp;
  487. Slapi_Value **bvlp;
  488. char *c;
  489. if (NULL == ivals) {
  490. return 1;
  491. }
  492. *ivals = NULL;
  493. if (NULL == bvals) {
  494. return 1;
  495. }
  496. switch ( ftype ) {
  497. case LDAP_FILTER_EQUALITY:
  498. /* allocate a new array for the normalized values */
  499. for ( bvlp = bvals; bvlp && *bvlp; bvlp++ ) {
  500. numbvals++;
  501. }
  502. nbvals = (Slapi_Value **) slapi_ch_calloc( (numbvals + 1), sizeof(Slapi_Value *));
  503. for ( bvlp = bvals, nbvlp = nbvals; bvlp && *bvlp; bvlp++, nbvlp++ ) {
  504. c = slapi_ch_strdup(slapi_value_get_string(*bvlp));
  505. default_dirstring_normalize_int( c, 1 );
  506. *nbvlp = slapi_value_new_string_passin(c);
  507. c = NULL;
  508. }
  509. *ivals = nbvals;
  510. break;
  511. case LDAP_FILTER_APPROX:
  512. case LDAP_FILTER_SUBSTRINGS:
  513. default:
  514. /* default plugin only handles equality so far */
  515. LDAPDebug( LDAP_DEBUG_ANY,
  516. "default_dirstring_values2keys: unsupported ftype 0x%x\n",
  517. ftype, 0, 0 );
  518. break;
  519. }
  520. return(0);
  521. }
  522. static
  523. int default_dirstring_assertion2keys_ava(Slapi_PBlock *pb,Slapi_Value *val,Slapi_Value ***ivals,int ftype )
  524. {
  525. return(0);
  526. }
  527. static
  528. int default_dirstring_cmp(struct berval *v1,struct berval *v2, int normalize)
  529. {
  530. return(0);
  531. }
  532. static
  533. void default_dirstring_normalize(Slapi_PBlock *pb, char *s, int trim_spaces, char **alt)
  534. {
  535. default_dirstring_normalize_int(s, trim_spaces);
  536. }
  537. static
  538. void default_dirstring_normalize_int(char *s, int trim_spaces)
  539. {
  540. char *head = s;
  541. char *d;
  542. int prevspace, curspace;
  543. if (NULL == s) {
  544. return;
  545. }
  546. d = s;
  547. if (trim_spaces) {
  548. /* strip leading blanks */
  549. while (ldap_utf8isspace(s)) {
  550. LDAP_UTF8INC(s);
  551. }
  552. }
  553. /* handle value of all spaces - turn into single space */
  554. if ( *s == '\0' && s != d ) {
  555. *d++ = ' ';
  556. *d = '\0';
  557. return;
  558. }
  559. prevspace = 0;
  560. while ( *s ) {
  561. int ssz, dsz;
  562. curspace = ldap_utf8isspace(s);
  563. /* compress multiple blanks */
  564. if ( prevspace && curspace ) {
  565. LDAP_UTF8INC(s);
  566. continue;
  567. }
  568. prevspace = curspace;
  569. slapi_utf8ToLower((unsigned char*)s, (unsigned char *)d, &ssz, &dsz);
  570. s += ssz;
  571. d += dsz;
  572. }
  573. *d = '\0';
  574. /* strip trailing blanks */
  575. if (prevspace && trim_spaces) {
  576. char *nd;
  577. nd = ldap_utf8prev(d);
  578. while (nd && nd >= head && ldap_utf8isspace(nd)) {
  579. d = nd;
  580. nd = ldap_utf8prev(d);
  581. *d = '\0';
  582. }
  583. }
  584. }
  585. static struct slapdplugin *
  586. attr_syntax_default_plugin ( const char *nameoroid )
  587. {
  588. struct slapdplugin *pi = NULL;
  589. /*
  590. * create a new plugin structure and
  591. * set the plugin function pointers.
  592. */
  593. pi = (struct slapdplugin *)slapi_ch_calloc(1, sizeof(struct slapdplugin));
  594. pi->plg_dn = slapi_ch_strdup("default plugin for directory string syntax");
  595. pi->plg_closed = 0;
  596. pi->plg_syntax_oid = slapi_ch_strdup(nameoroid);
  597. pi->plg_syntax_filter_ava = (IFP) default_dirstring_filter_ava;
  598. pi->plg_syntax_values2keys = (IFP) default_dirstring_values2keys;
  599. pi->plg_syntax_assertion2keys_ava = (IFP) default_dirstring_assertion2keys_ava;
  600. pi->plg_syntax_compare = (IFP) default_dirstring_cmp;
  601. pi->plg_syntax_normalize = (VFPV) default_dirstring_normalize;
  602. return (pi);
  603. }
  604. /* check syntax */
  605. static void *
  606. attr_syntax_get_plugin_by_name_with_default( const char *type )
  607. {
  608. struct asyntaxinfo *asi;
  609. void *plugin = NULL;
  610. /*
  611. * first we look for this attribute type explictly
  612. */
  613. if ( (asi = attr_syntax_get_by_name(type)) == NULL ) {
  614. /*
  615. * no syntax for this type... return Octet String
  616. * syntax. we accomplish this by looking up a well known
  617. * attribute type that has that syntax.
  618. */
  619. asi = attr_syntax_get_by_name(ATTR_WITH_OCTETSTRING_SYNTAX);
  620. if (asi == NULL)
  621. asi = default_asi;
  622. }
  623. if ( NULL != asi ) {
  624. plugin = asi->asi_plugin;
  625. attr_syntax_return( asi );
  626. }
  627. return( plugin );
  628. }
  629. static struct asyntaxinfo *
  630. attr_syntax_dup( struct asyntaxinfo *a )
  631. {
  632. struct asyntaxinfo *newas = attr_syntax_new();
  633. newas->asi_aliases = cool_charray_dup( a->asi_aliases );
  634. newas->asi_name = slapi_ch_strdup( a->asi_name );
  635. newas->asi_desc = slapi_ch_strdup( a->asi_desc );
  636. newas->asi_superior = slapi_ch_strdup( a->asi_superior );
  637. newas->asi_mr_equality = slapi_ch_strdup( a->asi_mr_equality );
  638. newas->asi_mr_ordering = slapi_ch_strdup( a->asi_mr_ordering );
  639. newas->asi_mr_substring = slapi_ch_strdup( a->asi_mr_substring );
  640. newas->asi_extensions = schema_copy_extensions( a->asi_extensions );
  641. newas->asi_plugin = a->asi_plugin;
  642. newas->asi_flags = a->asi_flags;
  643. newas->asi_oid = slapi_ch_strdup( a->asi_oid);
  644. newas->asi_syntaxlength = a->asi_syntaxlength;
  645. newas->asi_mr_eq_plugin = a->asi_mr_eq_plugin;
  646. newas->asi_mr_ord_plugin = a->asi_mr_ord_plugin;
  647. newas->asi_mr_sub_plugin = a->asi_mr_sub_plugin;
  648. return( newas );
  649. }
  650. /*
  651. * Add a new attribute type to the schema.
  652. *
  653. * Returns an LDAP error code (LDAP_SUCCESS if all goes well).
  654. */
  655. int
  656. attr_syntax_add( struct asyntaxinfo *asip )
  657. {
  658. int i, rc = LDAP_SUCCESS;
  659. int nolock = asip->asi_flags & SLAPI_ATTR_FLAG_NOLOCKING;
  660. struct asyntaxinfo *oldas_from_oid = NULL, *oldas_from_name = NULL;
  661. /* attr names may have subtypes in them, and we may not want this
  662. if strip_subtypes is true, the ; and anything after it in the
  663. attr name or alias will be stripped */
  664. /*int strip_subtypes = 1;*/
  665. /* make sure the oid is unique */
  666. if ( NULL != ( oldas_from_oid = attr_syntax_get_by_oid_locking_optional(
  667. asip->asi_oid, !nolock))) {
  668. if ( 0 == (asip->asi_flags & SLAPI_ATTR_FLAG_OVERRIDE)) {
  669. /* failure - OID is in use; no override flag */
  670. rc = LDAP_TYPE_OR_VALUE_EXISTS;
  671. goto cleanup_and_return;
  672. }
  673. }
  674. /*
  675. * Make sure the primary name is unique OR, if override is allowed, that
  676. * the primary name and OID point to the same schema definition.
  677. */
  678. if ( NULL != ( oldas_from_name = attr_syntax_get_by_name_locking_optional(
  679. asip->asi_name, !nolock))) {
  680. if ( 0 == (asip->asi_flags & SLAPI_ATTR_FLAG_OVERRIDE)
  681. || ( oldas_from_oid != oldas_from_name )) {
  682. /* failure; no override flag OR OID and name don't match */
  683. rc = LDAP_TYPE_OR_VALUE_EXISTS;
  684. goto cleanup_and_return;
  685. }
  686. /* Flag for deletion. We are going to override this attr */
  687. attr_syntax_delete(oldas_from_name);
  688. } else if ( NULL != oldas_from_oid ) {
  689. /* failure - OID is in use but name does not exist */
  690. rc = LDAP_TYPE_OR_VALUE_EXISTS;
  691. goto cleanup_and_return;
  692. }
  693. if ( NULL != asip->asi_aliases ) {
  694. /* make sure the aliases are unique */
  695. for (i = 0; asip->asi_aliases[i] != NULL; ++i) {
  696. struct asyntaxinfo *tmpasi;
  697. if ( NULL != ( tmpasi =
  698. attr_syntax_get_by_name_locking_optional(
  699. asip->asi_aliases[i], !nolock))) {
  700. if (asip->asi_flags & SLAPI_ATTR_FLAG_OVERRIDE) {
  701. /* Flag for tmpasi for deletion. It will be free'd
  702. * when attr_syntax_return is called. */
  703. attr_syntax_delete(tmpasi);
  704. } else {
  705. /* failure - one of the aliases is already in use */
  706. rc = LDAP_TYPE_OR_VALUE_EXISTS;
  707. }
  708. attr_syntax_return_locking_optional( tmpasi, !nolock );
  709. if ( LDAP_SUCCESS != rc ) {
  710. goto cleanup_and_return;
  711. }
  712. }
  713. }
  714. }
  715. /* the no lock flag is not worth keeping around */
  716. asip->asi_flags &= ~SLAPI_ATTR_FLAG_NOLOCKING;
  717. /* ditto for the override one */
  718. asip->asi_flags &= ~SLAPI_ATTR_FLAG_OVERRIDE;
  719. attr_syntax_add_by_oid( asip->asi_oid, asip, !nolock);
  720. attr_syntax_add_by_name( asip, !nolock);
  721. cleanup_and_return:
  722. attr_syntax_return_locking_optional( oldas_from_oid, !nolock );
  723. attr_syntax_return_locking_optional( oldas_from_name, !nolock );
  724. return rc;
  725. }
  726. static int
  727. attr_syntax_create_default( const char *name, const char *oid,
  728. const char *syntax, unsigned long extraflags )
  729. {
  730. int rc = 0;
  731. char *names[2];
  732. unsigned long std_flags = SLAPI_ATTR_FLAG_STD_ATTR | SLAPI_ATTR_FLAG_OPATTR;
  733. names[0] = (char *)name;
  734. names[1] = NULL;
  735. if (default_asi)
  736. return (rc);
  737. rc = attr_syntax_create( oid, names,
  738. "internal server defined attribute type",
  739. NULL, /* superior */
  740. NULL, NULL, NULL, /* matching rules */
  741. NULL, syntax,
  742. SLAPI_SYNTAXLENGTH_NONE,
  743. std_flags | extraflags,
  744. &default_asi );
  745. if ( rc == 0 && default_asi->asi_plugin == 0)
  746. default_asi->asi_plugin = attr_syntax_default_plugin (syntax );
  747. return (rc);
  748. }
  749. /*
  750. * Returns an LDAP result code.
  751. */
  752. int
  753. attr_syntax_create(
  754. const char *attr_oid,
  755. char *const *attr_names,
  756. const char *attr_desc,
  757. const char *attr_superior,
  758. const char *mr_equality,
  759. const char *mr_ordering,
  760. const char *mr_substring,
  761. schemaext *extensions,
  762. const char *attr_syntax,
  763. int syntaxlength,
  764. unsigned long flags,
  765. struct asyntaxinfo **asip
  766. )
  767. {
  768. char *s;
  769. struct asyntaxinfo a;
  770. int rc = LDAP_SUCCESS;
  771. /* XXXmcs: had to cast away const in many places below */
  772. memset(&a, 0, sizeof(a));
  773. *asip = NULL;
  774. a.asi_name = slapi_ch_strdup(attr_names[0]);
  775. if ( NULL != attr_names[1] ) {
  776. a.asi_aliases = (char **)&attr_names[1]; /* all but the zero'th element */
  777. }
  778. a.asi_desc = (char*)attr_desc;
  779. a.asi_oid = (char*)attr_oid;
  780. a.asi_superior = (char*)attr_superior;
  781. a.asi_mr_equality = (char*)mr_equality;
  782. a.asi_mr_ordering = (char*)mr_ordering;
  783. a.asi_mr_substring = (char*)mr_substring;
  784. a.asi_extensions = extensions;
  785. a.asi_plugin = plugin_syntax_find( attr_syntax );
  786. a.asi_syntaxlength = syntaxlength;
  787. /* ideally, we would report an error and fail to start if there was some problem
  788. with the matching rule - but since this functionality is new, and we might
  789. cause havoc if lots of servers failed to start because of bogus schema, we
  790. just report an error here - at some point in the future, we should actually
  791. report an error and exit, or allow the user to control the behavior - for
  792. now, just log an error, and address each case
  793. */
  794. if (mr_equality && !slapi_matchingrule_is_compat(mr_equality, attr_syntax)) {
  795. slapi_log_error(SLAPI_LOG_FATAL, "attr_syntax_create",
  796. "Error: the EQUALITY matching rule [%s] is not compatible "
  797. "with the syntax [%s] for the attribute [%s]\n",
  798. mr_equality, attr_syntax, attr_names[0]);
  799. #ifdef ENFORCE_MR_SYNTAX_COMPAT
  800. rc = LDAP_INAPPROPRIATE_MATCHING;
  801. goto done;
  802. #endif /* ENFORCE_MR_SYNTAX_COMPAT */
  803. }
  804. a.asi_mr_eq_plugin = plugin_mr_find( mr_equality );
  805. if (mr_ordering && !slapi_matchingrule_is_compat(mr_ordering, attr_syntax)) {
  806. slapi_log_error(SLAPI_LOG_FATAL, "attr_syntax_create",
  807. "Error: the ORDERING matching rule [%s] is not compatible "
  808. "with the syntax [%s] for the attribute [%s]\n",
  809. mr_ordering, attr_syntax, attr_names[0]);
  810. #ifdef ENFORCE_MR_SYNTAX_COMPAT
  811. rc = LDAP_INAPPROPRIATE_MATCHING;
  812. goto done;
  813. #endif /* ENFORCE_MR_SYNTAX_COMPAT */
  814. }
  815. a.asi_mr_ord_plugin = plugin_mr_find( mr_ordering );
  816. if (mr_substring && !slapi_matchingrule_is_compat(mr_substring, attr_syntax)) {
  817. slapi_log_error(SLAPI_LOG_FATAL, "attr_syntax_create",
  818. "Error: the SUBSTR matching rule [%s] is not compatible "
  819. "with the syntax [%s] for the attribute [%s]\n",
  820. mr_substring, attr_syntax, attr_names[0]);
  821. #ifdef ENFORCE_MR_SYNTAX_COMPAT
  822. rc = LDAP_INAPPROPRIATE_MATCHING;
  823. goto done;
  824. #endif /* ENFORCE_MR_SYNTAX_COMPAT */
  825. }
  826. a.asi_mr_sub_plugin = plugin_mr_find( mr_substring );
  827. a.asi_flags = flags;
  828. /*
  829. * If the 'return exact case' option is on (the default), we store the
  830. * first name (the canonical one) unchanged so that attribute names are
  831. * returned exactly as they appear in the schema configuration files.
  832. * But if 'return exact case' has been turned off, we convert the name
  833. * to lowercase. In Netscape Directory Server 4.x and earlier versions,
  834. * the default was to convert to lowercase.
  835. */
  836. if (!config_get_return_exact_case()) {
  837. for (s = a.asi_name; *s; ++s) {
  838. *s = TOLOWER(*s);
  839. }
  840. }
  841. *asip = attr_syntax_dup(&a);
  842. #ifdef ENFORCE_MR_SYNTAX_COMPAT
  843. done:
  844. #endif /* ENFORCE_MR_SYNTAX_COMPAT */
  845. slapi_ch_free((void **)&a.asi_name);
  846. return rc;
  847. }
  848. /*
  849. * slapi_attr_type2plugin - return the plugin handling the attribute type
  850. * if type is unknown, we return the caseIgnoreString plugin used by the
  851. * objectClass attribute type.
  852. */
  853. int
  854. slapi_attr_type2plugin( const char *type, void **pi )
  855. {
  856. char buf[SLAPD_TYPICAL_ATTRIBUTE_NAME_MAX_LENGTH];
  857. char *tmp, *basetype;
  858. int rc;
  859. basetype = buf;
  860. if ( (tmp = slapi_attr_basetype( type, buf, sizeof(buf) )) != NULL ) {
  861. basetype = tmp;
  862. }
  863. rc = -1;
  864. *pi = attr_syntax_get_plugin_by_name_with_default( basetype );
  865. if ( NULL != *pi ) {
  866. rc = 0;
  867. }
  868. slapi_ch_free_string(&tmp);
  869. return( rc );
  870. }
  871. /* deprecated -- not MT safe (pointer into asi is returned!) */
  872. int
  873. slapi_attr_get_oid( const Slapi_Attr *a, char **oid )
  874. {
  875. struct asyntaxinfo *asi = attr_syntax_get_by_name(a->a_type);
  876. if (asi) {
  877. *oid = asi->asi_oid;
  878. attr_syntax_return(asi);
  879. return( 0 );
  880. } else {
  881. *oid = NULL;
  882. return( -1 );
  883. }
  884. }
  885. /* The caller must dispose of oid by calling slapi_ch_free(). */
  886. int
  887. slapi_attr_get_oid_copy( const Slapi_Attr *a, char **oidp )
  888. {
  889. struct asyntaxinfo *asi = attr_syntax_get_by_name(a->a_type);
  890. if (asi) {
  891. *oidp = slapi_ch_strdup( asi->asi_oid );
  892. attr_syntax_return(asi);
  893. return( 0 );
  894. } else {
  895. *oidp = NULL;
  896. return( -1 );
  897. }
  898. }
  899. /* Returns the oid of the syntax of the Slapi_Attr that's passed in.
  900. * The caller must dispose of oid by calling slapi_ch_free_string(). */
  901. int
  902. slapi_attr_get_syntax_oid_copy( const Slapi_Attr *a, char **oidp )
  903. {
  904. const char *oid;
  905. if (a && ((oid = attr_get_syntax_oid(a)))) {
  906. *oidp = slapi_ch_strdup(oid);
  907. return( 0 );
  908. } else {
  909. *oidp = NULL;
  910. return( -1 );
  911. }
  912. }
  913. int
  914. slapi_attr_is_dn_syntax_attr(Slapi_Attr *attr)
  915. {
  916. const char *syntaxoid = NULL;
  917. int dn_syntax = 0; /* not DN, by default */
  918. if (attr && attr->a_flags & SLAPI_ATTR_FLAG_SYNTAX_IS_DN)
  919. /* it was checked before */
  920. return(1);
  921. if (attr && attr->a_plugin == NULL) {
  922. slapi_attr_init_syntax (attr);
  923. }
  924. if (attr && attr->a_plugin) { /* If not set, there is no way to get the info */
  925. if ((syntaxoid = attr_get_syntax_oid(attr))) {
  926. dn_syntax = ((0 == strcmp(syntaxoid, NAMEANDOPTIONALUID_SYNTAX_OID))
  927. || (0 == strcmp(syntaxoid, DN_SYNTAX_OID)));
  928. }
  929. if (dn_syntax)
  930. attr->a_flags |= SLAPI_ATTR_FLAG_SYNTAX_IS_DN;
  931. }
  932. return dn_syntax;
  933. }
  934. int
  935. slapi_attr_is_dn_syntax_type(char *type)
  936. {
  937. const char *syntaxoid = NULL;
  938. int dn_syntax = 0; /* not DN, by default */
  939. struct asyntaxinfo * asi;
  940. asi = attr_syntax_get_by_name(type);
  941. if (asi && asi->asi_plugin) { /* If not set, there is no way to get the info */
  942. if ((syntaxoid = asi->asi_plugin->plg_syntax_oid)) {
  943. dn_syntax = ((0 == strcmp(syntaxoid, NAMEANDOPTIONALUID_SYNTAX_OID))
  944. || (0 == strcmp(syntaxoid, DN_SYNTAX_OID)));
  945. }
  946. }
  947. return dn_syntax;
  948. }
  949. #ifdef ATTR_LDAP_DEBUG
  950. PRIntn
  951. attr_syntax_printnode(PLHashEntry *he, PRIntn i, void *arg)
  952. {
  953. char *alias = (char *)he->key;
  954. struct asyntaxinfo *a = (struct asyntaxinfo *)he->value;
  955. schemaext *ext = a->asi_extensions;
  956. printf( " name: %s\n", a->asi_name );
  957. printf( "\t flags : 0x%x\n", a->asi_flags );
  958. printf( "\t alias : %s\n", alias );
  959. printf( "\t desc : %s\n", a->asi_desc );
  960. printf( "\t oid : %s\n", a->asi_oid );
  961. printf( "\t superior : %s\n", a->asi_superior );
  962. printf( "\t mr_equality : %s\n", a->asi_mr_equality );
  963. printf( "\t mr_ordering : %s\n", a->asi_mr_ordering );
  964. printf( "\t mr_substring: %s\n", a->asi_mr_substring );
  965. while( ext ) {
  966. for ( i = 0; ext->values && ext->values[i]; i++ ) {
  967. printf( "\t %s : %s\n", ext->term, ext->values[i]);
  968. }
  969. ext = ext->next;
  970. }
  971. printf( "\tplugin: %p\n", a->asi_plugin );
  972. printf( "--------------\n" );
  973. return HT_ENUMERATE_NEXT;
  974. }
  975. void
  976. attr_syntax_print()
  977. {
  978. printf( "*** attr_syntax_print ***\n" );
  979. PL_HashTableEnumerateEntries(name2asi, attr_syntax_printnode, 0);
  980. }
  981. #endif
  982. /* lowercase the attr name and chop trailing spaces */
  983. /* note that s may contain options also, e.g., userCertificate;binary */
  984. char *
  985. attr_syntax_normalize_no_lookup_ext( char *s, int flags )
  986. {
  987. char *save, *tmps;
  988. tmps = slapi_ch_strdup(s);
  989. for ( save = tmps; (*tmps != '\0') && (*tmps != ' '); tmps++ )
  990. {
  991. *tmps = TOLOWER( *tmps );
  992. }
  993. *tmps = '\0';
  994. if (flags & ATTR_SYNTAX_NORM_ORIG_ATTR) {
  995. /* Chop trailing spaces + following strings */
  996. *(s + (tmps - save)) = '\0';
  997. }
  998. return save;
  999. }
  1000. char *
  1001. attr_syntax_normalize_no_lookup( const char *s )
  1002. {
  1003. char *save, *tmps;
  1004. tmps = slapi_ch_strdup(s);
  1005. for ( save = tmps; (*tmps != '\0') && (*tmps != ' '); tmps++ )
  1006. {
  1007. *tmps = TOLOWER( *tmps );
  1008. }
  1009. *tmps = '\0';
  1010. return save;
  1011. }
  1012. struct enum_arg_wrapper {
  1013. AttrEnumFunc aef;
  1014. void *arg;
  1015. };
  1016. PRIntn
  1017. attr_syntax_enumerate_internal(PLHashEntry *he, PRIntn i, void *arg)
  1018. {
  1019. struct enum_arg_wrapper *eaw = (struct enum_arg_wrapper *)arg;
  1020. int rc;
  1021. rc = (eaw->aef)((struct asyntaxinfo *)he->value, eaw->arg);
  1022. if ( ATTR_SYNTAX_ENUM_STOP == rc ) {
  1023. rc = HT_ENUMERATE_STOP;
  1024. } else if ( ATTR_SYNTAX_ENUM_REMOVE == rc ) {
  1025. rc = HT_ENUMERATE_REMOVE;
  1026. } else {
  1027. rc = HT_ENUMERATE_NEXT;
  1028. }
  1029. return rc;
  1030. }
  1031. static void
  1032. attr_syntax_enumerate_attrs_ext( PLHashTable *ht,
  1033. AttrEnumFunc aef, void *arg )
  1034. {
  1035. struct enum_arg_wrapper eaw;
  1036. eaw.aef = aef;
  1037. eaw.arg = arg;
  1038. if (!ht)
  1039. return;
  1040. PL_HashTableEnumerateEntries(ht, attr_syntax_enumerate_internal, &eaw);
  1041. }
  1042. void
  1043. attr_syntax_enumerate_attrs(AttrEnumFunc aef, void *arg, PRBool writelock )
  1044. {
  1045. if (!oid2asi)
  1046. return;
  1047. if ( writelock ) {
  1048. AS_LOCK_WRITE(oid2asi_lock);
  1049. AS_LOCK_WRITE(name2asi_lock);
  1050. } else {
  1051. AS_LOCK_READ(oid2asi_lock);
  1052. AS_LOCK_READ(name2asi_lock);
  1053. }
  1054. attr_syntax_enumerate_attrs_ext(oid2asi, aef, arg);
  1055. if ( writelock ) {
  1056. AS_UNLOCK_WRITE(name2asi_lock);
  1057. AS_UNLOCK_WRITE(oid2asi_lock);
  1058. } else {
  1059. AS_UNLOCK_READ(name2asi_lock);
  1060. AS_UNLOCK_READ(oid2asi_lock);
  1061. }
  1062. }
  1063. struct attr_syntax_enum_flaginfo {
  1064. unsigned long asef_flag;
  1065. };
  1066. static int
  1067. attr_syntax_clear_flag_callback(struct asyntaxinfo *asip, void *arg)
  1068. {
  1069. struct attr_syntax_enum_flaginfo *fi;
  1070. PR_ASSERT( asip != NULL );
  1071. fi = (struct attr_syntax_enum_flaginfo *)arg;
  1072. PR_ASSERT( fi != NULL );
  1073. asip->asi_flags &= ~(fi->asef_flag);
  1074. return ATTR_SYNTAX_ENUM_NEXT;
  1075. }
  1076. static int
  1077. attr_syntax_delete_if_not_flagged(struct asyntaxinfo *asip, void *arg)
  1078. {
  1079. struct attr_syntax_enum_flaginfo *fi;
  1080. PR_ASSERT( asip != NULL );
  1081. fi = (struct attr_syntax_enum_flaginfo *)arg;
  1082. PR_ASSERT( fi != NULL );
  1083. if ( 0 == ( asip->asi_flags & fi->asef_flag )) {
  1084. attr_syntax_delete_no_lock( asip, PR_FALSE );
  1085. return ATTR_SYNTAX_ENUM_REMOVE;
  1086. } else {
  1087. return ATTR_SYNTAX_ENUM_NEXT;
  1088. }
  1089. }
  1090. static int
  1091. attr_syntax_force_to_delete(struct asyntaxinfo *asip, void *arg)
  1092. {
  1093. struct attr_syntax_enum_flaginfo *fi;
  1094. PR_ASSERT( asip != NULL );
  1095. fi = (struct attr_syntax_enum_flaginfo *)arg;
  1096. PR_ASSERT( fi != NULL );
  1097. attr_syntax_delete_no_lock( asip, PR_FALSE );
  1098. return ATTR_SYNTAX_ENUM_REMOVE;
  1099. }
  1100. /*
  1101. * Clear 'flag' within all attribute definitions.
  1102. */
  1103. void
  1104. attr_syntax_all_clear_flag( unsigned long flag )
  1105. {
  1106. struct attr_syntax_enum_flaginfo fi;
  1107. memset( &fi, 0, sizeof(fi));
  1108. fi.asef_flag = flag;
  1109. attr_syntax_enumerate_attrs( attr_syntax_clear_flag_callback,
  1110. (void *)&fi, PR_TRUE );
  1111. }
  1112. /*
  1113. * Delete all attribute definitions that do not contain any bits of 'flag'
  1114. * in their flags.
  1115. */
  1116. void
  1117. attr_syntax_delete_all_not_flagged( unsigned long flag )
  1118. {
  1119. struct attr_syntax_enum_flaginfo fi;
  1120. memset( &fi, 0, sizeof(fi));
  1121. fi.asef_flag = flag;
  1122. attr_syntax_enumerate_attrs( attr_syntax_delete_if_not_flagged,
  1123. (void *)&fi, PR_TRUE );
  1124. }
  1125. /*
  1126. * Delete all attribute definitions
  1127. */
  1128. void
  1129. attr_syntax_delete_all()
  1130. {
  1131. struct attr_syntax_enum_flaginfo fi;
  1132. memset( &fi, 0, sizeof(fi));
  1133. attr_syntax_enumerate_attrs( attr_syntax_force_to_delete,
  1134. (void *)&fi, PR_TRUE );
  1135. }
  1136. /*
  1137. * Delete all attribute definitions without attr_syntax lock.
  1138. * The caller is responsible for the lock.
  1139. */
  1140. void
  1141. attr_syntax_delete_all_for_schemareload(unsigned long flag)
  1142. {
  1143. struct attr_syntax_enum_flaginfo fi;
  1144. memset(&fi, 0, sizeof(fi));
  1145. fi.asef_flag = flag;
  1146. attr_syntax_enumerate_attrs_ext(oid2asi, attr_syntax_delete_if_not_flagged,
  1147. (void *)&fi);
  1148. }
  1149. #define ATTR_DEFAULT_SYNTAX_OID "1.1"
  1150. #define ATTR_DEFAULT_SYNTAX "defaultdirstringsyntax"
  1151. static int
  1152. attr_syntax_init(void)
  1153. {
  1154. int schema_modify_enabled = config_get_schemamod();
  1155. if (!schema_modify_enabled) asi_locking = 0;
  1156. if (!oid2asi)
  1157. {
  1158. oid2asi = PL_NewHashTable(2047, hashNocaseString,
  1159. hashNocaseCompare,
  1160. PL_CompareValues, 0, 0);
  1161. if ( asi_locking && NULL == ( oid2asi_lock = slapi_new_rwlock())) {
  1162. if(oid2asi) PL_HashTableDestroy(oid2asi);
  1163. oid2asi = NULL;
  1164. slapi_log_error( SLAPI_LOG_FATAL, "attr_syntax_init",
  1165. "slapi_new_rwlock() for oid2asi lock failed\n" );
  1166. return 1;
  1167. }
  1168. }
  1169. if (!name2asi)
  1170. {
  1171. name2asi = PL_NewHashTable(2047, hashNocaseString,
  1172. hashNocaseCompare,
  1173. PL_CompareValues, 0, 0);
  1174. if ( asi_locking && NULL == ( name2asi_lock = slapi_new_rwlock())) {
  1175. if(name2asi) PL_HashTableDestroy(name2asi);
  1176. name2asi = NULL;
  1177. slapi_log_error( SLAPI_LOG_FATAL, "attr_syntax_init",
  1178. "slapi_new_rwlock() for oid2asi lock failed\n" );
  1179. return 1;
  1180. }
  1181. /* add a default syntax plugin as fallback, required during startup
  1182. */
  1183. attr_syntax_create_default( ATTR_DEFAULT_SYNTAX,
  1184. ATTR_DEFAULT_SYNTAX_OID,
  1185. DIRSTRING_SYNTAX_OID,
  1186. SLAPI_ATTR_FLAG_NOUSERMOD| SLAPI_ATTR_FLAG_NOEXPOSE);
  1187. }
  1188. return 0;
  1189. }
  1190. int
  1191. slapi_attr_syntax_exists(const char *attr_name)
  1192. {
  1193. return attr_syntax_exists(attr_name);
  1194. }
  1195. /*
  1196. * Keep the internally added schema in the hash table,
  1197. * which are re-added if the schema is reloaded.
  1198. */
  1199. static int
  1200. attr_syntax_internal_asi_add_ht(struct asyntaxinfo *asip)
  1201. {
  1202. if (!internalasi) {
  1203. internalasi = PL_NewHashTable(64, hashNocaseString,
  1204. hashNocaseCompare,
  1205. PL_CompareValues, 0, 0);
  1206. }
  1207. if (!internalasi) {
  1208. slapi_log_error(SLAPI_LOG_FATAL, "attr_syntax_internal_asi_add_ht",
  1209. "Failed to create HashTable.\n");
  1210. return 1;
  1211. }
  1212. if (!PL_HashTableLookup(internalasi, asip->asi_oid)) {
  1213. struct asyntaxinfo *asip_copy = attr_syntax_dup(asip);
  1214. if (!asip_copy) {
  1215. slapi_log_error(SLAPI_LOG_FATAL, "attr_syntax_internal_asi_add_ht",
  1216. "Failed to duplicate asyntaxinfo: %s.\n",
  1217. asip->asi_name);
  1218. return 1;
  1219. }
  1220. PL_HashTableAdd(internalasi, asip_copy->asi_oid, asip_copy);
  1221. }
  1222. return 0;
  1223. }
  1224. /*
  1225. * Add an attribute syntax using some default flags, etc.
  1226. * Returns an LDAP error code (LDAP_SUCCESS if all goes well)
  1227. */
  1228. int
  1229. slapi_add_internal_attr_syntax( const char *name, const char *oid,
  1230. const char *syntax, const char *mr_equality, unsigned long extraflags )
  1231. {
  1232. int rc = LDAP_SUCCESS;
  1233. struct asyntaxinfo *asip;
  1234. char *names[2];
  1235. unsigned long std_flags = SLAPI_ATTR_FLAG_STD_ATTR | SLAPI_ATTR_FLAG_OPATTR;
  1236. names[0] = (char *)name;
  1237. names[1] = NULL;
  1238. rc = attr_syntax_create( oid, names,
  1239. "internal server defined attribute type",
  1240. NULL, /* superior */
  1241. mr_equality, NULL, NULL, /* matching rules */
  1242. NULL, syntax,
  1243. SLAPI_SYNTAXLENGTH_NONE,
  1244. std_flags | extraflags,
  1245. &asip );
  1246. if ( rc == LDAP_SUCCESS ) {
  1247. rc = attr_syntax_add( asip );
  1248. if ( rc == LDAP_SUCCESS ) {
  1249. if (attr_syntax_internal_asi_add_ht(asip)) {
  1250. slapi_log_error(SLAPI_LOG_FATAL,
  1251. "slapi_add_internal_attr_syntax",
  1252. "Failed to stash internal asyntaxinfo: %s.\n",
  1253. asip->asi_name);
  1254. }
  1255. }
  1256. }
  1257. return rc;
  1258. }
  1259. /* Adding internal asyncinfo via slapi_reload_internal_attr_syntax */
  1260. static int
  1261. attr_syntax_internal_asi_add(struct asyntaxinfo *asip, void *arg)
  1262. {
  1263. struct asyntaxinfo *asip_copy;
  1264. int rc = 0;
  1265. if (!asip) {
  1266. return 1;
  1267. }
  1268. /* Copy is needed since when reloading the schema,
  1269. * existing syntax info is cleaned up. */
  1270. asip_copy = attr_syntax_dup(asip);
  1271. rc = attr_syntax_add(asip_copy);
  1272. if (LDAP_SUCCESS != rc) {
  1273. attr_syntax_free(asip_copy);
  1274. }
  1275. return rc;
  1276. }
  1277. /* Reload internal attribute syntax stashed in the internalasi hashtable. */
  1278. int
  1279. slapi_reload_internal_attr_syntax()
  1280. {
  1281. int rc = LDAP_SUCCESS;
  1282. if (!internalasi) {
  1283. slapi_log_error(SLAPI_LOG_TRACE, "attr_reload_internal_attr_syntax",
  1284. "No internal attribute syntax to reload.\n");
  1285. return rc;
  1286. }
  1287. attr_syntax_enumerate_attrs_ext(internalasi, attr_syntax_internal_asi_add, NULL);
  1288. return rc;
  1289. }