schema.c 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400
  1. /* $OpenLDAP$ */
  2. /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  3. *
  4. * Copyright 1998-2022 The OpenLDAP Foundation.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted only as authorized by the OpenLDAP
  9. * Public License.
  10. *
  11. * A copy of this license is available in the file LICENSE in the
  12. * top-level directory of the distribution or, alternatively, at
  13. * <http://www.OpenLDAP.org/license.html>.
  14. */
  15. /*
  16. * schema.c: parsing routines used by servers and clients to process
  17. * schema definitions
  18. */
  19. #include "portable.h"
  20. #include <stdio.h>
  21. #include <ac/stdlib.h>
  22. #include <ac/string.h>
  23. #include <ac/time.h>
  24. #include "ldap-int.h"
  25. #include <ldap_schema.h>
  26. static const char EndOfInput[] = "end of input";
  27. static const char *
  28. choose_name( char *names[], const char *fallback )
  29. {
  30. return (names != NULL && names[0] != NULL) ? names[0] : fallback;
  31. }
  32. LDAP_CONST char *
  33. ldap_syntax2name( LDAPSyntax * syn )
  34. {
  35. if (!syn) return NULL;
  36. return( syn->syn_oid );
  37. }
  38. LDAP_CONST char *
  39. ldap_matchingrule2name( LDAPMatchingRule * mr )
  40. {
  41. if (!mr) return NULL;
  42. return( choose_name( mr->mr_names, mr->mr_oid ) );
  43. }
  44. LDAP_CONST char *
  45. ldap_matchingruleuse2name( LDAPMatchingRuleUse * mru )
  46. {
  47. if (!mru) return NULL;
  48. return( choose_name( mru->mru_names, mru->mru_oid ) );
  49. }
  50. LDAP_CONST char *
  51. ldap_attributetype2name( LDAPAttributeType * at )
  52. {
  53. if (!at) return NULL;
  54. return( choose_name( at->at_names, at->at_oid ) );
  55. }
  56. LDAP_CONST char *
  57. ldap_objectclass2name( LDAPObjectClass * oc )
  58. {
  59. if (!oc) return NULL;
  60. return( choose_name( oc->oc_names, oc->oc_oid ) );
  61. }
  62. LDAP_CONST char *
  63. ldap_contentrule2name( LDAPContentRule * cr )
  64. {
  65. if (!cr) return NULL;
  66. return( choose_name( cr->cr_names, cr->cr_oid ) );
  67. }
  68. LDAP_CONST char *
  69. ldap_nameform2name( LDAPNameForm * nf )
  70. {
  71. if (!nf) return NULL;
  72. return( choose_name( nf->nf_names, nf->nf_oid ) );
  73. }
  74. LDAP_CONST char *
  75. ldap_structurerule2name( LDAPStructureRule * sr )
  76. {
  77. if (!sr) return NULL;
  78. return( choose_name( sr->sr_names, NULL ) );
  79. }
  80. /*
  81. * When pretty printing the entities we will be appending to a buffer.
  82. * Since checking for overflow, realloc'ing and checking if no error
  83. * is extremely boring, we will use a protection layer that will let
  84. * us blissfully ignore the error until the end. This layer is
  85. * implemented with the help of the next type.
  86. */
  87. typedef struct safe_string {
  88. char * val;
  89. ber_len_t size;
  90. ber_len_t pos;
  91. int at_whsp;
  92. } safe_string;
  93. static safe_string *
  94. new_safe_string(int size)
  95. {
  96. safe_string * ss;
  97. ss = LDAP_MALLOC(sizeof(safe_string));
  98. if ( !ss )
  99. return(NULL);
  100. ss->val = LDAP_MALLOC(size);
  101. if ( !ss->val ) {
  102. LDAP_FREE(ss);
  103. return(NULL);
  104. }
  105. ss->size = size;
  106. ss->pos = 0;
  107. ss->at_whsp = 0;
  108. return ss;
  109. }
  110. static void
  111. safe_string_free(safe_string * ss)
  112. {
  113. if ( !ss )
  114. return;
  115. LDAP_FREE(ss->val);
  116. LDAP_FREE(ss);
  117. }
  118. #if 0 /* unused */
  119. static char *
  120. safe_string_val(safe_string * ss)
  121. {
  122. ss->val[ss->pos] = '\0';
  123. return(ss->val);
  124. }
  125. #endif
  126. static char *
  127. safe_strdup(safe_string * ss)
  128. {
  129. char *ret = LDAP_MALLOC(ss->pos+1);
  130. if (!ret)
  131. return NULL;
  132. AC_MEMCPY(ret, ss->val, ss->pos);
  133. ret[ss->pos] = '\0';
  134. return ret;
  135. }
  136. static int
  137. append_to_safe_string(safe_string * ss, char * s)
  138. {
  139. int l = strlen(s);
  140. char * temp;
  141. /*
  142. * Some runaway process is trying to append to a string that
  143. * overflowed and we could not extend.
  144. */
  145. if ( !ss->val )
  146. return -1;
  147. /* We always make sure there is at least one position available */
  148. if ( ss->pos + l >= ss->size-1 ) {
  149. ss->size *= 2;
  150. if ( ss->pos + l >= ss->size-1 ) {
  151. ss->size = ss->pos + l + 1;
  152. }
  153. temp = LDAP_REALLOC(ss->val, ss->size);
  154. if ( !temp ) {
  155. /* Trouble, out of memory */
  156. LDAP_FREE(ss->val);
  157. return -1;
  158. }
  159. ss->val = temp;
  160. }
  161. strncpy(&ss->val[ss->pos], s, l);
  162. ss->pos += l;
  163. if ( ss->pos > 0 && LDAP_SPACE(ss->val[ss->pos-1]) )
  164. ss->at_whsp = 1;
  165. else
  166. ss->at_whsp = 0;
  167. return 0;
  168. }
  169. static int
  170. print_literal(safe_string *ss, char *s)
  171. {
  172. return(append_to_safe_string(ss,s));
  173. }
  174. static int
  175. print_whsp(safe_string *ss)
  176. {
  177. if ( ss->at_whsp )
  178. return(append_to_safe_string(ss,""));
  179. else
  180. return(append_to_safe_string(ss," "));
  181. }
  182. static int
  183. print_numericoid(safe_string *ss, char *s)
  184. {
  185. if ( s )
  186. return(append_to_safe_string(ss,s));
  187. else
  188. return(append_to_safe_string(ss,""));
  189. }
  190. /* This one is identical to print_qdescr */
  191. static int
  192. print_qdstring(safe_string *ss, char *s)
  193. {
  194. print_whsp(ss);
  195. print_literal(ss,"'");
  196. append_to_safe_string(ss,s);
  197. print_literal(ss,"'");
  198. return(print_whsp(ss));
  199. }
  200. static int
  201. print_qdescr(safe_string *ss, char *s)
  202. {
  203. print_whsp(ss);
  204. print_literal(ss,"'");
  205. append_to_safe_string(ss,s);
  206. print_literal(ss,"'");
  207. return(print_whsp(ss));
  208. }
  209. static int
  210. print_qdescrlist(safe_string *ss, char **sa)
  211. {
  212. char **sp;
  213. int ret = 0;
  214. for (sp=sa; *sp; sp++) {
  215. ret = print_qdescr(ss,*sp);
  216. }
  217. /* If the list was empty, we return zero that is potentially
  218. * incorrect, but since we will be still appending things, the
  219. * overflow will be detected later. Maybe FIX.
  220. */
  221. return(ret);
  222. }
  223. static int
  224. print_qdescrs(safe_string *ss, char **sa)
  225. {
  226. /* The only way to represent an empty list is as a qdescrlist
  227. * so, if the list is empty we treat it as a long list.
  228. * Really, this is what the syntax mandates. We should not
  229. * be here if the list was empty, but if it happens, a label
  230. * has already been output and we cannot undo it.
  231. */
  232. if ( !sa[0] || ( sa[0] && sa[1] ) ) {
  233. print_whsp(ss);
  234. print_literal(ss,"("/*)*/);
  235. print_qdescrlist(ss,sa);
  236. print_literal(ss,/*(*/")");
  237. return(print_whsp(ss));
  238. } else {
  239. return(print_qdescr(ss,*sa));
  240. }
  241. }
  242. static int
  243. print_woid(safe_string *ss, char *s)
  244. {
  245. print_whsp(ss);
  246. append_to_safe_string(ss,s);
  247. return print_whsp(ss);
  248. }
  249. static int
  250. print_oidlist(safe_string *ss, char **sa)
  251. {
  252. char **sp;
  253. for (sp=sa; *(sp+1); sp++) {
  254. print_woid(ss,*sp);
  255. print_literal(ss,"$");
  256. }
  257. return(print_woid(ss,*sp));
  258. }
  259. static int
  260. print_oids(safe_string *ss, char **sa)
  261. {
  262. if ( sa[0] && sa[1] ) {
  263. print_literal(ss,"("/*)*/);
  264. print_oidlist(ss,sa);
  265. print_whsp(ss);
  266. return(print_literal(ss,/*(*/")"));
  267. } else {
  268. return(print_woid(ss,*sa));
  269. }
  270. }
  271. static int
  272. print_noidlen(safe_string *ss, char *s, int l)
  273. {
  274. char buf[64];
  275. int ret;
  276. ret = print_numericoid(ss,s);
  277. if ( l ) {
  278. snprintf(buf, sizeof buf, "{%d}",l);
  279. ret = print_literal(ss,buf);
  280. }
  281. return(ret);
  282. }
  283. static int
  284. print_ruleid(safe_string *ss, int rid)
  285. {
  286. char buf[64];
  287. snprintf(buf, sizeof buf, "%d", rid);
  288. return print_literal(ss,buf);
  289. }
  290. static int
  291. print_ruleids(safe_string *ss, int n, int *rids)
  292. {
  293. int i;
  294. if( n == 1 ) {
  295. print_ruleid(ss,rids[0]);
  296. return print_whsp(ss);
  297. } else {
  298. print_literal(ss,"("/*)*/);
  299. for( i=0; i<n; i++ ) {
  300. print_whsp(ss);
  301. print_ruleid(ss,rids[i]);
  302. }
  303. print_whsp(ss);
  304. return print_literal(ss,/*(*/")");
  305. }
  306. }
  307. static int
  308. print_extensions(safe_string *ss, LDAPSchemaExtensionItem **extensions)
  309. {
  310. LDAPSchemaExtensionItem **ext;
  311. if ( extensions ) {
  312. print_whsp(ss);
  313. for ( ext = extensions; *ext != NULL; ext++ ) {
  314. print_literal(ss, (*ext)->lsei_name);
  315. print_whsp(ss);
  316. /* Should be print_qdstrings */
  317. print_qdescrs(ss, (*ext)->lsei_values);
  318. print_whsp(ss);
  319. }
  320. }
  321. return 0;
  322. }
  323. char *
  324. ldap_syntax2str( LDAPSyntax * syn )
  325. {
  326. struct berval bv;
  327. if (ldap_syntax2bv( syn, &bv ))
  328. return(bv.bv_val);
  329. else
  330. return NULL;
  331. }
  332. struct berval *
  333. ldap_syntax2bv( LDAPSyntax * syn, struct berval *bv )
  334. {
  335. safe_string * ss;
  336. if ( !syn || !bv )
  337. return NULL;
  338. ss = new_safe_string(256);
  339. if ( !ss )
  340. return NULL;
  341. print_literal(ss,"("/*)*/);
  342. print_whsp(ss);
  343. print_numericoid(ss, syn->syn_oid);
  344. print_whsp(ss);
  345. if ( syn->syn_desc ) {
  346. print_literal(ss,"DESC");
  347. print_qdstring(ss,syn->syn_desc);
  348. }
  349. print_whsp(ss);
  350. print_extensions(ss, syn->syn_extensions);
  351. print_literal(ss,/*(*/ ")");
  352. bv->bv_val = safe_strdup(ss);
  353. bv->bv_len = ss->pos;
  354. safe_string_free(ss);
  355. return(bv);
  356. }
  357. char *
  358. ldap_matchingrule2str( LDAPMatchingRule * mr )
  359. {
  360. struct berval bv;
  361. if (ldap_matchingrule2bv( mr, &bv ))
  362. return(bv.bv_val);
  363. else
  364. return NULL;
  365. }
  366. struct berval *
  367. ldap_matchingrule2bv( LDAPMatchingRule * mr, struct berval *bv )
  368. {
  369. safe_string * ss;
  370. if ( !mr || !bv )
  371. return NULL;
  372. ss = new_safe_string(256);
  373. if ( !ss )
  374. return NULL;
  375. print_literal(ss,"(" /*)*/);
  376. print_whsp(ss);
  377. print_numericoid(ss, mr->mr_oid);
  378. print_whsp(ss);
  379. if ( mr->mr_names ) {
  380. print_literal(ss,"NAME");
  381. print_qdescrs(ss,mr->mr_names);
  382. }
  383. if ( mr->mr_desc ) {
  384. print_literal(ss,"DESC");
  385. print_qdstring(ss,mr->mr_desc);
  386. }
  387. if ( mr->mr_obsolete ) {
  388. print_literal(ss, "OBSOLETE");
  389. print_whsp(ss);
  390. }
  391. if ( mr->mr_syntax_oid ) {
  392. print_literal(ss,"SYNTAX");
  393. print_whsp(ss);
  394. print_literal(ss, mr->mr_syntax_oid);
  395. print_whsp(ss);
  396. }
  397. print_whsp(ss);
  398. print_extensions(ss, mr->mr_extensions);
  399. print_literal(ss,/*(*/")");
  400. bv->bv_val = safe_strdup(ss);
  401. bv->bv_len = ss->pos;
  402. safe_string_free(ss);
  403. return(bv);
  404. }
  405. char *
  406. ldap_matchingruleuse2str( LDAPMatchingRuleUse * mru )
  407. {
  408. struct berval bv;
  409. if (ldap_matchingruleuse2bv( mru, &bv ))
  410. return(bv.bv_val);
  411. else
  412. return NULL;
  413. }
  414. struct berval *
  415. ldap_matchingruleuse2bv( LDAPMatchingRuleUse * mru, struct berval *bv )
  416. {
  417. safe_string * ss;
  418. if ( !mru || !bv )
  419. return NULL;
  420. ss = new_safe_string(256);
  421. if ( !ss )
  422. return NULL;
  423. print_literal(ss,"(" /*)*/);
  424. print_whsp(ss);
  425. print_numericoid(ss, mru->mru_oid);
  426. print_whsp(ss);
  427. if ( mru->mru_names ) {
  428. print_literal(ss,"NAME");
  429. print_qdescrs(ss,mru->mru_names);
  430. }
  431. if ( mru->mru_desc ) {
  432. print_literal(ss,"DESC");
  433. print_qdstring(ss,mru->mru_desc);
  434. }
  435. if ( mru->mru_obsolete ) {
  436. print_literal(ss, "OBSOLETE");
  437. print_whsp(ss);
  438. }
  439. if ( mru->mru_applies_oids ) {
  440. print_literal(ss,"APPLIES");
  441. print_whsp(ss);
  442. print_oids(ss, mru->mru_applies_oids);
  443. print_whsp(ss);
  444. }
  445. print_whsp(ss);
  446. print_extensions(ss, mru->mru_extensions);
  447. print_literal(ss,/*(*/")");
  448. bv->bv_val = safe_strdup(ss);
  449. bv->bv_len = ss->pos;
  450. safe_string_free(ss);
  451. return(bv);
  452. }
  453. char *
  454. ldap_objectclass2str( LDAPObjectClass * oc )
  455. {
  456. struct berval bv;
  457. if (ldap_objectclass2bv( oc, &bv ))
  458. return(bv.bv_val);
  459. else
  460. return NULL;
  461. }
  462. struct berval *
  463. ldap_objectclass2bv( LDAPObjectClass * oc, struct berval *bv )
  464. {
  465. safe_string * ss;
  466. if ( !oc || !bv )
  467. return NULL;
  468. ss = new_safe_string(256);
  469. if ( !ss )
  470. return NULL;
  471. print_literal(ss,"("/*)*/);
  472. print_whsp(ss);
  473. print_numericoid(ss, oc->oc_oid);
  474. print_whsp(ss);
  475. if ( oc->oc_names ) {
  476. print_literal(ss,"NAME");
  477. print_qdescrs(ss,oc->oc_names);
  478. }
  479. if ( oc->oc_desc ) {
  480. print_literal(ss,"DESC");
  481. print_qdstring(ss,oc->oc_desc);
  482. }
  483. if ( oc->oc_obsolete ) {
  484. print_literal(ss, "OBSOLETE");
  485. print_whsp(ss);
  486. }
  487. if ( oc->oc_sup_oids ) {
  488. print_literal(ss,"SUP");
  489. print_whsp(ss);
  490. print_oids(ss,oc->oc_sup_oids);
  491. print_whsp(ss);
  492. }
  493. switch (oc->oc_kind) {
  494. case LDAP_SCHEMA_ABSTRACT:
  495. print_literal(ss,"ABSTRACT");
  496. break;
  497. case LDAP_SCHEMA_STRUCTURAL:
  498. print_literal(ss,"STRUCTURAL");
  499. break;
  500. case LDAP_SCHEMA_AUXILIARY:
  501. print_literal(ss,"AUXILIARY");
  502. break;
  503. default:
  504. print_literal(ss,"KIND-UNKNOWN");
  505. break;
  506. }
  507. print_whsp(ss);
  508. if ( oc->oc_at_oids_must ) {
  509. print_literal(ss,"MUST");
  510. print_whsp(ss);
  511. print_oids(ss,oc->oc_at_oids_must);
  512. print_whsp(ss);
  513. }
  514. if ( oc->oc_at_oids_may ) {
  515. print_literal(ss,"MAY");
  516. print_whsp(ss);
  517. print_oids(ss,oc->oc_at_oids_may);
  518. print_whsp(ss);
  519. }
  520. print_whsp(ss);
  521. print_extensions(ss, oc->oc_extensions);
  522. print_literal(ss, /*(*/")");
  523. bv->bv_val = safe_strdup(ss);
  524. bv->bv_len = ss->pos;
  525. safe_string_free(ss);
  526. return(bv);
  527. }
  528. char *
  529. ldap_contentrule2str( LDAPContentRule * cr )
  530. {
  531. struct berval bv;
  532. if (ldap_contentrule2bv( cr, &bv ))
  533. return(bv.bv_val);
  534. else
  535. return NULL;
  536. }
  537. struct berval *
  538. ldap_contentrule2bv( LDAPContentRule * cr, struct berval *bv )
  539. {
  540. safe_string * ss;
  541. if ( !cr || !bv )
  542. return NULL;
  543. ss = new_safe_string(256);
  544. if ( !ss )
  545. return NULL;
  546. print_literal(ss,"("/*)*/);
  547. print_whsp(ss);
  548. print_numericoid(ss, cr->cr_oid);
  549. print_whsp(ss);
  550. if ( cr->cr_names ) {
  551. print_literal(ss,"NAME");
  552. print_qdescrs(ss,cr->cr_names);
  553. }
  554. if ( cr->cr_desc ) {
  555. print_literal(ss,"DESC");
  556. print_qdstring(ss,cr->cr_desc);
  557. }
  558. if ( cr->cr_obsolete ) {
  559. print_literal(ss, "OBSOLETE");
  560. print_whsp(ss);
  561. }
  562. if ( cr->cr_oc_oids_aux ) {
  563. print_literal(ss,"AUX");
  564. print_whsp(ss);
  565. print_oids(ss,cr->cr_oc_oids_aux);
  566. print_whsp(ss);
  567. }
  568. if ( cr->cr_at_oids_must ) {
  569. print_literal(ss,"MUST");
  570. print_whsp(ss);
  571. print_oids(ss,cr->cr_at_oids_must);
  572. print_whsp(ss);
  573. }
  574. if ( cr->cr_at_oids_may ) {
  575. print_literal(ss,"MAY");
  576. print_whsp(ss);
  577. print_oids(ss,cr->cr_at_oids_may);
  578. print_whsp(ss);
  579. }
  580. if ( cr->cr_at_oids_not ) {
  581. print_literal(ss,"NOT");
  582. print_whsp(ss);
  583. print_oids(ss,cr->cr_at_oids_not);
  584. print_whsp(ss);
  585. }
  586. print_whsp(ss);
  587. print_extensions(ss, cr->cr_extensions);
  588. print_literal(ss, /*(*/")");
  589. bv->bv_val = safe_strdup(ss);
  590. bv->bv_len = ss->pos;
  591. safe_string_free(ss);
  592. return(bv);
  593. }
  594. char *
  595. ldap_structurerule2str( LDAPStructureRule * sr )
  596. {
  597. struct berval bv;
  598. if (ldap_structurerule2bv( sr, &bv ))
  599. return(bv.bv_val);
  600. else
  601. return NULL;
  602. }
  603. struct berval *
  604. ldap_structurerule2bv( LDAPStructureRule * sr, struct berval *bv )
  605. {
  606. safe_string * ss;
  607. if ( !sr || !bv )
  608. return NULL;
  609. ss = new_safe_string(256);
  610. if ( !ss )
  611. return NULL;
  612. print_literal(ss,"("/*)*/);
  613. print_whsp(ss);
  614. print_ruleid(ss, sr->sr_ruleid);
  615. print_whsp(ss);
  616. if ( sr->sr_names ) {
  617. print_literal(ss,"NAME");
  618. print_qdescrs(ss,sr->sr_names);
  619. }
  620. if ( sr->sr_desc ) {
  621. print_literal(ss,"DESC");
  622. print_qdstring(ss,sr->sr_desc);
  623. }
  624. if ( sr->sr_obsolete ) {
  625. print_literal(ss, "OBSOLETE");
  626. print_whsp(ss);
  627. }
  628. print_literal(ss,"FORM");
  629. print_whsp(ss);
  630. print_woid(ss,sr->sr_nameform);
  631. print_whsp(ss);
  632. if ( sr->sr_nsup_ruleids ) {
  633. print_literal(ss,"SUP");
  634. print_whsp(ss);
  635. print_ruleids(ss,sr->sr_nsup_ruleids,sr->sr_sup_ruleids);
  636. print_whsp(ss);
  637. }
  638. print_whsp(ss);
  639. print_extensions(ss, sr->sr_extensions);
  640. print_literal(ss, /*(*/")");
  641. bv->bv_val = safe_strdup(ss);
  642. bv->bv_len = ss->pos;
  643. safe_string_free(ss);
  644. return(bv);
  645. }
  646. char *
  647. ldap_nameform2str( LDAPNameForm * nf )
  648. {
  649. struct berval bv;
  650. if (ldap_nameform2bv( nf, &bv ))
  651. return(bv.bv_val);
  652. else
  653. return NULL;
  654. }
  655. struct berval *
  656. ldap_nameform2bv( LDAPNameForm * nf, struct berval *bv )
  657. {
  658. safe_string * ss;
  659. if ( !nf || !bv )
  660. return NULL;
  661. ss = new_safe_string(256);
  662. if ( !ss )
  663. return NULL;
  664. print_literal(ss,"("/*)*/);
  665. print_whsp(ss);
  666. print_numericoid(ss, nf->nf_oid);
  667. print_whsp(ss);
  668. if ( nf->nf_names ) {
  669. print_literal(ss,"NAME");
  670. print_qdescrs(ss,nf->nf_names);
  671. }
  672. if ( nf->nf_desc ) {
  673. print_literal(ss,"DESC");
  674. print_qdstring(ss,nf->nf_desc);
  675. }
  676. if ( nf->nf_obsolete ) {
  677. print_literal(ss, "OBSOLETE");
  678. print_whsp(ss);
  679. }
  680. print_literal(ss,"OC");
  681. print_whsp(ss);
  682. print_woid(ss,nf->nf_objectclass);
  683. print_whsp(ss);
  684. print_literal(ss,"MUST");
  685. print_whsp(ss);
  686. print_oids(ss,nf->nf_at_oids_must);
  687. print_whsp(ss);
  688. if ( nf->nf_at_oids_may ) {
  689. print_literal(ss,"MAY");
  690. print_whsp(ss);
  691. print_oids(ss,nf->nf_at_oids_may);
  692. print_whsp(ss);
  693. }
  694. print_whsp(ss);
  695. print_extensions(ss, nf->nf_extensions);
  696. print_literal(ss, /*(*/")");
  697. bv->bv_val = safe_strdup(ss);
  698. bv->bv_len = ss->pos;
  699. safe_string_free(ss);
  700. return(bv);
  701. }
  702. char *
  703. ldap_attributetype2str( LDAPAttributeType * at )
  704. {
  705. struct berval bv;
  706. if (ldap_attributetype2bv( at, &bv ))
  707. return(bv.bv_val);
  708. else
  709. return NULL;
  710. }
  711. struct berval *
  712. ldap_attributetype2bv( LDAPAttributeType * at, struct berval *bv )
  713. {
  714. safe_string * ss;
  715. if ( !at || !bv )
  716. return NULL;
  717. ss = new_safe_string(256);
  718. if ( !ss )
  719. return NULL;
  720. print_literal(ss,"("/*)*/);
  721. print_whsp(ss);
  722. print_numericoid(ss, at->at_oid);
  723. print_whsp(ss);
  724. if ( at->at_names ) {
  725. print_literal(ss,"NAME");
  726. print_qdescrs(ss,at->at_names);
  727. }
  728. if ( at->at_desc ) {
  729. print_literal(ss,"DESC");
  730. print_qdstring(ss,at->at_desc);
  731. }
  732. if ( at->at_obsolete ) {
  733. print_literal(ss, "OBSOLETE");
  734. print_whsp(ss);
  735. }
  736. if ( at->at_sup_oid ) {
  737. print_literal(ss,"SUP");
  738. print_woid(ss,at->at_sup_oid);
  739. }
  740. if ( at->at_equality_oid ) {
  741. print_literal(ss,"EQUALITY");
  742. print_woid(ss,at->at_equality_oid);
  743. }
  744. if ( at->at_ordering_oid ) {
  745. print_literal(ss,"ORDERING");
  746. print_woid(ss,at->at_ordering_oid);
  747. }
  748. if ( at->at_substr_oid ) {
  749. print_literal(ss,"SUBSTR");
  750. print_woid(ss,at->at_substr_oid);
  751. }
  752. if ( at->at_syntax_oid ) {
  753. print_literal(ss,"SYNTAX");
  754. print_whsp(ss);
  755. print_noidlen(ss,at->at_syntax_oid,at->at_syntax_len);
  756. print_whsp(ss);
  757. }
  758. if ( at->at_single_value == LDAP_SCHEMA_YES ) {
  759. print_literal(ss,"SINGLE-VALUE");
  760. print_whsp(ss);
  761. }
  762. if ( at->at_collective == LDAP_SCHEMA_YES ) {
  763. print_literal(ss,"COLLECTIVE");
  764. print_whsp(ss);
  765. }
  766. if ( at->at_no_user_mod == LDAP_SCHEMA_YES ) {
  767. print_literal(ss,"NO-USER-MODIFICATION");
  768. print_whsp(ss);
  769. }
  770. if ( at->at_usage != LDAP_SCHEMA_USER_APPLICATIONS ) {
  771. print_literal(ss,"USAGE");
  772. print_whsp(ss);
  773. switch (at->at_usage) {
  774. case LDAP_SCHEMA_DIRECTORY_OPERATION:
  775. print_literal(ss,"directoryOperation");
  776. break;
  777. case LDAP_SCHEMA_DISTRIBUTED_OPERATION:
  778. print_literal(ss,"distributedOperation");
  779. break;
  780. case LDAP_SCHEMA_DSA_OPERATION:
  781. print_literal(ss,"dSAOperation");
  782. break;
  783. default:
  784. print_literal(ss,"UNKNOWN");
  785. break;
  786. }
  787. }
  788. print_whsp(ss);
  789. print_extensions(ss, at->at_extensions);
  790. print_literal(ss,/*(*/")");
  791. bv->bv_val = safe_strdup(ss);
  792. bv->bv_len = ss->pos;
  793. safe_string_free(ss);
  794. return(bv);
  795. }
  796. /*
  797. * Now come the parsers. There is one parser for each entity type:
  798. * objectclasses, attributetypes, etc.
  799. *
  800. * Each of them is written as a recursive-descent parser, except that
  801. * none of them is really recursive. But the idea is kept: there
  802. * is one routine per non-terminal that either gobbles lexical tokens
  803. * or calls lower-level routines, etc.
  804. *
  805. * The scanner is implemented in the routine get_token. Actually,
  806. * get_token is more than a scanner and will return tokens that are
  807. * in fact non-terminals in the grammar. So you can see the whole
  808. * approach as the combination of a low-level bottom-up recognizer
  809. * combined with a scanner and a number of top-down parsers. Or just
  810. * consider that the real grammars recognized by the parsers are not
  811. * those of the standards. As a matter of fact, our parsers are more
  812. * liberal than the spec when there is no ambiguity.
  813. *
  814. * The difference is pretty academic (modulo bugs or incorrect
  815. * interpretation of the specs).
  816. */
  817. typedef enum tk_t {
  818. TK_NOENDQUOTE = -2,
  819. TK_OUTOFMEM = -1,
  820. TK_EOS = 0,
  821. TK_UNEXPCHAR = 1,
  822. TK_BAREWORD = 2,
  823. TK_QDSTRING = 3,
  824. TK_LEFTPAREN = 4,
  825. TK_RIGHTPAREN = 5,
  826. TK_DOLLAR = 6,
  827. TK_QDESCR = TK_QDSTRING
  828. } tk_t;
  829. static tk_t
  830. get_token( const char ** sp, char ** token_val )
  831. {
  832. tk_t kind;
  833. const char * p;
  834. const char * q;
  835. char * res;
  836. *token_val = NULL;
  837. switch (**sp) {
  838. case '\0':
  839. kind = TK_EOS;
  840. (*sp)++;
  841. break;
  842. case '(':
  843. kind = TK_LEFTPAREN;
  844. (*sp)++;
  845. break;
  846. case ')':
  847. kind = TK_RIGHTPAREN;
  848. (*sp)++;
  849. break;
  850. case '$':
  851. kind = TK_DOLLAR;
  852. (*sp)++;
  853. break;
  854. case '\'':
  855. kind = TK_QDSTRING;
  856. (*sp)++;
  857. p = *sp;
  858. while ( **sp != '\'' && **sp != '\0' )
  859. (*sp)++;
  860. if ( **sp == '\'' ) {
  861. q = *sp;
  862. res = LDAP_MALLOC(q-p+1);
  863. if ( !res ) {
  864. kind = TK_OUTOFMEM;
  865. } else {
  866. strncpy(res,p,q-p);
  867. res[q-p] = '\0';
  868. *token_val = res;
  869. }
  870. (*sp)++;
  871. } else {
  872. kind = TK_NOENDQUOTE;
  873. }
  874. break;
  875. default:
  876. kind = TK_BAREWORD;
  877. p = *sp;
  878. while ( !LDAP_SPACE(**sp) &&
  879. **sp != '(' &&
  880. **sp != ')' &&
  881. **sp != '$' &&
  882. **sp != '\'' &&
  883. /* for suggested minimum upper bound on the number
  884. * of characters (RFC 4517) */
  885. **sp != '{' &&
  886. **sp != '\0' )
  887. (*sp)++;
  888. q = *sp;
  889. res = LDAP_MALLOC(q-p+1);
  890. if ( !res ) {
  891. kind = TK_OUTOFMEM;
  892. } else {
  893. strncpy(res,p,q-p);
  894. res[q-p] = '\0';
  895. *token_val = res;
  896. }
  897. break;
  898. /* kind = TK_UNEXPCHAR; */
  899. /* break; */
  900. }
  901. return kind;
  902. }
  903. /* Gobble optional whitespace */
  904. static void
  905. parse_whsp(const char **sp)
  906. {
  907. while (LDAP_SPACE(**sp))
  908. (*sp)++;
  909. }
  910. /* TBC:!!
  911. * General note for all parsers: to guarantee the algorithm halts they
  912. * must always advance the pointer even when an error is found. For
  913. * this one is not that important since an error here is fatal at the
  914. * upper layers, but it is a simple strategy that will not get in
  915. * endless loops.
  916. */
  917. /* Parse a sequence of dot-separated decimal strings */
  918. char *
  919. ldap_int_parse_numericoid(const char **sp, int *code, const int flags)
  920. {
  921. char * res = NULL;
  922. const char * start = *sp;
  923. int len;
  924. int quoted = 0;
  925. /* Netscape puts the SYNTAX value in quotes (incorrectly) */
  926. if ( flags & LDAP_SCHEMA_ALLOW_QUOTED && **sp == '\'' ) {
  927. quoted = 1;
  928. (*sp)++;
  929. start++;
  930. }
  931. /* Each iteration of this loop gets one decimal string */
  932. while (**sp) {
  933. if ( !LDAP_DIGIT(**sp) ) {
  934. /*
  935. * Initial char is not a digit or char after dot is
  936. * not a digit
  937. */
  938. *code = LDAP_SCHERR_NODIGIT;
  939. return NULL;
  940. }
  941. (*sp)++;
  942. while ( LDAP_DIGIT(**sp) )
  943. (*sp)++;
  944. if ( **sp != '.' )
  945. break;
  946. /* Otherwise, gobble the dot and loop again */
  947. (*sp)++;
  948. }
  949. /* Now *sp points at the char past the numericoid. Perfect. */
  950. len = *sp - start;
  951. if ( flags & LDAP_SCHEMA_ALLOW_QUOTED && quoted ) {
  952. if ( **sp == '\'' ) {
  953. (*sp)++;
  954. } else {
  955. *code = LDAP_SCHERR_UNEXPTOKEN;
  956. return NULL;
  957. }
  958. }
  959. if (flags & LDAP_SCHEMA_SKIP) {
  960. res = (char *)start;
  961. } else {
  962. res = LDAP_MALLOC(len+1);
  963. if (!res) {
  964. *code = LDAP_SCHERR_OUTOFMEM;
  965. return(NULL);
  966. }
  967. strncpy(res,start,len);
  968. res[len] = '\0';
  969. }
  970. return(res);
  971. }
  972. /* Parse a sequence of dot-separated decimal strings */
  973. int
  974. ldap_int_parse_ruleid(const char **sp, int *code, const int flags, int *ruleid)
  975. {
  976. *ruleid=0;
  977. if ( !LDAP_DIGIT(**sp) ) {
  978. *code = LDAP_SCHERR_NODIGIT;
  979. return -1;
  980. }
  981. *ruleid = (**sp) - '0';
  982. (*sp)++;
  983. while ( LDAP_DIGIT(**sp) ) {
  984. *ruleid *= 10;
  985. *ruleid += (**sp) - '0';
  986. (*sp)++;
  987. }
  988. return 0;
  989. }
  990. /* Parse a qdescr or a list of them enclosed in () */
  991. static char **
  992. parse_qdescrs(const char **sp, int *code)
  993. {
  994. char ** res;
  995. char ** res1;
  996. tk_t kind;
  997. char * sval;
  998. int size;
  999. int pos;
  1000. parse_whsp(sp);
  1001. kind = get_token(sp,&sval);
  1002. if ( kind == TK_LEFTPAREN ) {
  1003. /* Let's presume there will be at least 2 entries */
  1004. size = 3;
  1005. res = LDAP_CALLOC(3,sizeof(char *));
  1006. if ( !res ) {
  1007. *code = LDAP_SCHERR_OUTOFMEM;
  1008. return NULL;
  1009. }
  1010. pos = 0;
  1011. while (1) {
  1012. parse_whsp(sp);
  1013. kind = get_token(sp,&sval);
  1014. if ( kind == TK_RIGHTPAREN )
  1015. break;
  1016. if ( kind == TK_QDESCR ) {
  1017. if ( pos == size-2 ) {
  1018. size++;
  1019. res1 = LDAP_REALLOC(res,size*sizeof(char *));
  1020. if ( !res1 ) {
  1021. LDAP_VFREE(res);
  1022. LDAP_FREE(sval);
  1023. *code = LDAP_SCHERR_OUTOFMEM;
  1024. return(NULL);
  1025. }
  1026. res = res1;
  1027. }
  1028. res[pos++] = sval;
  1029. res[pos] = NULL;
  1030. parse_whsp(sp);
  1031. } else {
  1032. LDAP_VFREE(res);
  1033. LDAP_FREE(sval);
  1034. *code = LDAP_SCHERR_UNEXPTOKEN;
  1035. return(NULL);
  1036. }
  1037. }
  1038. parse_whsp(sp);
  1039. return(res);
  1040. } else if ( kind == TK_QDESCR ) {
  1041. res = LDAP_CALLOC(2,sizeof(char *));
  1042. if ( !res ) {
  1043. *code = LDAP_SCHERR_OUTOFMEM;
  1044. return NULL;
  1045. }
  1046. res[0] = sval;
  1047. res[1] = NULL;
  1048. parse_whsp(sp);
  1049. return res;
  1050. } else {
  1051. LDAP_FREE(sval);
  1052. *code = LDAP_SCHERR_BADNAME;
  1053. return NULL;
  1054. }
  1055. }
  1056. /* Parse a woid */
  1057. static char *
  1058. parse_woid(const char **sp, int *code)
  1059. {
  1060. char * sval;
  1061. tk_t kind;
  1062. parse_whsp(sp);
  1063. kind = get_token(sp, &sval);
  1064. if ( kind != TK_BAREWORD ) {
  1065. LDAP_FREE(sval);
  1066. *code = LDAP_SCHERR_UNEXPTOKEN;
  1067. return NULL;
  1068. }
  1069. parse_whsp(sp);
  1070. return sval;
  1071. }
  1072. /* Parse a noidlen */
  1073. static char *
  1074. parse_noidlen(const char **sp, int *code, int *len, int flags)
  1075. {
  1076. char * sval;
  1077. const char *savepos;
  1078. int quoted = 0;
  1079. int allow_quoted = ( flags & LDAP_SCHEMA_ALLOW_QUOTED );
  1080. int allow_oidmacro = ( flags & LDAP_SCHEMA_ALLOW_OID_MACRO );
  1081. *len = 0;
  1082. /* Netscape puts the SYNTAX value in quotes (incorrectly) */
  1083. if ( allow_quoted && **sp == '\'' ) {
  1084. quoted = 1;
  1085. (*sp)++;
  1086. }
  1087. savepos = *sp;
  1088. sval = ldap_int_parse_numericoid(sp, code, 0);
  1089. if ( !sval ) {
  1090. if ( allow_oidmacro
  1091. && *sp == savepos
  1092. && *code == LDAP_SCHERR_NODIGIT )
  1093. {
  1094. if ( get_token(sp, &sval) != TK_BAREWORD ) {
  1095. if ( sval != NULL ) {
  1096. LDAP_FREE(sval);
  1097. }
  1098. return NULL;
  1099. }
  1100. } else {
  1101. return NULL;
  1102. }
  1103. }
  1104. if ( **sp == '{' /*}*/ ) {
  1105. (*sp)++;
  1106. *len = atoi(*sp);
  1107. while ( LDAP_DIGIT(**sp) )
  1108. (*sp)++;
  1109. if ( **sp != /*{*/ '}' ) {
  1110. *code = LDAP_SCHERR_UNEXPTOKEN;
  1111. LDAP_FREE(sval);
  1112. return NULL;
  1113. }
  1114. (*sp)++;
  1115. }
  1116. if ( allow_quoted && quoted ) {
  1117. if ( **sp == '\'' ) {
  1118. (*sp)++;
  1119. } else {
  1120. *code = LDAP_SCHERR_UNEXPTOKEN;
  1121. LDAP_FREE(sval);
  1122. return NULL;
  1123. }
  1124. }
  1125. return sval;
  1126. }
  1127. /*
  1128. * Next routine will accept a qdstring in place of an oid if
  1129. * allow_quoted is set. This is necessary to interoperate with
  1130. * Netscape Directory server that will improperly quote each oid (at
  1131. * least those of the descr kind) in the SUP clause.
  1132. */
  1133. /* Parse a woid or a $-separated list of them enclosed in () */
  1134. static char **
  1135. parse_oids(const char **sp, int *code, const int allow_quoted)
  1136. {
  1137. char ** res;
  1138. char ** res1;
  1139. tk_t kind;
  1140. char * sval;
  1141. int size;
  1142. int pos;
  1143. /*
  1144. * Strictly speaking, doing this here accepts whsp before the
  1145. * ( at the beginning of an oidlist, but this is harmless. Also,
  1146. * we are very liberal in what we accept as an OID. Maybe
  1147. * refine later.
  1148. */
  1149. parse_whsp(sp);
  1150. kind = get_token(sp,&sval);
  1151. if ( kind == TK_LEFTPAREN ) {
  1152. /* Let's presume there will be at least 2 entries */
  1153. size = 3;
  1154. res = LDAP_CALLOC(3,sizeof(char *));
  1155. if ( !res ) {
  1156. *code = LDAP_SCHERR_OUTOFMEM;
  1157. return NULL;
  1158. }
  1159. pos = 0;
  1160. parse_whsp(sp);
  1161. kind = get_token(sp,&sval);
  1162. if ( kind == TK_BAREWORD ||
  1163. ( allow_quoted && kind == TK_QDSTRING ) ) {
  1164. res[pos++] = sval;
  1165. res[pos] = NULL;
  1166. } else if ( kind == TK_RIGHTPAREN ) {
  1167. /* FIXME: be liberal in what we accept... */
  1168. parse_whsp(sp);
  1169. LDAP_FREE(res);
  1170. return NULL;
  1171. } else {
  1172. *code = LDAP_SCHERR_UNEXPTOKEN;
  1173. LDAP_FREE(sval);
  1174. LDAP_VFREE(res);
  1175. return NULL;
  1176. }
  1177. parse_whsp(sp);
  1178. while (1) {
  1179. kind = get_token(sp,&sval);
  1180. if ( kind == TK_RIGHTPAREN )
  1181. break;
  1182. if ( kind == TK_DOLLAR ) {
  1183. parse_whsp(sp);
  1184. kind = get_token(sp,&sval);
  1185. if ( kind == TK_BAREWORD ||
  1186. ( allow_quoted &&
  1187. kind == TK_QDSTRING ) ) {
  1188. if ( pos == size-2 ) {
  1189. size++;
  1190. res1 = LDAP_REALLOC(res,size*sizeof(char *));
  1191. if ( !res1 ) {
  1192. LDAP_FREE(sval);
  1193. LDAP_VFREE(res);
  1194. *code = LDAP_SCHERR_OUTOFMEM;
  1195. return(NULL);
  1196. }
  1197. res = res1;
  1198. }
  1199. res[pos++] = sval;
  1200. res[pos] = NULL;
  1201. } else {
  1202. *code = LDAP_SCHERR_UNEXPTOKEN;
  1203. LDAP_FREE(sval);
  1204. LDAP_VFREE(res);
  1205. return NULL;
  1206. }
  1207. parse_whsp(sp);
  1208. } else {
  1209. *code = LDAP_SCHERR_UNEXPTOKEN;
  1210. LDAP_FREE(sval);
  1211. LDAP_VFREE(res);
  1212. return NULL;
  1213. }
  1214. }
  1215. parse_whsp(sp);
  1216. return(res);
  1217. } else if ( kind == TK_BAREWORD ||
  1218. ( allow_quoted && kind == TK_QDSTRING ) ) {
  1219. res = LDAP_CALLOC(2,sizeof(char *));
  1220. if ( !res ) {
  1221. LDAP_FREE(sval);
  1222. *code = LDAP_SCHERR_OUTOFMEM;
  1223. return NULL;
  1224. }
  1225. res[0] = sval;
  1226. res[1] = NULL;
  1227. parse_whsp(sp);
  1228. return res;
  1229. } else {
  1230. LDAP_FREE(sval);
  1231. *code = LDAP_SCHERR_BADNAME;
  1232. return NULL;
  1233. }
  1234. }
  1235. static int
  1236. add_extension(LDAPSchemaExtensionItem ***extensions,
  1237. char * name, char ** values)
  1238. {
  1239. int n;
  1240. LDAPSchemaExtensionItem **tmp, *ext;
  1241. ext = LDAP_CALLOC(1, sizeof(LDAPSchemaExtensionItem));
  1242. if ( !ext )
  1243. return 1;
  1244. ext->lsei_name = name;
  1245. ext->lsei_values = values;
  1246. if ( !*extensions ) {
  1247. *extensions =
  1248. LDAP_CALLOC(2, sizeof(LDAPSchemaExtensionItem *));
  1249. if ( !*extensions ) {
  1250. LDAP_FREE( ext );
  1251. return 1;
  1252. }
  1253. n = 0;
  1254. } else {
  1255. for ( n=0; (*extensions)[n] != NULL; n++ )
  1256. ;
  1257. tmp = LDAP_REALLOC(*extensions,
  1258. (n+2)*sizeof(LDAPSchemaExtensionItem *));
  1259. if ( !tmp ) {
  1260. LDAP_FREE( ext );
  1261. return 1;
  1262. }
  1263. *extensions = tmp;
  1264. }
  1265. (*extensions)[n] = ext;
  1266. (*extensions)[n+1] = NULL;
  1267. return 0;
  1268. }
  1269. static void
  1270. free_extensions(LDAPSchemaExtensionItem **extensions)
  1271. {
  1272. LDAPSchemaExtensionItem **ext;
  1273. if ( extensions ) {
  1274. for ( ext = extensions; *ext != NULL; ext++ ) {
  1275. LDAP_FREE((*ext)->lsei_name);
  1276. LDAP_VFREE((*ext)->lsei_values);
  1277. LDAP_FREE(*ext);
  1278. }
  1279. LDAP_FREE(extensions);
  1280. }
  1281. }
  1282. void
  1283. ldap_syntax_free( LDAPSyntax * syn )
  1284. {
  1285. if ( !syn ) return;
  1286. LDAP_FREE(syn->syn_oid);
  1287. if (syn->syn_names) LDAP_VFREE(syn->syn_names);
  1288. if (syn->syn_desc) LDAP_FREE(syn->syn_desc);
  1289. free_extensions(syn->syn_extensions);
  1290. LDAP_FREE(syn);
  1291. }
  1292. LDAPSyntax *
  1293. ldap_str2syntax( LDAP_CONST char * s,
  1294. int * code,
  1295. LDAP_CONST char ** errp,
  1296. LDAP_CONST unsigned flags )
  1297. {
  1298. tk_t kind;
  1299. const char * ss = s;
  1300. char * sval;
  1301. int seen_name = 0;
  1302. int seen_desc = 0;
  1303. LDAPSyntax * syn;
  1304. char ** ext_vals;
  1305. if ( !s ) {
  1306. *code = LDAP_SCHERR_EMPTY;
  1307. *errp = "";
  1308. return NULL;
  1309. }
  1310. *errp = s;
  1311. syn = LDAP_CALLOC(1,sizeof(LDAPSyntax));
  1312. if ( !syn ) {
  1313. *code = LDAP_SCHERR_OUTOFMEM;
  1314. return NULL;
  1315. }
  1316. kind = get_token(&ss,&sval);
  1317. if ( kind != TK_LEFTPAREN ) {
  1318. LDAP_FREE(sval);
  1319. *code = LDAP_SCHERR_NOLEFTPAREN;
  1320. ldap_syntax_free(syn);
  1321. return NULL;
  1322. }
  1323. parse_whsp(&ss);
  1324. syn->syn_oid = ldap_int_parse_numericoid(&ss,code,0);
  1325. if ( !syn->syn_oid ) {
  1326. *errp = ss;
  1327. ldap_syntax_free(syn);
  1328. return NULL;
  1329. }
  1330. parse_whsp(&ss);
  1331. /*
  1332. * Beyond this point we will be liberal and accept the items
  1333. * in any order.
  1334. */
  1335. while (1) {
  1336. kind = get_token(&ss,&sval);
  1337. switch (kind) {
  1338. case TK_EOS:
  1339. *code = LDAP_SCHERR_NORIGHTPAREN;
  1340. *errp = EndOfInput;
  1341. ldap_syntax_free(syn);
  1342. return NULL;
  1343. case TK_RIGHTPAREN:
  1344. return syn;
  1345. case TK_BAREWORD:
  1346. if ( !strcasecmp(sval,"NAME") ) {
  1347. LDAP_FREE(sval);
  1348. if ( seen_name ) {
  1349. *code = LDAP_SCHERR_DUPOPT;
  1350. *errp = ss;
  1351. ldap_syntax_free(syn);
  1352. return(NULL);
  1353. }
  1354. seen_name = 1;
  1355. syn->syn_names = parse_qdescrs(&ss,code);
  1356. if ( !syn->syn_names ) {
  1357. if ( *code != LDAP_SCHERR_OUTOFMEM )
  1358. *code = LDAP_SCHERR_BADNAME;
  1359. *errp = ss;
  1360. ldap_syntax_free(syn);
  1361. return NULL;
  1362. }
  1363. } else if ( !strcasecmp(sval,"DESC") ) {
  1364. LDAP_FREE(sval);
  1365. if ( seen_desc ) {
  1366. *code = LDAP_SCHERR_DUPOPT;
  1367. *errp = ss;
  1368. ldap_syntax_free(syn);
  1369. return(NULL);
  1370. }
  1371. seen_desc = 1;
  1372. parse_whsp(&ss);
  1373. kind = get_token(&ss,&sval);
  1374. if ( kind != TK_QDSTRING ) {
  1375. *code = LDAP_SCHERR_UNEXPTOKEN;
  1376. *errp = ss;
  1377. LDAP_FREE(sval);
  1378. ldap_syntax_free(syn);
  1379. return NULL;
  1380. }
  1381. syn->syn_desc = sval;
  1382. parse_whsp(&ss);
  1383. } else if ( sval[0] == 'X' && sval[1] == '-' ) {
  1384. /* Should be parse_qdstrings */
  1385. ext_vals = parse_qdescrs(&ss, code);
  1386. if ( !ext_vals ) {
  1387. *errp = ss;
  1388. ldap_syntax_free(syn);
  1389. return NULL;
  1390. }
  1391. if ( add_extension(&syn->syn_extensions,
  1392. sval, ext_vals) ) {
  1393. *code = LDAP_SCHERR_OUTOFMEM;
  1394. *errp = ss;
  1395. LDAP_FREE(sval);
  1396. ldap_syntax_free(syn);
  1397. return NULL;
  1398. }
  1399. } else {
  1400. *code = LDAP_SCHERR_UNEXPTOKEN;
  1401. *errp = ss;
  1402. LDAP_FREE(sval);
  1403. ldap_syntax_free(syn);
  1404. return NULL;
  1405. }
  1406. break;
  1407. default:
  1408. *code = LDAP_SCHERR_UNEXPTOKEN;
  1409. *errp = ss;
  1410. LDAP_FREE(sval);
  1411. ldap_syntax_free(syn);
  1412. return NULL;
  1413. }
  1414. }
  1415. }
  1416. void
  1417. ldap_matchingrule_free( LDAPMatchingRule * mr )
  1418. {
  1419. if (!mr) return;
  1420. LDAP_FREE(mr->mr_oid);
  1421. if (mr->mr_names) LDAP_VFREE(mr->mr_names);
  1422. if (mr->mr_desc) LDAP_FREE(mr->mr_desc);
  1423. if (mr->mr_syntax_oid) LDAP_FREE(mr->mr_syntax_oid);
  1424. free_extensions(mr->mr_extensions);
  1425. LDAP_FREE(mr);
  1426. }
  1427. LDAPMatchingRule *
  1428. ldap_str2matchingrule( LDAP_CONST char * s,
  1429. int * code,
  1430. LDAP_CONST char ** errp,
  1431. LDAP_CONST unsigned flags )
  1432. {
  1433. tk_t kind;
  1434. const char * ss = s;
  1435. char * sval;
  1436. int seen_name = 0;
  1437. int seen_desc = 0;
  1438. int seen_obsolete = 0;
  1439. int seen_syntax = 0;
  1440. LDAPMatchingRule * mr;
  1441. char ** ext_vals;
  1442. const char * savepos;
  1443. if ( !s ) {
  1444. *code = LDAP_SCHERR_EMPTY;
  1445. *errp = "";
  1446. return NULL;
  1447. }
  1448. *errp = s;
  1449. mr = LDAP_CALLOC(1,sizeof(LDAPMatchingRule));
  1450. if ( !mr ) {
  1451. *code = LDAP_SCHERR_OUTOFMEM;
  1452. return NULL;
  1453. }
  1454. kind = get_token(&ss,&sval);
  1455. if ( kind != TK_LEFTPAREN ) {
  1456. *code = LDAP_SCHERR_NOLEFTPAREN;
  1457. LDAP_FREE(sval);
  1458. ldap_matchingrule_free(mr);
  1459. return NULL;
  1460. }
  1461. parse_whsp(&ss);
  1462. savepos = ss;
  1463. mr->mr_oid = ldap_int_parse_numericoid(&ss,code,flags);
  1464. if ( !mr->mr_oid ) {
  1465. if ( flags & LDAP_SCHEMA_ALLOW_NO_OID ) {
  1466. /* Backtracking */
  1467. ss = savepos;
  1468. kind = get_token(&ss,&sval);
  1469. if ( kind == TK_BAREWORD ) {
  1470. if ( !strcasecmp(sval, "NAME") ||
  1471. !strcasecmp(sval, "DESC") ||
  1472. !strcasecmp(sval, "OBSOLETE") ||
  1473. !strcasecmp(sval, "SYNTAX") ||
  1474. !strncasecmp(sval, "X-", 2) ) {
  1475. /* Missing OID, backtrack */
  1476. ss = savepos;
  1477. } else {
  1478. /* Non-numerical OID, ignore */
  1479. }
  1480. }
  1481. LDAP_FREE(sval);
  1482. } else {
  1483. *errp = ss;
  1484. ldap_matchingrule_free(mr);
  1485. return NULL;
  1486. }
  1487. }
  1488. parse_whsp(&ss);
  1489. /*
  1490. * Beyond this point we will be liberal and accept the items
  1491. * in any order.
  1492. */
  1493. while (1) {
  1494. kind = get_token(&ss,&sval);
  1495. switch (kind) {
  1496. case TK_EOS:
  1497. *code = LDAP_SCHERR_NORIGHTPAREN;
  1498. *errp = EndOfInput;
  1499. ldap_matchingrule_free(mr);
  1500. return NULL;
  1501. case TK_RIGHTPAREN:
  1502. if( !seen_syntax ) {
  1503. *code = LDAP_SCHERR_MISSING;
  1504. ldap_matchingrule_free(mr);
  1505. return NULL;
  1506. }
  1507. return mr;
  1508. case TK_BAREWORD:
  1509. if ( !strcasecmp(sval,"NAME") ) {
  1510. LDAP_FREE(sval);
  1511. if ( seen_name ) {
  1512. *code = LDAP_SCHERR_DUPOPT;
  1513. *errp = ss;
  1514. ldap_matchingrule_free(mr);
  1515. return(NULL);
  1516. }
  1517. seen_name = 1;
  1518. mr->mr_names = parse_qdescrs(&ss,code);
  1519. if ( !mr->mr_names ) {
  1520. if ( *code != LDAP_SCHERR_OUTOFMEM )
  1521. *code = LDAP_SCHERR_BADNAME;
  1522. *errp = ss;
  1523. ldap_matchingrule_free(mr);
  1524. return NULL;
  1525. }
  1526. } else if ( !strcasecmp(sval,"DESC") ) {
  1527. LDAP_FREE(sval);
  1528. if ( seen_desc ) {
  1529. *code = LDAP_SCHERR_DUPOPT;
  1530. *errp = ss;
  1531. ldap_matchingrule_free(mr);
  1532. return(NULL);
  1533. }
  1534. seen_desc = 1;
  1535. parse_whsp(&ss);
  1536. kind = get_token(&ss,&sval);
  1537. if ( kind != TK_QDSTRING ) {
  1538. *code = LDAP_SCHERR_UNEXPTOKEN;
  1539. *errp = ss;
  1540. LDAP_FREE(sval);
  1541. ldap_matchingrule_free(mr);
  1542. return NULL;
  1543. }
  1544. mr->mr_desc = sval;
  1545. parse_whsp(&ss);
  1546. } else if ( !strcasecmp(sval,"OBSOLETE") ) {
  1547. LDAP_FREE(sval);
  1548. if ( seen_obsolete ) {
  1549. *code = LDAP_SCHERR_DUPOPT;
  1550. *errp = ss;
  1551. ldap_matchingrule_free(mr);
  1552. return(NULL);
  1553. }
  1554. seen_obsolete = 1;
  1555. mr->mr_obsolete = LDAP_SCHEMA_YES;
  1556. parse_whsp(&ss);
  1557. } else if ( !strcasecmp(sval,"SYNTAX") ) {
  1558. LDAP_FREE(sval);
  1559. if ( seen_syntax ) {
  1560. *code = LDAP_SCHERR_DUPOPT;
  1561. *errp = ss;
  1562. ldap_matchingrule_free(mr);
  1563. return(NULL);
  1564. }
  1565. seen_syntax = 1;
  1566. parse_whsp(&ss);
  1567. mr->mr_syntax_oid =
  1568. ldap_int_parse_numericoid(&ss,code,flags);
  1569. if ( !mr->mr_syntax_oid ) {
  1570. *errp = ss;
  1571. ldap_matchingrule_free(mr);
  1572. return NULL;
  1573. }
  1574. parse_whsp(&ss);
  1575. } else if ( sval[0] == 'X' && sval[1] == '-' ) {
  1576. /* Should be parse_qdstrings */
  1577. ext_vals = parse_qdescrs(&ss, code);
  1578. if ( !ext_vals ) {
  1579. *errp = ss;
  1580. ldap_matchingrule_free(mr);
  1581. return NULL;
  1582. }
  1583. if ( add_extension(&mr->mr_extensions,
  1584. sval, ext_vals) ) {
  1585. *code = LDAP_SCHERR_OUTOFMEM;
  1586. *errp = ss;
  1587. LDAP_FREE(sval);
  1588. ldap_matchingrule_free(mr);
  1589. return NULL;
  1590. }
  1591. } else {
  1592. *code = LDAP_SCHERR_UNEXPTOKEN;
  1593. *errp = ss;
  1594. LDAP_FREE(sval);
  1595. ldap_matchingrule_free(mr);
  1596. return NULL;
  1597. }
  1598. break;
  1599. default:
  1600. *code = LDAP_SCHERR_UNEXPTOKEN;
  1601. *errp = ss;
  1602. LDAP_FREE(sval);
  1603. ldap_matchingrule_free(mr);
  1604. return NULL;
  1605. }
  1606. }
  1607. }
  1608. void
  1609. ldap_matchingruleuse_free( LDAPMatchingRuleUse * mru )
  1610. {
  1611. if (!mru) return;
  1612. LDAP_FREE(mru->mru_oid);
  1613. if (mru->mru_names) LDAP_VFREE(mru->mru_names);
  1614. if (mru->mru_desc) LDAP_FREE(mru->mru_desc);
  1615. if (mru->mru_applies_oids) LDAP_VFREE(mru->mru_applies_oids);
  1616. free_extensions(mru->mru_extensions);
  1617. LDAP_FREE(mru);
  1618. }
  1619. LDAPMatchingRuleUse *
  1620. ldap_str2matchingruleuse( LDAP_CONST char * s,
  1621. int * code,
  1622. LDAP_CONST char ** errp,
  1623. LDAP_CONST unsigned flags )
  1624. {
  1625. tk_t kind;
  1626. const char * ss = s;
  1627. char * sval;
  1628. int seen_name = 0;
  1629. int seen_desc = 0;
  1630. int seen_obsolete = 0;
  1631. int seen_applies = 0;
  1632. LDAPMatchingRuleUse * mru;
  1633. char ** ext_vals;
  1634. const char * savepos;
  1635. if ( !s ) {
  1636. *code = LDAP_SCHERR_EMPTY;
  1637. *errp = "";
  1638. return NULL;
  1639. }
  1640. *errp = s;
  1641. mru = LDAP_CALLOC(1,sizeof(LDAPMatchingRuleUse));
  1642. if ( !mru ) {
  1643. *code = LDAP_SCHERR_OUTOFMEM;
  1644. return NULL;
  1645. }
  1646. kind = get_token(&ss,&sval);
  1647. if ( kind != TK_LEFTPAREN ) {
  1648. *code = LDAP_SCHERR_NOLEFTPAREN;
  1649. LDAP_FREE(sval);
  1650. ldap_matchingruleuse_free(mru);
  1651. return NULL;
  1652. }
  1653. parse_whsp(&ss);
  1654. savepos = ss;
  1655. mru->mru_oid = ldap_int_parse_numericoid(&ss,code,flags);
  1656. if ( !mru->mru_oid ) {
  1657. if ( flags & LDAP_SCHEMA_ALLOW_NO_OID ) {
  1658. /* Backtracking */
  1659. ss = savepos;
  1660. kind = get_token(&ss,&sval);
  1661. if ( kind == TK_BAREWORD ) {
  1662. if ( !strcasecmp(sval, "NAME") ||
  1663. !strcasecmp(sval, "DESC") ||
  1664. !strcasecmp(sval, "OBSOLETE") ||
  1665. !strcasecmp(sval, "APPLIES") ||
  1666. !strncasecmp(sval, "X-", 2) ) {
  1667. /* Missing OID, backtrack */
  1668. ss = savepos;
  1669. } else {
  1670. /* Non-numerical OID, ignore */
  1671. }
  1672. }
  1673. LDAP_FREE(sval);
  1674. } else {
  1675. *errp = ss;
  1676. ldap_matchingruleuse_free(mru);
  1677. return NULL;
  1678. }
  1679. }
  1680. parse_whsp(&ss);
  1681. /*
  1682. * Beyond this point we will be liberal and accept the items
  1683. * in any order.
  1684. */
  1685. while (1) {
  1686. kind = get_token(&ss,&sval);
  1687. switch (kind) {
  1688. case TK_EOS:
  1689. *code = LDAP_SCHERR_NORIGHTPAREN;
  1690. *errp = EndOfInput;
  1691. ldap_matchingruleuse_free(mru);
  1692. return NULL;
  1693. case TK_RIGHTPAREN:
  1694. if( !seen_applies ) {
  1695. *code = LDAP_SCHERR_MISSING;
  1696. ldap_matchingruleuse_free(mru);
  1697. return NULL;
  1698. }
  1699. return mru;
  1700. case TK_BAREWORD:
  1701. if ( !strcasecmp(sval,"NAME") ) {
  1702. LDAP_FREE(sval);
  1703. if ( seen_name ) {
  1704. *code = LDAP_SCHERR_DUPOPT;
  1705. *errp = ss;
  1706. ldap_matchingruleuse_free(mru);
  1707. return(NULL);
  1708. }
  1709. seen_name = 1;
  1710. mru->mru_names = parse_qdescrs(&ss,code);
  1711. if ( !mru->mru_names ) {
  1712. if ( *code != LDAP_SCHERR_OUTOFMEM )
  1713. *code = LDAP_SCHERR_BADNAME;
  1714. *errp = ss;
  1715. ldap_matchingruleuse_free(mru);
  1716. return NULL;
  1717. }
  1718. } else if ( !strcasecmp(sval,"DESC") ) {
  1719. LDAP_FREE(sval);
  1720. if ( seen_desc ) {
  1721. *code = LDAP_SCHERR_DUPOPT;
  1722. *errp = ss;
  1723. ldap_matchingruleuse_free(mru);
  1724. return(NULL);
  1725. }
  1726. seen_desc = 1;
  1727. parse_whsp(&ss);
  1728. kind = get_token(&ss,&sval);
  1729. if ( kind != TK_QDSTRING ) {
  1730. *code = LDAP_SCHERR_UNEXPTOKEN;
  1731. *errp = ss;
  1732. LDAP_FREE(sval);
  1733. ldap_matchingruleuse_free(mru);
  1734. return NULL;
  1735. }
  1736. mru->mru_desc = sval;
  1737. parse_whsp(&ss);
  1738. } else if ( !strcasecmp(sval,"OBSOLETE") ) {
  1739. LDAP_FREE(sval);
  1740. if ( seen_obsolete ) {
  1741. *code = LDAP_SCHERR_DUPOPT;
  1742. *errp = ss;
  1743. ldap_matchingruleuse_free(mru);
  1744. return(NULL);
  1745. }
  1746. seen_obsolete = 1;
  1747. mru->mru_obsolete = LDAP_SCHEMA_YES;
  1748. parse_whsp(&ss);
  1749. } else if ( !strcasecmp(sval,"APPLIES") ) {
  1750. LDAP_FREE(sval);
  1751. if ( seen_applies ) {
  1752. *code = LDAP_SCHERR_DUPOPT;
  1753. *errp = ss;
  1754. ldap_matchingruleuse_free(mru);
  1755. return(NULL);
  1756. }
  1757. seen_applies = 1;
  1758. mru->mru_applies_oids = parse_oids(&ss,
  1759. code,
  1760. flags);
  1761. if ( !mru->mru_applies_oids && *code != LDAP_SUCCESS ) {
  1762. *errp = ss;
  1763. ldap_matchingruleuse_free(mru);
  1764. return NULL;
  1765. }
  1766. } else if ( sval[0] == 'X' && sval[1] == '-' ) {
  1767. /* Should be parse_qdstrings */
  1768. ext_vals = parse_qdescrs(&ss, code);
  1769. if ( !ext_vals ) {
  1770. *errp = ss;
  1771. ldap_matchingruleuse_free(mru);
  1772. return NULL;
  1773. }
  1774. if ( add_extension(&mru->mru_extensions,
  1775. sval, ext_vals) ) {
  1776. *code = LDAP_SCHERR_OUTOFMEM;
  1777. *errp = ss;
  1778. LDAP_FREE(sval);
  1779. ldap_matchingruleuse_free(mru);
  1780. return NULL;
  1781. }
  1782. } else {
  1783. *code = LDAP_SCHERR_UNEXPTOKEN;
  1784. *errp = ss;
  1785. LDAP_FREE(sval);
  1786. ldap_matchingruleuse_free(mru);
  1787. return NULL;
  1788. }
  1789. break;
  1790. default:
  1791. *code = LDAP_SCHERR_UNEXPTOKEN;
  1792. *errp = ss;
  1793. LDAP_FREE(sval);
  1794. ldap_matchingruleuse_free(mru);
  1795. return NULL;
  1796. }
  1797. }
  1798. }
  1799. void
  1800. ldap_attributetype_free(LDAPAttributeType * at)
  1801. {
  1802. if (!at) return;
  1803. LDAP_FREE(at->at_oid);
  1804. if (at->at_names) LDAP_VFREE(at->at_names);
  1805. if (at->at_desc) LDAP_FREE(at->at_desc);
  1806. if (at->at_sup_oid) LDAP_FREE(at->at_sup_oid);
  1807. if (at->at_equality_oid) LDAP_FREE(at->at_equality_oid);
  1808. if (at->at_ordering_oid) LDAP_FREE(at->at_ordering_oid);
  1809. if (at->at_substr_oid) LDAP_FREE(at->at_substr_oid);
  1810. if (at->at_syntax_oid) LDAP_FREE(at->at_syntax_oid);
  1811. free_extensions(at->at_extensions);
  1812. LDAP_FREE(at);
  1813. }
  1814. LDAPAttributeType *
  1815. ldap_str2attributetype( LDAP_CONST char * s,
  1816. int * code,
  1817. LDAP_CONST char ** errp,
  1818. LDAP_CONST unsigned flags )
  1819. {
  1820. tk_t kind;
  1821. const char * ss = s;
  1822. char * sval;
  1823. int seen_name = 0;
  1824. int seen_desc = 0;
  1825. int seen_obsolete = 0;
  1826. int seen_sup = 0;
  1827. int seen_equality = 0;
  1828. int seen_ordering = 0;
  1829. int seen_substr = 0;
  1830. int seen_syntax = 0;
  1831. int seen_usage = 0;
  1832. LDAPAttributeType * at;
  1833. char ** ext_vals;
  1834. const char * savepos;
  1835. if ( !s ) {
  1836. *code = LDAP_SCHERR_EMPTY;
  1837. *errp = "";
  1838. return NULL;
  1839. }
  1840. *errp = s;
  1841. at = LDAP_CALLOC(1,sizeof(LDAPAttributeType));
  1842. if ( !at ) {
  1843. *code = LDAP_SCHERR_OUTOFMEM;
  1844. return NULL;
  1845. }
  1846. kind = get_token(&ss,&sval);
  1847. if ( kind != TK_LEFTPAREN ) {
  1848. *code = LDAP_SCHERR_NOLEFTPAREN;
  1849. LDAP_FREE(sval);
  1850. ldap_attributetype_free(at);
  1851. return NULL;
  1852. }
  1853. /*
  1854. * Definitions MUST begin with an OID in the numericoid format.
  1855. * However, this routine is used by clients to parse the response
  1856. * from servers and very well known servers will provide an OID
  1857. * in the wrong format or even no OID at all. We do our best to
  1858. * extract info from those servers.
  1859. */
  1860. parse_whsp(&ss);
  1861. savepos = ss;
  1862. at->at_oid = ldap_int_parse_numericoid(&ss,code,0);
  1863. if ( !at->at_oid ) {
  1864. if ( ( flags & ( LDAP_SCHEMA_ALLOW_NO_OID
  1865. | LDAP_SCHEMA_ALLOW_OID_MACRO ) )
  1866. && (ss == savepos) )
  1867. {
  1868. /* Backtracking */
  1869. ss = savepos;
  1870. kind = get_token(&ss,&sval);
  1871. if ( kind == TK_BAREWORD ) {
  1872. if ( !strcasecmp(sval, "NAME") ||
  1873. !strcasecmp(sval, "DESC") ||
  1874. !strcasecmp(sval, "OBSOLETE") ||
  1875. !strcasecmp(sval, "SUP") ||
  1876. !strcasecmp(sval, "EQUALITY") ||
  1877. !strcasecmp(sval, "ORDERING") ||
  1878. !strcasecmp(sval, "SUBSTR") ||
  1879. !strcasecmp(sval, "SYNTAX") ||
  1880. !strcasecmp(sval, "SINGLE-VALUE") ||
  1881. !strcasecmp(sval, "COLLECTIVE") ||
  1882. !strcasecmp(sval, "NO-USER-MODIFICATION") ||
  1883. !strcasecmp(sval, "USAGE") ||
  1884. !strncasecmp(sval, "X-", 2) )
  1885. {
  1886. /* Missing OID, backtrack */
  1887. ss = savepos;
  1888. } else if ( flags
  1889. & LDAP_SCHEMA_ALLOW_OID_MACRO)
  1890. {
  1891. /* Non-numerical OID ... */
  1892. int len = ss-savepos;
  1893. at->at_oid = LDAP_MALLOC(len+1);
  1894. if ( !at->at_oid ) {
  1895. ldap_attributetype_free(at);
  1896. return NULL;
  1897. }
  1898. strncpy(at->at_oid, savepos, len);
  1899. at->at_oid[len] = 0;
  1900. }
  1901. }
  1902. LDAP_FREE(sval);
  1903. } else {
  1904. *errp = ss;
  1905. ldap_attributetype_free(at);
  1906. return NULL;
  1907. }
  1908. }
  1909. parse_whsp(&ss);
  1910. /*
  1911. * Beyond this point we will be liberal and accept the items
  1912. * in any order.
  1913. */
  1914. while (1) {
  1915. kind = get_token(&ss,&sval);
  1916. switch (kind) {
  1917. case TK_EOS:
  1918. *code = LDAP_SCHERR_NORIGHTPAREN;
  1919. *errp = EndOfInput;
  1920. ldap_attributetype_free(at);
  1921. return NULL;
  1922. case TK_RIGHTPAREN:
  1923. return at;
  1924. case TK_BAREWORD:
  1925. if ( !strcasecmp(sval,"NAME") ) {
  1926. LDAP_FREE(sval);
  1927. if ( seen_name ) {
  1928. *code = LDAP_SCHERR_DUPOPT;
  1929. *errp = ss;
  1930. ldap_attributetype_free(at);
  1931. return(NULL);
  1932. }
  1933. seen_name = 1;
  1934. at->at_names = parse_qdescrs(&ss,code);
  1935. if ( !at->at_names ) {
  1936. if ( *code != LDAP_SCHERR_OUTOFMEM )
  1937. *code = LDAP_SCHERR_BADNAME;
  1938. *errp = ss;
  1939. ldap_attributetype_free(at);
  1940. return NULL;
  1941. }
  1942. } else if ( !strcasecmp(sval,"DESC") ) {
  1943. LDAP_FREE(sval);
  1944. if ( seen_desc ) {
  1945. *code = LDAP_SCHERR_DUPOPT;
  1946. *errp = ss;
  1947. ldap_attributetype_free(at);
  1948. return(NULL);
  1949. }
  1950. seen_desc = 1;
  1951. parse_whsp(&ss);
  1952. kind = get_token(&ss,&sval);
  1953. if ( kind != TK_QDSTRING ) {
  1954. *code = LDAP_SCHERR_UNEXPTOKEN;
  1955. *errp = ss;
  1956. LDAP_FREE(sval);
  1957. ldap_attributetype_free(at);
  1958. return NULL;
  1959. }
  1960. at->at_desc = sval;
  1961. parse_whsp(&ss);
  1962. } else if ( !strcasecmp(sval,"OBSOLETE") ) {
  1963. LDAP_FREE(sval);
  1964. if ( seen_obsolete ) {
  1965. *code = LDAP_SCHERR_DUPOPT;
  1966. *errp = ss;
  1967. ldap_attributetype_free(at);
  1968. return(NULL);
  1969. }
  1970. seen_obsolete = 1;
  1971. at->at_obsolete = LDAP_SCHEMA_YES;
  1972. parse_whsp(&ss);
  1973. } else if ( !strcasecmp(sval,"SUP") ) {
  1974. LDAP_FREE(sval);
  1975. if ( seen_sup ) {
  1976. *code = LDAP_SCHERR_DUPOPT;
  1977. *errp = ss;
  1978. ldap_attributetype_free(at);
  1979. return(NULL);
  1980. }
  1981. seen_sup = 1;
  1982. at->at_sup_oid = parse_woid(&ss,code);
  1983. if ( !at->at_sup_oid ) {
  1984. *errp = ss;
  1985. ldap_attributetype_free(at);
  1986. return NULL;
  1987. }
  1988. } else if ( !strcasecmp(sval,"EQUALITY") ) {
  1989. LDAP_FREE(sval);
  1990. if ( seen_equality ) {
  1991. *code = LDAP_SCHERR_DUPOPT;
  1992. *errp = ss;
  1993. ldap_attributetype_free(at);
  1994. return(NULL);
  1995. }
  1996. seen_equality = 1;
  1997. at->at_equality_oid = parse_woid(&ss,code);
  1998. if ( !at->at_equality_oid ) {
  1999. *errp = ss;
  2000. ldap_attributetype_free(at);
  2001. return NULL;
  2002. }
  2003. } else if ( !strcasecmp(sval,"ORDERING") ) {
  2004. LDAP_FREE(sval);
  2005. if ( seen_ordering ) {
  2006. *code = LDAP_SCHERR_DUPOPT;
  2007. *errp = ss;
  2008. ldap_attributetype_free(at);
  2009. return(NULL);
  2010. }
  2011. seen_ordering = 1;
  2012. at->at_ordering_oid = parse_woid(&ss,code);
  2013. if ( !at->at_ordering_oid ) {
  2014. *errp = ss;
  2015. ldap_attributetype_free(at);
  2016. return NULL;
  2017. }
  2018. } else if ( !strcasecmp(sval,"SUBSTR") ) {
  2019. LDAP_FREE(sval);
  2020. if ( seen_substr ) {
  2021. *code = LDAP_SCHERR_DUPOPT;
  2022. *errp = ss;
  2023. ldap_attributetype_free(at);
  2024. return(NULL);
  2025. }
  2026. seen_substr = 1;
  2027. at->at_substr_oid = parse_woid(&ss,code);
  2028. if ( !at->at_substr_oid ) {
  2029. *errp = ss;
  2030. ldap_attributetype_free(at);
  2031. return NULL;
  2032. }
  2033. } else if ( !strcasecmp(sval,"SYNTAX") ) {
  2034. LDAP_FREE(sval);
  2035. if ( seen_syntax ) {
  2036. *code = LDAP_SCHERR_DUPOPT;
  2037. *errp = ss;
  2038. ldap_attributetype_free(at);
  2039. return(NULL);
  2040. }
  2041. seen_syntax = 1;
  2042. parse_whsp(&ss);
  2043. savepos = ss;
  2044. at->at_syntax_oid =
  2045. parse_noidlen(&ss,
  2046. code,
  2047. &at->at_syntax_len,
  2048. flags);
  2049. if ( !at->at_syntax_oid ) {
  2050. if ( flags & LDAP_SCHEMA_ALLOW_OID_MACRO ) {
  2051. kind = get_token(&ss,&sval);
  2052. if (kind == TK_BAREWORD)
  2053. {
  2054. char *sp = strchr(sval, '{');
  2055. at->at_syntax_oid = sval;
  2056. if (sp)
  2057. {
  2058. *sp++ = 0;
  2059. at->at_syntax_len = atoi(sp);
  2060. while ( LDAP_DIGIT(*sp) )
  2061. sp++;
  2062. if ( *sp != '}' ) {
  2063. *code = LDAP_SCHERR_UNEXPTOKEN;
  2064. *errp = ss;
  2065. ldap_attributetype_free(at);
  2066. return NULL;
  2067. }
  2068. }
  2069. }
  2070. } else {
  2071. *errp = ss;
  2072. ldap_attributetype_free(at);
  2073. return NULL;
  2074. }
  2075. }
  2076. parse_whsp(&ss);
  2077. } else if ( !strcasecmp(sval,"SINGLE-VALUE") ) {
  2078. LDAP_FREE(sval);
  2079. if ( at->at_single_value ) {
  2080. *code = LDAP_SCHERR_DUPOPT;
  2081. *errp = ss;
  2082. ldap_attributetype_free(at);
  2083. return(NULL);
  2084. }
  2085. at->at_single_value = LDAP_SCHEMA_YES;
  2086. parse_whsp(&ss);
  2087. } else if ( !strcasecmp(sval,"COLLECTIVE") ) {
  2088. LDAP_FREE(sval);
  2089. if ( at->at_collective ) {
  2090. *code = LDAP_SCHERR_DUPOPT;
  2091. *errp = ss;
  2092. ldap_attributetype_free(at);
  2093. return(NULL);
  2094. }
  2095. at->at_collective = LDAP_SCHEMA_YES;
  2096. parse_whsp(&ss);
  2097. } else if ( !strcasecmp(sval,"NO-USER-MODIFICATION") ) {
  2098. LDAP_FREE(sval);
  2099. if ( at->at_no_user_mod ) {
  2100. *code = LDAP_SCHERR_DUPOPT;
  2101. *errp = ss;
  2102. ldap_attributetype_free(at);
  2103. return(NULL);
  2104. }
  2105. at->at_no_user_mod = LDAP_SCHEMA_YES;
  2106. parse_whsp(&ss);
  2107. } else if ( !strcasecmp(sval,"USAGE") ) {
  2108. LDAP_FREE(sval);
  2109. if ( seen_usage ) {
  2110. *code = LDAP_SCHERR_DUPOPT;
  2111. *errp = ss;
  2112. ldap_attributetype_free(at);
  2113. return(NULL);
  2114. }
  2115. seen_usage = 1;
  2116. parse_whsp(&ss);
  2117. kind = get_token(&ss,&sval);
  2118. if ( kind != TK_BAREWORD ) {
  2119. *code = LDAP_SCHERR_UNEXPTOKEN;
  2120. *errp = ss;
  2121. LDAP_FREE(sval);
  2122. ldap_attributetype_free(at);
  2123. return NULL;
  2124. }
  2125. if ( !strcasecmp(sval,"userApplications") )
  2126. at->at_usage =
  2127. LDAP_SCHEMA_USER_APPLICATIONS;
  2128. else if ( !strcasecmp(sval,"directoryOperation") )
  2129. at->at_usage =
  2130. LDAP_SCHEMA_DIRECTORY_OPERATION;
  2131. else if ( !strcasecmp(sval,"distributedOperation") )
  2132. at->at_usage =
  2133. LDAP_SCHEMA_DISTRIBUTED_OPERATION;
  2134. else if ( !strcasecmp(sval,"dSAOperation") )
  2135. at->at_usage =
  2136. LDAP_SCHEMA_DSA_OPERATION;
  2137. else {
  2138. *code = LDAP_SCHERR_UNEXPTOKEN;
  2139. *errp = ss;
  2140. LDAP_FREE(sval);
  2141. ldap_attributetype_free(at);
  2142. return NULL;
  2143. }
  2144. LDAP_FREE(sval);
  2145. parse_whsp(&ss);
  2146. } else if ( sval[0] == 'X' && sval[1] == '-' ) {
  2147. /* Should be parse_qdstrings */
  2148. ext_vals = parse_qdescrs(&ss, code);
  2149. if ( !ext_vals ) {
  2150. *errp = ss;
  2151. ldap_attributetype_free(at);
  2152. return NULL;
  2153. }
  2154. if ( add_extension(&at->at_extensions,
  2155. sval, ext_vals) ) {
  2156. *code = LDAP_SCHERR_OUTOFMEM;
  2157. *errp = ss;
  2158. LDAP_FREE(sval);
  2159. ldap_attributetype_free(at);
  2160. return NULL;
  2161. }
  2162. } else {
  2163. *code = LDAP_SCHERR_UNEXPTOKEN;
  2164. *errp = ss;
  2165. LDAP_FREE(sval);
  2166. ldap_attributetype_free(at);
  2167. return NULL;
  2168. }
  2169. break;
  2170. default:
  2171. *code = LDAP_SCHERR_UNEXPTOKEN;
  2172. *errp = ss;
  2173. LDAP_FREE(sval);
  2174. ldap_attributetype_free(at);
  2175. return NULL;
  2176. }
  2177. }
  2178. }
  2179. void
  2180. ldap_objectclass_free(LDAPObjectClass * oc)
  2181. {
  2182. if (!oc) return;
  2183. LDAP_FREE(oc->oc_oid);
  2184. if (oc->oc_names) LDAP_VFREE(oc->oc_names);
  2185. if (oc->oc_desc) LDAP_FREE(oc->oc_desc);
  2186. if (oc->oc_sup_oids) LDAP_VFREE(oc->oc_sup_oids);
  2187. if (oc->oc_at_oids_must) LDAP_VFREE(oc->oc_at_oids_must);
  2188. if (oc->oc_at_oids_may) LDAP_VFREE(oc->oc_at_oids_may);
  2189. free_extensions(oc->oc_extensions);
  2190. LDAP_FREE(oc);
  2191. }
  2192. LDAPObjectClass *
  2193. ldap_str2objectclass( LDAP_CONST char * s,
  2194. int * code,
  2195. LDAP_CONST char ** errp,
  2196. LDAP_CONST unsigned flags )
  2197. {
  2198. tk_t kind;
  2199. const char * ss = s;
  2200. char * sval;
  2201. int seen_name = 0;
  2202. int seen_desc = 0;
  2203. int seen_obsolete = 0;
  2204. int seen_sup = 0;
  2205. int seen_kind = 0;
  2206. int seen_must = 0;
  2207. int seen_may = 0;
  2208. LDAPObjectClass * oc;
  2209. char ** ext_vals;
  2210. const char * savepos;
  2211. if ( !s ) {
  2212. *code = LDAP_SCHERR_EMPTY;
  2213. *errp = "";
  2214. return NULL;
  2215. }
  2216. *errp = s;
  2217. oc = LDAP_CALLOC(1,sizeof(LDAPObjectClass));
  2218. if ( !oc ) {
  2219. *code = LDAP_SCHERR_OUTOFMEM;
  2220. return NULL;
  2221. }
  2222. oc->oc_kind = LDAP_SCHEMA_STRUCTURAL;
  2223. kind = get_token(&ss,&sval);
  2224. if ( kind != TK_LEFTPAREN ) {
  2225. *code = LDAP_SCHERR_NOLEFTPAREN;
  2226. LDAP_FREE(sval);
  2227. ldap_objectclass_free(oc);
  2228. return NULL;
  2229. }
  2230. /*
  2231. * Definitions MUST begin with an OID in the numericoid format.
  2232. * However, this routine is used by clients to parse the response
  2233. * from servers and very well known servers will provide an OID
  2234. * in the wrong format or even no OID at all. We do our best to
  2235. * extract info from those servers.
  2236. */
  2237. parse_whsp(&ss);
  2238. savepos = ss;
  2239. oc->oc_oid = ldap_int_parse_numericoid(&ss,code,0);
  2240. if ( !oc->oc_oid ) {
  2241. if ( (flags & LDAP_SCHEMA_ALLOW_ALL) && (ss == savepos) ) {
  2242. /* Backtracking */
  2243. ss = savepos;
  2244. kind = get_token(&ss,&sval);
  2245. if ( kind == TK_BAREWORD ) {
  2246. if ( !strcasecmp(sval, "NAME") ||
  2247. !strcasecmp(sval, "DESC") ||
  2248. !strcasecmp(sval, "OBSOLETE") ||
  2249. !strcasecmp(sval, "SUP") ||
  2250. !strcasecmp(sval, "ABSTRACT") ||
  2251. !strcasecmp(sval, "STRUCTURAL") ||
  2252. !strcasecmp(sval, "AUXILIARY") ||
  2253. !strcasecmp(sval, "MUST") ||
  2254. !strcasecmp(sval, "MAY") ||
  2255. !strncasecmp(sval, "X-", 2) ) {
  2256. /* Missing OID, backtrack */
  2257. ss = savepos;
  2258. } else if ( flags &
  2259. LDAP_SCHEMA_ALLOW_OID_MACRO ) {
  2260. /* Non-numerical OID, ignore */
  2261. int len = ss-savepos;
  2262. oc->oc_oid = LDAP_MALLOC(len+1);
  2263. if ( !oc->oc_oid ) {
  2264. ldap_objectclass_free(oc);
  2265. return NULL;
  2266. }
  2267. strncpy(oc->oc_oid, savepos, len);
  2268. oc->oc_oid[len] = 0;
  2269. }
  2270. }
  2271. LDAP_FREE(sval);
  2272. *code = 0;
  2273. } else {
  2274. *errp = ss;
  2275. ldap_objectclass_free(oc);
  2276. return NULL;
  2277. }
  2278. }
  2279. parse_whsp(&ss);
  2280. /*
  2281. * Beyond this point we will be liberal an accept the items
  2282. * in any order.
  2283. */
  2284. while (1) {
  2285. kind = get_token(&ss,&sval);
  2286. switch (kind) {
  2287. case TK_EOS:
  2288. *code = LDAP_SCHERR_NORIGHTPAREN;
  2289. *errp = EndOfInput;
  2290. ldap_objectclass_free(oc);
  2291. return NULL;
  2292. case TK_RIGHTPAREN:
  2293. return oc;
  2294. case TK_BAREWORD:
  2295. if ( !strcasecmp(sval,"NAME") ) {
  2296. LDAP_FREE(sval);
  2297. if ( seen_name ) {
  2298. *code = LDAP_SCHERR_DUPOPT;
  2299. *errp = ss;
  2300. ldap_objectclass_free(oc);
  2301. return(NULL);
  2302. }
  2303. seen_name = 1;
  2304. oc->oc_names = parse_qdescrs(&ss,code);
  2305. if ( !oc->oc_names ) {
  2306. if ( *code != LDAP_SCHERR_OUTOFMEM )
  2307. *code = LDAP_SCHERR_BADNAME;
  2308. *errp = ss;
  2309. ldap_objectclass_free(oc);
  2310. return NULL;
  2311. }
  2312. } else if ( !strcasecmp(sval,"DESC") ) {
  2313. LDAP_FREE(sval);
  2314. if ( seen_desc ) {
  2315. *code = LDAP_SCHERR_DUPOPT;
  2316. *errp = ss;
  2317. ldap_objectclass_free(oc);
  2318. return(NULL);
  2319. }
  2320. seen_desc = 1;
  2321. parse_whsp(&ss);
  2322. kind = get_token(&ss,&sval);
  2323. if ( kind != TK_QDSTRING ) {
  2324. *code = LDAP_SCHERR_UNEXPTOKEN;
  2325. *errp = ss;
  2326. LDAP_FREE(sval);
  2327. ldap_objectclass_free(oc);
  2328. return NULL;
  2329. }
  2330. oc->oc_desc = sval;
  2331. parse_whsp(&ss);
  2332. } else if ( !strcasecmp(sval,"OBSOLETE") ) {
  2333. LDAP_FREE(sval);
  2334. if ( seen_obsolete ) {
  2335. *code = LDAP_SCHERR_DUPOPT;
  2336. *errp = ss;
  2337. ldap_objectclass_free(oc);
  2338. return(NULL);
  2339. }
  2340. seen_obsolete = 1;
  2341. oc->oc_obsolete = LDAP_SCHEMA_YES;
  2342. parse_whsp(&ss);
  2343. } else if ( !strcasecmp(sval,"SUP") ) {
  2344. LDAP_FREE(sval);
  2345. if ( seen_sup ) {
  2346. *code = LDAP_SCHERR_DUPOPT;
  2347. *errp = ss;
  2348. ldap_objectclass_free(oc);
  2349. return(NULL);
  2350. }
  2351. seen_sup = 1;
  2352. oc->oc_sup_oids = parse_oids(&ss,
  2353. code,
  2354. flags);
  2355. if ( !oc->oc_sup_oids && *code != LDAP_SUCCESS ) {
  2356. *errp = ss;
  2357. ldap_objectclass_free(oc);
  2358. return NULL;
  2359. }
  2360. *code = 0;
  2361. } else if ( !strcasecmp(sval,"ABSTRACT") ) {
  2362. LDAP_FREE(sval);
  2363. if ( seen_kind ) {
  2364. *code = LDAP_SCHERR_DUPOPT;
  2365. *errp = ss;
  2366. ldap_objectclass_free(oc);
  2367. return(NULL);
  2368. }
  2369. seen_kind = 1;
  2370. oc->oc_kind = LDAP_SCHEMA_ABSTRACT;
  2371. parse_whsp(&ss);
  2372. } else if ( !strcasecmp(sval,"STRUCTURAL") ) {
  2373. LDAP_FREE(sval);
  2374. if ( seen_kind ) {
  2375. *code = LDAP_SCHERR_DUPOPT;
  2376. *errp = ss;
  2377. ldap_objectclass_free(oc);
  2378. return(NULL);
  2379. }
  2380. seen_kind = 1;
  2381. oc->oc_kind = LDAP_SCHEMA_STRUCTURAL;
  2382. parse_whsp(&ss);
  2383. } else if ( !strcasecmp(sval,"AUXILIARY") ) {
  2384. LDAP_FREE(sval);
  2385. if ( seen_kind ) {
  2386. *code = LDAP_SCHERR_DUPOPT;
  2387. *errp = ss;
  2388. ldap_objectclass_free(oc);
  2389. return(NULL);
  2390. }
  2391. seen_kind = 1;
  2392. oc->oc_kind = LDAP_SCHEMA_AUXILIARY;
  2393. parse_whsp(&ss);
  2394. } else if ( !strcasecmp(sval,"MUST") ) {
  2395. LDAP_FREE(sval);
  2396. if ( seen_must ) {
  2397. *code = LDAP_SCHERR_DUPOPT;
  2398. *errp = ss;
  2399. ldap_objectclass_free(oc);
  2400. return(NULL);
  2401. }
  2402. seen_must = 1;
  2403. oc->oc_at_oids_must = parse_oids(&ss,code,0);
  2404. if ( !oc->oc_at_oids_must && *code != LDAP_SUCCESS ) {
  2405. *errp = ss;
  2406. ldap_objectclass_free(oc);
  2407. return NULL;
  2408. }
  2409. *code = 0;
  2410. parse_whsp(&ss);
  2411. } else if ( !strcasecmp(sval,"MAY") ) {
  2412. LDAP_FREE(sval);
  2413. if ( seen_may ) {
  2414. *code = LDAP_SCHERR_DUPOPT;
  2415. *errp = ss;
  2416. ldap_objectclass_free(oc);
  2417. return(NULL);
  2418. }
  2419. seen_may = 1;
  2420. oc->oc_at_oids_may = parse_oids(&ss,code,0);
  2421. if ( !oc->oc_at_oids_may && *code != LDAP_SUCCESS ) {
  2422. *errp = ss;
  2423. ldap_objectclass_free(oc);
  2424. return NULL;
  2425. }
  2426. *code = 0;
  2427. parse_whsp(&ss);
  2428. } else if ( sval[0] == 'X' && sval[1] == '-' ) {
  2429. /* Should be parse_qdstrings */
  2430. ext_vals = parse_qdescrs(&ss, code);
  2431. *code = 0;
  2432. if ( !ext_vals ) {
  2433. *errp = ss;
  2434. ldap_objectclass_free(oc);
  2435. return NULL;
  2436. }
  2437. if ( add_extension(&oc->oc_extensions,
  2438. sval, ext_vals) ) {
  2439. *code = LDAP_SCHERR_OUTOFMEM;
  2440. *errp = ss;
  2441. LDAP_FREE(sval);
  2442. ldap_objectclass_free(oc);
  2443. return NULL;
  2444. }
  2445. } else {
  2446. *code = LDAP_SCHERR_UNEXPTOKEN;
  2447. *errp = ss;
  2448. LDAP_FREE(sval);
  2449. ldap_objectclass_free(oc);
  2450. return NULL;
  2451. }
  2452. break;
  2453. default:
  2454. *code = LDAP_SCHERR_UNEXPTOKEN;
  2455. *errp = ss;
  2456. LDAP_FREE(sval);
  2457. ldap_objectclass_free(oc);
  2458. return NULL;
  2459. }
  2460. }
  2461. }
  2462. void
  2463. ldap_contentrule_free(LDAPContentRule * cr)
  2464. {
  2465. if (!cr) return;
  2466. LDAP_FREE(cr->cr_oid);
  2467. if (cr->cr_names) LDAP_VFREE(cr->cr_names);
  2468. if (cr->cr_desc) LDAP_FREE(cr->cr_desc);
  2469. if (cr->cr_oc_oids_aux) LDAP_VFREE(cr->cr_oc_oids_aux);
  2470. if (cr->cr_at_oids_must) LDAP_VFREE(cr->cr_at_oids_must);
  2471. if (cr->cr_at_oids_may) LDAP_VFREE(cr->cr_at_oids_may);
  2472. if (cr->cr_at_oids_not) LDAP_VFREE(cr->cr_at_oids_not);
  2473. free_extensions(cr->cr_extensions);
  2474. LDAP_FREE(cr);
  2475. }
  2476. LDAPContentRule *
  2477. ldap_str2contentrule( LDAP_CONST char * s,
  2478. int * code,
  2479. LDAP_CONST char ** errp,
  2480. LDAP_CONST unsigned flags )
  2481. {
  2482. tk_t kind;
  2483. const char * ss = s;
  2484. char * sval;
  2485. int seen_name = 0;
  2486. int seen_desc = 0;
  2487. int seen_obsolete = 0;
  2488. int seen_aux = 0;
  2489. int seen_must = 0;
  2490. int seen_may = 0;
  2491. int seen_not = 0;
  2492. LDAPContentRule * cr;
  2493. char ** ext_vals;
  2494. const char * savepos;
  2495. if ( !s ) {
  2496. *code = LDAP_SCHERR_EMPTY;
  2497. *errp = "";
  2498. return NULL;
  2499. }
  2500. *errp = s;
  2501. cr = LDAP_CALLOC(1,sizeof(LDAPContentRule));
  2502. if ( !cr ) {
  2503. *code = LDAP_SCHERR_OUTOFMEM;
  2504. return NULL;
  2505. }
  2506. kind = get_token(&ss,&sval);
  2507. if ( kind != TK_LEFTPAREN ) {
  2508. *code = LDAP_SCHERR_NOLEFTPAREN;
  2509. LDAP_FREE(sval);
  2510. ldap_contentrule_free(cr);
  2511. return NULL;
  2512. }
  2513. /*
  2514. * Definitions MUST begin with an OID in the numericoid format.
  2515. */
  2516. parse_whsp(&ss);
  2517. savepos = ss;
  2518. cr->cr_oid = ldap_int_parse_numericoid(&ss,code,0);
  2519. if ( !cr->cr_oid ) {
  2520. if ( (flags & LDAP_SCHEMA_ALLOW_ALL) && (ss == savepos) ) {
  2521. /* Backtracking */
  2522. ss = savepos;
  2523. kind = get_token(&ss,&sval);
  2524. if ( kind == TK_BAREWORD ) {
  2525. if ( !strcasecmp(sval, "NAME") ||
  2526. !strcasecmp(sval, "DESC") ||
  2527. !strcasecmp(sval, "OBSOLETE") ||
  2528. !strcasecmp(sval, "AUX") ||
  2529. !strcasecmp(sval, "MUST") ||
  2530. !strcasecmp(sval, "MAY") ||
  2531. !strcasecmp(sval, "NOT") ||
  2532. !strncasecmp(sval, "X-", 2) ) {
  2533. /* Missing OID, backtrack */
  2534. ss = savepos;
  2535. } else if ( flags &
  2536. LDAP_SCHEMA_ALLOW_OID_MACRO ) {
  2537. /* Non-numerical OID, ignore */
  2538. int len = ss-savepos;
  2539. cr->cr_oid = LDAP_MALLOC(len+1);
  2540. if ( !cr->cr_oid ) {
  2541. ldap_contentrule_free(cr);
  2542. return NULL;
  2543. }
  2544. strncpy(cr->cr_oid, savepos, len);
  2545. cr->cr_oid[len] = 0;
  2546. }
  2547. }
  2548. LDAP_FREE(sval);
  2549. } else {
  2550. *errp = ss;
  2551. ldap_contentrule_free(cr);
  2552. return NULL;
  2553. }
  2554. }
  2555. parse_whsp(&ss);
  2556. /*
  2557. * Beyond this point we will be liberal an accept the items
  2558. * in any order.
  2559. */
  2560. while (1) {
  2561. kind = get_token(&ss,&sval);
  2562. switch (kind) {
  2563. case TK_EOS:
  2564. *code = LDAP_SCHERR_NORIGHTPAREN;
  2565. *errp = EndOfInput;
  2566. ldap_contentrule_free(cr);
  2567. return NULL;
  2568. case TK_RIGHTPAREN:
  2569. return cr;
  2570. case TK_BAREWORD:
  2571. if ( !strcasecmp(sval,"NAME") ) {
  2572. LDAP_FREE(sval);
  2573. if ( seen_name ) {
  2574. *code = LDAP_SCHERR_DUPOPT;
  2575. *errp = ss;
  2576. ldap_contentrule_free(cr);
  2577. return(NULL);
  2578. }
  2579. seen_name = 1;
  2580. cr->cr_names = parse_qdescrs(&ss,code);
  2581. if ( !cr->cr_names ) {
  2582. if ( *code != LDAP_SCHERR_OUTOFMEM )
  2583. *code = LDAP_SCHERR_BADNAME;
  2584. *errp = ss;
  2585. ldap_contentrule_free(cr);
  2586. return NULL;
  2587. }
  2588. } else if ( !strcasecmp(sval,"DESC") ) {
  2589. LDAP_FREE(sval);
  2590. if ( seen_desc ) {
  2591. *code = LDAP_SCHERR_DUPOPT;
  2592. *errp = ss;
  2593. ldap_contentrule_free(cr);
  2594. return(NULL);
  2595. }
  2596. seen_desc = 1;
  2597. parse_whsp(&ss);
  2598. kind = get_token(&ss,&sval);
  2599. if ( kind != TK_QDSTRING ) {
  2600. *code = LDAP_SCHERR_UNEXPTOKEN;
  2601. *errp = ss;
  2602. LDAP_FREE(sval);
  2603. ldap_contentrule_free(cr);
  2604. return NULL;
  2605. }
  2606. cr->cr_desc = sval;
  2607. parse_whsp(&ss);
  2608. } else if ( !strcasecmp(sval,"OBSOLETE") ) {
  2609. LDAP_FREE(sval);
  2610. if ( seen_obsolete ) {
  2611. *code = LDAP_SCHERR_DUPOPT;
  2612. *errp = ss;
  2613. ldap_contentrule_free(cr);
  2614. return(NULL);
  2615. }
  2616. seen_obsolete = 1;
  2617. cr->cr_obsolete = LDAP_SCHEMA_YES;
  2618. parse_whsp(&ss);
  2619. } else if ( !strcasecmp(sval,"AUX") ) {
  2620. LDAP_FREE(sval);
  2621. if ( seen_aux ) {
  2622. *code = LDAP_SCHERR_DUPOPT;
  2623. *errp = ss;
  2624. ldap_contentrule_free(cr);
  2625. return(NULL);
  2626. }
  2627. seen_aux = 1;
  2628. cr->cr_oc_oids_aux = parse_oids(&ss,code,0);
  2629. if ( !cr->cr_oc_oids_aux ) {
  2630. *errp = ss;
  2631. ldap_contentrule_free(cr);
  2632. return NULL;
  2633. }
  2634. parse_whsp(&ss);
  2635. } else if ( !strcasecmp(sval,"MUST") ) {
  2636. LDAP_FREE(sval);
  2637. if ( seen_must ) {
  2638. *code = LDAP_SCHERR_DUPOPT;
  2639. *errp = ss;
  2640. ldap_contentrule_free(cr);
  2641. return(NULL);
  2642. }
  2643. seen_must = 1;
  2644. cr->cr_at_oids_must = parse_oids(&ss,code,0);
  2645. if ( !cr->cr_at_oids_must && *code != LDAP_SUCCESS ) {
  2646. *errp = ss;
  2647. ldap_contentrule_free(cr);
  2648. return NULL;
  2649. }
  2650. parse_whsp(&ss);
  2651. } else if ( !strcasecmp(sval,"MAY") ) {
  2652. LDAP_FREE(sval);
  2653. if ( seen_may ) {
  2654. *code = LDAP_SCHERR_DUPOPT;
  2655. *errp = ss;
  2656. ldap_contentrule_free(cr);
  2657. return(NULL);
  2658. }
  2659. seen_may = 1;
  2660. cr->cr_at_oids_may = parse_oids(&ss,code,0);
  2661. if ( !cr->cr_at_oids_may && *code != LDAP_SUCCESS ) {
  2662. *errp = ss;
  2663. ldap_contentrule_free(cr);
  2664. return NULL;
  2665. }
  2666. parse_whsp(&ss);
  2667. } else if ( !strcasecmp(sval,"NOT") ) {
  2668. LDAP_FREE(sval);
  2669. if ( seen_not ) {
  2670. *code = LDAP_SCHERR_DUPOPT;
  2671. *errp = ss;
  2672. ldap_contentrule_free(cr);
  2673. return(NULL);
  2674. }
  2675. seen_not = 1;
  2676. cr->cr_at_oids_not = parse_oids(&ss,code,0);
  2677. if ( !cr->cr_at_oids_not && *code != LDAP_SUCCESS ) {
  2678. *errp = ss;
  2679. ldap_contentrule_free(cr);
  2680. return NULL;
  2681. }
  2682. parse_whsp(&ss);
  2683. } else if ( sval[0] == 'X' && sval[1] == '-' ) {
  2684. /* Should be parse_qdstrings */
  2685. ext_vals = parse_qdescrs(&ss, code);
  2686. if ( !ext_vals ) {
  2687. *errp = ss;
  2688. ldap_contentrule_free(cr);
  2689. return NULL;
  2690. }
  2691. if ( add_extension(&cr->cr_extensions,
  2692. sval, ext_vals) ) {
  2693. *code = LDAP_SCHERR_OUTOFMEM;
  2694. *errp = ss;
  2695. LDAP_FREE(sval);
  2696. ldap_contentrule_free(cr);
  2697. return NULL;
  2698. }
  2699. } else {
  2700. *code = LDAP_SCHERR_UNEXPTOKEN;
  2701. *errp = ss;
  2702. LDAP_FREE(sval);
  2703. ldap_contentrule_free(cr);
  2704. return NULL;
  2705. }
  2706. break;
  2707. default:
  2708. *code = LDAP_SCHERR_UNEXPTOKEN;
  2709. *errp = ss;
  2710. LDAP_FREE(sval);
  2711. ldap_contentrule_free(cr);
  2712. return NULL;
  2713. }
  2714. }
  2715. }
  2716. void
  2717. ldap_structurerule_free(LDAPStructureRule * sr)
  2718. {
  2719. if (!sr) return;
  2720. if (sr->sr_names) LDAP_VFREE(sr->sr_names);
  2721. if (sr->sr_desc) LDAP_FREE(sr->sr_desc);
  2722. if (sr->sr_nameform) LDAP_FREE(sr->sr_nameform);
  2723. if (sr->sr_sup_ruleids) LDAP_FREE(sr->sr_sup_ruleids);
  2724. free_extensions(sr->sr_extensions);
  2725. LDAP_FREE(sr);
  2726. }
  2727. LDAPStructureRule *
  2728. ldap_str2structurerule( LDAP_CONST char * s,
  2729. int * code,
  2730. LDAP_CONST char ** errp,
  2731. LDAP_CONST unsigned flags )
  2732. {
  2733. tk_t kind;
  2734. int ret;
  2735. const char * ss = s;
  2736. char * sval;
  2737. int seen_name = 0;
  2738. int seen_desc = 0;
  2739. int seen_obsolete = 0;
  2740. int seen_nameform = 0;
  2741. LDAPStructureRule * sr;
  2742. char ** ext_vals;
  2743. const char * savepos;
  2744. if ( !s ) {
  2745. *code = LDAP_SCHERR_EMPTY;
  2746. *errp = "";
  2747. return NULL;
  2748. }
  2749. *errp = s;
  2750. sr = LDAP_CALLOC(1,sizeof(LDAPStructureRule));
  2751. if ( !sr ) {
  2752. *code = LDAP_SCHERR_OUTOFMEM;
  2753. return NULL;
  2754. }
  2755. kind = get_token(&ss,&sval);
  2756. if ( kind != TK_LEFTPAREN ) {
  2757. *code = LDAP_SCHERR_NOLEFTPAREN;
  2758. LDAP_FREE(sval);
  2759. ldap_structurerule_free(sr);
  2760. return NULL;
  2761. }
  2762. /*
  2763. * Definitions MUST begin with a ruleid.
  2764. */
  2765. parse_whsp(&ss);
  2766. savepos = ss;
  2767. ret = ldap_int_parse_ruleid(&ss,code,0,&sr->sr_ruleid);
  2768. if ( ret ) {
  2769. *errp = ss;
  2770. ldap_structurerule_free(sr);
  2771. return NULL;
  2772. }
  2773. parse_whsp(&ss);
  2774. /*
  2775. * Beyond this point we will be liberal an accept the items
  2776. * in any order.
  2777. */
  2778. while (1) {
  2779. kind = get_token(&ss,&sval);
  2780. switch (kind) {
  2781. case TK_EOS:
  2782. *code = LDAP_SCHERR_NORIGHTPAREN;
  2783. *errp = EndOfInput;
  2784. ldap_structurerule_free(sr);
  2785. return NULL;
  2786. case TK_RIGHTPAREN:
  2787. if( !seen_nameform ) {
  2788. *code = LDAP_SCHERR_MISSING;
  2789. ldap_structurerule_free(sr);
  2790. return NULL;
  2791. }
  2792. return sr;
  2793. case TK_BAREWORD:
  2794. if ( !strcasecmp(sval,"NAME") ) {
  2795. LDAP_FREE(sval);
  2796. if ( seen_name ) {
  2797. *code = LDAP_SCHERR_DUPOPT;
  2798. *errp = ss;
  2799. ldap_structurerule_free(sr);
  2800. return(NULL);
  2801. }
  2802. seen_name = 1;
  2803. sr->sr_names = parse_qdescrs(&ss,code);
  2804. if ( !sr->sr_names ) {
  2805. if ( *code != LDAP_SCHERR_OUTOFMEM )
  2806. *code = LDAP_SCHERR_BADNAME;
  2807. *errp = ss;
  2808. ldap_structurerule_free(sr);
  2809. return NULL;
  2810. }
  2811. } else if ( !strcasecmp(sval,"DESC") ) {
  2812. LDAP_FREE(sval);
  2813. if ( seen_desc ) {
  2814. *code = LDAP_SCHERR_DUPOPT;
  2815. *errp = ss;
  2816. ldap_structurerule_free(sr);
  2817. return(NULL);
  2818. }
  2819. seen_desc = 1;
  2820. parse_whsp(&ss);
  2821. kind = get_token(&ss,&sval);
  2822. if ( kind != TK_QDSTRING ) {
  2823. *code = LDAP_SCHERR_UNEXPTOKEN;
  2824. *errp = ss;
  2825. LDAP_FREE(sval);
  2826. ldap_structurerule_free(sr);
  2827. return NULL;
  2828. }
  2829. sr->sr_desc = sval;
  2830. parse_whsp(&ss);
  2831. } else if ( !strcasecmp(sval,"OBSOLETE") ) {
  2832. LDAP_FREE(sval);
  2833. if ( seen_obsolete ) {
  2834. *code = LDAP_SCHERR_DUPOPT;
  2835. *errp = ss;
  2836. ldap_structurerule_free(sr);
  2837. return(NULL);
  2838. }
  2839. seen_obsolete = 1;
  2840. sr->sr_obsolete = LDAP_SCHEMA_YES;
  2841. parse_whsp(&ss);
  2842. } else if ( !strcasecmp(sval,"FORM") ) {
  2843. LDAP_FREE(sval);
  2844. if ( seen_nameform ) {
  2845. *code = LDAP_SCHERR_DUPOPT;
  2846. *errp = ss;
  2847. ldap_structurerule_free(sr);
  2848. return(NULL);
  2849. }
  2850. seen_nameform = 1;
  2851. sr->sr_nameform = parse_woid(&ss,code);
  2852. if ( !sr->sr_nameform ) {
  2853. *errp = ss;
  2854. ldap_structurerule_free(sr);
  2855. return NULL;
  2856. }
  2857. parse_whsp(&ss);
  2858. } else if ( sval[0] == 'X' && sval[1] == '-' ) {
  2859. /* Should be parse_qdstrings */
  2860. ext_vals = parse_qdescrs(&ss, code);
  2861. if ( !ext_vals ) {
  2862. *errp = ss;
  2863. ldap_structurerule_free(sr);
  2864. return NULL;
  2865. }
  2866. if ( add_extension(&sr->sr_extensions,
  2867. sval, ext_vals) ) {
  2868. *code = LDAP_SCHERR_OUTOFMEM;
  2869. *errp = ss;
  2870. LDAP_FREE(sval);
  2871. ldap_structurerule_free(sr);
  2872. return NULL;
  2873. }
  2874. } else {
  2875. *code = LDAP_SCHERR_UNEXPTOKEN;
  2876. *errp = ss;
  2877. LDAP_FREE(sval);
  2878. ldap_structurerule_free(sr);
  2879. return NULL;
  2880. }
  2881. break;
  2882. default:
  2883. *code = LDAP_SCHERR_UNEXPTOKEN;
  2884. *errp = ss;
  2885. LDAP_FREE(sval);
  2886. ldap_structurerule_free(sr);
  2887. return NULL;
  2888. }
  2889. }
  2890. }
  2891. void
  2892. ldap_nameform_free(LDAPNameForm * nf)
  2893. {
  2894. if (!nf) return;
  2895. LDAP_FREE(nf->nf_oid);
  2896. if (nf->nf_names) LDAP_VFREE(nf->nf_names);
  2897. if (nf->nf_desc) LDAP_FREE(nf->nf_desc);
  2898. if (nf->nf_objectclass) LDAP_FREE(nf->nf_objectclass);
  2899. if (nf->nf_at_oids_must) LDAP_VFREE(nf->nf_at_oids_must);
  2900. if (nf->nf_at_oids_may) LDAP_VFREE(nf->nf_at_oids_may);
  2901. free_extensions(nf->nf_extensions);
  2902. LDAP_FREE(nf);
  2903. }
  2904. LDAPNameForm *
  2905. ldap_str2nameform( LDAP_CONST char * s,
  2906. int * code,
  2907. LDAP_CONST char ** errp,
  2908. LDAP_CONST unsigned flags )
  2909. {
  2910. tk_t kind;
  2911. const char * ss = s;
  2912. char * sval;
  2913. int seen_name = 0;
  2914. int seen_desc = 0;
  2915. int seen_obsolete = 0;
  2916. int seen_class = 0;
  2917. int seen_must = 0;
  2918. int seen_may = 0;
  2919. LDAPNameForm * nf;
  2920. char ** ext_vals;
  2921. const char * savepos;
  2922. if ( !s ) {
  2923. *code = LDAP_SCHERR_EMPTY;
  2924. *errp = "";
  2925. return NULL;
  2926. }
  2927. *errp = s;
  2928. nf = LDAP_CALLOC(1,sizeof(LDAPNameForm));
  2929. if ( !nf ) {
  2930. *code = LDAP_SCHERR_OUTOFMEM;
  2931. return NULL;
  2932. }
  2933. kind = get_token(&ss,&sval);
  2934. if ( kind != TK_LEFTPAREN ) {
  2935. *code = LDAP_SCHERR_NOLEFTPAREN;
  2936. LDAP_FREE(sval);
  2937. ldap_nameform_free(nf);
  2938. return NULL;
  2939. }
  2940. /*
  2941. * Definitions MUST begin with an OID in the numericoid format.
  2942. * However, this routine is used by clients to parse the response
  2943. * from servers and very well known servers will provide an OID
  2944. * in the wrong format or even no OID at all. We do our best to
  2945. * extract info from those servers.
  2946. */
  2947. parse_whsp(&ss);
  2948. savepos = ss;
  2949. nf->nf_oid = ldap_int_parse_numericoid(&ss,code,0);
  2950. if ( !nf->nf_oid ) {
  2951. *errp = ss;
  2952. ldap_nameform_free(nf);
  2953. return NULL;
  2954. }
  2955. parse_whsp(&ss);
  2956. /*
  2957. * Beyond this point we will be liberal an accept the items
  2958. * in any order.
  2959. */
  2960. while (1) {
  2961. kind = get_token(&ss,&sval);
  2962. switch (kind) {
  2963. case TK_EOS:
  2964. *code = LDAP_SCHERR_NORIGHTPAREN;
  2965. *errp = EndOfInput;
  2966. ldap_nameform_free(nf);
  2967. return NULL;
  2968. case TK_RIGHTPAREN:
  2969. if( !seen_class || !seen_must ) {
  2970. *code = LDAP_SCHERR_MISSING;
  2971. ldap_nameform_free(nf);
  2972. return NULL;
  2973. }
  2974. return nf;
  2975. case TK_BAREWORD:
  2976. if ( !strcasecmp(sval,"NAME") ) {
  2977. LDAP_FREE(sval);
  2978. if ( seen_name ) {
  2979. *code = LDAP_SCHERR_DUPOPT;
  2980. *errp = ss;
  2981. ldap_nameform_free(nf);
  2982. return(NULL);
  2983. }
  2984. seen_name = 1;
  2985. nf->nf_names = parse_qdescrs(&ss,code);
  2986. if ( !nf->nf_names ) {
  2987. if ( *code != LDAP_SCHERR_OUTOFMEM )
  2988. *code = LDAP_SCHERR_BADNAME;
  2989. *errp = ss;
  2990. ldap_nameform_free(nf);
  2991. return NULL;
  2992. }
  2993. } else if ( !strcasecmp(sval,"DESC") ) {
  2994. LDAP_FREE(sval);
  2995. if ( seen_desc ) {
  2996. *code = LDAP_SCHERR_DUPOPT;
  2997. *errp = ss;
  2998. ldap_nameform_free(nf);
  2999. return(NULL);
  3000. }
  3001. seen_desc = 1;
  3002. parse_whsp(&ss);
  3003. kind = get_token(&ss,&sval);
  3004. if ( kind != TK_QDSTRING ) {
  3005. *code = LDAP_SCHERR_UNEXPTOKEN;
  3006. *errp = ss;
  3007. LDAP_FREE(sval);
  3008. ldap_nameform_free(nf);
  3009. return NULL;
  3010. }
  3011. nf->nf_desc = sval;
  3012. parse_whsp(&ss);
  3013. } else if ( !strcasecmp(sval,"OBSOLETE") ) {
  3014. LDAP_FREE(sval);
  3015. if ( seen_obsolete ) {
  3016. *code = LDAP_SCHERR_DUPOPT;
  3017. *errp = ss;
  3018. ldap_nameform_free(nf);
  3019. return(NULL);
  3020. }
  3021. seen_obsolete = 1;
  3022. nf->nf_obsolete = LDAP_SCHEMA_YES;
  3023. parse_whsp(&ss);
  3024. } else if ( !strcasecmp(sval,"OC") ) {
  3025. LDAP_FREE(sval);
  3026. if ( seen_class ) {
  3027. *code = LDAP_SCHERR_DUPOPT;
  3028. *errp = ss;
  3029. ldap_nameform_free(nf);
  3030. return(NULL);
  3031. }
  3032. seen_class = 1;
  3033. nf->nf_objectclass = parse_woid(&ss,code);
  3034. if ( !nf->nf_objectclass ) {
  3035. *errp = ss;
  3036. ldap_nameform_free(nf);
  3037. return NULL;
  3038. }
  3039. } else if ( !strcasecmp(sval,"MUST") ) {
  3040. LDAP_FREE(sval);
  3041. if ( seen_must ) {
  3042. *code = LDAP_SCHERR_DUPOPT;
  3043. *errp = ss;
  3044. ldap_nameform_free(nf);
  3045. return(NULL);
  3046. }
  3047. seen_must = 1;
  3048. nf->nf_at_oids_must = parse_oids(&ss,code,0);
  3049. if ( !nf->nf_at_oids_must && *code != LDAP_SUCCESS ) {
  3050. *errp = ss;
  3051. ldap_nameform_free(nf);
  3052. return NULL;
  3053. }
  3054. parse_whsp(&ss);
  3055. } else if ( !strcasecmp(sval,"MAY") ) {
  3056. LDAP_FREE(sval);
  3057. if ( seen_may ) {
  3058. *code = LDAP_SCHERR_DUPOPT;
  3059. *errp = ss;
  3060. ldap_nameform_free(nf);
  3061. return(NULL);
  3062. }
  3063. seen_may = 1;
  3064. nf->nf_at_oids_may = parse_oids(&ss,code,0);
  3065. if ( !nf->nf_at_oids_may && *code != LDAP_SUCCESS ) {
  3066. *errp = ss;
  3067. ldap_nameform_free(nf);
  3068. return NULL;
  3069. }
  3070. parse_whsp(&ss);
  3071. } else if ( sval[0] == 'X' && sval[1] == '-' ) {
  3072. /* Should be parse_qdstrings */
  3073. ext_vals = parse_qdescrs(&ss, code);
  3074. if ( !ext_vals ) {
  3075. *errp = ss;
  3076. ldap_nameform_free(nf);
  3077. return NULL;
  3078. }
  3079. if ( add_extension(&nf->nf_extensions,
  3080. sval, ext_vals) ) {
  3081. *code = LDAP_SCHERR_OUTOFMEM;
  3082. *errp = ss;
  3083. LDAP_FREE(sval);
  3084. ldap_nameform_free(nf);
  3085. return NULL;
  3086. }
  3087. } else {
  3088. *code = LDAP_SCHERR_UNEXPTOKEN;
  3089. *errp = ss;
  3090. LDAP_FREE(sval);
  3091. ldap_nameform_free(nf);
  3092. return NULL;
  3093. }
  3094. break;
  3095. default:
  3096. *code = LDAP_SCHERR_UNEXPTOKEN;
  3097. *errp = ss;
  3098. LDAP_FREE(sval);
  3099. ldap_nameform_free(nf);
  3100. return NULL;
  3101. }
  3102. }
  3103. }
  3104. static char *const err2text[] = {
  3105. N_("Success"),
  3106. N_("Out of memory"),
  3107. N_("Unexpected token"),
  3108. N_("Missing opening parenthesis"),
  3109. N_("Missing closing parenthesis"),
  3110. N_("Expecting digit"),
  3111. N_("Expecting a name"),
  3112. N_("Bad description"),
  3113. N_("Bad superiors"),
  3114. N_("Duplicate option"),
  3115. N_("Unexpected end of data"),
  3116. N_("Missing required field"),
  3117. N_("Out of order field")
  3118. };
  3119. char *
  3120. ldap_scherr2str(int code)
  3121. {
  3122. if ( code < 0 || code >= (int)(sizeof(err2text)/sizeof(char *)) ) {
  3123. return _("Unknown error");
  3124. } else {
  3125. return _(err2text[code]);
  3126. }
  3127. }