schema.c 229 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. /* schema.c - routines to enforce schema definitions */
  42. #include <stdio.h>
  43. #include <string.h>
  44. #include <sys/types.h>
  45. #include <sys/stat.h>
  46. #include <prio.h>
  47. #include <plstr.h>
  48. #include <plhash.h>
  49. #include "slap.h"
  50. #if defined(USE_OPENLDAP)
  51. #include <ldap_schema.h> /* openldap schema parser */
  52. #endif
  53. typedef struct sizedbuffer
  54. {
  55. char *buffer;
  56. size_t size;
  57. } sizedbuffer;
  58. typedef char *(*schema_strstr_fn_t)( const char *big, const char *little);
  59. /*
  60. * The schema_oc_kind_strings array is indexed by oc_kind values, i.e.,
  61. * OC_KIND_STRUCTURAL (0), OC_KIND_AUXILIARY (1), or OC_KIND_ABSTRACT (2).
  62. * The leading and trailing spaces are intentional.
  63. */
  64. #define SCHEMA_OC_KIND_COUNT 3
  65. static char *schema_oc_kind_strings_with_spaces[] = {
  66. " ABSTRACT ",
  67. " STRUCTURAL ",
  68. " AUXILIARY ",
  69. };
  70. /* constant strings (used in a few places) */
  71. static const char *schema_obsolete_with_spaces = " OBSOLETE ";
  72. static const char *schema_collective_with_spaces = " COLLECTIVE ";
  73. static const char *schema_nousermod_with_spaces = " NO-USER-MODIFICATION ";
  74. /* user defined origin array */
  75. static char *schema_user_defined_origin[] = {
  76. "user defined",
  77. NULL
  78. };
  79. /* The policies for the replication of the schema are
  80. * - base policy
  81. * - extended policies
  82. * Those policies are enforced when the server is acting as a supplier and
  83. * when it is acting as a consumer
  84. *
  85. * Base policy:
  86. * Supplier: before pushing the schema, the supplier checks that each objectclass/attribute of
  87. * the consumer schema is a subset of the objectclass/attribute of the supplier schema
  88. * Consumer: before accepting a schema (from replication), the consumer checks that
  89. * each objectclass/attribute of the consumer schema is a subset of the objectclass/attribute
  90. * of the supplier schema
  91. * Extended policies:
  92. * They are stored in repl_schema_policy_t and specifies an "action" to be taken
  93. * for specific objectclass/attribute.
  94. * Supplier: extended policies are stored in entry "cn=supplierUpdatePolicy,cn=replSchema,cn=config"
  95. * and uploaded in static variable: supplier_policy
  96. * Before pushing the schema, for each objectclass/attribute defined in supplier_policy:
  97. * if its "action" is REPL_SCHEMA_UPDATE_ACCEPT_VALUE, it is not checked that the
  98. * attribute/objectclass of the consumer is a subset of the attribute/objectclass
  99. * of the supplier schema.
  100. *
  101. * if its "action" is REPL_SCHEMA_UPDATE_REJECT_VALUE and the consumer schema contains
  102. * attribute/objectclass, then schema is not pushed
  103. *
  104. * Consumer: extended policies are stored in entry "cn=consumerUpdatePolicy,cn=replSchema,cn=config"
  105. * and uploaded in static variable: consumer_policy
  106. * before accepting a schema (from replication), for each objectclass/attribute defined in
  107. * consumer_policy:
  108. * if its "action" is REPL_SCHEMA_UPDATE_ACCEPT_VALUE, it is not checked that the
  109. * attribute/objectclass of the consumer is a subset of the attribute/objectclass
  110. * of the supplier schema.
  111. *
  112. * if its "action" is REPL_SCHEMA_UPDATE_REJECT_VALUE and the consumer schema contains
  113. * attribute/objectclass, then schema is not accepted
  114. *
  115. */
  116. typedef struct schema_item {
  117. int action; /* REPL_SCHEMA_UPDATE_ACCEPT_VALUE or REPL_SCHEMA_UPDATE_REJECT_VALUE */
  118. char *name_or_oid;
  119. struct schema_item *next;
  120. } schema_item_t;
  121. typedef struct repl_schema_policy {
  122. schema_item_t *objectclasses;
  123. schema_item_t *attributes;
  124. } repl_schema_policy_t;
  125. /*
  126. * pschemadse is based on the general implementation in dse
  127. */
  128. static struct dse *pschemadse= NULL;
  129. static void oc_add_nolock(struct objclass *newoc);
  130. static int oc_delete_nolock (char *ocname);
  131. static int oc_replace_nolock(const char *ocname, struct objclass *newoc);
  132. static int oc_check_required(Slapi_PBlock *, Slapi_Entry *,struct objclass *);
  133. static int oc_check_allowed_sv(Slapi_PBlock *, Slapi_Entry *e, const char *type, struct objclass **oclist );
  134. static int schema_delete_objectclasses ( Slapi_Entry *entryBefore,
  135. LDAPMod *mod, char *errorbuf, size_t errorbufsize,
  136. int schema_ds4x_compat );
  137. static int schema_delete_attributes ( Slapi_Entry *entryBefore,
  138. LDAPMod *mod, char *errorbuf, size_t errorbufsize);
  139. static int schema_add_attribute ( Slapi_PBlock *pb, LDAPMod *mod,
  140. char *errorbuf, size_t errorbufsize, int schema_ds4x_compat );
  141. static int schema_add_objectclass ( Slapi_PBlock *pb, LDAPMod *mod,
  142. char *errorbuf, size_t errorbufsize, int schema_ds4x_compat );
  143. static int schema_replace_attributes ( Slapi_PBlock *pb, LDAPMod *mod,
  144. char *errorbuf, size_t errorbufsize );
  145. static int schema_replace_objectclasses ( Slapi_PBlock *pb, LDAPMod *mod,
  146. char *errorbuf, size_t errorbufsize );
  147. static int schema_check_name(char *name, PRBool isAttribute, char *errorbuf,
  148. size_t errorbufsize );
  149. static int schema_check_oid(const char *name, const char *oid,
  150. PRBool isAttribute, char *errorbuf, size_t errorbufsize);
  151. static int isExtensibleObjectclass(const char *objectclass);
  152. static int strip_oc_options ( struct objclass *poc );
  153. static char *stripOption (char *attr);
  154. static int schema_extension_cmp(schemaext *e1, schemaext *e2);
  155. static int put_tagged_oid( char *outp, const char *tag, const char *oid,
  156. const char *suffix, int enquote );
  157. static void strcat_oids( char *buf, char *prefix, char **oids,
  158. int schema_ds4x_compat );
  159. static size_t strcat_extensions( char *buf, schemaext *extension );
  160. static size_t strlen_null_ok(const char *s);
  161. static int strcpy_count( char *dst, const char *src );
  162. static int refresh_user_defined_schema(Slapi_PBlock *pb, Slapi_Entry* entryBefore, Slapi_Entry* e, int *returncode, char *returntext, void *arg);
  163. static int schema_check_oc_attrs ( struct objclass *poc, char *errorbuf,
  164. size_t errorbufsize, int stripOptions );
  165. static struct objclass *oc_find_nolock( const char *ocname_or_oid, struct objclass *oc_private, PRBool use_private );
  166. static struct objclass *oc_find_oid_nolock( const char *ocoid );
  167. static void oc_free( struct objclass **ocp );
  168. static PRBool oc_equal( struct objclass *oc1, struct objclass *oc2 );
  169. static PRBool attr_syntax_equal( struct asyntaxinfo *asi1,
  170. struct asyntaxinfo *asi2 );
  171. static int schema_strcmp( const char *s1, const char *s2 );
  172. static int schema_strcmp_array( char **sa1, char **sa2,
  173. const char *ignorestr );
  174. static PRBool schema_type_is_interesting( const char *type );
  175. static void schema_create_errormsg( char *errorbuf, size_t errorbufsize,
  176. const char *prefix, const char *name, const char *fmt, ... )
  177. #ifdef __GNUC__
  178. __attribute__ ((format (printf, 5, 6)));
  179. #else
  180. ;
  181. #endif
  182. static int schema_at_superset_check(struct asyntaxinfo *at_list1, struct asyntaxinfo *at_list2, char *message, int replica_role);
  183. static int schema_at_superset_check_syntax_oids(char *oid1, char *oid2);
  184. static int schema_at_superset_check_mr(struct asyntaxinfo *a1, struct asyntaxinfo *a2, char *info);
  185. static int parse_at_str(const char *input, struct asyntaxinfo **asipp, char *errorbuf, size_t errorbufsize,
  186. PRUint32 schema_flags, int is_user_defined, int schema_ds4x_compat, int is_remote);
  187. static int extension_is_user_defined( schemaext *extensions );
  188. static size_t strcat_qdlist( char *buf, char *prefix, char **qdlist );
  189. #if defined (USE_OPENLDAP)
  190. /*
  191. * openldap
  192. */
  193. static int parse_attr_str(const char *input, struct asyntaxinfo **asipp, char *errorbuf, size_t errorbufsize,
  194. PRUint32 schema_flags, int is_user_defined, int schema_ds4x_compat, int is_remote);
  195. static int parse_objclass_str(const char *input, struct objclass **oc, char *errorbuf, size_t errorbufsize,
  196. PRUint32 schema_flags, int is_user_defined, int schema_ds4x_compat, struct objclass* private_schema );
  197. #else
  198. /*
  199. * mozldap
  200. */
  201. static char **parse_qdescrs(const char *s, int *n);
  202. static char **parse_qdstrings(const char *s, int *n);
  203. static char **parse_qdlist(const char *s, int *n, int strip_options);
  204. static void free_qdlist(char **vals, int n);
  205. static int read_at_ldif(const char *input, struct asyntaxinfo **asipp,
  206. char *errorbuf, size_t errorbufsize, PRUint32 flags,
  207. int is_user_defined, int schema_ds4x_compat, int is_remote);
  208. static int read_oc_ldif ( const char *input, struct objclass **oc,
  209. char *errorbuf, size_t errorbufsize, PRUint32 flags, int is_user_defined,
  210. int schema_ds4x_compat );
  211. static int get_flag_keyword( const char *keyword, int flag_value,
  212. const char **inputp, schema_strstr_fn_t strstr_fn );
  213. static char *get_tagged_oid( const char *tag, const char **inputp,
  214. schema_strstr_fn_t strstr_fn );
  215. static char **read_dollar_values ( char *vals);
  216. static schemaext *parse_extensions( const char *schema_value, char **default_list );
  217. #endif
  218. /*
  219. * Some utility functions for dealing with a dynamic buffer
  220. */
  221. static struct sizedbuffer *sizedbuffer_construct(size_t size);
  222. static void sizedbuffer_destroy(struct sizedbuffer *p);
  223. static void sizedbuffer_allocate(struct sizedbuffer *p, size_t sizeneeded);
  224. /*
  225. * Constant strings that we pass to schema_create_errormsg().
  226. */
  227. static const char *schema_errprefix_oc = "object class %s: ";
  228. static const char *schema_errprefix_at = "attribute type %s: ";
  229. static const char *schema_errprefix_generic = "%s: ";
  230. /* Defined the policies for the replication of the schema */
  231. static repl_schema_policy_t supplier_policy = {0};
  232. static repl_schema_policy_t consumer_policy = {0};
  233. static Slapi_RWLock *schema_policy_lock = NULL;
  234. static int schema_check_policy(int replica_role, int schema_item, char *name, char *oid);
  235. static void schema_load_repl_policy(const char *dn, repl_schema_policy_t *replica);
  236. /*
  237. * A "cached" copy of the "ignore trailing spaces" config. setting.
  238. * This is set during initialization only (server restart required for
  239. * changes to take effect). We do things this way to avoid lock/unlock
  240. * mutex sequences inside performance critical code.
  241. */
  242. static int schema_ignore_trailing_spaces =
  243. SLAPD_DEFAULT_SCHEMA_IGNORE_TRAILING_SPACES;
  244. /* R/W lock used to serialize access to the schema DSE */
  245. static Slapi_RWLock *schema_dse_lock = NULL;
  246. /*
  247. * The schema_dse_mandatory_init_callonce structure is used by NSPR to ensure
  248. * that schema_dse_mandatory_init() is called at most once.
  249. */
  250. static PRCallOnceType schema_dse_mandatory_init_callonce = { 0, 0, 0 };
  251. static int parse_at_str(const char *input, struct asyntaxinfo **asipp, char *errorbuf, size_t errorbufsize,
  252. PRUint32 schema_flags, int is_user_defined, int schema_ds4x_compat, int is_remote)
  253. {
  254. #ifdef USE_OPENLDAP
  255. return parse_attr_str(input, asipp, errorbuf, errorbufsize, schema_flags, is_user_defined,schema_ds4x_compat,is_remote);
  256. #else
  257. return read_at_ldif(input, asipp, errorbuf, errorbufsize, schema_flags, is_user_defined,schema_ds4x_compat,is_remote);
  258. #endif
  259. }
  260. static int parse_oc_str(const char *input, struct objclass **oc, char *errorbuf,
  261. size_t errorbufsize, PRUint32 schema_flags, int is_user_defined,
  262. int schema_ds4x_compat, struct objclass* private_schema )
  263. {
  264. #ifdef USE_OPENLDAP
  265. return parse_objclass_str (input, oc, errorbuf, errorbufsize, schema_flags, is_user_defined, schema_ds4x_compat, private_schema );
  266. #else
  267. return read_oc_ldif (input, oc, errorbuf, errorbufsize, schema_flags, is_user_defined, schema_ds4x_compat );
  268. #endif
  269. }
  270. /* Essential initialization. Returns PRSuccess if successful */
  271. static PRStatus
  272. schema_dse_mandatory_init( void )
  273. {
  274. if ( NULL == ( schema_dse_lock = slapi_new_rwlock())) {
  275. slapi_log_error( SLAPI_LOG_FATAL, "schema_dse_mandatory_init",
  276. "slapi_new_rwlock() for schema DSE lock failed\n" );
  277. return PR_FAILURE;
  278. }
  279. schema_ignore_trailing_spaces = config_get_schema_ignore_trailing_spaces();
  280. return PR_SUCCESS;
  281. }
  282. void
  283. schema_destroy_dse_lock()
  284. {
  285. if(schema_dse_lock){
  286. slapi_destroy_rwlock(schema_dse_lock);
  287. schema_dse_lock = NULL;
  288. }
  289. }
  290. void
  291. slapi_schema_get_repl_entries(char **repl_schema_top, char ** repl_schema_supplier, char **repl_schema_consumer, char **default_supplier_policy, char **default_consumer_policy)
  292. {
  293. *repl_schema_top = ENTRY_REPL_SCHEMA_TOP;
  294. *repl_schema_supplier = ENTRY_REPL_SCHEMA_SUPPLIER;
  295. *repl_schema_consumer = ENTRY_REPL_SCHEMA_CONSUMER;
  296. *default_supplier_policy = DEFAULT_SUPPLIER_POLICY;
  297. *default_consumer_policy = DEFAULT_CONSUMER_POLICY;
  298. }
  299. /* It gets the attributes (see attrName)values in the entry, and add
  300. * the policies in the provided list
  301. *
  302. * Entry: Slapi_entry with DN being ENTRY_REPL_SCHEMA_SUPPLIER or ENTRY_REPL_SCHEMA_CONSUMER
  303. * attrName: name defining the policy object (objectclass/attribute) and the action
  304. * ATTR_SCHEMA_UPDATE_OBJECTCLASS_ACCEPT
  305. * ATTR_SCHEMA_UPDATE_OBJECTCLASS_REJECT
  306. * ATTR_SCHEMA_UPDATE_ATTRIBUTE_ACCEPT
  307. * ATTR_SCHEMA_UPDATE_ATTRIBUTE_REJECT
  308. * *list: is the list of schema_item_t containing the policies (it can be list of objectclasses or attributes)
  309. *
  310. */
  311. static
  312. void schema_policy_add_action(Slapi_Entry *entry, char *attrName, schema_item_t **list)
  313. {
  314. Slapi_Attr *attr = NULL;
  315. schema_item_t *schema_item;
  316. char *value;
  317. int action;
  318. /* Retrieve the expected action from the attribute name */
  319. if ((strcasecmp(attrName, ATTR_SCHEMA_UPDATE_OBJECTCLASS_ACCEPT) == 0) ||
  320. (strcasecmp(attrName, ATTR_SCHEMA_UPDATE_ATTRIBUTE_ACCEPT) == 0)) {
  321. action = REPL_SCHEMA_UPDATE_ACCEPT_VALUE;
  322. } else {
  323. action = REPL_SCHEMA_UPDATE_REJECT_VALUE;
  324. }
  325. /* Retrieve the given attribute from the entry */
  326. slapi_entry_attr_find(entry, attrName, &attr);
  327. if (attr != NULL) {
  328. Slapi_Value *sval = NULL;
  329. const struct berval *attrVal = NULL;
  330. int k = slapi_attr_first_value(attr, &sval);
  331. /* For each value adds the policy in the list */
  332. while (k != -1) {
  333. attrVal = slapi_value_get_berval(sval);
  334. schema_item = (schema_item_t *) slapi_ch_calloc(1, sizeof(schema_item_t));
  335. /* Get the schema name_or_oid */
  336. value = (char *) slapi_ch_malloc(attrVal->bv_len + 1);
  337. memcpy(value, attrVal->bv_val, attrVal->bv_len);
  338. value[attrVal->bv_len] = '\0';
  339. schema_item->name_or_oid = value;
  340. /* Set the action on that item */
  341. schema_item->action = action;
  342. /* Add it on the head of the list */
  343. schema_item->next = *list;
  344. *list = schema_item;
  345. /* Get the next name_or_oid */
  346. k = slapi_attr_next_value(attr, k, &sval);
  347. }
  348. }
  349. }
  350. /* Caller must hold schema_policy_lock in write */
  351. static void
  352. schema_load_repl_policy(const char *dn, repl_schema_policy_t *replica)
  353. {
  354. Slapi_DN sdn;
  355. Slapi_Entry *entry = NULL;
  356. schema_item_t *schema_item, *next;
  357. if (replica == NULL) {
  358. return;
  359. }
  360. /* Start to free the previous policy */
  361. /* first the objectclasses policies */
  362. for (schema_item = replica->objectclasses; schema_item; ) {
  363. slapi_ch_free((void **) &schema_item->name_or_oid);
  364. next = schema_item->next;
  365. slapi_ch_free((void **) &schema_item);
  366. schema_item = next;
  367. }
  368. replica->objectclasses = NULL;
  369. /* second the attributes policies */
  370. for (schema_item = replica->attributes; schema_item; ) {
  371. slapi_ch_free((void **) &schema_item->name_or_oid);
  372. next = schema_item->next;
  373. slapi_ch_free((void **) &schema_item);
  374. schema_item = next;
  375. }
  376. replica->attributes = NULL;
  377. /* Load the replication policy of the schema */
  378. slapi_sdn_init_dn_byref( &sdn, dn );
  379. if (slapi_search_internal_get_entry(&sdn, NULL, &entry, plugin_get_default_component_id()) == LDAP_SUCCESS) {
  380. /* fill the policies (accept/reject) regarding objectclass */
  381. schema_policy_add_action(entry, ATTR_SCHEMA_UPDATE_OBJECTCLASS_ACCEPT, &replica->objectclasses);
  382. schema_policy_add_action(entry, ATTR_SCHEMA_UPDATE_OBJECTCLASS_REJECT, &replica->objectclasses);
  383. /* fill the policies (accept/reject) regarding attribute */
  384. schema_policy_add_action(entry, ATTR_SCHEMA_UPDATE_ATTRIBUTE_ACCEPT, &replica->attributes);
  385. schema_policy_add_action(entry, ATTR_SCHEMA_UPDATE_ATTRIBUTE_REJECT, &replica->attributes);
  386. slapi_entry_free( entry );
  387. }
  388. slapi_sdn_done(&sdn);
  389. }
  390. /* It load the policies (if they are defined) regarding the replication of the schema
  391. * depending if the instance behaves as a consumer or a supplier
  392. * It returns 0 if success
  393. */
  394. int
  395. slapi_schema_load_repl_policies()
  396. {
  397. if (schema_policy_lock == NULL) {
  398. if (NULL == (schema_policy_lock = slapi_new_rwlock())) {
  399. slapi_log_error(SLAPI_LOG_FATAL, "slapi_schema_load_repl_policies",
  400. "slapi_new_rwlock() for schema replication policy lock failed\n");
  401. return -1;
  402. }
  403. }
  404. slapi_rwlock_wrlock( schema_policy_lock );
  405. schema_load_repl_policy((const char *) ENTRY_REPL_SCHEMA_SUPPLIER, &supplier_policy);
  406. schema_load_repl_policy((const char *) ENTRY_REPL_SCHEMA_CONSUMER, &consumer_policy);
  407. slapi_rwlock_unlock( schema_policy_lock );
  408. return 0;
  409. }
  410. /*
  411. * It checks if the name/oid of the provided schema item (objectclass/attribute)
  412. * is defined in the schema replication policy.
  413. * If the replica role is a supplier, it takes the policy from supplier_policy else
  414. * it takes it from the consumer_policy.
  415. * Then depending on the schema_item, it takes the objectclasses or attributes policies
  416. *
  417. * If it find the name/oid in the policies, it returns
  418. * REPL_SCHEMA_UPDATE_ACCEPT_VALUE: This schema item is accepted and can not prevent schema update
  419. * REPL_SCHEMA_UPDATE_REJECT_VALUE: This schema item is rejected and prevents the schema update
  420. * REPL_SCHEMA_UPDATE_UNKNOWN_VALUE: This schema item as no defined policy
  421. *
  422. * Caller must hold schema_policy_lock in read
  423. */
  424. static int
  425. schema_check_policy(int replica_role, int schema_item, char *name, char *oid)
  426. {
  427. repl_schema_policy_t *repl_policy;
  428. schema_item_t *policy;
  429. /* depending on the role, we take the supplier or the consumer policy */
  430. if (replica_role == REPL_SCHEMA_AS_SUPPLIER) {
  431. repl_policy = &supplier_policy;
  432. } else {
  433. repl_policy = &consumer_policy;
  434. }
  435. /* Now take the correct schema item policy */
  436. if (schema_item == REPL_SCHEMA_OBJECTCLASS) {
  437. policy = repl_policy->objectclasses;
  438. } else {
  439. policy = repl_policy->attributes;
  440. }
  441. /* Try to find the name/oid in the defined policies */
  442. while (policy) {
  443. if ((strcasecmp( name, policy->name_or_oid) == 0) || (strcasecmp( oid, policy->name_or_oid) == 0)) {
  444. return policy->action;
  445. }
  446. policy = policy->next;
  447. }
  448. return REPL_SCHEMA_UPDATE_UNKNOWN_VALUE;
  449. }
  450. static void
  451. schema_dse_lock_read( void )
  452. {
  453. if ( NULL != schema_dse_lock ||
  454. PR_SUCCESS == PR_CallOnce( &schema_dse_mandatory_init_callonce,
  455. schema_dse_mandatory_init )) {
  456. slapi_rwlock_rdlock( schema_dse_lock );
  457. }
  458. }
  459. static void
  460. schema_dse_lock_write( void )
  461. {
  462. if ( NULL != schema_dse_lock ||
  463. PR_SUCCESS == PR_CallOnce( &schema_dse_mandatory_init_callonce,
  464. schema_dse_mandatory_init )) {
  465. slapi_rwlock_wrlock( schema_dse_lock );
  466. }
  467. }
  468. static void
  469. schema_dse_unlock( void )
  470. {
  471. if ( schema_dse_lock != NULL ) {
  472. slapi_rwlock_unlock( schema_dse_lock );
  473. }
  474. }
  475. static int
  476. dont_allow_that(Slapi_PBlock *pb, Slapi_Entry* entryBefore, Slapi_Entry* e, int *returncode, char *returntext, void *arg)
  477. {
  478. *returncode = LDAP_UNWILLING_TO_PERFORM;
  479. return SLAPI_DSE_CALLBACK_ERROR;
  480. }
  481. #if !defined(USE_OPENLDAP)
  482. static const char *
  483. skipWS(const char *s)
  484. {
  485. while (s && isascii(*s) && isspace(*s) )
  486. ++s;
  487. if ((isascii(*s)) == 0) {
  488. return NULL;
  489. }
  490. return s;
  491. }
  492. /*
  493. * like strchr() but strings within single quotes are skipped.
  494. */
  495. static char *
  496. strchr_skip_quoted_strings( char *s, int c )
  497. {
  498. int in_quote = 0;
  499. while ( *s != '\0' ) {
  500. if ( *s == '\'' ) {
  501. in_quote = 1 - in_quote; /* toggle */
  502. } else if ( !in_quote && *s == c ) {
  503. return s;
  504. }
  505. ++s;
  506. }
  507. return( NULL );
  508. }
  509. /**
  510. * parses a string containing a qdescrs or qdstrings (as described by
  511. * RFC 2252, section 4.1) into an array of strings; the second parameter
  512. * will hold the actual number of strings in the array. The returned array
  513. * is NULL terminated.
  514. *
  515. * This function can handle qdescrs or qdstrings because the only
  516. * difference between the two is that fewer characters are allowed in
  517. * a qdescr (our parsing code does not check anyway) and we want to
  518. * strip attribute options when parsing qdescrs (indicated by a non-zero
  519. * strip_options parameter).
  520. */
  521. static char **
  522. parse_qdlist(const char *s, int *n, int strip_options)
  523. {
  524. char **retval = 0;
  525. char *work = 0;
  526. char *start = 0, *end = 0;
  527. int num = 0;
  528. int in_quote = 0;
  529. if (n)
  530. *n = 0;
  531. if (!s || !*s || !n) {
  532. return retval;
  533. }
  534. /* make a working copy of the given string */
  535. work = slapi_ch_strdup(s);
  536. /* count the number of qdescr items in the string e.g. just count
  537. the number of spaces */
  538. /* for a single qdescr, the terminal character will be the final
  539. single quote; for a qdesclist, the terminal will be the close
  540. parenthesis */
  541. end = strrchr(work, '\'');
  542. if ((start = strchr_skip_quoted_strings(work, '(')) != NULL)
  543. end = strchr_skip_quoted_strings(work, ')');
  544. else
  545. start = strchr(work, '\'');
  546. if (!end) /* already nulled out */
  547. end = work + strlen(work);
  548. if (start) {
  549. num = 1;
  550. /* first pass: count number of items and zero out non useful tokens */
  551. for (; *start && (start != end); ++start) {
  552. if (*start == '\'' ) {
  553. in_quote = 1 - in_quote; /* toggle */
  554. *start = 0;
  555. } else if ( !in_quote && ((*start == ' ') || (*start == '(') ||
  556. (*start == ')'))) {
  557. if (*start == ' ') {
  558. num++;
  559. }
  560. *start = 0;
  561. }
  562. }
  563. *start = 0;
  564. /* allocate retval; num will be >= actual number of items */
  565. retval = (char**)slapi_ch_calloc(num+1, sizeof(char *));
  566. /* second pass: copy strings into the return value and set the
  567. actual number of items returned */
  568. start = work;
  569. while (start != end) {
  570. /* skip over nulls */
  571. while (!*start && (start != end))
  572. ++start;
  573. if (start == end)
  574. break;
  575. retval[*n] = slapi_ch_strdup(start);
  576. /*
  577. * A qdescr list may contain attribute options; we just strip
  578. * them here. In the future, we may want to support them or do
  579. * something really fancy with them
  580. */
  581. if ( strip_options ) {
  582. stripOption(retval[*n]);
  583. }
  584. (*n)++;
  585. start += strlen(start);
  586. }
  587. PR_ASSERT( *n <= num ); /* sanity check */
  588. retval[*n] = NULL;
  589. } else {
  590. /* syntax error - no start and/or end delimiters */
  591. }
  592. /* free the working string */
  593. slapi_ch_free((void **)&work);
  594. return retval;
  595. }
  596. /**
  597. * parses a string containing a qdescrs (as described by RFC 2252, section 4.1)
  598. * into an array of strings; the second parameter will hold the actual number
  599. * of strings in the array. The returned array is NULL terminated.
  600. */
  601. static char **
  602. parse_qdescrs(const char *s, int *n)
  603. {
  604. return parse_qdlist( s, n, 1 /* strip attribute options */ );
  605. }
  606. /*
  607. * Parses a string containing a qdstrings (see RFC 2252, section 4.1) into
  608. * an array of strings; the second parameter will hold the actual number
  609. * of strings in the array.
  610. */
  611. static char **
  612. parse_qdstrings(const char *s, int *n)
  613. {
  614. return parse_qdlist( s, n, 0 /* DO NOT strip attribute options */ );
  615. }
  616. static void
  617. free_qdlist(char **vals, int n)
  618. {
  619. int ii;
  620. for (ii = 0; ii < n; ++ii)
  621. slapi_ch_free((void **)&(vals[ii]));
  622. slapi_ch_free((void **)&vals);
  623. }
  624. #endif /* not openldap */
  625. /*
  626. * slapi_entry_schema_check - check that entry e conforms to the schema
  627. * required by its object class(es). returns 0 if so, non-zero otherwise.
  628. * [ the pblock is used to check if this is a replicated operation.
  629. * you may pass in NULL if this isn't part of an operation. ]
  630. * the pblock is also used to return a reason why schema checking failed.
  631. * it is also used to get schema flags
  632. * if replicated operations should be checked use slapi_entry_schema_check_ext
  633. */
  634. int
  635. slapi_entry_schema_check( Slapi_PBlock *pb, Slapi_Entry *e )
  636. {
  637. return (slapi_entry_schema_check_ext(pb, e, 0));
  638. }
  639. int
  640. slapi_entry_schema_check_ext( Slapi_PBlock *pb, Slapi_Entry *e, int repl_check )
  641. {
  642. struct objclass **oclist;
  643. struct objclass *oc;
  644. const char *ocname;
  645. Slapi_Attr *a, *aoc;
  646. Slapi_Value *v;
  647. int ret = 0;
  648. int schemacheck = config_get_schemacheck();
  649. int is_replicated_operation = 0;
  650. int is_extensible_object = 0;
  651. int i, oc_count = 0;
  652. int unknown_class = 0;
  653. char errtext[ BUFSIZ ];
  654. PRUint32 schema_flags = 0;
  655. /*
  656. * say the schema checked out ok if we're not checking schema at
  657. * all, or if this is a replication update.
  658. */
  659. if (pb != NULL) {
  660. slapi_pblock_get(pb, SLAPI_IS_REPLICATED_OPERATION, &is_replicated_operation);
  661. slapi_pblock_get(pb, SLAPI_SCHEMA_FLAGS, &schema_flags);
  662. }
  663. if ( schemacheck == 0 || (is_replicated_operation && !repl_check)) {
  664. return( 0 );
  665. }
  666. /* find the object class attribute - could error out here */
  667. if ( (aoc = attrlist_find( e->e_attrs, "objectclass" )) == NULL ) {
  668. LDAPDebug( LDAP_DEBUG_ANY,
  669. "Entry \"%s\" required attribute \"objectclass\" missing\n",
  670. slapi_entry_get_dn_const(e), 0, 0 );
  671. if (pb) {
  672. PR_snprintf( errtext, sizeof( errtext ),
  673. "missing required attribute \"objectclass\"\n" );
  674. slapi_pblock_set( pb, SLAPI_PB_RESULT_TEXT, errtext );
  675. }
  676. return( 1 );
  677. }
  678. /*
  679. * Create an array of pointers to the objclass definitions.
  680. */
  681. i= slapi_attr_first_value(aoc,&v);
  682. while (i != -1) {
  683. oc_count++;
  684. i= slapi_attr_next_value(aoc,i,&v);
  685. }
  686. oclist = (struct objclass**)
  687. slapi_ch_malloc((oc_count+1)*sizeof(struct objclass*));
  688. /*
  689. * Need the read lock to create the oc array and while we use it.
  690. */
  691. if (!(schema_flags & DSE_SCHEMA_LOCKED))
  692. oc_lock_read();
  693. oc_count = 0;
  694. for (i= slapi_attr_first_value(aoc,&v); i != -1;
  695. i= slapi_attr_next_value(aoc,i,&v)) {
  696. ocname = slapi_value_get_string(v);
  697. if ( !ocname ) {
  698. LDAPDebug( LDAP_DEBUG_ANY,
  699. "Entry \"%s\" \"objectclass\" value missing\n",
  700. slapi_entry_get_dn_const(e), 0, 0 );
  701. if (pb) {
  702. PR_snprintf( errtext, sizeof( errtext ),
  703. "missing \"objectclass\" value\n" );
  704. slapi_pblock_set( pb, SLAPI_PB_RESULT_TEXT, errtext );
  705. }
  706. ret = 1;
  707. goto out;
  708. }
  709. if ( isExtensibleObjectclass( ocname )) {
  710. /*
  711. * if the entry is an extensibleObject, just check to see if
  712. * the required attributes for whatever other objectclasses the
  713. * entry might be are present. All other attributes are allowed
  714. */
  715. is_extensible_object = 1;
  716. continue;
  717. }
  718. if ((oc = oc_find_nolock( ocname, NULL, PR_FALSE )) != NULL ) {
  719. oclist[oc_count++] = oc;
  720. } else {
  721. /* we don't know about the oc; return an appropriate error message */
  722. char ebuf[ BUFSIZ ];
  723. size_t ocname_len = strlen( ocname );
  724. const char *extra_msg = "";
  725. if ( ocname_len > 0 && isspace( ocname[ ocname_len-1 ] )) {
  726. if ( ocname_len > 1 && isspace( ocname[ ocname_len-2 ] )) {
  727. extra_msg = " (remove the trailing spaces)";
  728. } else {
  729. extra_msg = " (remove the trailing space)";
  730. }
  731. }
  732. LDAPDebug( LDAP_DEBUG_ANY,
  733. "Entry \"%s\" has unknown object class \"%s\"%s\n",
  734. slapi_entry_get_dn_const(e),
  735. escape_string(ocname, ebuf), extra_msg );
  736. if (pb) {
  737. PR_snprintf( errtext, sizeof( errtext ),
  738. "unknown object class \"%s\"%s\n",
  739. escape_string(ocname, ebuf), extra_msg );
  740. slapi_pblock_set( pb, SLAPI_PB_RESULT_TEXT, errtext );
  741. }
  742. unknown_class = 1;
  743. }
  744. }
  745. oclist[oc_count] = NULL;
  746. if (unknown_class) {
  747. /* failure */
  748. ret = 1;
  749. goto out;
  750. }
  751. /*
  752. * go through all the checking so we can log everything
  753. * wrong with the entry. some day, we might want to return
  754. * this information to the client as an error message.
  755. */
  756. /*
  757. * check that the entry has required attrs for each oc
  758. */
  759. for (i = 0; oclist[i] != NULL; i++) {
  760. if ( oc_check_required( pb, e, oclist[i] ) != 0 ) {
  761. ret = 1;
  762. goto out;
  763. }
  764. }
  765. /*
  766. * check that each attr in the entry is allowed by some oc,
  767. * and that single-valued attrs only have one value
  768. */
  769. {
  770. Slapi_Attr *prevattr;
  771. i = slapi_entry_first_attr(e, &a);
  772. while (-1 != i && 0 == ret)
  773. {
  774. if (is_extensible_object == 0 &&
  775. unknown_class == 0 &&
  776. !slapi_attr_flag_is_set(a, SLAPI_ATTR_FLAG_OPATTR))
  777. {
  778. char *attrtype;
  779. slapi_attr_get_type(a, &attrtype);
  780. if (oc_check_allowed_sv(pb, e, attrtype, oclist) != 0)
  781. {
  782. ret = 1;
  783. }
  784. }
  785. if ( slapi_attr_flag_is_set( a, SLAPI_ATTR_FLAG_SINGLE ) ) {
  786. if (slapi_valueset_count(&a->a_present_values) > 1)
  787. {
  788. LDAPDebug( LDAP_DEBUG_ANY,
  789. "Entry \"%s\" single-valued attribute \"%s\" has multiple values\n",
  790. slapi_entry_get_dn_const(e),
  791. a->a_type, 0 );
  792. if (pb) {
  793. PR_snprintf( errtext, sizeof( errtext ),
  794. "single-valued attribute \"%s\" has multiple values\n",
  795. a->a_type );
  796. slapi_pblock_set( pb, SLAPI_PB_RESULT_TEXT, errtext );
  797. }
  798. ret = 1;
  799. }
  800. }
  801. prevattr = a;
  802. i = slapi_entry_next_attr(e, prevattr, &a);
  803. }
  804. }
  805. out:
  806. /* Done with the oc array so can release the lock */
  807. if (!(schema_flags & DSE_SCHEMA_LOCKED))
  808. oc_unlock();
  809. slapi_ch_free((void**)&oclist);
  810. return( ret );
  811. }
  812. /*
  813. * The caller must obtain a read lock first by calling oc_lock_read().
  814. */
  815. static int
  816. oc_check_required( Slapi_PBlock *pb, Slapi_Entry *e, struct objclass *oc )
  817. {
  818. int i;
  819. int rc = 0; /* success, by default */
  820. Slapi_Attr *a;
  821. if (oc == NULL || oc->oc_required == NULL || oc->oc_required[0] == NULL) {
  822. return 0; /* success, as none required */
  823. }
  824. /* for each required attribute */
  825. for ( i = 0; oc->oc_required[i] != NULL; i++ ) {
  826. /* see if it's in the entry */
  827. for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
  828. if ( slapi_attr_type_cmp( oc->oc_required[i], a->a_type,
  829. SLAPI_TYPE_CMP_SUBTYPE ) == 0 ) {
  830. break;
  831. }
  832. }
  833. /* not there => schema violation */
  834. if ( a == NULL ) {
  835. char errtext[ BUFSIZ ];
  836. LDAPDebug( LDAP_DEBUG_ANY,
  837. "Entry \"%s\" missing attribute \"%s\" required"
  838. " by object class \"%s\"\n",
  839. slapi_entry_get_dn_const(e),
  840. oc->oc_required[i], oc->oc_name);
  841. if (pb) {
  842. PR_snprintf( errtext, sizeof( errtext ),
  843. "missing attribute \"%s\" required"
  844. " by object class \"%s\"\n",
  845. oc->oc_required[i], oc->oc_name );
  846. slapi_pblock_set( pb, SLAPI_PB_RESULT_TEXT, errtext );
  847. }
  848. rc = 1; /* failure */
  849. }
  850. }
  851. return rc;
  852. }
  853. /*
  854. * The caller must obtain a read lock first by calling oc_lock_read().
  855. */
  856. static int
  857. oc_check_allowed_sv(Slapi_PBlock *pb, Slapi_Entry *e, const char *type, struct objclass **oclist )
  858. {
  859. struct objclass *oc;
  860. int i, j;
  861. int rc = 1; /* failure */
  862. /* always allow objectclass and entryid attributes */
  863. /* MFW XXX THESE SHORTCUTS SHOULD NOT BE NECESSARY BUT THEY MASK
  864. * MFW XXX OTHER BUGS IN THE SERVER.
  865. */
  866. if ( slapi_attr_type_cmp( type, "objectclass", SLAPI_TYPE_CMP_EXACT ) == 0 ) {
  867. return( 0 );
  868. } else if ( slapi_attr_type_cmp( type, "entryid", SLAPI_TYPE_CMP_EXACT ) == 0 ) {
  869. return( 0 );
  870. }
  871. /* check that the type appears as req or opt in at least one oc */
  872. for (i = 0; rc != 0 && oclist[i] != NULL; i++) {
  873. oc = oclist[i];
  874. /* does it require the type? */
  875. for ( j = 0; oc->oc_required && oc->oc_required[j] != NULL; j++ ) {
  876. if ( slapi_attr_type_cmp( oc->oc_required[j],
  877. type, SLAPI_TYPE_CMP_SUBTYPE ) == 0 ) {
  878. rc = 0;
  879. break;
  880. }
  881. }
  882. if ( 0 != rc ) {
  883. /* does it allow the type? */
  884. for ( j = 0; oc->oc_allowed && oc->oc_allowed[j] != NULL; j++ ) {
  885. if ( slapi_attr_type_cmp( oc->oc_allowed[j],
  886. type, SLAPI_TYPE_CMP_SUBTYPE ) == 0 ||
  887. strcmp( oc->oc_allowed[j],"*" ) == 0 ) {
  888. rc = 0;
  889. break;
  890. }
  891. }
  892. /* maybe the next oc allows it */
  893. }
  894. }
  895. if ( 0 != rc ) {
  896. char errtext[ BUFSIZ ];
  897. char ebuf[ BUFSIZ ];
  898. LDAPDebug( LDAP_DEBUG_ANY,
  899. "Entry \"%s\" -- attribute \"%s\" not allowed\n",
  900. slapi_entry_get_dn_const(e),
  901. escape_string( type, ebuf ),
  902. 0);
  903. if (pb) {
  904. PR_snprintf( errtext, sizeof( errtext ),
  905. "attribute \"%s\" not allowed\n",
  906. escape_string( type, ebuf ) );
  907. slapi_pblock_set( pb, SLAPI_PB_RESULT_TEXT, errtext );
  908. }
  909. }
  910. return rc;
  911. }
  912. /*
  913. * oc_find_name() will return a strdup'd string or NULL if the objectclass
  914. * could not be found.
  915. */
  916. char *
  917. oc_find_name( const char *name_or_oid )
  918. {
  919. struct objclass *oc;
  920. char *ocname = NULL;
  921. oc_lock_read();
  922. if ( NULL != ( oc = oc_find_nolock( name_or_oid, NULL, PR_FALSE ))) {
  923. ocname = slapi_ch_strdup( oc->oc_name );
  924. }
  925. oc_unlock();
  926. return ocname;
  927. }
  928. /*
  929. * oc_find_nolock will return a pointer to the objectclass which has the
  930. * same name OR oid.
  931. * NULL is returned if no match is found or `name_or_oid' is NULL.
  932. */
  933. static struct objclass *
  934. oc_find_nolock( const char *ocname_or_oid, struct objclass *oc_private, PRBool use_private)
  935. {
  936. struct objclass *oc;
  937. if ( NULL != ocname_or_oid ) {
  938. if ( !schema_ignore_trailing_spaces ) {
  939. if (use_private) {
  940. oc = oc_private;
  941. } else {
  942. oc = g_get_global_oc_nolock();
  943. }
  944. for ( ; oc != NULL; oc = oc->oc_next ) {
  945. if ( ( strcasecmp( oc->oc_name, ocname_or_oid ) == 0 )
  946. || ( oc->oc_oid &&
  947. strcasecmp( oc->oc_oid, ocname_or_oid ) == 0 )) {
  948. return( oc );
  949. }
  950. }
  951. } else {
  952. const char *p;
  953. size_t len;
  954. /*
  955. * Ignore trailing spaces when comparing object class names.
  956. */
  957. for ( p = ocname_or_oid, len = 0; (*p != '\0') && (*p != ' ');
  958. p++, len++ ) {
  959. ; /* NULL */
  960. }
  961. if (use_private) {
  962. oc = oc_private;
  963. } else {
  964. oc = g_get_global_oc_nolock();
  965. }
  966. for ( ; oc != NULL; oc = oc->oc_next ) {
  967. if ( ( (strncasecmp( oc->oc_name, ocname_or_oid, len ) == 0)
  968. && (len == strlen(oc->oc_name)) )
  969. ||
  970. ( oc->oc_oid &&
  971. ( strncasecmp( oc->oc_oid, ocname_or_oid, len ) == 0)
  972. && (len == strlen(oc->oc_oid)) ) ) {
  973. return( oc );
  974. }
  975. }
  976. }
  977. }
  978. return( NULL );
  979. }
  980. /*
  981. * oc_find_oid_nolock will return a pointer to the objectclass which has
  982. * the same oid.
  983. * NULL is returned if no match is found or `ocoid' is NULL.
  984. */
  985. static struct objclass *
  986. oc_find_oid_nolock( const char *ocoid )
  987. {
  988. struct objclass *oc;
  989. if ( NULL != ocoid ) {
  990. for ( oc = g_get_global_oc_nolock(); oc != NULL; oc = oc->oc_next ) {
  991. if ( ( oc->oc_oid &&
  992. ( strcasecmp( oc->oc_oid, ocoid ) == 0)) ){
  993. return( oc );
  994. }
  995. }
  996. }
  997. return( NULL );
  998. }
  999. /*
  1000. We need to keep the objectclasses in the same order as defined in the ldif files. If not
  1001. SUP dependencies will break. When the user redefines an existing objectclass this code
  1002. makes sure it is put back in the same order it was read to from the ldif file. It also
  1003. verifies that the entries oc_superior value preceeds it in the chain. If not it will not
  1004. allow the entry to be added. This makes sure that the ldif will be written back correctly.
  1005. */
  1006. static int
  1007. oc_replace_nolock(const char *ocname, struct objclass *newoc) {
  1008. struct objclass *oc, *pnext;
  1009. int rc = LDAP_SUCCESS;
  1010. PRBool saw_sup=PR_FALSE;
  1011. oc = g_get_global_oc_nolock();
  1012. if(newoc->oc_superior == NULL)
  1013. {
  1014. saw_sup=PR_TRUE;
  1015. }
  1016. /* don't check SUP dependency for first one because it always/should be top */
  1017. if (strcasecmp (oc->oc_name, ocname) == 0) {
  1018. newoc->oc_next=oc->oc_next;
  1019. g_set_global_oc_nolock ( newoc );
  1020. oc_free( &oc );
  1021. } else {
  1022. for (pnext = oc ; pnext != NULL;
  1023. oc = pnext, pnext = pnext->oc_next) {
  1024. if(pnext->oc_name == NULL) {
  1025. rc = LDAP_OPERATIONS_ERROR;
  1026. break;
  1027. }
  1028. if(newoc->oc_superior != NULL) {
  1029. if(strcasecmp( pnext->oc_name, newoc->oc_superior) == 0)
  1030. {
  1031. saw_sup=PR_TRUE;
  1032. }
  1033. }
  1034. if (strcasecmp ( pnext->oc_name, ocname ) == 0) {
  1035. if(saw_sup)
  1036. {
  1037. oc->oc_next=newoc;
  1038. newoc->oc_next=pnext->oc_next;
  1039. oc_free( &pnext );
  1040. break;
  1041. } else
  1042. {
  1043. rc = LDAP_TYPE_OR_VALUE_EXISTS;
  1044. break;
  1045. }
  1046. }
  1047. }
  1048. }
  1049. return rc;
  1050. }
  1051. static int
  1052. oc_delete_nolock (char *ocname)
  1053. {
  1054. struct objclass *oc, *pnext;
  1055. int rc = 0; /* failure */
  1056. oc = g_get_global_oc_nolock();
  1057. /* special case if we're removing the first oc */
  1058. if (strcasecmp (oc->oc_name, ocname) == 0) {
  1059. g_set_global_oc_nolock ( oc->oc_next );
  1060. oc_free( &oc );
  1061. rc = 1;
  1062. } else {
  1063. for (pnext = oc->oc_next ; pnext != NULL;
  1064. oc = pnext, pnext = pnext->oc_next) {
  1065. if (strcasecmp ( pnext->oc_name, ocname ) == 0) {
  1066. oc->oc_next = pnext->oc_next;
  1067. oc_free( &pnext );
  1068. rc = 1;
  1069. break;
  1070. }
  1071. }
  1072. }
  1073. return rc;
  1074. }
  1075. static void
  1076. oc_delete_all_nolock( void )
  1077. {
  1078. struct objclass *oc, *pnext;
  1079. oc = g_get_global_oc_nolock();
  1080. for (pnext = oc->oc_next; oc;
  1081. oc = pnext, pnext = oc?oc->oc_next:NULL) {
  1082. oc_free( &oc );
  1083. }
  1084. g_set_global_oc_nolock ( NULL );
  1085. }
  1086. /*
  1087. * Compare two objectclass definitions for equality. Return PR_TRUE if
  1088. * they are equivalent and PR_FALSE if not.
  1089. *
  1090. * The oc_required and oc_allowed arrays are ignored.
  1091. * The string "user defined" is ignored within the origins array.
  1092. * The following flags are ignored:
  1093. * OC_FLAG_STANDARD_OC
  1094. * OC_FLAG_USER_OC
  1095. * OC_FLAG_REDEFINED_OC
  1096. */
  1097. static PRBool
  1098. oc_equal( struct objclass *oc1, struct objclass *oc2 )
  1099. {
  1100. PRUint8 flagmask;
  1101. if ( schema_strcmp( oc1->oc_name, oc2->oc_name ) != 0
  1102. || schema_strcmp( oc1->oc_desc, oc2->oc_desc ) != 0
  1103. || schema_strcmp( oc1->oc_oid, oc2->oc_oid ) != 0
  1104. || schema_strcmp( oc1->oc_superior, oc2->oc_superior ) != 0 ) {
  1105. return PR_FALSE;
  1106. }
  1107. flagmask = ~(OC_FLAG_STANDARD_OC | OC_FLAG_USER_OC | OC_FLAG_REDEFINED_OC);
  1108. if ( oc1->oc_kind != oc2->oc_kind
  1109. || ( oc1->oc_flags & flagmask ) != ( oc2->oc_flags & flagmask )) {
  1110. return PR_FALSE;
  1111. }
  1112. if ( schema_strcmp_array( oc1->oc_orig_required, oc2->oc_orig_required,
  1113. NULL ) != 0
  1114. || schema_strcmp_array( oc1->oc_orig_allowed, oc2->oc_orig_allowed,
  1115. NULL ) != 0
  1116. || schema_extension_cmp( oc1->oc_extensions, oc2->oc_extensions ) != 0 ) {
  1117. return PR_FALSE;
  1118. }
  1119. return PR_TRUE;
  1120. }
  1121. #ifdef OC_DEBUG
  1122. static int
  1123. oc_print( struct objclass *oc )
  1124. {
  1125. int i;
  1126. printf( "object class %s\n", oc->oc_name );
  1127. if ( oc->oc_required != NULL ) {
  1128. printf( "\trequires %s", oc->oc_required[0] );
  1129. for ( i = 1; oc->oc_required[i] != NULL; i++ ) {
  1130. printf( ",%s", oc->oc_required[i] );
  1131. }
  1132. printf( "\n" );
  1133. }
  1134. if ( oc->oc_allowed != NULL ) {
  1135. printf( "\tallows %s", oc->oc_allowed[0] );
  1136. for ( i = 1; oc->oc_allowed[i] != NULL; i++ ) {
  1137. printf( ",%s", oc->oc_allowed[i] );
  1138. }
  1139. printf( "\n" );
  1140. }
  1141. return 0;
  1142. }
  1143. #endif
  1144. /*
  1145. * Compare the X-ORIGIN extension, other extensions can be ignored
  1146. */
  1147. static int
  1148. schema_extension_cmp(schemaext *e1, schemaext *e2)
  1149. {
  1150. schemaext *e1_head = e1;
  1151. schemaext *e2_head = e2;
  1152. int found = 0;
  1153. int e1_has_origin = 0;
  1154. int e2_has_origin = 0;
  1155. int i, ii;
  1156. if(e1 == NULL && e2 == NULL){
  1157. return 0; /* match */
  1158. } else if (e1 == NULL || e2 == NULL){
  1159. return -1;
  1160. }
  1161. while(e1){
  1162. if(strcmp(e1->term, "X-ORIGIN")){
  1163. e1 = e1->next;
  1164. continue;
  1165. }
  1166. e1_has_origin = 1;
  1167. while(e2){
  1168. if(strcmp(e1->term, e2->term) == 0)
  1169. {
  1170. e2_has_origin = 1;
  1171. if(e1->values == NULL && e2->values == NULL){
  1172. return 0;
  1173. } else if (e1->values == NULL || e2->values == NULL){
  1174. return -1;
  1175. }
  1176. for (i = 0; e1->values[i]; i++)
  1177. {
  1178. found = 0;
  1179. for(ii = 0; e2->values[ii]; ii++)
  1180. {
  1181. if(strcmp(e1->values[i], e2->values[ii]) == 0){
  1182. found = 1;
  1183. break;
  1184. }
  1185. }
  1186. if(!found){
  1187. return -1;
  1188. }
  1189. }
  1190. /* So far so good, move on to the next check */
  1191. goto next;
  1192. }
  1193. e2 = e2->next;
  1194. }
  1195. e2 = e2_head;
  1196. e1 = e1->next;
  1197. }
  1198. if(e1_has_origin != e2_has_origin){
  1199. return -1;
  1200. } else if (e1_has_origin == 0 && e2_has_origin == 0){
  1201. return 0;
  1202. }
  1203. next:
  1204. /*
  1205. * We know that e2 has the same extensions as e1, but does e1 have all the extensions as e2?
  1206. * Run the compare in reverse...
  1207. */
  1208. found = 0;
  1209. e1 = e1_head;
  1210. e2 = e2_head;
  1211. while(e2){
  1212. if(strcmp(e2->term, "X-ORIGIN")){
  1213. e2 = e2->next;
  1214. continue;
  1215. }
  1216. while(e1){
  1217. if(strcmp(e2->term, e1->term) == 0)
  1218. {
  1219. if(e2->values == NULL && e1->values == NULL){
  1220. return 0;
  1221. } else if (e1->values == NULL || e2->values == NULL){
  1222. return -1;
  1223. }
  1224. for (i = 0; e2->values[i]; i++)
  1225. {
  1226. found = 0;
  1227. for(ii = 0; e1->values[ii]; ii++)
  1228. {
  1229. if(strcmp(e2->values[i], e1->values[ii]) == 0){
  1230. found = 1;
  1231. break;
  1232. }
  1233. }
  1234. if(!found){
  1235. return -1;
  1236. }
  1237. }
  1238. return 0;
  1239. }
  1240. e1 = e1->next;
  1241. }
  1242. e1 = e1_head;
  1243. e2 = e2->next;
  1244. }
  1245. return 0;
  1246. }
  1247. /*
  1248. * Compare two attrsyntax definitions for equality. Return PR_TRUE if
  1249. * they are equivalent and PR_FALSE if not.
  1250. *
  1251. * The string "user defined" is ignored within the origins array.
  1252. * The following flags are ignored:
  1253. * SLAPI_ATTR_FLAG_STD_ATTR
  1254. * SLAPI_ATTR_FLAG_NOLOCKING
  1255. * SLAPI_ATTR_FLAG_OVERRIDE
  1256. */
  1257. static PRBool
  1258. attr_syntax_equal( struct asyntaxinfo *asi1, struct asyntaxinfo *asi2 )
  1259. {
  1260. unsigned long flagmask;
  1261. flagmask = ~( SLAPI_ATTR_FLAG_STD_ATTR | SLAPI_ATTR_FLAG_NOLOCKING
  1262. | SLAPI_ATTR_FLAG_OVERRIDE );
  1263. if ( schema_strcmp( asi1->asi_oid, asi2->asi_oid ) != 0
  1264. || schema_strcmp( asi1->asi_name, asi2->asi_name ) != 0
  1265. || schema_strcmp( asi1->asi_desc, asi2->asi_desc ) != 0
  1266. || schema_strcmp( asi1->asi_superior, asi2->asi_superior ) != 0
  1267. || schema_strcmp( asi1->asi_mr_equality, asi2->asi_mr_equality )
  1268. != 0
  1269. || schema_strcmp( asi1->asi_mr_ordering, asi2->asi_mr_ordering )
  1270. != 0
  1271. || schema_strcmp( asi1->asi_mr_substring,
  1272. asi2->asi_mr_substring ) != 0 ) {
  1273. return PR_FALSE;
  1274. }
  1275. if ( schema_strcmp_array( asi1->asi_aliases, asi2->asi_aliases, NULL ) != 0
  1276. || schema_extension_cmp (asi1->asi_extensions, asi2->asi_extensions) != 0
  1277. || asi1->asi_plugin != asi2->asi_plugin
  1278. || ( asi1->asi_flags & flagmask ) !=
  1279. ( asi2->asi_flags & flagmask )
  1280. || asi1->asi_syntaxlength != asi2->asi_syntaxlength ) {
  1281. return PR_FALSE;
  1282. }
  1283. return PR_TRUE;
  1284. }
  1285. /*
  1286. * Like strcmp(), but a NULL string pointer is treated as equivalent to
  1287. * another NULL one and NULL is treated as "less than" all non-NULL values.
  1288. */
  1289. static int
  1290. schema_strcmp( const char *s1, const char *s2 )
  1291. {
  1292. if ( s1 == NULL ) {
  1293. if ( s2 == NULL ) {
  1294. return 0; /* equal */
  1295. }
  1296. return -1; /* s1 < s2 */
  1297. }
  1298. if ( s2 == NULL ) {
  1299. return 1; /* s1 > s2 */
  1300. }
  1301. return strcmp( s1, s2 );
  1302. }
  1303. /*
  1304. * Invoke strcmp() on each string in an array. If one array has fewer elements
  1305. * than the other, it is treated as "less than" the other. Two NULL or
  1306. * empty arrays (or one NULL and one empty) are considered to be equivalent.
  1307. *
  1308. * If ignorestr is non-NULL, occurrences of that string are ignored.
  1309. */
  1310. static int
  1311. schema_strcmp_array( char **sa1, char **sa2, const char *ignorestr )
  1312. {
  1313. int i1, i2, rc;
  1314. if ( sa1 == NULL || *sa1 == NULL ) {
  1315. if ( sa2 == NULL || *sa2 == NULL ) {
  1316. return 0; /* equal */
  1317. }
  1318. return -1; /* sa1 < sa2 */
  1319. }
  1320. if ( sa2 == NULL || *sa2 == NULL ) {
  1321. return 1; /* sa1 > sa2 */
  1322. }
  1323. rc = 0;
  1324. i1 = i2 = 0;
  1325. while ( sa1[i1] != NULL && sa2[i2] != NULL ) {
  1326. if ( NULL != ignorestr ) {
  1327. if ( 0 == strcmp( sa1[i1], ignorestr )) {
  1328. ++i1;
  1329. continue;
  1330. }
  1331. if ( 0 == strcmp( sa2[i2], ignorestr )) {
  1332. ++i2;
  1333. continue;
  1334. }
  1335. }
  1336. rc = strcmp( sa1[i1], sa2[i2] );
  1337. ++i1;
  1338. ++i2;
  1339. }
  1340. if ( rc == 0 ) { /* all matched so far */
  1341. /* get rid of trailing ignored strings (if any) */
  1342. if ( NULL != ignorestr ) {
  1343. if ( sa1[i1] != NULL && 0 == strcmp( sa1[i1], ignorestr )) {
  1344. ++i1;
  1345. }
  1346. if ( sa2[i2] != NULL && 0 == strcmp( sa2[i2], ignorestr )) {
  1347. ++i2;
  1348. }
  1349. }
  1350. /* check for differing array lengths */
  1351. if ( sa2[i2] != NULL ) {
  1352. rc = -1; /* sa1 < sa2 -- fewer elements */
  1353. } else if ( sa1[i1] != NULL ) {
  1354. rc = 1; /* sa1 > sa2 -- more elements */
  1355. }
  1356. }
  1357. return rc;
  1358. }
  1359. struct attr_enum_wrapper {
  1360. Slapi_Attr **attrs;
  1361. int enquote_sup_oc;
  1362. struct sizedbuffer *psbAttrTypes;
  1363. int user_defined_only;
  1364. int schema_ds4x_compat;
  1365. };
  1366. static int
  1367. schema_attr_enum_callback(struct asyntaxinfo *asip, void *arg)
  1368. {
  1369. struct attr_enum_wrapper *aew = (struct attr_enum_wrapper *)arg;
  1370. int aliaslen = 0;
  1371. struct berval val;
  1372. struct berval *vals[2] = {0, 0};
  1373. const char *attr_desc, *syntaxoid;
  1374. char *outp, syntaxlengthbuf[ 128 ];
  1375. int i;
  1376. vals[0] = &val;
  1377. if (!asip) {
  1378. LDAPDebug(LDAP_DEBUG_ANY,
  1379. "Error: no attribute types in schema_attr_enum_callback\n",
  1380. 0, 0, 0);
  1381. return ATTR_SYNTAX_ENUM_NEXT;
  1382. }
  1383. if (aew->user_defined_only &&
  1384. (asip->asi_flags & SLAPI_ATTR_FLAG_STD_ATTR)) {
  1385. return ATTR_SYNTAX_ENUM_NEXT; /* not user defined */
  1386. }
  1387. if ( aew->schema_ds4x_compat ) {
  1388. attr_desc = ( asip->asi_flags & SLAPI_ATTR_FLAG_STD_ATTR)
  1389. ? ATTR_STANDARD_STRING : ATTR_USERDEF_STRING;
  1390. } else {
  1391. attr_desc = asip->asi_desc;
  1392. }
  1393. if ( asip->asi_aliases != NULL ) {
  1394. for ( i = 0; asip->asi_aliases[i] != NULL; ++i ) {
  1395. aliaslen += strlen( asip->asi_aliases[i] );
  1396. }
  1397. }
  1398. syntaxoid = asip->asi_plugin->plg_syntax_oid;
  1399. if ( !aew->schema_ds4x_compat &&
  1400. asip->asi_syntaxlength != SLAPI_SYNTAXLENGTH_NONE ) {
  1401. /* sprintf() is safe because syntaxlengthbuf is large enough */
  1402. sprintf( syntaxlengthbuf, "{%d}", asip->asi_syntaxlength );
  1403. } else {
  1404. *syntaxlengthbuf = '\0';
  1405. }
  1406. /*
  1407. * XXX: 256 is a magic number... it must be big enough to account for
  1408. * all of the fixed sized items we output.
  1409. */
  1410. sizedbuffer_allocate(aew->psbAttrTypes,256+strlen(asip->asi_oid)+
  1411. strlen(asip->asi_name) +
  1412. aliaslen + strlen_null_ok(attr_desc) +
  1413. strlen(syntaxoid) +
  1414. strlen_null_ok(asip->asi_superior) +
  1415. strlen_null_ok(asip->asi_mr_equality) +
  1416. strlen_null_ok(asip->asi_mr_ordering) +
  1417. strlen_null_ok(asip->asi_mr_substring) +
  1418. strcat_extensions( NULL, asip->asi_extensions ));
  1419. /*
  1420. * Overall strategy is to maintain a pointer to the next location in
  1421. * the output buffer so we can do simple strcpy's, sprintf's, etc.
  1422. * That pointer is `outp'. Each item that is output includes a trailing
  1423. * space, so there is no need to include a leading one in the next item.
  1424. */
  1425. outp = aew->psbAttrTypes->buffer;
  1426. outp += sprintf(outp, "( %s NAME ", asip->asi_oid);
  1427. if ( asip->asi_aliases == NULL || asip->asi_aliases[0] == NULL ) {
  1428. /* only one name */
  1429. outp += sprintf(outp, "'%s' ", asip->asi_name);
  1430. } else {
  1431. /* several names */
  1432. outp += sprintf(outp, "( '%s' ", asip->asi_name);
  1433. for ( i = 0; asip->asi_aliases[i] != NULL; ++i ) {
  1434. outp += sprintf(outp, "'%s' ", asip->asi_aliases[i]);
  1435. }
  1436. outp += strcpy_count(outp, ") ");
  1437. }
  1438. /* DESC is optional */
  1439. if (attr_desc && *attr_desc) {
  1440. outp += sprintf( outp, "DESC '%s'", attr_desc );
  1441. }
  1442. if ( !aew->schema_ds4x_compat &&
  1443. ( asip->asi_flags & SLAPI_ATTR_FLAG_OBSOLETE )) {
  1444. outp += strcpy_count( outp, schema_obsolete_with_spaces );
  1445. } else {
  1446. outp += strcpy_count( outp, " " );
  1447. }
  1448. if ( !aew->schema_ds4x_compat ) {
  1449. outp += put_tagged_oid( outp, "SUP ",
  1450. asip->asi_superior, NULL, aew->enquote_sup_oc );
  1451. outp += put_tagged_oid( outp, "EQUALITY ",
  1452. asip->asi_mr_equality, NULL, aew->enquote_sup_oc );
  1453. outp += put_tagged_oid( outp, "ORDERING ",
  1454. asip->asi_mr_ordering, NULL, aew->enquote_sup_oc );
  1455. outp += put_tagged_oid( outp, "SUBSTR ",
  1456. asip->asi_mr_substring, NULL, aew->enquote_sup_oc );
  1457. }
  1458. outp += put_tagged_oid( outp, "SYNTAX ", syntaxoid, syntaxlengthbuf,
  1459. aew->enquote_sup_oc );
  1460. if (asip->asi_flags & SLAPI_ATTR_FLAG_SINGLE) {
  1461. outp += strcpy_count(outp, "SINGLE-VALUE ");
  1462. }
  1463. if ( !aew->schema_ds4x_compat ) {
  1464. if (asip->asi_flags & SLAPI_ATTR_FLAG_COLLECTIVE ) {
  1465. outp += strcpy_count( outp, 1 + schema_collective_with_spaces );
  1466. }
  1467. if (asip->asi_flags & SLAPI_ATTR_FLAG_NOUSERMOD ) {
  1468. outp += strcpy_count( outp, 1 + schema_nousermod_with_spaces );
  1469. }
  1470. if (asip->asi_flags & SLAPI_ATTR_FLAG_DISTRIBUTED_OPERATION) {
  1471. outp += strcpy_count(outp, "USAGE distributedOperation ");
  1472. } else if (asip->asi_flags & SLAPI_ATTR_FLAG_DSA_OPERATION) {
  1473. outp += strcpy_count(outp, "USAGE dSAOperation ");
  1474. } else if (asip->asi_flags & SLAPI_ATTR_FLAG_OPATTR) {
  1475. outp += strcpy_count(outp, "USAGE directoryOperation ");
  1476. }
  1477. outp += strcat_extensions( outp, asip->asi_extensions );
  1478. }
  1479. outp += strcpy_count(outp, ")");
  1480. val.bv_val = aew->psbAttrTypes->buffer;
  1481. val.bv_len = outp - aew->psbAttrTypes->buffer;
  1482. attrlist_merge(aew->attrs, "attributetypes", vals);
  1483. return ATTR_SYNTAX_ENUM_NEXT;
  1484. }
  1485. struct syntax_enum_wrapper {
  1486. Slapi_Attr **attrs;
  1487. struct sizedbuffer *psbSyntaxDescription;
  1488. };
  1489. static int
  1490. schema_syntax_enum_callback(char **names, Slapi_PluginDesc *plugindesc,
  1491. void *arg)
  1492. {
  1493. struct syntax_enum_wrapper *sew = (struct syntax_enum_wrapper *)arg;
  1494. char *oid, *desc;
  1495. int i;
  1496. struct berval val;
  1497. struct berval *vals[2] = {0, 0};
  1498. vals[0] = &val;
  1499. oid = NULL;
  1500. if ( names != NULL ) {
  1501. for ( i = 0; names[i] != NULL; ++i ) {
  1502. if ( isdigit( names[i][0] )) {
  1503. oid = names[i];
  1504. break;
  1505. }
  1506. }
  1507. }
  1508. if ( oid == NULL ) { /* must have an OID */
  1509. LDAPDebug(LDAP_DEBUG_ANY, "Error: no OID found in"
  1510. " schema_syntax_enum_callback for syntax %s\n",
  1511. ( names == NULL ) ? "unknown" : names[0], 0, 0);
  1512. return 1;
  1513. }
  1514. desc = names[0]; /* by convention, the first name is the "official" one */
  1515. /*
  1516. * RFC 2252 section 4.3.3 Syntax Description says:
  1517. *
  1518. * The following BNF may be used to associate a short description with a
  1519. * syntax OBJECT IDENTIFIER. Implementors should note that future
  1520. * versions of this document may expand this definition to include
  1521. * additional terms. Terms whose identifier begins with "X-" are
  1522. * reserved for private experiments, and MUST be followed by a
  1523. * <qdstrings>.
  1524. *
  1525. * SyntaxDescription = "(" whsp
  1526. * numericoid whsp
  1527. * [ "DESC" qdstring ]
  1528. * whsp ")"
  1529. *
  1530. * And section 5.3.1 ldapSyntaxes says:
  1531. *
  1532. * Servers MAY use this attribute to list the syntaxes which are
  1533. * implemented. Each value corresponds to one syntax.
  1534. *
  1535. * ( 1.3.6.1.4.1.1466.101.120.16 NAME 'ldapSyntaxes'
  1536. * EQUALITY objectIdentifierFirstComponentMatch
  1537. * SYNTAX 1.3.6.1.4.1.1466.115.121.1.54 USAGE directoryOperation )
  1538. */
  1539. if ( desc == NULL ) {
  1540. /* allocate enough room for "( )" and '\0' at end */
  1541. sizedbuffer_allocate(sew->psbSyntaxDescription, strlen(oid) + 5);
  1542. sprintf(sew->psbSyntaxDescription->buffer, "( %s )", oid );
  1543. } else {
  1544. /* allocate enough room for "( ) DESC '' " and '\0' at end */
  1545. sizedbuffer_allocate(sew->psbSyntaxDescription,
  1546. strlen(oid) + strlen(desc) + 13);
  1547. sprintf(sew->psbSyntaxDescription->buffer, "( %s DESC '%s' )",
  1548. oid, desc );
  1549. }
  1550. val.bv_val = sew->psbSyntaxDescription->buffer;
  1551. val.bv_len = strlen(sew->psbSyntaxDescription->buffer);
  1552. attrlist_merge(sew->attrs, "ldapSyntaxes", vals);
  1553. return 1;
  1554. }
  1555. struct listargs{
  1556. char **attrs;
  1557. unsigned long flag;
  1558. };
  1559. static int
  1560. schema_list_attributes_callback(struct asyntaxinfo *asi, void *arg)
  1561. {
  1562. struct listargs *aew = (struct listargs *)arg;
  1563. if (!asi) {
  1564. LDAPDebug(LDAP_DEBUG_ANY, "Error: no attribute types in schema_list_attributes_callback\n",
  1565. 0, 0, 0);
  1566. return ATTR_SYNTAX_ENUM_NEXT;
  1567. }
  1568. if (aew->flag && (asi->asi_flags & aew->flag)) {
  1569. #if defined(USE_OLD_UNHASHED)
  1570. /* skip unhashed password */
  1571. if (!is_type_forbidden(asi->asi_name)) {
  1572. #endif
  1573. charray_add(&aew->attrs, slapi_ch_strdup(asi->asi_name));
  1574. if (NULL != asi->asi_aliases) {
  1575. int i;
  1576. for ( i = 0; asi->asi_aliases[i] != NULL; ++i ) {
  1577. charray_add(&aew->attrs,
  1578. slapi_ch_strdup(asi->asi_aliases[i]));
  1579. }
  1580. }
  1581. #if defined(USE_OLD_UNHASHED)
  1582. }
  1583. #endif
  1584. }
  1585. return ATTR_SYNTAX_ENUM_NEXT;
  1586. }
  1587. /* Return the list of attributes names matching attribute flags */
  1588. char **
  1589. slapi_schema_list_attribute_names(unsigned long flag)
  1590. {
  1591. struct listargs aew;
  1592. memset(&aew,0,sizeof(struct listargs));
  1593. aew.flag=flag;
  1594. attr_syntax_enumerate_attrs(schema_list_attributes_callback, &aew,
  1595. PR_FALSE);
  1596. return aew.attrs;
  1597. }
  1598. /*
  1599. * returntext is always at least SLAPI_DSE_RETURNTEXT_SIZE bytes in size.
  1600. */
  1601. int
  1602. read_schema_dse(
  1603. Slapi_PBlock *pb,
  1604. Slapi_Entry *pschema_info_e,
  1605. Slapi_Entry *entryAfter,
  1606. int *returncode,
  1607. char *returntext /* not used */,
  1608. void *arg /* not used */ )
  1609. {
  1610. struct berval val;
  1611. struct berval *vals[2];
  1612. struct objclass *oc;
  1613. struct matchingRuleList *mrl=NULL;
  1614. struct sizedbuffer *psbObjectClasses= sizedbuffer_construct(BUFSIZ);
  1615. struct sizedbuffer *psbAttrTypes= sizedbuffer_construct(BUFSIZ);
  1616. struct sizedbuffer *psbMatchingRule= sizedbuffer_construct(BUFSIZ);
  1617. struct sizedbuffer *psbSyntaxDescription = sizedbuffer_construct(BUFSIZ);
  1618. struct attr_enum_wrapper aew;
  1619. struct syntax_enum_wrapper sew;
  1620. const CSN *csn;
  1621. char *mr_desc, *mr_name, *oc_description;
  1622. char **allowed, **required;
  1623. PRUint32 schema_flags = 0;
  1624. int enquote_sup_oc = config_get_enquote_sup_oc();
  1625. int schema_ds4x_compat = config_get_ds4_compatible_schema();
  1626. int user_defined_only = 0;
  1627. int i;
  1628. vals[0] = &val;
  1629. vals[1] = NULL;
  1630. slapi_pblock_get(pb, SLAPI_SCHEMA_FLAGS, (void*)&schema_flags);
  1631. user_defined_only = (schema_flags & DSE_SCHEMA_USER_DEFINED_ONLY) ? 1 : 0;
  1632. attrlist_delete (&pschema_info_e->e_attrs, "objectclasses");
  1633. attrlist_delete (&pschema_info_e->e_attrs, "attributetypes");
  1634. attrlist_delete (&pschema_info_e->e_attrs, "matchingRules");
  1635. attrlist_delete (&pschema_info_e->e_attrs, "ldapSyntaxes");
  1636. /*
  1637. * attrlist_delete (&pschema_info_e->e_attrs, "matchingRuleUse");
  1638. */
  1639. schema_dse_lock_read();
  1640. oc_lock_read();
  1641. /* return the objectclasses */
  1642. for (oc = g_get_global_oc_nolock(); oc != NULL; oc = oc->oc_next)
  1643. {
  1644. size_t size= 0;
  1645. int need_extra_space = 1;
  1646. if (user_defined_only &&
  1647. !((oc->oc_flags & OC_FLAG_USER_OC) ||
  1648. (oc->oc_flags & OC_FLAG_REDEFINED_OC) ))
  1649. {
  1650. continue;
  1651. }
  1652. /*
  1653. * XXX: 256 is a magic number... it must be large enough to fit
  1654. * all of the fixed size items including description (DESC),
  1655. * kind (STRUCTURAL, AUXILIARY, or ABSTRACT), and the OBSOLETE flag.
  1656. */
  1657. if ( schema_ds4x_compat ) {
  1658. oc_description = (oc->oc_flags & OC_FLAG_STANDARD_OC) ?
  1659. OC_STANDARD_STRING : OC_USERDEF_STRING;
  1660. } else {
  1661. oc_description = oc->oc_desc;
  1662. }
  1663. size = 256+strlen_null_ok(oc->oc_oid) + strlen(oc->oc_name) +
  1664. strlen_null_ok(oc_description) + strcat_extensions( NULL, oc->oc_extensions );
  1665. required = schema_ds4x_compat ? oc->oc_required : oc->oc_orig_required;
  1666. if (required && required[0]) {
  1667. for (i = 0 ; required[i]; i++)
  1668. size+= 16 + strlen(required[i]);
  1669. }
  1670. allowed = schema_ds4x_compat ? oc->oc_allowed : oc->oc_orig_allowed;
  1671. if (allowed && allowed[0]) {
  1672. for (i = 0 ; allowed[i]; i++)
  1673. size+= 16 + strlen(allowed[i]);
  1674. }
  1675. sizedbuffer_allocate(psbObjectClasses,size);
  1676. /* put the OID and the NAME */
  1677. sprintf (psbObjectClasses->buffer, "( %s NAME '%s'", (oc->oc_oid) ? oc->oc_oid : "", oc->oc_name);
  1678. /* The DESC (description) is OPTIONAL */
  1679. if (oc_description) {
  1680. strcat(psbObjectClasses->buffer, " DESC '");
  1681. /*
  1682. * We want to list an empty description
  1683. * element if it was defined that way.
  1684. */
  1685. if (*oc_description) {
  1686. strcat(psbObjectClasses->buffer, oc_description);
  1687. }
  1688. strcat(psbObjectClasses->buffer, "'");
  1689. need_extra_space = 1;
  1690. }
  1691. /* put the OBSOLETE keyword */
  1692. if (!schema_ds4x_compat && (oc->oc_flags & OC_FLAG_OBSOLETE)) {
  1693. strcat(psbObjectClasses->buffer, schema_obsolete_with_spaces);
  1694. need_extra_space = 0;
  1695. }
  1696. /* put the SUP superior objectclass */
  1697. if (0 != strcasecmp(oc->oc_name, "top")) { /* top has no SUP */
  1698. /*
  1699. * Some AUXILIARY AND ABSTRACT objectclasses may not have a SUP either
  1700. * for compatability, every objectclass other than top must have a SUP
  1701. */
  1702. if (schema_ds4x_compat || (oc->oc_superior && *oc->oc_superior)) {
  1703. if (need_extra_space) {
  1704. strcat(psbObjectClasses->buffer, " ");
  1705. }
  1706. strcat(psbObjectClasses->buffer, "SUP ");
  1707. strcat(psbObjectClasses->buffer, (enquote_sup_oc ? "'" : ""));
  1708. strcat(psbObjectClasses->buffer, ((oc->oc_superior && *oc->oc_superior) ?
  1709. oc->oc_superior : "top"));
  1710. strcat(psbObjectClasses->buffer, (enquote_sup_oc ? "'" : ""));
  1711. need_extra_space = 1;
  1712. }
  1713. }
  1714. /* put the kind of objectclass */
  1715. if (schema_ds4x_compat) {
  1716. if (need_extra_space) {
  1717. strcat(psbObjectClasses->buffer, " ");
  1718. }
  1719. } else {
  1720. strcat(psbObjectClasses->buffer, schema_oc_kind_strings_with_spaces[oc->oc_kind]);
  1721. }
  1722. strcat_oids( psbObjectClasses->buffer, "MUST", required, schema_ds4x_compat );
  1723. strcat_oids( psbObjectClasses->buffer, "MAY", allowed, schema_ds4x_compat );
  1724. if ( !schema_ds4x_compat ) {
  1725. strcat_extensions( psbObjectClasses->buffer, oc->oc_extensions );
  1726. }
  1727. strcat( psbObjectClasses->buffer, ")");
  1728. val.bv_val = psbObjectClasses->buffer;
  1729. val.bv_len = strlen (psbObjectClasses->buffer);
  1730. attrlist_merge (&pschema_info_e->e_attrs, "objectclasses", vals);
  1731. }
  1732. oc_unlock();
  1733. /* now return the attrs */
  1734. aew.attrs = &pschema_info_e->e_attrs;
  1735. aew.enquote_sup_oc = enquote_sup_oc;
  1736. aew.psbAttrTypes = psbAttrTypes;
  1737. aew.user_defined_only = user_defined_only;
  1738. aew.schema_ds4x_compat = schema_ds4x_compat;
  1739. attr_syntax_enumerate_attrs(schema_attr_enum_callback, &aew, PR_FALSE);
  1740. /* return the set of matching rules we support */
  1741. for (mrl = g_get_global_mrl(); !user_defined_only && mrl != NULL; mrl = mrl->mrl_next) {
  1742. mr_name = mrl->mr_entry->mr_name ? mrl->mr_entry->mr_name : "";
  1743. mr_desc = mrl->mr_entry->mr_desc ? mrl->mr_entry->mr_desc : "";
  1744. sizedbuffer_allocate(psbMatchingRule,128 + strlen_null_ok(mrl->mr_entry->mr_oid) +
  1745. strlen(mr_name)+ strlen(mr_desc) + strlen_null_ok(mrl->mr_entry->mr_syntax));
  1746. if ( schema_ds4x_compat ) {
  1747. sprintf(psbMatchingRule->buffer,
  1748. "( %s NAME '%s' DESC '%s' SYNTAX %s%s%s )",
  1749. (mrl->mr_entry->mr_oid ? mrl->mr_entry->mr_oid : ""),
  1750. mr_name, mr_desc, enquote_sup_oc ? "'" : "",
  1751. mrl->mr_entry->mr_syntax ? mrl->mr_entry->mr_syntax : "" ,
  1752. enquote_sup_oc ? "'" : "");
  1753. } else if ( NULL != mrl->mr_entry->mr_oid &&
  1754. NULL != mrl->mr_entry->mr_syntax ){
  1755. char *p;
  1756. sprintf(psbMatchingRule->buffer, "( %s ", mrl->mr_entry->mr_oid );
  1757. p = psbMatchingRule->buffer + strlen(psbMatchingRule->buffer);
  1758. if ( *mr_name != '\0' ) {
  1759. sprintf(p, "NAME '%s' ", mr_name );
  1760. p += strlen(p);
  1761. }
  1762. if ( *mr_desc != '\0' ) {
  1763. sprintf(p, "DESC '%s' ", mr_desc );
  1764. p += strlen(p);
  1765. }
  1766. sprintf(p, "SYNTAX %s )", mrl->mr_entry->mr_syntax );
  1767. }
  1768. val.bv_val = psbMatchingRule->buffer;
  1769. val.bv_len = strlen (psbMatchingRule->buffer);
  1770. attrlist_merge (&pschema_info_e->e_attrs, "matchingRules", vals);
  1771. }
  1772. if ( !schema_ds4x_compat && !user_defined_only ) {
  1773. /* return the set of syntaxes we support */
  1774. sew.attrs = &pschema_info_e->e_attrs;
  1775. sew.psbSyntaxDescription = psbSyntaxDescription;
  1776. plugin_syntax_enumerate(schema_syntax_enum_callback, &sew);
  1777. }
  1778. csn = g_get_global_schema_csn();
  1779. if (NULL != csn) {
  1780. char csn_str[CSN_STRSIZE + 1];
  1781. csn_as_string(csn, PR_FALSE, csn_str);
  1782. slapi_entry_attr_delete(pschema_info_e, "nsschemacsn");
  1783. slapi_entry_add_string(pschema_info_e, "nsschemacsn", csn_str);
  1784. }
  1785. schema_dse_unlock();
  1786. sizedbuffer_destroy(psbObjectClasses);
  1787. sizedbuffer_destroy(psbAttrTypes);
  1788. sizedbuffer_destroy(psbMatchingRule);
  1789. sizedbuffer_destroy(psbSyntaxDescription);
  1790. *returncode= LDAP_SUCCESS;
  1791. return SLAPI_DSE_CALLBACK_OK;
  1792. }
  1793. /* helper for deleting mods (we do not want to be applied) from the mods array */
  1794. static void
  1795. mod_free(LDAPMod *mod)
  1796. {
  1797. ber_bvecfree(mod->mod_bvalues);
  1798. slapi_ch_free((void**)&(mod->mod_type));
  1799. slapi_ch_free((void**)&mod);
  1800. }
  1801. /*
  1802. * modify_schema_dse: called by do_modify() when target is cn=schema
  1803. *
  1804. * Add/Delete attributes and objectclasses from the schema
  1805. * Supported mod_ops are LDAP_MOD_DELETE and LDAP_MOD_ADD
  1806. *
  1807. * Note that the in-memory DSE Slapi_Entry object does NOT hold the
  1808. * attributeTypes and objectClasses attributes -- it only holds
  1809. * non-schema related attributes such as aci.
  1810. *
  1811. * returntext is always at least SLAPI_DSE_RETURNTEXT_SIZE bytes in size.
  1812. */
  1813. int
  1814. modify_schema_dse (Slapi_PBlock *pb, Slapi_Entry *entryBefore, Slapi_Entry *entryAfter, int *returncode, char *returntext, void *arg)
  1815. {
  1816. int i, rc= SLAPI_DSE_CALLBACK_OK; /* default is to apply changes to the DSE */
  1817. char *schema_dse_attr_name;
  1818. LDAPMod **mods = NULL;
  1819. int num_mods = 0; /* count the number of mods */
  1820. int schema_ds4x_compat = config_get_ds4_compatible_schema();
  1821. int schema_modify_enabled = config_get_schemamod();
  1822. int reapply_mods = 0;
  1823. int is_replicated_operation = 0;
  1824. if (!schema_modify_enabled) {
  1825. *returncode = LDAP_UNWILLING_TO_PERFORM;
  1826. schema_create_errormsg( returntext, SLAPI_DSE_RETURNTEXT_SIZE,
  1827. schema_errprefix_generic, "Generic",
  1828. "schema update is disabled" );
  1829. return (SLAPI_DSE_CALLBACK_ERROR);
  1830. }
  1831. slapi_pblock_get( pb, SLAPI_MODIFY_MODS, &mods );
  1832. slapi_pblock_get( pb, SLAPI_IS_REPLICATED_OPERATION, &is_replicated_operation);
  1833. schema_dse_lock_write();
  1834. /*
  1835. * Process each modification. Stop as soon as we hit an error.
  1836. *
  1837. * XXXmcs: known bugs: we don't operate on a copy of the schema, so it
  1838. * is possible for some schema changes to be made but not all of them.
  1839. * True for DS 4.x as well, although it tried to keep going even after
  1840. * an error was detected (which was very wrong).
  1841. */
  1842. for (i = 0; rc == SLAPI_DSE_CALLBACK_OK && mods[i]; i++) {
  1843. schema_dse_attr_name = (char *) mods[i]->mod_type;
  1844. num_mods++; /* incr the number of mods */
  1845. /*
  1846. * skip attribute types that we do not recognize (the DSE code will
  1847. * handle them).
  1848. */
  1849. if ( !schema_type_is_interesting( schema_dse_attr_name )) {
  1850. continue;
  1851. }
  1852. /*
  1853. * Delete an objectclass or attribute
  1854. */
  1855. if (SLAPI_IS_MOD_DELETE(mods[i]->mod_op)) {
  1856. if (strcasecmp (mods[i]->mod_type, "objectclasses") == 0) {
  1857. *returncode = schema_delete_objectclasses (entryBefore, mods[i],
  1858. returntext, SLAPI_DSE_RETURNTEXT_SIZE, schema_ds4x_compat );
  1859. }
  1860. else if (strcasecmp (mods[i]->mod_type, "attributetypes") == 0) {
  1861. *returncode = schema_delete_attributes (entryBefore, mods[i],
  1862. returntext, SLAPI_DSE_RETURNTEXT_SIZE );
  1863. }
  1864. else {
  1865. *returncode= LDAP_NO_SUCH_ATTRIBUTE;
  1866. schema_create_errormsg( returntext, SLAPI_DSE_RETURNTEXT_SIZE,
  1867. schema_errprefix_generic, mods[i]->mod_type,
  1868. "Only object classes and attribute types may be deleted" );
  1869. }
  1870. if ( LDAP_SUCCESS != *returncode ) {
  1871. rc= SLAPI_DSE_CALLBACK_ERROR;
  1872. } else {
  1873. reapply_mods = 1;
  1874. }
  1875. }
  1876. /*
  1877. * Replace an objectclass,attribute, or schema CSN
  1878. */
  1879. else if (SLAPI_IS_MOD_REPLACE(mods[i]->mod_op)) {
  1880. int replace_allowed = 0;
  1881. slapdFrontendConfig_t *slapdFrontendConfig;
  1882. slapdFrontendConfig = getFrontendConfig();
  1883. CFG_LOCK_READ( slapdFrontendConfig );
  1884. if ( 0 == strcasecmp( slapdFrontendConfig->schemareplace,
  1885. CONFIG_SCHEMAREPLACE_STR_ON )) {
  1886. replace_allowed = 1;
  1887. } else if ( 0 == strcasecmp( slapdFrontendConfig->schemareplace,
  1888. CONFIG_SCHEMAREPLACE_STR_REPLICATION_ONLY )) {
  1889. replace_allowed = is_replicated_operation;
  1890. }
  1891. CFG_UNLOCK_READ( slapdFrontendConfig );
  1892. if ( !replace_allowed ) {
  1893. *returncode= LDAP_UNWILLING_TO_PERFORM;
  1894. schema_create_errormsg( returntext, SLAPI_DSE_RETURNTEXT_SIZE,
  1895. schema_errprefix_generic, mods[i]->mod_type,
  1896. "Replace is not allowed on the subschema subentry" );
  1897. rc = SLAPI_DSE_CALLBACK_ERROR;
  1898. } else {
  1899. if (strcasecmp (mods[i]->mod_type, "attributetypes") == 0) {
  1900. if (is_replicated_operation) {
  1901. /*
  1902. * before accepting the schema checks if the local consumer schema is not
  1903. * a superset of the supplier schema
  1904. */
  1905. if (schema_attributetypes_superset_check(mods[i]->mod_bvalues, OC_CONSUMER)) {
  1906. schema_create_errormsg( returntext, SLAPI_DSE_RETURNTEXT_SIZE,
  1907. schema_errprefix_generic, mods[i]->mod_type,
  1908. "Replace is not possible, local consumer schema is a superset of the supplier" );
  1909. slapi_log_error(SLAPI_LOG_FATAL, "schema",
  1910. "Local %s must not be overwritten (set replication log for additional info)\n",
  1911. mods[i]->mod_type);
  1912. *returncode = LDAP_UNWILLING_TO_PERFORM;
  1913. } else {
  1914. /*
  1915. * Replace all attributes
  1916. */
  1917. *returncode = schema_replace_attributes( pb, mods[i], returntext,
  1918. SLAPI_DSE_RETURNTEXT_SIZE );
  1919. }
  1920. } else {
  1921. /*
  1922. * Replace all objectclasses
  1923. */
  1924. *returncode = schema_replace_attributes( pb, mods[i], returntext,
  1925. SLAPI_DSE_RETURNTEXT_SIZE );
  1926. }
  1927. } else if (strcasecmp (mods[i]->mod_type, "objectclasses") == 0) {
  1928. if (is_replicated_operation) {
  1929. /* before accepting the schema checks if the local consumer schema is not
  1930. * a superset of the supplier schema
  1931. */
  1932. if (schema_objectclasses_superset_check(mods[i]->mod_bvalues, OC_CONSUMER)) {
  1933. schema_create_errormsg( returntext, SLAPI_DSE_RETURNTEXT_SIZE,
  1934. schema_errprefix_generic, mods[i]->mod_type,
  1935. "Replace is not possible, local consumer schema is a superset of the supplier" );
  1936. slapi_log_error(SLAPI_LOG_FATAL, "schema",
  1937. "Local %s must not be overwritten (set replication log for additional info)\n",
  1938. mods[i]->mod_type);
  1939. *returncode = LDAP_UNWILLING_TO_PERFORM;
  1940. } else {
  1941. /*
  1942. * Replace all objectclasses
  1943. */
  1944. *returncode = schema_replace_objectclasses(pb, mods[i],
  1945. returntext, SLAPI_DSE_RETURNTEXT_SIZE);
  1946. }
  1947. } else {
  1948. /*
  1949. * Replace all objectclasses
  1950. */
  1951. *returncode = schema_replace_objectclasses(pb, mods[i],
  1952. returntext, SLAPI_DSE_RETURNTEXT_SIZE);
  1953. }
  1954. } else if (strcasecmp (mods[i]->mod_type, "nsschemacsn") == 0) {
  1955. if (is_replicated_operation) {
  1956. /* Update the schema CSN */
  1957. if (mods[i]->mod_bvalues && mods[i]->mod_bvalues[0] &&
  1958. mods[i]->mod_bvalues[0]->bv_val &&
  1959. mods[i]->mod_bvalues[0]->bv_len > 0) {
  1960. char new_csn_string[CSN_STRSIZE + 1];
  1961. CSN *new_schema_csn;
  1962. memcpy(new_csn_string, mods[i]->mod_bvalues[0]->bv_val,
  1963. mods[i]->mod_bvalues[0]->bv_len);
  1964. new_csn_string[mods[i]->mod_bvalues[0]->bv_len] = '\0';
  1965. new_schema_csn = csn_new_by_string(new_csn_string);
  1966. if (NULL != new_schema_csn) {
  1967. g_set_global_schema_csn(new_schema_csn); /* csn is consumed */
  1968. }
  1969. }
  1970. }
  1971. } else {
  1972. *returncode= LDAP_UNWILLING_TO_PERFORM; /* XXXmcs: best error? */
  1973. schema_create_errormsg( returntext, SLAPI_DSE_RETURNTEXT_SIZE,
  1974. schema_errprefix_generic, mods[i]->mod_type,
  1975. "Only object classes and attribute types may be replaced" );
  1976. }
  1977. }
  1978. if ( LDAP_SUCCESS != *returncode ) {
  1979. rc= SLAPI_DSE_CALLBACK_ERROR;
  1980. } else {
  1981. reapply_mods = 1; /* we have at least some modifications we need to reapply */
  1982. }
  1983. }
  1984. /*
  1985. * Add an objectclass or attribute
  1986. */
  1987. else if (SLAPI_IS_MOD_ADD(mods[i]->mod_op)) {
  1988. if (strcasecmp (mods[i]->mod_type, "attributetypes") == 0) {
  1989. /*
  1990. * Add a new attribute
  1991. */
  1992. *returncode = schema_add_attribute ( pb, mods[i], returntext,
  1993. SLAPI_DSE_RETURNTEXT_SIZE, schema_ds4x_compat );
  1994. }
  1995. else if (strcasecmp (mods[i]->mod_type, "objectclasses") == 0) {
  1996. /*
  1997. * Add a new objectclass
  1998. */
  1999. *returncode = schema_add_objectclass ( pb, mods[i], returntext,
  2000. SLAPI_DSE_RETURNTEXT_SIZE, schema_ds4x_compat );
  2001. }
  2002. else {
  2003. if ( schema_ds4x_compat ) {
  2004. *returncode= LDAP_NO_SUCH_ATTRIBUTE;
  2005. } else {
  2006. *returncode= LDAP_UNWILLING_TO_PERFORM; /* XXXmcs: best error? */
  2007. }
  2008. schema_create_errormsg( returntext, SLAPI_DSE_RETURNTEXT_SIZE,
  2009. schema_errprefix_generic, mods[i]->mod_type,
  2010. "Only object classes and attribute types may be added" );
  2011. }
  2012. if ( LDAP_SUCCESS != *returncode ) {
  2013. rc= SLAPI_DSE_CALLBACK_ERROR;
  2014. } else {
  2015. reapply_mods = 1; /* we have at least some modifications we need to reapply */
  2016. }
  2017. }
  2018. /*
  2019. ** No value was specified to modify, the user probably tried
  2020. ** to delete all attributetypes or all objectclasses, which
  2021. ** isn't allowed
  2022. */
  2023. if (!mods[i]->mod_vals.modv_strvals)
  2024. {
  2025. if ( schema_ds4x_compat ) {
  2026. *returncode= LDAP_INVALID_SYNTAX;
  2027. } else {
  2028. *returncode= LDAP_UNWILLING_TO_PERFORM; /* XXXmcs: best error? */
  2029. }
  2030. schema_create_errormsg( returntext, SLAPI_DSE_RETURNTEXT_SIZE,
  2031. schema_errprefix_generic, mods[i]->mod_type,
  2032. "No target attribute type or object class specified" );
  2033. rc= SLAPI_DSE_CALLBACK_ERROR;
  2034. }
  2035. }
  2036. if(rc==SLAPI_DSE_CALLBACK_OK && reapply_mods)
  2037. {
  2038. CSN *new_schema_csn;
  2039. int newindex = 0; /* mods array index */
  2040. /* tell the "unholy" dse_modify code to reapply the mods and use
  2041. that result instead of the initial result; we must remove the attributes
  2042. we manage in this code from the mods
  2043. */
  2044. slapi_pblock_set(pb, SLAPI_DSE_REAPPLY_MODS, (void *)&reapply_mods);
  2045. /* because we are reapplying the mods, we want the entryAfter to
  2046. look just like the entryBefore, except that "our" attributes
  2047. will have been removed
  2048. */
  2049. /* delete the mods from the mods array */
  2050. for (i = 0; i < num_mods ; i++) {
  2051. const char *attrname = mods[i]->mod_type;
  2052. /* delete this attr from the entry */
  2053. slapi_entry_attr_delete(entryAfter, attrname);
  2054. if ( schema_type_is_interesting( attrname )) {
  2055. mod_free(mods[i]);
  2056. mods[i] = NULL;
  2057. } else {
  2058. /* add the original value of the attr back to the entry after */
  2059. Slapi_Attr *origattr = NULL;
  2060. Slapi_ValueSet *origvalues = NULL;
  2061. slapi_entry_attr_find(entryBefore, attrname, &origattr);
  2062. if (NULL != origattr) {
  2063. slapi_attr_get_valueset(origattr, &origvalues);
  2064. if (NULL != origvalues) {
  2065. slapi_entry_add_valueset(entryAfter, attrname, origvalues);
  2066. slapi_valueset_free(origvalues);
  2067. }
  2068. }
  2069. mods[newindex++] = mods[i];
  2070. }
  2071. }
  2072. mods[newindex] = NULL;
  2073. /*
  2074. * Since we successfully updated the schema, we need to generate
  2075. * a new schema CSN for non-replicated operations.
  2076. */
  2077. /* XXXmcs: I wonder if we should update the schema CSN even when no
  2078. * attribute types or OCs were changed? That way, an administrator
  2079. * could force schema replication to occur by submitting a modify
  2080. * operation that did not really do anything, such as:
  2081. *
  2082. * dn:cn=schema
  2083. * changetype:modify
  2084. * replace:cn
  2085. * cn:schema
  2086. */
  2087. if (!is_replicated_operation)
  2088. {
  2089. new_schema_csn = csn_new();
  2090. if (NULL != new_schema_csn) {
  2091. char csn_str[CSN_STRSIZE + 1];
  2092. csn_set_replicaid(new_schema_csn, 0);
  2093. csn_set_time(new_schema_csn, current_time());
  2094. g_set_global_schema_csn(new_schema_csn);
  2095. slapi_entry_attr_delete(entryBefore, "nsschemacsn");
  2096. csn_as_string(new_schema_csn, PR_FALSE, csn_str);
  2097. slapi_entry_add_string(entryBefore, "nsschemacsn", csn_str);
  2098. }
  2099. }
  2100. }
  2101. schema_dse_unlock();
  2102. return rc;
  2103. }
  2104. CSN *
  2105. dup_global_schema_csn()
  2106. {
  2107. CSN *schema_csn;
  2108. schema_dse_lock_read();
  2109. schema_csn = csn_dup ( g_get_global_schema_csn() );
  2110. schema_dse_unlock();
  2111. return schema_csn;
  2112. }
  2113. /*
  2114. * Remove all attribute types and objectclasses from the entry and
  2115. * then add back the user defined ones based on the contents of the
  2116. * schema hash tables.
  2117. *
  2118. * Returns SLAPI_DSE_CALLBACK_OK is all goes well.
  2119. *
  2120. * returntext is always at least SLAPI_DSE_RETURNTEXT_SIZE bytes in size.
  2121. */
  2122. static int
  2123. refresh_user_defined_schema( Slapi_PBlock *pb, Slapi_Entry *pschema_info_e, Slapi_Entry *entryAfter, int *returncode, char *returntext, void *arg /* not used */ )
  2124. {
  2125. int rc;
  2126. Slapi_PBlock *mypbptr = pb;
  2127. Slapi_PBlock mypb;
  2128. const CSN *schema_csn;
  2129. PRUint32 schema_flags = DSE_SCHEMA_USER_DEFINED_ONLY;
  2130. pblock_init(&mypb);
  2131. slapi_entry_attr_delete( pschema_info_e, "objectclasses");
  2132. slapi_entry_attr_delete( pschema_info_e, "attributetypes");
  2133. /* for write callbacks, no pb is supplied, so use our own */
  2134. if (!mypbptr) {
  2135. mypbptr = &mypb;
  2136. }
  2137. slapi_pblock_set(mypbptr, SLAPI_SCHEMA_FLAGS, &schema_flags);
  2138. rc = read_schema_dse(mypbptr, pschema_info_e, NULL, returncode, returntext, NULL);
  2139. schema_csn = g_get_global_schema_csn();
  2140. if (NULL != schema_csn) {
  2141. char csn_str[CSN_STRSIZE + 1];
  2142. slapi_entry_attr_delete(pschema_info_e, "nsschemacsn");
  2143. csn_as_string(schema_csn, PR_FALSE, csn_str);
  2144. slapi_entry_add_string(pschema_info_e, "nsschemacsn", csn_str);
  2145. }
  2146. pblock_done(&mypb);
  2147. return rc;
  2148. }
  2149. /* oc_add_nolock
  2150. * Add the objectClass newoc to the global list of objectclasses
  2151. */
  2152. static void
  2153. oc_add_nolock(struct objclass *newoc)
  2154. {
  2155. struct objclass *poc;
  2156. poc = g_get_global_oc_nolock();
  2157. if ( NULL == poc ) {
  2158. g_set_global_oc_nolock(newoc);
  2159. } else {
  2160. for ( ; (poc != NULL) && (poc->oc_next != NULL); poc = poc->oc_next) {
  2161. ;
  2162. }
  2163. poc->oc_next = newoc;
  2164. newoc->oc_next = NULL;
  2165. }
  2166. }
  2167. /*
  2168. * Delete one or more objectClasses from our internal data structure.
  2169. *
  2170. * Return an LDAP error code (LDAP_SUCCESS if all goes well).
  2171. * If an error occurs, explanatory text is copied into 'errorbuf'.
  2172. *
  2173. * This function should not send an LDAP result; that is the caller's
  2174. * responsibility.
  2175. */
  2176. static int
  2177. schema_delete_objectclasses( Slapi_Entry *entryBefore, LDAPMod *mod,
  2178. char *errorbuf, size_t errorbufsize, int schema_ds4x_compat )
  2179. {
  2180. int i;
  2181. int rc = LDAP_SUCCESS; /* optimistic */
  2182. struct objclass *poc, *poc2, *delete_oc = NULL;
  2183. if ( NULL == mod->mod_bvalues ) {
  2184. schema_create_errormsg( errorbuf, errorbufsize, schema_errprefix_oc,
  2185. NULL, "Cannot remove all schema object classes" );
  2186. return LDAP_UNWILLING_TO_PERFORM;
  2187. }
  2188. for (i = 0; mod->mod_bvalues[i]; i++) {
  2189. if ( LDAP_SUCCESS != ( rc = parse_oc_str (
  2190. (const char *)mod->mod_bvalues[i]->bv_val, &delete_oc,
  2191. errorbuf, errorbufsize, 0, 0, schema_ds4x_compat, NULL))) {
  2192. return rc;
  2193. }
  2194. oc_lock_write();
  2195. if ((poc = oc_find_nolock(delete_oc->oc_name, NULL, PR_FALSE)) != NULL) {
  2196. /* check to see if any objectclasses inherit from this oc */
  2197. for (poc2 = g_get_global_oc_nolock(); poc2 != NULL; poc2 = poc2->oc_next) {
  2198. if (poc2->oc_superior &&
  2199. (strcasecmp (poc2->oc_superior, delete_oc->oc_name) == 0)) {
  2200. schema_create_errormsg( errorbuf, errorbufsize, schema_errprefix_oc,
  2201. delete_oc->oc_name, "Cannot delete an object class"
  2202. " which has child object classes" );
  2203. rc = LDAP_UNWILLING_TO_PERFORM;
  2204. goto unlock_and_return;
  2205. }
  2206. }
  2207. if ( (poc->oc_flags & OC_FLAG_STANDARD_OC) == 0) {
  2208. oc_delete_nolock (poc->oc_name);
  2209. }
  2210. else {
  2211. schema_create_errormsg( errorbuf, errorbufsize, schema_errprefix_oc,
  2212. delete_oc->oc_name, "Cannot delete a standard object class" );
  2213. rc = LDAP_UNWILLING_TO_PERFORM;
  2214. goto unlock_and_return;
  2215. }
  2216. }
  2217. else {
  2218. schema_create_errormsg( errorbuf, errorbufsize, schema_errprefix_oc,
  2219. delete_oc->oc_name, "Is unknown. Cannot delete." );
  2220. rc = schema_ds4x_compat ? LDAP_NO_SUCH_OBJECT : LDAP_NO_SUCH_ATTRIBUTE;
  2221. goto unlock_and_return;
  2222. }
  2223. oc_free( &delete_oc );
  2224. oc_unlock();
  2225. }
  2226. return rc;
  2227. unlock_and_return:
  2228. oc_free( &delete_oc );
  2229. oc_unlock();
  2230. return rc;
  2231. }
  2232. static int
  2233. schema_return(int rc,struct sizedbuffer * psb1,struct sizedbuffer *psb2,struct sizedbuffer *psb3,struct sizedbuffer *psb4)
  2234. {
  2235. sizedbuffer_destroy(psb1);
  2236. sizedbuffer_destroy(psb2);
  2237. sizedbuffer_destroy(psb3);
  2238. sizedbuffer_destroy(psb4);
  2239. return rc;
  2240. }
  2241. /*
  2242. * Delete one or more attributeTypes from our internal data structure.
  2243. *
  2244. * Return an LDAP error code (LDAP_SUCCESS if all goes well).
  2245. * If an error occurs, explanatory text is copied into 'errorbuf'.
  2246. *
  2247. * This function should not send an LDAP result; that is the caller's
  2248. * responsibility.
  2249. */
  2250. static int
  2251. schema_delete_attributes ( Slapi_Entry *entryBefore, LDAPMod *mod,
  2252. char *errorbuf, size_t errorbufsize)
  2253. {
  2254. char *attr_ldif, *oc_list_type = "";
  2255. asyntaxinfo *a;
  2256. struct objclass *oc = NULL;
  2257. int i, k, attr_in_use_by_an_oc = 0;
  2258. struct sizedbuffer *psbAttrName= sizedbuffer_construct(BUFSIZ);
  2259. struct sizedbuffer *psbAttrOid= sizedbuffer_construct(BUFSIZ);
  2260. struct sizedbuffer *psbAttrSyntax= sizedbuffer_construct(BUFSIZ);
  2261. if (NULL == mod->mod_bvalues) {
  2262. schema_create_errormsg( errorbuf, errorbufsize, schema_errprefix_at,
  2263. NULL, "Cannot remove all schema attribute types" );
  2264. return schema_return(LDAP_UNWILLING_TO_PERFORM,psbAttrOid,psbAttrName,
  2265. psbAttrSyntax,NULL);
  2266. }
  2267. for (i = 0; mod->mod_bvalues[i]; i++) {
  2268. attr_ldif =(char *) mod->mod_bvalues[i]->bv_val;
  2269. /* normalize the attr ldif */
  2270. for ( k = 0; attr_ldif[k]; k++) {
  2271. if (attr_ldif[k] == '\'' ||
  2272. attr_ldif[k] == '(' ||
  2273. attr_ldif[k] == ')' ) {
  2274. attr_ldif[k] = ' ';
  2275. }
  2276. attr_ldif[k] = tolower (attr_ldif[k]);
  2277. }
  2278. sizedbuffer_allocate(psbAttrName,strlen(attr_ldif));
  2279. sizedbuffer_allocate(psbAttrOid,strlen(attr_ldif));
  2280. sizedbuffer_allocate(psbAttrSyntax,strlen(attr_ldif));
  2281. sscanf (attr_ldif, "%s name %s syntax %s",
  2282. psbAttrOid->buffer, psbAttrName->buffer, psbAttrSyntax->buffer);
  2283. if ((a = attr_syntax_get_by_name ( psbAttrName->buffer)) != NULL ) {
  2284. /* only modify attrs which were user defined */
  2285. if (a->asi_flags & SLAPI_ATTR_FLAG_STD_ATTR) {
  2286. schema_create_errormsg( errorbuf, errorbufsize, schema_errprefix_at,
  2287. psbAttrName->buffer,
  2288. "Cannot delete a standard attribute type" );
  2289. attr_syntax_return( a );
  2290. return schema_return(LDAP_UNWILLING_TO_PERFORM,psbAttrOid,psbAttrName,
  2291. psbAttrSyntax,NULL);
  2292. }
  2293. /* Do not allow deletion if referenced by an object class. */
  2294. oc_lock_read();
  2295. attr_in_use_by_an_oc = 0;
  2296. for ( oc = g_get_global_oc_nolock(); oc != NULL; oc = oc->oc_next ) {
  2297. if (NULL != oc->oc_required) {
  2298. for ( k = 0; oc->oc_required[k] != NULL; k++ ) {
  2299. if ( 0 == slapi_attr_type_cmp( oc->oc_required[k], a->asi_name,
  2300. SLAPI_TYPE_CMP_EXACT )) {
  2301. oc_list_type = "MUST";
  2302. attr_in_use_by_an_oc = 1;
  2303. break;
  2304. }
  2305. }
  2306. }
  2307. if (!attr_in_use_by_an_oc && NULL != oc->oc_allowed) {
  2308. for ( k = 0; oc->oc_allowed[k] != NULL; k++ ) {
  2309. if ( 0 == slapi_attr_type_cmp( oc->oc_allowed[k], a->asi_name,
  2310. SLAPI_TYPE_CMP_EXACT )) {
  2311. oc_list_type = "MAY";
  2312. attr_in_use_by_an_oc = 1;
  2313. break;
  2314. }
  2315. }
  2316. }
  2317. if (attr_in_use_by_an_oc) {
  2318. schema_create_errormsg( errorbuf, errorbufsize, schema_errprefix_at,
  2319. psbAttrName->buffer, "Is included in the %s list for object class %s. Cannot delete.",
  2320. oc_list_type, oc->oc_name );
  2321. break;
  2322. }
  2323. }
  2324. oc_unlock();
  2325. if (attr_in_use_by_an_oc) {
  2326. attr_syntax_return( a );
  2327. return schema_return(LDAP_UNWILLING_TO_PERFORM,psbAttrOid,psbAttrName,
  2328. psbAttrSyntax,NULL);
  2329. }
  2330. /* Delete it. */
  2331. attr_syntax_delete( a );
  2332. attr_syntax_return( a );
  2333. }
  2334. else {
  2335. /* unknown attribute */
  2336. schema_create_errormsg( errorbuf, errorbufsize, schema_errprefix_at,
  2337. psbAttrName->buffer, "Is unknown. Cannot delete." );
  2338. return schema_return(LDAP_NO_SUCH_ATTRIBUTE,psbAttrOid,psbAttrName,
  2339. psbAttrSyntax,NULL);
  2340. }
  2341. }
  2342. return schema_return(LDAP_SUCCESS,psbAttrOid,psbAttrName,psbAttrSyntax,
  2343. NULL);
  2344. }
  2345. static int
  2346. schema_add_attribute ( Slapi_PBlock *pb, LDAPMod *mod, char *errorbuf,
  2347. size_t errorbufsize, int schema_ds4x_compat )
  2348. {
  2349. int i;
  2350. char *attr_ldif;
  2351. /* LPXXX: Eventually, we should not allocate the buffers in parse_at_str
  2352. * for each attribute, but use the same buffer for all.
  2353. * This is not done yet, so it's useless to allocate buffers for nothing.
  2354. */
  2355. /* struct sizedbuffer *psbAttrName= sizedbuffer_construct(BUFSIZ); */
  2356. /* struct sizedbuffer *psbAttrOid= sizedbuffer_construct(BUFSIZ); */
  2357. /* struct sizedbuffer *psbAttrDesc= sizedbuffer_construct(BUFSIZ); */
  2358. /* struct sizedbuffer *psbAttrSyntax= sizedbuffer_construct(BUFSIZ); */
  2359. int status = 0;
  2360. for (i = 0; LDAP_SUCCESS == status && mod->mod_bvalues[i]; i++) {
  2361. PRUint32 nolock = 0; /* lock global resources during normal operation */
  2362. attr_ldif = (char *) mod->mod_bvalues[i]->bv_val;
  2363. status = parse_at_str(attr_ldif, NULL, errorbuf, errorbufsize,
  2364. nolock, 1 /* user defined */, schema_ds4x_compat, 1);
  2365. if ( LDAP_SUCCESS != status ) {
  2366. break; /* stop on first error */
  2367. }
  2368. }
  2369. /* free everything */
  2370. /* sizedbuffer_destroy(psbAttrOid); */
  2371. /* sizedbuffer_destroy(psbAttrName); */
  2372. /* sizedbuffer_destroy(psbAttrDesc); */
  2373. /* sizedbuffer_destroy(psbAttrSyntax); */
  2374. return status;
  2375. }
  2376. /*
  2377. * Returns an LDAP error code (LDAP_SUCCESS if all goes well)
  2378. */
  2379. static int
  2380. add_oc_internal(struct objclass *pnew_oc, char *errorbuf, size_t errorbufsize,
  2381. int schema_ds4x_compat, PRUint32 flags )
  2382. {
  2383. struct objclass *oldoc_by_name, *oldoc_by_oid, *psup_oc = NULL;
  2384. int redefined_oc = 0, rc=0;
  2385. asyntaxinfo *pasyntaxinfo = 0;
  2386. if (!(flags & DSE_SCHEMA_LOCKED))
  2387. oc_lock_write();
  2388. oldoc_by_name = oc_find_nolock (pnew_oc->oc_name, NULL, PR_FALSE);
  2389. oldoc_by_oid = oc_find_nolock (pnew_oc->oc_oid, NULL, PR_FALSE);
  2390. /* Check to see if the objectclass name and the objectclass oid are already
  2391. * in use by an existing objectclass. If an existing objectclass is already
  2392. * using the name or oid, the name and the oid should map to the same objectclass.
  2393. * Otherwise, return an error.
  2394. */
  2395. if ( oldoc_by_name != oldoc_by_oid ) {
  2396. schema_create_errormsg( errorbuf, errorbufsize, schema_errprefix_oc,
  2397. pnew_oc->oc_name, "The name does not match the OID \"%s\". "
  2398. "Another object class is already using the name or OID.",
  2399. pnew_oc->oc_oid);
  2400. rc = LDAP_TYPE_OR_VALUE_EXISTS;
  2401. }
  2402. /*
  2403. * Set a flag so we know if we are updating an existing OC definition.
  2404. */
  2405. if ( !rc ) {
  2406. if ( NULL != oldoc_by_name ) {
  2407. redefined_oc = 1;
  2408. } else {
  2409. /*
  2410. * If we are not updating an existing OC, check that the new
  2411. * oid is not already in use.
  2412. */
  2413. if ( NULL != oldoc_by_oid ) {
  2414. schema_create_errormsg( errorbuf, errorbufsize,
  2415. schema_errprefix_oc, pnew_oc->oc_name,
  2416. "The OID \"%s\" is already used by the object class \"%s\"",
  2417. pnew_oc->oc_oid, oldoc_by_oid->oc_name);
  2418. rc = LDAP_TYPE_OR_VALUE_EXISTS;
  2419. }
  2420. }
  2421. }
  2422. /* check to see if the superior oc exists */
  2423. if (!rc && pnew_oc->oc_superior &&
  2424. ((psup_oc = oc_find_nolock (pnew_oc->oc_superior, NULL, PR_FALSE)) == NULL)) {
  2425. schema_create_errormsg( errorbuf, errorbufsize, schema_errprefix_oc,
  2426. pnew_oc->oc_name, "Superior object class \"%s\" does not exist",
  2427. pnew_oc->oc_superior);
  2428. rc = LDAP_TYPE_OR_VALUE_EXISTS;
  2429. }
  2430. /* inherit the attributes from the superior oc */
  2431. if (!rc && psup_oc ) {
  2432. if ( psup_oc->oc_required ) {
  2433. charray_merge( &pnew_oc->oc_required, psup_oc->oc_required, 1 );
  2434. }
  2435. if ( psup_oc->oc_allowed ) {
  2436. charray_merge ( &pnew_oc->oc_allowed, psup_oc->oc_allowed, 1 );
  2437. }
  2438. }
  2439. /* check to see if the oid is already in use by an attribute */
  2440. if (!rc && (pasyntaxinfo = attr_syntax_get_by_oid(pnew_oc->oc_oid))) {
  2441. schema_create_errormsg( errorbuf, errorbufsize, schema_errprefix_oc,
  2442. pnew_oc->oc_name,
  2443. "The OID \"%s\" is also used by the attribute type \"%s\"",
  2444. pnew_oc->oc_oid, pasyntaxinfo->asi_name);
  2445. rc = LDAP_TYPE_OR_VALUE_EXISTS;
  2446. attr_syntax_return( pasyntaxinfo );
  2447. }
  2448. /* check to see if the objectclass name is valid */
  2449. if (!rc && !(flags & DSE_SCHEMA_NO_CHECK) &&
  2450. schema_check_name ( pnew_oc->oc_name, PR_FALSE, errorbuf, errorbufsize )
  2451. == 0 ) {
  2452. rc = schema_ds4x_compat ? LDAP_OPERATIONS_ERROR : LDAP_INVALID_SYNTAX;
  2453. }
  2454. /* check to see if the oid is valid */
  2455. if (!rc && !(flags & DSE_SCHEMA_NO_CHECK))
  2456. {
  2457. struct sizedbuffer *psbOcOid, *psbOcName;
  2458. psbOcName = sizedbuffer_construct(strlen(pnew_oc->oc_name) + 1);
  2459. psbOcOid = sizedbuffer_construct(strlen(pnew_oc->oc_oid) + 1);
  2460. strcpy(psbOcName->buffer, pnew_oc->oc_name);
  2461. strcpy(psbOcOid->buffer, pnew_oc->oc_oid);
  2462. if (!schema_check_oid ( psbOcName->buffer, psbOcOid->buffer, PR_FALSE,
  2463. errorbuf, errorbufsize))
  2464. rc = schema_ds4x_compat ? LDAP_OPERATIONS_ERROR : LDAP_INVALID_SYNTAX;
  2465. sizedbuffer_destroy(psbOcName);
  2466. sizedbuffer_destroy(psbOcOid);
  2467. }
  2468. /* check to see if the oc's attributes are valid */
  2469. if (!rc && !(flags & DSE_SCHEMA_NO_CHECK) &&
  2470. schema_check_oc_attrs ( pnew_oc, errorbuf, errorbufsize,
  2471. 0 /* don't strip options */ ) == 0 ) {
  2472. rc = schema_ds4x_compat ? LDAP_OPERATIONS_ERROR : LDAP_INVALID_SYNTAX;
  2473. }
  2474. /* insert new objectclass exactly where the old one one in the linked list*/
  2475. if ( !rc && redefined_oc ) {
  2476. pnew_oc->oc_flags |= OC_FLAG_REDEFINED_OC;
  2477. rc=oc_replace_nolock( pnew_oc->oc_name, pnew_oc);
  2478. }
  2479. if (!rc && !redefined_oc ) {
  2480. oc_add_nolock(pnew_oc);
  2481. }
  2482. if (!rc && redefined_oc ) {
  2483. oc_update_inheritance_nolock( pnew_oc );
  2484. }
  2485. if (!(flags & DSE_SCHEMA_LOCKED))
  2486. oc_unlock();
  2487. return rc;
  2488. }
  2489. /*
  2490. * Process a replace modify suboperation for attributetypes.
  2491. *
  2492. * XXXmcs: At present, readonly (bundled) schema definitions can't be
  2493. * removed. If that is attempted, we just keep them without generating
  2494. * an error.
  2495. *
  2496. * Our algorithm is:
  2497. *
  2498. * Clear the "keep" flags on the all existing attr. definitions.
  2499. *
  2500. * For each replacement value:
  2501. * If the value exactly matches an existing schema definition,
  2502. * set that definition's keep flag.
  2503. *
  2504. * Else if the OID in the replacement value matches an existing
  2505. * definition, delete the old definition and add the new one. Set
  2506. * the keep flag on the newly added definition.
  2507. *
  2508. * Else add the new definition. Set the keep flag on the newly
  2509. * added definition.
  2510. *
  2511. * For each definition that is not flagged keep, delete.
  2512. *
  2513. * Clear all remaining "keep" flags.
  2514. *
  2515. * Note that replace was not supported at all before iDS 5.0.
  2516. */
  2517. static int
  2518. schema_replace_attributes ( Slapi_PBlock *pb, LDAPMod *mod, char *errorbuf,
  2519. size_t errorbufsize )
  2520. {
  2521. int i, rc = LDAP_SUCCESS;
  2522. struct asyntaxinfo *newasip, *oldasip;
  2523. PRUint32 schema_flags = 0;
  2524. if ( NULL == mod->mod_bvalues ) {
  2525. schema_create_errormsg( errorbuf, errorbufsize, schema_errprefix_at,
  2526. NULL, "Cannot remove all schema attribute types" );
  2527. return LDAP_UNWILLING_TO_PERFORM;
  2528. }
  2529. slapi_pblock_get(pb, SLAPI_SCHEMA_FLAGS, &schema_flags);
  2530. if (!(schema_flags & (DSE_SCHEMA_NO_LOAD|DSE_SCHEMA_NO_CHECK))) {
  2531. /* clear all of the "keep" flags unless it's from schema-reload */
  2532. attr_syntax_all_clear_flag( SLAPI_ATTR_FLAG_KEEP );
  2533. }
  2534. for ( i = 0; mod->mod_bvalues[i] != NULL; ++i ) {
  2535. if ( LDAP_SUCCESS != ( rc = parse_at_str( mod->mod_bvalues[i]->bv_val,
  2536. &newasip, errorbuf, errorbufsize, 0, 1, 0, 0 ))) {
  2537. goto clean_up_and_return;
  2538. }
  2539. /*
  2540. * Check for a match with an existing type and
  2541. * handle the various cases.
  2542. */
  2543. if ( NULL == ( oldasip =
  2544. attr_syntax_get_by_oid( newasip->asi_oid ))) {
  2545. /* new attribute type */
  2546. LDAPDebug( LDAP_DEBUG_TRACE, "schema_replace_attributes:"
  2547. " new type %s (OID %s)\n",
  2548. newasip->asi_name, newasip->asi_oid, 0 );
  2549. } else {
  2550. /* the name matches -- check the rest */
  2551. if ( attr_syntax_equal( newasip, oldasip )) {
  2552. /* unchanged attribute type -- just mark it as one to keep */
  2553. oldasip->asi_flags |= SLAPI_ATTR_FLAG_KEEP;
  2554. attr_syntax_free( newasip );
  2555. newasip = NULL;
  2556. } else {
  2557. /* modified attribute type */
  2558. LDAPDebug( LDAP_DEBUG_TRACE, "schema_replace_attributes:"
  2559. " replacing type %s (OID %s)\n",
  2560. newasip->asi_name, newasip->asi_oid, 0 );
  2561. /* flag for deletion */
  2562. attr_syntax_delete( oldasip );
  2563. }
  2564. attr_syntax_return( oldasip );
  2565. }
  2566. if ( NULL != newasip ) { /* add new or replacement definition */
  2567. rc = attr_syntax_add( newasip );
  2568. if ( LDAP_SUCCESS != rc ) {
  2569. schema_create_errormsg( errorbuf, errorbufsize,
  2570. schema_errprefix_at, newasip->asi_name,
  2571. "Could not be added (OID is \"%s\")",
  2572. newasip->asi_oid );
  2573. attr_syntax_free( newasip );
  2574. goto clean_up_and_return;
  2575. }
  2576. newasip->asi_flags |= SLAPI_ATTR_FLAG_KEEP;
  2577. }
  2578. }
  2579. /*
  2580. * Delete all of the definitions that are not marked "keep" or "standard".
  2581. *
  2582. * XXXmcs: we should consider reporting an error if any read only types
  2583. * remain....
  2584. */
  2585. attr_syntax_delete_all_not_flagged( SLAPI_ATTR_FLAG_KEEP |
  2586. SLAPI_ATTR_FLAG_STD_ATTR );
  2587. clean_up_and_return:
  2588. if (!(schema_flags & (DSE_SCHEMA_NO_LOAD|DSE_SCHEMA_NO_CHECK))) {
  2589. /* clear all of the "keep" flags unless it's from schema-reload */
  2590. attr_syntax_all_clear_flag( SLAPI_ATTR_FLAG_KEEP );
  2591. }
  2592. return rc;
  2593. }
  2594. static int
  2595. schema_add_objectclass ( Slapi_PBlock *pb, LDAPMod *mod, char *errorbuf,
  2596. size_t errorbufsize, int schema_ds4x_compat )
  2597. {
  2598. struct objclass *pnew_oc = NULL;
  2599. char *newoc_ldif;
  2600. int j, rc=0;
  2601. for (j = 0; mod->mod_bvalues[j]; j++) {
  2602. newoc_ldif = (char *) mod->mod_bvalues[j]->bv_val;
  2603. if ( LDAP_SUCCESS != (rc = parse_oc_str ( newoc_ldif, &pnew_oc,
  2604. errorbuf, errorbufsize, 0, 1 /* user defined */,
  2605. schema_ds4x_compat, NULL))) {
  2606. oc_free( &pnew_oc );
  2607. return rc;
  2608. }
  2609. if ( LDAP_SUCCESS != (rc = add_oc_internal(pnew_oc, errorbuf,
  2610. errorbufsize, schema_ds4x_compat, 0/* no restriction */))) {
  2611. oc_free( &pnew_oc );
  2612. return rc;
  2613. }
  2614. normalize_oc();
  2615. }
  2616. return LDAP_SUCCESS;
  2617. }
  2618. /*
  2619. * Process a replace modify suboperation for objectclasses.
  2620. *
  2621. * XXXmcs: At present, readonly (bundled) schema definitions can't be
  2622. * removed. If that is attempted, we just keep them without generating
  2623. * an error.
  2624. *
  2625. * Our algorithm is:
  2626. *
  2627. * Lock the global objectclass linked list.
  2628. *
  2629. * Create a new empty (temporary) linked list, initially empty.
  2630. *
  2631. * For each replacement value:
  2632. * If the value exactly matches an existing schema definition,
  2633. * move the existing definition from the current global list to the
  2634. * temporary list
  2635. *
  2636. * Else if the OID in the replacement value matches an existing
  2637. * definition, delete the old definition from the current global
  2638. * list and add the new one to the temporary list.
  2639. *
  2640. * Else add the new definition to the temporary list.
  2641. *
  2642. * Delete all definitions that remain on the current global list.
  2643. *
  2644. * Make the temporary list the current global list.
  2645. *
  2646. * Note that since the objectclass definitions are stored in a linked list,
  2647. * this algorithm is O(N * M) where N is the number of existing objectclass
  2648. * definitions and M is the number of replacement definitions.
  2649. * XXXmcs: Yuck. We should use a hash table for the OC definitions.
  2650. *
  2651. * Note that replace was not supported at all by DS versions prior to 5.0
  2652. */
  2653. static int
  2654. schema_replace_objectclasses ( Slapi_PBlock *pb, LDAPMod *mod, char *errorbuf,
  2655. size_t errorbufsize )
  2656. {
  2657. struct objclass *newocp, *curlisthead, *prevocp, *tmpocp;
  2658. struct objclass *newlisthead = NULL, *newlistend = NULL;
  2659. int i, rc = LDAP_SUCCESS;
  2660. if ( NULL == mod->mod_bvalues ) {
  2661. schema_create_errormsg( errorbuf, errorbufsize, schema_errprefix_oc,
  2662. NULL, "Cannot remove all schema object classes" );
  2663. return LDAP_UNWILLING_TO_PERFORM;
  2664. }
  2665. oc_lock_write();
  2666. curlisthead = g_get_global_oc_nolock();
  2667. for ( i = 0; mod->mod_bvalues[i] != NULL; ++i ) {
  2668. struct objclass *addocp = NULL;
  2669. if ( LDAP_SUCCESS != ( rc = parse_oc_str( mod->mod_bvalues[i]->bv_val,
  2670. &newocp, errorbuf, errorbufsize, DSE_SCHEMA_NO_GLOCK,
  2671. 1 /* user defined */, 0 /* no DS 4.x compat issues */ , NULL))) {
  2672. rc = LDAP_INVALID_SYNTAX;
  2673. goto clean_up_and_return;
  2674. }
  2675. prevocp = NULL;
  2676. for ( tmpocp = curlisthead; tmpocp != NULL; tmpocp = tmpocp->oc_next ) {
  2677. if ( 0 == strcasecmp( tmpocp->oc_oid, newocp->oc_oid ) ) {
  2678. /* the names match -- remove from the current list */
  2679. if ( tmpocp == curlisthead ) {
  2680. curlisthead = tmpocp->oc_next;
  2681. /* The global oc list is scanned in parse_oc_str above,
  2682. if there are multiple objectclasses to be updated.
  2683. Needs to maintain the list dynamically. */
  2684. g_set_global_oc_nolock( curlisthead );
  2685. } else {
  2686. if (prevocp) prevocp->oc_next = tmpocp->oc_next;
  2687. }
  2688. tmpocp->oc_next = NULL;
  2689. /* check for a full match */
  2690. if ( oc_equal( tmpocp, newocp )) {
  2691. /* no changes: keep existing definition and discard new */
  2692. oc_free( &newocp );
  2693. addocp = tmpocp;
  2694. } else {
  2695. /* some differences: discard old and keep the new one */
  2696. oc_free( &tmpocp );
  2697. LDAPDebug( LDAP_DEBUG_TRACE, "schema_replace_objectclasses:"
  2698. " replacing object class %s (OID %s)\n",
  2699. newocp->oc_name, newocp->oc_oid, 0 );
  2700. addocp = newocp;
  2701. }
  2702. break; /* we found it -- exit the loop */
  2703. }
  2704. prevocp = tmpocp;
  2705. }
  2706. if ( NULL == addocp ) {
  2707. LDAPDebug( LDAP_DEBUG_TRACE, "schema_replace_objectclasses:"
  2708. " new object class %s (OID %s)\n",
  2709. newocp->oc_name, newocp->oc_oid, 0 );
  2710. addocp = newocp;
  2711. }
  2712. /* add the objectclass to the end of the new list */
  2713. if ( NULL != addocp ) {
  2714. if ( NULL == newlisthead ) {
  2715. newlisthead = addocp;
  2716. } else {
  2717. newlistend->oc_next = addocp;
  2718. }
  2719. newlistend = addocp;
  2720. }
  2721. }
  2722. clean_up_and_return:
  2723. if ( LDAP_SUCCESS == rc ) {
  2724. /*
  2725. * Delete all remaining OCs that are on the old list AND are not
  2726. * "standard" classes.
  2727. */
  2728. struct objclass *nextocp;
  2729. prevocp = NULL;
  2730. for ( tmpocp = curlisthead; tmpocp != NULL; tmpocp = nextocp ) {
  2731. if ( 0 == ( tmpocp->oc_flags & OC_FLAG_STANDARD_OC )) {
  2732. /* not a standard definition -- remove it */
  2733. if ( tmpocp == curlisthead ) {
  2734. curlisthead = tmpocp->oc_next;
  2735. } else {
  2736. if (prevocp) {
  2737. prevocp->oc_next = tmpocp->oc_next;
  2738. }
  2739. }
  2740. nextocp = tmpocp->oc_next;
  2741. oc_free( &tmpocp );
  2742. } else {
  2743. /*
  2744. * XXXmcs: we could generate an error, but for now we do not.
  2745. */
  2746. nextocp = tmpocp->oc_next;
  2747. prevocp = tmpocp;
  2748. #if 0
  2749. schema_create_errormsg( errorbuf, errorbufsize,
  2750. schema_errprefix_oc, tmpocp->oc_name,
  2751. "Cannot delete a standard object class" );
  2752. rc = LDAP_UNWILLING_TO_PERFORM;
  2753. break;
  2754. #endif
  2755. }
  2756. }
  2757. }
  2758. /*
  2759. * Combine the two lists by adding the new list to the end of the old
  2760. * one.
  2761. */
  2762. if ( NULL != curlisthead ) {
  2763. for ( tmpocp = curlisthead; tmpocp->oc_next != NULL;
  2764. tmpocp = tmpocp->oc_next ) {
  2765. ;/*NULL*/
  2766. }
  2767. tmpocp->oc_next = newlisthead;
  2768. newlisthead = curlisthead;
  2769. }
  2770. /*
  2771. * Install the new list as the global one, replacing the old one.
  2772. */
  2773. g_set_global_oc_nolock( newlisthead );
  2774. oc_unlock();
  2775. return rc;
  2776. }
  2777. schemaext *
  2778. schema_copy_extensions(schemaext *extensions)
  2779. {
  2780. schemaext *ext = NULL, *head = NULL;
  2781. while(extensions){
  2782. schemaext *newext = (schemaext *)slapi_ch_calloc(1, sizeof(schemaext));
  2783. newext->term = slapi_ch_strdup(extensions->term);
  2784. newext->values = charray_dup(extensions->values);
  2785. newext->value_count = extensions->value_count;
  2786. if(ext == NULL){
  2787. ext = newext;
  2788. head = newext;
  2789. } else {
  2790. ext->next = newext;
  2791. ext = newext;
  2792. }
  2793. extensions = extensions->next;
  2794. }
  2795. return head;
  2796. }
  2797. void
  2798. schema_free_extensions(schemaext *extensions)
  2799. {
  2800. if(extensions){
  2801. schemaext *prev;
  2802. while(extensions){
  2803. slapi_ch_free_string(&extensions->term);
  2804. charray_free(extensions->values);
  2805. prev = extensions;
  2806. extensions = extensions->next;
  2807. slapi_ch_free( (void **)&prev);
  2808. }
  2809. }
  2810. }
  2811. static void
  2812. oc_free( struct objclass **ocp )
  2813. {
  2814. struct objclass *oc;
  2815. if ( NULL != ocp && NULL != *ocp ) {
  2816. oc = *ocp;
  2817. slapi_ch_free( (void **)&oc->oc_name );
  2818. slapi_ch_free( (void **)&oc->oc_desc );
  2819. slapi_ch_free( (void **)&oc->oc_oid );
  2820. slapi_ch_free( (void **)&oc->oc_superior );
  2821. charray_free( oc->oc_required );
  2822. charray_free( oc->oc_allowed );
  2823. charray_free( oc->oc_orig_required );
  2824. charray_free( oc->oc_orig_allowed );
  2825. schema_free_extensions( oc->oc_extensions );
  2826. slapi_ch_free( (void **)&oc );
  2827. *ocp = NULL;
  2828. }
  2829. }
  2830. #if !defined (USE_OPENLDAP)
  2831. /*
  2832. * read_oc_ldif_return
  2833. * Free all the memory that read_oc_ldif() allocated, and return the retVal
  2834. *
  2835. * It's nice to do all the freeing in one spot, as read_oc_ldif() returns sideways
  2836. */
  2837. static int
  2838. read_oc_ldif_return( int retVal,
  2839. char *oid,
  2840. struct sizedbuffer *name,
  2841. char *sup,
  2842. char *desc )
  2843. {
  2844. slapi_ch_free((void **)&oid);
  2845. sizedbuffer_destroy( name );
  2846. slapi_ch_free((void **)&sup);
  2847. slapi_ch_free((void **)&desc);
  2848. return retVal;
  2849. }
  2850. /*
  2851. * read_oc_ldif
  2852. * Read the value of the objectclasses attribute in cn=schema, convert it
  2853. * into an objectclass struct.
  2854. *
  2855. * Arguments:
  2856. *
  2857. * input : value of objectclasses attribute to read
  2858. * oc : pointer write the objectclass to
  2859. * errorbuf : buffer to write any errors to
  2860. * is_user_defined : if non-zero, force objectclass to be user defined
  2861. * schema_flags : Any or none of the following bits could be set
  2862. * DSE_SCHEMA_NO_CHECK -- schema won't be checked
  2863. * DSE_SCHEMA_NO_GLOCK -- don't lock global resources
  2864. * DSE_SCHEMA_LOCKED -- already locked with
  2865. * reload_schemafile_lock;
  2866. * no further lock needed
  2867. * schema_ds4x_compat: if non-zero, act like Netscape DS 4.x
  2868. *
  2869. * Returns: an LDAP error code
  2870. *
  2871. * LDAP_SUCCESS if the objectclass was sucessfully read, the new
  2872. * objectclass will be written to oc
  2873. *
  2874. * All others: there was an error, an error message will
  2875. * be written to errorbuf
  2876. */
  2877. static int
  2878. read_oc_ldif ( const char *input, struct objclass **oc, char *errorbuf,
  2879. size_t errorbufsize, PRUint32 schema_flags, int is_user_defined,
  2880. int schema_ds4x_compat )
  2881. {
  2882. int i, j;
  2883. const char *pstart, *nextinput;
  2884. struct objclass *pnew_oc, *psup_oc;
  2885. char **RequiredAttrsArray, **AllowedAttrsArray;
  2886. char **OrigRequiredAttrsArray, **OrigAllowedAttrsArray;
  2887. char *pend, *pOcOid, *pOcSup, *pOcDesc;
  2888. struct sizedbuffer *psbOcName= sizedbuffer_construct(BUFSIZ);
  2889. PRUint8 kind, flags;
  2890. int invalid_syntax_error;
  2891. schema_strstr_fn_t keyword_strstr_fn;
  2892. schemaext *extensions = NULL;
  2893. /*
  2894. * From RFC 2252 section 4.4:
  2895. *
  2896. * ObjectClassDescription = "(" whsp
  2897. * numericoid whsp ; ObjectClass identifier
  2898. * [ "NAME" qdescrs ]
  2899. * [ "DESC" qdstring ]
  2900. * [ "OBSOLETE" whsp ]
  2901. * [ "SUP" oids ] ; Superior ObjectClasses
  2902. * [ ( "ABSTRACT" / "STRUCTURAL" / "AUXILIARY" ) whsp ]
  2903. * ; default structural
  2904. * [ "MUST" oids ] ; AttributeTypes
  2905. * [ "MAY" oids ] ; AttributeTypes
  2906. * whsp ")"
  2907. *
  2908. * XXXmcs: Our parsing technique is poor. In (Netscape) DS 4.12 and earlier
  2909. * releases, parsing was mostly done by looking anywhere within the input
  2910. * string for various keywords such as "MUST". But if, for example, a
  2911. * description contains the word "must", the parser would take assume that
  2912. * the tokens following the word were attribute types or OIDs. Bad news.
  2913. *
  2914. * In iDS 5.0 and later, we parse in order left to right and advance a
  2915. * pointer as we consume the input string (the nextinput variable). We
  2916. * also use a case-insensitive search when looking for keywords such as
  2917. * DESC. But the parser will still be fooled by sequences like:
  2918. *
  2919. * ( 1.2.3.4 NAME 'testOC' MUST ( DESC cn ) )
  2920. *
  2921. * Someday soon we will need to write a real parser.
  2922. *
  2923. * Compatibility notes: if schema_ds4x_compat is set, we:
  2924. * 1. always parse from the beginning of the string
  2925. * 2. use a case-insensitive compare when looking for keywords, e.g., MUST
  2926. */
  2927. if ( schema_ds4x_compat ) {
  2928. keyword_strstr_fn = PL_strcasestr;
  2929. invalid_syntax_error = LDAP_OPERATIONS_ERROR;
  2930. } else {
  2931. keyword_strstr_fn = PL_strstr;
  2932. invalid_syntax_error = LDAP_INVALID_SYNTAX;
  2933. }
  2934. flags = 0;
  2935. pOcOid = pOcSup = pOcDesc = NULL;
  2936. if ( NULL == input || '\0' == input[0] ) {
  2937. schema_create_errormsg( errorbuf, errorbufsize, schema_errprefix_oc, NULL,
  2938. "One or more values are required for the objectClasses attribute" );
  2939. LDAPDebug ( LDAP_DEBUG_ANY, "NULL args passed to read_oc_ldif\n",0,0,0);
  2940. return read_oc_ldif_return( LDAP_OPERATIONS_ERROR, pOcOid, psbOcName,
  2941. pOcSup, pOcDesc );
  2942. }
  2943. nextinput = input;
  2944. /* look for the OID */
  2945. if ( NULL == ( pOcOid = get_tagged_oid( "(", &nextinput,
  2946. keyword_strstr_fn ))) {
  2947. schema_create_errormsg( errorbuf, errorbufsize, schema_errprefix_oc,
  2948. input, "Value is malformed. It must include a \"(\"");
  2949. return read_oc_ldif_return( invalid_syntax_error, pOcOid, psbOcName,
  2950. pOcSup, pOcDesc );
  2951. }
  2952. if ( schema_ds4x_compat || ( strcasecmp(pOcOid, "NAME") == 0))
  2953. nextinput = input;
  2954. /* look for the NAME */
  2955. if ( (pstart = (*keyword_strstr_fn)(nextinput, "NAME '")) != NULL ) {
  2956. pstart += 6;
  2957. sizedbuffer_allocate(psbOcName,strlen(pstart)+1);
  2958. if ( sscanf ( pstart, "%s", psbOcName->buffer ) > 0 ) {
  2959. /* strip the trailing single quote */
  2960. if ( psbOcName->buffer[strlen(psbOcName->buffer)-1] == '\'' ) {
  2961. psbOcName->buffer[strlen(psbOcName->buffer)-1] = '\0';
  2962. nextinput = pstart + strlen(psbOcName->buffer) + 1;
  2963. } else {
  2964. schema_create_errormsg( errorbuf, errorbufsize, schema_errprefix_oc,
  2965. input, "Value is malformed. It must include a single quote around"
  2966. " the name" );
  2967. return read_oc_ldif_return( invalid_syntax_error, pOcOid, psbOcName,
  2968. pOcSup, pOcDesc );
  2969. }
  2970. }
  2971. } else {
  2972. schema_create_errormsg( errorbuf, errorbufsize, schema_errprefix_oc,
  2973. input, "Value is malformed. It must include a \"NAME '\"");
  2974. return read_oc_ldif_return( invalid_syntax_error, pOcOid, psbOcName,
  2975. pOcSup, pOcDesc );
  2976. }
  2977. /*
  2978. ** if the objectclass ldif doesn't have an OID, we'll make the oid
  2979. ** ocname-oid
  2980. */
  2981. if ( strcasecmp ( pOcOid, "NAME" ) == 0 ) {
  2982. slapi_ch_free_string( &pOcOid );
  2983. pOcOid = slapi_ch_smprintf("%s-oid", psbOcName->buffer );
  2984. }
  2985. if ( schema_ds4x_compat ) nextinput = input;
  2986. /* look for an optional DESCription */
  2987. if ( (pstart = (*keyword_strstr_fn) ( nextinput, " DESC '")) != NULL ) {
  2988. pstart += 7;
  2989. if (( pend = strchr( pstart, '\'' )) == NULL ) {
  2990. pend = (char *)(pstart + strlen(pstart));
  2991. }
  2992. pOcDesc = slapi_ch_malloc( pend - pstart + 1 );
  2993. memcpy( pOcDesc, pstart, pend - pstart );
  2994. pOcDesc[ pend - pstart ] = '\0';
  2995. nextinput = pend + 1;
  2996. }
  2997. if ( schema_ds4x_compat ) nextinput = input;
  2998. /* look for the optional OBSOLETE marker */
  2999. flags |= get_flag_keyword( schema_obsolete_with_spaces,
  3000. OC_FLAG_OBSOLETE, &nextinput, keyword_strstr_fn );
  3001. if (!(schema_flags & DSE_SCHEMA_NO_GLOCK)) {
  3002. oc_lock_read(); /* needed because we access the superior oc */
  3003. }
  3004. if ( schema_ds4x_compat ) nextinput = input;
  3005. /*
  3006. * Look for the superior objectclass. We first look for a parenthesized
  3007. * list and if not found we look for a simple OID.
  3008. *
  3009. * XXXmcs: Since we do not yet support multiple superior objectclasses, we
  3010. * just grab the first OID in a parenthesized list.
  3011. */
  3012. if ( NULL == ( pOcSup = get_tagged_oid( " SUP (", &nextinput,
  3013. keyword_strstr_fn ))) {
  3014. pOcSup = get_tagged_oid( " SUP ", &nextinput, keyword_strstr_fn );
  3015. }
  3016. psup_oc = oc_find_nolock ( pOcSup, NULL, PR_FALSE);
  3017. if ( schema_ds4x_compat ) nextinput = input;
  3018. /* look for the optional kind (ABSTRACT, STRUCTURAL, AUXILIARY) */
  3019. for ( i = 0; i < SCHEMA_OC_KIND_COUNT; ++i ) {
  3020. if ( NULL != ( pstart = (*keyword_strstr_fn)( nextinput,
  3021. schema_oc_kind_strings_with_spaces[i] ))) {
  3022. kind = i;
  3023. nextinput = pstart + strlen( schema_oc_kind_strings_with_spaces[i] ) - 1;
  3024. break;
  3025. }
  3026. }
  3027. if ( i >= SCHEMA_OC_KIND_COUNT ) { /* not found */
  3028. if ( NULL != psup_oc && OC_KIND_ABSTRACT != psup_oc->oc_kind ) {
  3029. /* inherit kind from superior class if not ABSTRACT */
  3030. kind = psup_oc->oc_kind;
  3031. } else {
  3032. /* according to RFC 2252, the default is structural */
  3033. kind = OC_KIND_STRUCTURAL;
  3034. }
  3035. }
  3036. if ( schema_ds4x_compat ) nextinput = input;
  3037. /* look for required attributes (MUST) */
  3038. if ( (pstart = (*keyword_strstr_fn) (nextinput, " MUST ")) != NULL ) {
  3039. char *pRequiredAttrs;
  3040. int saw_open_paren = 0;
  3041. pstart += 6;
  3042. pstart = skipWS( pstart ); /* skip past any extra white space */
  3043. if ( *pstart == '(' ) {
  3044. saw_open_paren = 1;
  3045. ++pstart;
  3046. }
  3047. pRequiredAttrs = slapi_ch_strdup ( pstart );
  3048. if ( saw_open_paren && (pend = strchr (pRequiredAttrs, ')')) != NULL ) {
  3049. *pend = '\0';
  3050. } else if ((pend = strchr (pRequiredAttrs, ' ' )) != NULL ) {
  3051. *pend = '\0';
  3052. } else {
  3053. pend = pRequiredAttrs + strlen(pRequiredAttrs); /* at end of string */
  3054. }
  3055. nextinput = pstart + ( pend - pRequiredAttrs );
  3056. RequiredAttrsArray = read_dollar_values (pRequiredAttrs);
  3057. slapi_ch_free((void**)&pRequiredAttrs);
  3058. } else {
  3059. RequiredAttrsArray = (char **) slapi_ch_malloc (1 * sizeof(char *)) ;
  3060. RequiredAttrsArray[0] = NULL;
  3061. }
  3062. if ( schema_ds4x_compat ) nextinput = input;
  3063. /* look for allowed attributes (MAY) */
  3064. if ( (pstart = (*keyword_strstr_fn) (nextinput, " MAY ")) != NULL ) {
  3065. char *pAllowedAttrs;
  3066. int saw_open_paren = 0;
  3067. pstart += 5;
  3068. pstart = skipWS( pstart ); /* skip past any extra white space */
  3069. if ( *pstart == '(' ) {
  3070. saw_open_paren = 1;
  3071. ++pstart;
  3072. }
  3073. pAllowedAttrs = slapi_ch_strdup ( pstart );
  3074. if ( saw_open_paren && (pend = strchr (pAllowedAttrs, ')')) != NULL ) {
  3075. *pend = '\0';
  3076. } else if ((pend = strchr (pAllowedAttrs, ' ' )) != NULL ) {
  3077. *pend = '\0';
  3078. } else {
  3079. pend = pAllowedAttrs + strlen(pAllowedAttrs); /* at end of string */
  3080. }
  3081. nextinput = pstart + ( pend - pAllowedAttrs );
  3082. AllowedAttrsArray = read_dollar_values (pAllowedAttrs);
  3083. slapi_ch_free((void**)&pAllowedAttrs);
  3084. } else {
  3085. AllowedAttrsArray = (char **) slapi_ch_malloc (1 * sizeof(char *)) ;
  3086. AllowedAttrsArray[0] = NULL;
  3087. }
  3088. if ( schema_ds4x_compat ) nextinput = input;
  3089. /* look for X-ORIGIN list */
  3090. if (is_user_defined) {
  3091. /* add X-ORIGIN 'user defined' */
  3092. extensions = parse_extensions( nextinput, schema_user_defined_origin );
  3093. flags |= OC_FLAG_USER_OC;
  3094. } else {
  3095. /* add nothing */
  3096. extensions = parse_extensions( nextinput, NULL );
  3097. flags |= OC_FLAG_STANDARD_OC;
  3098. }
  3099. /* generate OrigRequiredAttrsArray and OrigAllowedAttrsArray */
  3100. if (psup_oc) {
  3101. int found_it;
  3102. OrigRequiredAttrsArray = (char **) slapi_ch_malloc (1 * sizeof(char *)) ;
  3103. OrigRequiredAttrsArray[0] = NULL;
  3104. OrigAllowedAttrsArray = (char **) slapi_ch_malloc (1 * sizeof(char *)) ;
  3105. OrigAllowedAttrsArray[0] = NULL;
  3106. if (psup_oc->oc_required) {
  3107. for (i = 0; RequiredAttrsArray[i]; i++) {
  3108. for (j = 0, found_it = 0; psup_oc->oc_required[j]; j++) {
  3109. if (strcasecmp (psup_oc->oc_required[j], RequiredAttrsArray[i]) == 0) {
  3110. found_it = 1;
  3111. }
  3112. }
  3113. if (!found_it) {
  3114. charray_add (&OrigRequiredAttrsArray, slapi_ch_strdup ( RequiredAttrsArray[i] ) );
  3115. }
  3116. }
  3117. }
  3118. if (psup_oc->oc_allowed) {
  3119. for (i = 0; AllowedAttrsArray[i]; i++) {
  3120. for (j = 0, found_it=0; psup_oc->oc_allowed[j]; j++) {
  3121. if (strcasecmp (psup_oc->oc_allowed[j], AllowedAttrsArray[i]) == 0) {
  3122. found_it = 1;
  3123. }
  3124. }
  3125. if (!found_it) {
  3126. charray_add (&OrigAllowedAttrsArray, slapi_ch_strdup (AllowedAttrsArray[i]) );
  3127. }
  3128. }
  3129. }
  3130. }
  3131. else {
  3132. /* if no parent oc */
  3133. OrigRequiredAttrsArray = charray_dup ( RequiredAttrsArray );
  3134. OrigAllowedAttrsArray = charray_dup ( AllowedAttrsArray );
  3135. }
  3136. if (!(schema_flags & DSE_SCHEMA_NO_GLOCK)) {
  3137. oc_unlock(); /* we are done accessing superior oc (psup_oc) */
  3138. }
  3139. /* finally -- create new objclass structure */
  3140. pnew_oc = (struct objclass *) slapi_ch_malloc (1 * sizeof (struct objclass));
  3141. pnew_oc->oc_name = slapi_ch_strdup ( psbOcName->buffer );
  3142. pnew_oc->oc_superior = pOcSup;
  3143. pOcSup = NULL; /* don't free this later */
  3144. pnew_oc->oc_oid = pOcOid;
  3145. pOcOid = NULL; /* don't free this later */
  3146. pnew_oc->oc_desc = pOcDesc;
  3147. pOcDesc = NULL; /* don't free this later */
  3148. pnew_oc->oc_required = RequiredAttrsArray;
  3149. pnew_oc->oc_allowed = AllowedAttrsArray;
  3150. pnew_oc->oc_orig_required = OrigRequiredAttrsArray;
  3151. pnew_oc->oc_orig_allowed = OrigAllowedAttrsArray;
  3152. pnew_oc->oc_extensions = extensions;
  3153. pnew_oc->oc_next = NULL;
  3154. pnew_oc->oc_flags = flags;
  3155. pnew_oc->oc_kind = kind;
  3156. *oc = pnew_oc;
  3157. return read_oc_ldif_return( LDAP_SUCCESS, pOcOid, psbOcName, pOcSup, pOcDesc );
  3158. }
  3159. static char **read_dollar_values ( char *vals) {
  3160. int i,k;
  3161. char **retVal;
  3162. static const char *charsToRemove = " ()";
  3163. /* get rid of all the parens and spaces */
  3164. for ( i = 0, k = 0; vals[i]; i++) {
  3165. if (!strchr(charsToRemove, vals[i])) {
  3166. vals[k++] = vals[i];
  3167. }
  3168. }
  3169. vals[k] = '\0';
  3170. retVal = slapi_str2charray (vals, "$");
  3171. return retVal;
  3172. }
  3173. /*
  3174. * if asipp is NULL, the attribute type is added to the global set of schema.
  3175. * if asipp is not NULL, the AT is not added but *asipp is set. When you are
  3176. * finished with *asipp, use attr_syntax_free() to dispose of it.
  3177. *
  3178. * schema_flags: Any or none of the following bits could be set
  3179. * DSE_SCHEMA_NO_CHECK -- schema won't be checked
  3180. * DSE_SCHEMA_NO_GLOCK -- locking of global resources is turned off;
  3181. * this saves time during initialization since
  3182. * the server operates in single threaded mode
  3183. * at that time or in reload_schemafile_lock.
  3184. * DSE_SCHEMA_LOCKED -- already locked with reload_schemafile_lock;
  3185. * no further lock needed
  3186. *
  3187. * if is_user_defined is true, force attribute type to be user defined.
  3188. *
  3189. * returns an LDAP error code (LDAP_SUCCESS if all goes well)
  3190. */
  3191. static int
  3192. read_at_ldif(const char *input, struct asyntaxinfo **asipp, char *errorbuf,
  3193. size_t errorbufsize, PRUint32 schema_flags, int is_user_defined,
  3194. int schema_ds4x_compat, int is_remote)
  3195. {
  3196. char *pStart, *pEnd;
  3197. char *pOid, *pSyntax, *pSuperior, *pMREquality, *pMROrdering, *pMRSubstring;
  3198. const char *nextinput;
  3199. struct sizedbuffer *psbAttrName= sizedbuffer_construct(BUFSIZ);
  3200. struct sizedbuffer *psbAttrDesc= sizedbuffer_construct(BUFSIZ);
  3201. int status = 0;
  3202. int syntaxlength;
  3203. char **attr_names = NULL;
  3204. char *first_attr_name = NULL;
  3205. int num_names = 0;
  3206. unsigned long flags = SLAPI_ATTR_FLAG_OVERRIDE;
  3207. const char *ss = 0;
  3208. struct asyntaxinfo *tmpasip;
  3209. int invalid_syntax_error;
  3210. schema_strstr_fn_t keyword_strstr_fn;
  3211. schemaext *extensions = NULL;
  3212. /*
  3213. * From RFC 2252 section 4.2:
  3214. *
  3215. * AttributeTypeDescription = "(" whsp
  3216. * numericoid whsp ; AttributeType identifier
  3217. * [ "NAME" qdescrs ] ; name used in AttributeType
  3218. * [ "DESC" qdstring ] ; description
  3219. * [ "OBSOLETE" whsp ]
  3220. * [ "SUP" woid ] ; derived from this other
  3221. * ; AttributeType
  3222. * [ "EQUALITY" woid ; Matching Rule name
  3223. * [ "ORDERING" woid ; Matching Rule name
  3224. * [ "SUBSTR" woid ] ; Matching Rule name
  3225. * [ "SYNTAX" whsp noidlen whsp ] ; see section 4.3
  3226. * [ "SINGLE-VALUE" whsp ] ; default multi-valued
  3227. * [ "COLLECTIVE" whsp ] ; default not collective
  3228. * [ "NO-USER-MODIFICATION" whsp ]; default user modifiable
  3229. * [ "USAGE" whsp AttributeUsage ]; default userApplications
  3230. * whsp ")"
  3231. *
  3232. * AttributeUsage =
  3233. * "userApplications" /
  3234. * "directoryOperation" /
  3235. * "distributedOperation" / ; DSA-shared
  3236. * "dSAOperation" ; DSA-specific, value depends on server
  3237. *
  3238. * XXXmcs: Our parsing technique is poor. In (Netscape) DS 4.12 and earlier
  3239. * releases, parsing was mostly done by looking anywhere within the input
  3240. * string for various keywords such as "EQUALITY". But if, for example, a
  3241. * description contains the word "equality", the parser would take assume
  3242. * that the token following the word was a matching rule. Bad news.
  3243. *
  3244. * In iDS 5.0 and later, we parse in order left to right and advance a
  3245. * pointer as we consume the input string (the nextinput variable). We
  3246. * also use a case-insensitive search when looking for keywords such as
  3247. * DESC. This is still less than ideal.
  3248. *
  3249. * Someday soon we will need to write a real parser.
  3250. *
  3251. * Compatibility notes: if schema_ds4x_compat is set, we:
  3252. * 1. always parse from the beginning of the string
  3253. * 2. use a case-insensitive compare when looking for keywords, e.g., DESC
  3254. */
  3255. if ( schema_ds4x_compat ) {
  3256. keyword_strstr_fn = PL_strcasestr;
  3257. invalid_syntax_error = LDAP_OPERATIONS_ERROR;
  3258. } else {
  3259. keyword_strstr_fn = PL_strstr;
  3260. invalid_syntax_error = LDAP_INVALID_SYNTAX;
  3261. }
  3262. if (schema_flags & DSE_SCHEMA_NO_GLOCK)
  3263. flags |= SLAPI_ATTR_FLAG_NOLOCKING;
  3264. psbAttrName->buffer[0] = '\0';
  3265. psbAttrDesc->buffer[0] = '\0';
  3266. pOid = pSyntax = pSuperior = NULL;
  3267. pMREquality = pMROrdering = pMRSubstring = NULL;
  3268. syntaxlength = SLAPI_SYNTAXLENGTH_NONE;
  3269. nextinput = input;
  3270. /* get the OID */
  3271. pOid = get_tagged_oid( "(", &nextinput, keyword_strstr_fn );
  3272. if (NULL == pOid) {
  3273. schema_create_errormsg( errorbuf, errorbufsize, schema_errprefix_at,
  3274. input, "Missing or invalid OID" );
  3275. status = invalid_syntax_error;
  3276. goto done;
  3277. }
  3278. if ( schema_ds4x_compat || (strcasecmp(pOid, "NAME") == 0))
  3279. nextinput = input;
  3280. /* look for the NAME (single or list of names) */
  3281. if ( (pStart = (*keyword_strstr_fn) ( nextinput, "NAME ")) != NULL ) {
  3282. pStart += 5;
  3283. sizedbuffer_allocate(psbAttrName,strlen(pStart)+1);
  3284. strcpy ( psbAttrName->buffer, pStart);
  3285. if (*pStart == '(')
  3286. pEnd = strchr(psbAttrName->buffer, ')');
  3287. else
  3288. pEnd = strchr(psbAttrName->buffer+1, '\'');
  3289. if (pEnd)
  3290. *(pEnd+1) = 0;
  3291. nextinput = pStart + strlen(psbAttrName->buffer) + 1;
  3292. attr_names = parse_qdescrs(psbAttrName->buffer, &num_names);
  3293. if ( NULL != attr_names ) {
  3294. first_attr_name = attr_names[0];
  3295. } else { /* NAME followed by nothing violates syntax */
  3296. schema_create_errormsg( errorbuf, errorbufsize, schema_errprefix_at,
  3297. input, "Missing or invalid attribute name" );
  3298. status = invalid_syntax_error;
  3299. goto done;
  3300. }
  3301. }
  3302. if ( schema_ds4x_compat ) nextinput = input;
  3303. /*
  3304. * if the attribute ldif doesn't have an OID, we'll make the oid
  3305. * attrname-oid
  3306. */
  3307. if ( (strcasecmp ( pOid, "NAME" ) == 0) && (first_attr_name)) {
  3308. slapi_ch_free_string( &pOid );
  3309. pOid = slapi_ch_smprintf("%s-oid", first_attr_name );
  3310. }
  3311. /* look for the optional DESCription */
  3312. if ( (pStart = (*keyword_strstr_fn) ( nextinput, "DESC '")) != NULL ) {
  3313. pStart += 6;
  3314. sizedbuffer_allocate(psbAttrDesc,strlen(pStart)+1);
  3315. strcpy ( psbAttrDesc->buffer, pStart);
  3316. if ( (pEnd = strchr (psbAttrDesc->buffer, '\'' )) != NULL ){
  3317. *pEnd ='\0';
  3318. }
  3319. nextinput = pStart + strlen(psbAttrDesc->buffer) + 1;
  3320. }
  3321. if ( schema_ds4x_compat ) nextinput = input;
  3322. /* look for the optional OBSOLETE marker */
  3323. flags |= get_flag_keyword( schema_obsolete_with_spaces,
  3324. SLAPI_ATTR_FLAG_OBSOLETE, &nextinput, keyword_strstr_fn );
  3325. if ( schema_ds4x_compat ) nextinput = input;
  3326. /* look for the optional SUPerior type */
  3327. pSuperior = get_tagged_oid( "SUP ", &nextinput, keyword_strstr_fn );
  3328. if ( schema_ds4x_compat ) nextinput = input;
  3329. /* look for the optional matching rules */
  3330. pMREquality = get_tagged_oid( "EQUALITY ", &nextinput, keyword_strstr_fn );
  3331. if ( schema_ds4x_compat ) nextinput = input;
  3332. pMROrdering = get_tagged_oid( "ORDERING ", &nextinput, keyword_strstr_fn );
  3333. if ( schema_ds4x_compat ) nextinput = input;
  3334. pMRSubstring = get_tagged_oid( "SUBSTR ", &nextinput, keyword_strstr_fn );
  3335. if ( schema_ds4x_compat ) nextinput = input;
  3336. /* look for the optional SYNTAX */
  3337. if ( NULL != ( pSyntax = get_tagged_oid( "SYNTAX ", &nextinput,
  3338. keyword_strstr_fn ))) {
  3339. /*
  3340. * Check for an optional {LEN}, which if present indicates a
  3341. * suggested maximum size for values of this attribute type.
  3342. *
  3343. * XXXmcs: we do not enforce length restrictions, but we do read
  3344. * and include them in the subschemasubentry.
  3345. */
  3346. if ( (pEnd = strchr ( pSyntax, '{')) != NULL /* balance } */ ) {
  3347. *pEnd = '\0';
  3348. syntaxlength = atoi( pEnd + 1 );
  3349. }
  3350. }
  3351. if ( schema_ds4x_compat ) nextinput = input;
  3352. /* look for the optional SINGLE-VALUE marker */
  3353. flags |= get_flag_keyword( " SINGLE-VALUE ",
  3354. SLAPI_ATTR_FLAG_SINGLE, &nextinput, keyword_strstr_fn );
  3355. if ( schema_ds4x_compat ) nextinput = input;
  3356. /* look for the optional COLLECTIVE marker */
  3357. flags |= get_flag_keyword( schema_collective_with_spaces,
  3358. SLAPI_ATTR_FLAG_COLLECTIVE, &nextinput, keyword_strstr_fn );
  3359. if ( schema_ds4x_compat ) nextinput = input;
  3360. /* look for the optional NO-USER-MODIFICATION marker */
  3361. flags |= get_flag_keyword( schema_nousermod_with_spaces,
  3362. SLAPI_ATTR_FLAG_NOUSERMOD, &nextinput, keyword_strstr_fn );
  3363. if ( schema_ds4x_compat ) nextinput = input;
  3364. /* look for the optional USAGE */
  3365. if (NULL != (ss = (*keyword_strstr_fn)(nextinput, " USAGE "))) {
  3366. ss += 7;
  3367. ss = skipWS(ss);
  3368. if (ss) {
  3369. if ( !PL_strncmp(ss, "directoryOperation",
  3370. strlen("directoryOperation"))) {
  3371. flags |= SLAPI_ATTR_FLAG_OPATTR;
  3372. }
  3373. if ( !PL_strncmp(ss, "distributedOperation",
  3374. strlen("distributedOperation"))) {
  3375. flags |= SLAPI_ATTR_FLAG_OPATTR|SLAPI_ATTR_FLAG_DISTRIBUTED_OPERATION;
  3376. }
  3377. if ( !PL_strncmp(ss, "dSAOperation",
  3378. strlen("dSAOperation"))) {
  3379. flags |= SLAPI_ATTR_FLAG_OPATTR|SLAPI_ATTR_FLAG_DSA_OPERATION;
  3380. }
  3381. if ( NULL == ( nextinput = strchr( ss, ' ' ))) {
  3382. nextinput = ss + strlen(ss);
  3383. }
  3384. }
  3385. }
  3386. if ( schema_ds4x_compat ) nextinput = input;
  3387. /* X-ORIGIN list */
  3388. if (is_user_defined) {
  3389. /* add X-ORIGIN 'user defined' */
  3390. extensions = parse_extensions( nextinput, schema_user_defined_origin );
  3391. } else {
  3392. /* add nothing extra*/
  3393. extensions = parse_extensions( nextinput, NULL );
  3394. flags |= SLAPI_ATTR_FLAG_STD_ATTR;
  3395. }
  3396. /* Do some sanity checking to make sure everything was read correctly */
  3397. if (NULL == pOid) {
  3398. schema_create_errormsg( errorbuf, errorbufsize, schema_errprefix_at,
  3399. first_attr_name, "Missing OID" );
  3400. status = invalid_syntax_error;
  3401. }
  3402. if (!status && (!attr_names || !num_names)) {
  3403. schema_create_errormsg( errorbuf, errorbufsize, schema_errprefix_at,
  3404. first_attr_name,
  3405. "Missing name (OID is \"%s\")", pOid );
  3406. status = invalid_syntax_error;
  3407. }
  3408. if (!status && (NULL != pSuperior)) {
  3409. struct asyntaxinfo *asi_parent;
  3410. asi_parent = attr_syntax_get_by_name(pSuperior);
  3411. /* if we find no match then server won't start or add the attribute type */
  3412. if (asi_parent == NULL) {
  3413. LDAPDebug (LDAP_DEBUG_PARSE,
  3414. "Cannot find parent attribute type \"%s\"\n",pSuperior,
  3415. NULL,NULL);
  3416. schema_create_errormsg( errorbuf, errorbufsize,
  3417. schema_errprefix_at, first_attr_name,
  3418. "Missing parent attribute syntax OID");
  3419. status = invalid_syntax_error;
  3420. /* We only want to use the parent syntax if a SYNTAX
  3421. * wasn't explicitly specified for this attribute. */
  3422. } else if ((NULL == pSyntax) || (NULL == pMREquality) || (NULL == pMRSubstring) ||
  3423. (NULL == pMROrdering)) {
  3424. char *pso = asi_parent->asi_plugin->plg_syntax_oid;
  3425. if (pso && (NULL == pSyntax)) {
  3426. pSyntax = slapi_ch_strdup(pso);
  3427. LDAPDebug (LDAP_DEBUG_TRACE,
  3428. "Inheriting syntax %s from parent type %s\n",
  3429. pSyntax, pSuperior,NULL);
  3430. } else if (NULL == pSyntax) {
  3431. schema_create_errormsg( errorbuf, errorbufsize,
  3432. schema_errprefix_at, first_attr_name,
  3433. "Missing parent attribute syntax OID");
  3434. status = invalid_syntax_error;
  3435. }
  3436. if (NULL == pMREquality) {
  3437. pMREquality = slapi_ch_strdup(asi_parent->asi_mr_equality);
  3438. }
  3439. if (NULL == pMRSubstring) {
  3440. pMRSubstring = slapi_ch_strdup(asi_parent->asi_mr_substring);
  3441. }
  3442. if (NULL == pMROrdering) {
  3443. pMROrdering = slapi_ch_strdup(asi_parent->asi_mr_ordering);
  3444. }
  3445. attr_syntax_return( asi_parent );
  3446. }
  3447. }
  3448. if (!status && (NULL == pSyntax)) {
  3449. schema_create_errormsg( errorbuf, errorbufsize, schema_errprefix_at,
  3450. first_attr_name, "Missing attribute syntax OID");
  3451. status = invalid_syntax_error;
  3452. }
  3453. if (!status && (plugin_syntax_find ( pSyntax ) == NULL) ) {
  3454. schema_create_errormsg( errorbuf, errorbufsize, schema_errprefix_at,
  3455. first_attr_name, "Unknown attribute syntax OID \"%s\"",
  3456. pSyntax );
  3457. status = invalid_syntax_error;
  3458. }
  3459. if (!status) {
  3460. struct objclass *poc;
  3461. /* check to make sure that the OID isn't being used by an objectclass */
  3462. if (!(schema_flags & DSE_SCHEMA_LOCKED))
  3463. oc_lock_read();
  3464. poc = oc_find_oid_nolock( pOid );
  3465. if ( poc != NULL) {
  3466. schema_create_errormsg( errorbuf, errorbufsize,
  3467. schema_errprefix_at, first_attr_name,
  3468. "The OID \"%s\" is also used by the object class \"%s\"",
  3469. pOid, poc->oc_name);
  3470. status = LDAP_TYPE_OR_VALUE_EXISTS;
  3471. }
  3472. if (!(schema_flags & DSE_SCHEMA_LOCKED))
  3473. oc_unlock();
  3474. }
  3475. if (!(schema_flags & DSE_SCHEMA_NO_CHECK) && !status) {
  3476. int ii;
  3477. /* check to see if the attribute name is valid */
  3478. for (ii = 0; !status && (ii < num_names); ++ii) {
  3479. if ( schema_check_name(attr_names[ii], PR_TRUE, errorbuf,
  3480. errorbufsize) == 0 ) {
  3481. status = invalid_syntax_error;
  3482. }
  3483. else if (!(flags & SLAPI_ATTR_FLAG_OVERRIDE) &&
  3484. attr_syntax_exists(attr_names[ii])) {
  3485. schema_create_errormsg( errorbuf, errorbufsize,
  3486. schema_errprefix_at, attr_names[ii],
  3487. "Could not be added because it already exists" );
  3488. status = LDAP_TYPE_OR_VALUE_EXISTS;
  3489. }
  3490. }
  3491. }
  3492. if (!(schema_flags & DSE_SCHEMA_NO_CHECK) && !status) {
  3493. if ( schema_check_oid ( first_attr_name, pOid, PR_TRUE, errorbuf,
  3494. errorbufsize ) == 0 ) {
  3495. status = invalid_syntax_error;
  3496. }
  3497. }
  3498. if (!status) {
  3499. struct asyntaxinfo *tmpasi;
  3500. if (!(flags & SLAPI_ATTR_FLAG_OVERRIDE) &&
  3501. ( NULL != ( tmpasi = attr_syntax_get_by_oid(pOid)))) {
  3502. schema_create_errormsg( errorbuf, errorbufsize,
  3503. schema_errprefix_at, first_attr_name,
  3504. "Could not be added because the OID \"%s\" is already in use",
  3505. pOid);
  3506. status = LDAP_TYPE_OR_VALUE_EXISTS;
  3507. attr_syntax_return( tmpasi );
  3508. }
  3509. }
  3510. if (!status) {
  3511. status = attr_syntax_create( pOid, attr_names,
  3512. *psbAttrDesc->buffer == '\0' ? NULL : psbAttrDesc->buffer,
  3513. pSuperior, pMREquality, pMROrdering, pMRSubstring, extensions,
  3514. pSyntax, syntaxlength, flags, &tmpasip );
  3515. }
  3516. if (!status) {
  3517. if ( NULL != asipp ) {
  3518. *asipp = tmpasip; /* just return it */
  3519. } else { /* add the new attribute to the global store */
  3520. status = attr_syntax_add( tmpasip );
  3521. if ( LDAP_SUCCESS != status ) {
  3522. if ( 0 != (flags & SLAPI_ATTR_FLAG_OVERRIDE) &&
  3523. LDAP_TYPE_OR_VALUE_EXISTS == status ) {
  3524. /*
  3525. * This can only occur if the name and OID don't match the
  3526. * attribute we are trying to override (all other cases of
  3527. * "type or value exists" were trapped above).
  3528. */
  3529. schema_create_errormsg( errorbuf, errorbufsize,
  3530. schema_errprefix_at, first_attr_name,
  3531. "Does not match the OID \"%s\". Another attribute"
  3532. " type is already using the name or OID.", pOid);
  3533. } else {
  3534. schema_create_errormsg( errorbuf, errorbufsize,
  3535. schema_errprefix_at, first_attr_name,
  3536. "Could not be added (OID is \"%s\")", pOid );
  3537. }
  3538. attr_syntax_free( tmpasip );
  3539. }
  3540. }
  3541. }
  3542. done:
  3543. /* free everything */
  3544. free_qdlist(attr_names, num_names);
  3545. sizedbuffer_destroy(psbAttrName);
  3546. sizedbuffer_destroy(psbAttrDesc);
  3547. slapi_ch_free((void **)&pOid);
  3548. slapi_ch_free((void **)&pSuperior);
  3549. slapi_ch_free((void **)&pMREquality);
  3550. slapi_ch_free((void **)&pMROrdering);
  3551. slapi_ch_free((void **)&pMRSubstring);
  3552. slapi_ch_free((void **)&pSyntax);
  3553. schema_free_extensions(extensions);
  3554. return status;
  3555. }
  3556. #else /* (USE_OPENLDAP) */
  3557. /* openldap attribute parser */
  3558. /*
  3559. * if asipp is NULL, the attribute type is added to the global set of schema.
  3560. * if asipp is not NULL, the AT is not added but *asipp is set. When you are
  3561. * finished with *asipp, use attr_syntax_free() to dispose of it.
  3562. *
  3563. * schema_flags: Any or none of the following bits could be set
  3564. * DSE_SCHEMA_NO_CHECK -- schema won't be checked
  3565. * DSE_SCHEMA_NO_GLOCK -- locking of global resources is turned off;
  3566. * this saves time during initialization since
  3567. * the server operates in single threaded mode
  3568. * at that time or in reload_schemafile_lock.
  3569. * DSE_SCHEMA_LOCKED -- already locked with reload_schemafile_lock;
  3570. * no further lock needed
  3571. *
  3572. * if is_user_defined is true, force attribute type to be user defined.
  3573. *
  3574. * returns an LDAP error code (LDAP_SUCCESS if all goes well)
  3575. */
  3576. static int
  3577. parse_attr_str(const char *input, struct asyntaxinfo **asipp, char *errorbuf,
  3578. size_t errorbufsize, PRUint32 schema_flags, int is_user_defined,
  3579. int schema_ds4x_compat, int is_remote)
  3580. {
  3581. struct asyntaxinfo *tmpasip;
  3582. struct asyntaxinfo *tmpasi;
  3583. schemaext *extensions = NULL, *head = NULL;
  3584. struct objclass *poc;
  3585. LDAPAttributeType *atype = NULL;
  3586. const char *errp;
  3587. char *first_attr_name = NULL;
  3588. char **attr_names = NULL;
  3589. unsigned long flags = SLAPI_ATTR_FLAG_OVERRIDE;
  3590. /* If we ever accept openldap schema directly, then make parser_flags configurable */
  3591. const int parser_flags = LDAP_SCHEMA_ALLOW_NONE | LDAP_SCHEMA_ALLOW_NO_OID;
  3592. int invalid_syntax_error;
  3593. int syntaxlength = SLAPI_SYNTAXLENGTH_NONE;
  3594. int num_names = 0;
  3595. int status = 0;
  3596. int rc = 0;
  3597. int a, aa;
  3598. /*
  3599. * OpenLDAP AttributeType struct
  3600. *
  3601. * typedef struct ldap_attributetype {
  3602. * char *at_oid; OID
  3603. * char **at_names; Names
  3604. * char *at_desc; Description
  3605. * int at_obsolete; Is obsolete?
  3606. * char *at_sup_oid; OID of superior type
  3607. * char *at_equality_oid; OID of equality matching rule
  3608. * char *at_ordering_oid; OID of ordering matching rule
  3609. * char *at_substr_oid; OID of substrings matching rule
  3610. * char *at_syntax_oid; OID of syntax of values
  3611. * int at_syntax_len; Suggested minimum maximum length
  3612. * int at_single_value; Is single-valued?
  3613. * int at_collective; Is collective?
  3614. * int at_no_user_mod; Are changes forbidden through LDAP?
  3615. * int at_usage; Usage, see below
  3616. * LDAPSchemaExtensionItem **at_extensions; Extensions
  3617. * } LDAPAttributeType;
  3618. */
  3619. /*
  3620. * Set the appropriate error code
  3621. */
  3622. if ( schema_ds4x_compat ) {
  3623. invalid_syntax_error = LDAP_OPERATIONS_ERROR;
  3624. } else {
  3625. invalid_syntax_error = LDAP_INVALID_SYNTAX;
  3626. }
  3627. /*
  3628. * Verify we have input
  3629. */
  3630. if(input == NULL || '\0' == input[0]){
  3631. schema_create_errormsg( errorbuf, errorbufsize, schema_errprefix_at, NULL,
  3632. "One or more values are required for the attributeTypes attribute" );
  3633. LDAPDebug ( LDAP_DEBUG_ANY, "NULL args passed to parse_attr_str\n",0,0,0);
  3634. return invalid_syntax_error;
  3635. }
  3636. /*
  3637. * Parse the line and create of attribute structure
  3638. */
  3639. while(isspace(*input)){
  3640. /* trim any leading spaces */
  3641. input++;
  3642. }
  3643. if((atype = ldap_str2attributetype(input, &rc, &errp, parser_flags )) == NULL){
  3644. schema_create_errormsg( errorbuf, errorbufsize, schema_errprefix_at, input,
  3645. "Failed to parse attribute, error(%d - %s) at (%s)", rc, ldap_scherr2str(rc), errp );
  3646. return invalid_syntax_error;
  3647. }
  3648. if (schema_flags & DSE_SCHEMA_NO_GLOCK){
  3649. flags |= SLAPI_ATTR_FLAG_NOLOCKING;
  3650. }
  3651. /*
  3652. * Check the NAME and update our name list
  3653. */
  3654. if ( NULL != atype->at_names ) {
  3655. for(; atype->at_names[num_names]; num_names++){
  3656. charray_add(&attr_names, slapi_ch_strdup(atype->at_names[num_names]));
  3657. }
  3658. first_attr_name = atype->at_names[0];
  3659. } else { /* NAME followed by nothing violates syntax */
  3660. schema_create_errormsg( errorbuf, errorbufsize, schema_errprefix_at, input,
  3661. "Missing or invalid attribute name" );
  3662. status = invalid_syntax_error;
  3663. goto done;
  3664. }
  3665. /*
  3666. * If the attribute type doesn't have an OID, we'll make the oid attrname-oid.
  3667. */
  3668. if (NULL == atype->at_oid) {
  3669. atype->at_oid = slapi_ch_smprintf("%s-oid", first_attr_name );
  3670. }
  3671. /*
  3672. * Set the flags
  3673. */
  3674. if(atype->at_obsolete){
  3675. flags |= SLAPI_ATTR_FLAG_OBSOLETE;
  3676. }
  3677. if(atype->at_single_value){
  3678. flags |= SLAPI_ATTR_FLAG_SINGLE;
  3679. }
  3680. if(atype->at_collective){
  3681. flags |= SLAPI_ATTR_FLAG_COLLECTIVE;
  3682. }
  3683. if(atype->at_no_user_mod){
  3684. flags |= SLAPI_ATTR_FLAG_NOUSERMOD;
  3685. }
  3686. if(atype->at_usage == LDAP_SCHEMA_DIRECTORY_OPERATION){
  3687. flags |= SLAPI_ATTR_FLAG_OPATTR;
  3688. }
  3689. if(atype->at_usage == LDAP_SCHEMA_DISTRIBUTED_OPERATION){
  3690. flags |= SLAPI_ATTR_FLAG_OPATTR|SLAPI_ATTR_FLAG_DISTRIBUTED_OPERATION;
  3691. }
  3692. if(atype->at_usage == LDAP_SCHEMA_DSA_OPERATION){
  3693. flags |= SLAPI_ATTR_FLAG_OPATTR|SLAPI_ATTR_FLAG_DSA_OPERATION;
  3694. }
  3695. /*
  3696. * Check the superior, and use it fill in any missing oids on this attribute
  3697. */
  3698. if (NULL != atype->at_sup_oid) {
  3699. struct asyntaxinfo *asi_parent;
  3700. asi_parent = attr_syntax_get_by_name(atype->at_sup_oid);
  3701. /* if we find no match then server won't start or add the attribute type */
  3702. if (asi_parent == NULL) {
  3703. LDAPDebug (LDAP_DEBUG_PARSE, "Cannot find parent attribute type \"%s\"\n",
  3704. atype->at_sup_oid, NULL, NULL);
  3705. schema_create_errormsg( errorbuf, errorbufsize, schema_errprefix_at, first_attr_name,
  3706. "Missing parent attribute syntax OID");
  3707. status = invalid_syntax_error;
  3708. goto done;
  3709. } else if ((NULL == atype->at_syntax_oid) || (NULL == atype->at_equality_oid) ||
  3710. (NULL == atype->at_substr_oid) || (NULL == atype->at_ordering_oid))
  3711. {
  3712. /*
  3713. * We only want to use the parent syntax if a SYNTAX
  3714. * wasn't explicitly specified for this attribute.
  3715. */
  3716. char *pso = asi_parent->asi_plugin->plg_syntax_oid;
  3717. if (pso && (NULL == atype->at_syntax_oid)) {
  3718. atype->at_syntax_oid = slapi_ch_strdup(pso);
  3719. LDAPDebug (LDAP_DEBUG_TRACE,
  3720. "Inheriting syntax %s from parent type %s\n",
  3721. atype->at_syntax_oid, atype->at_sup_oid,NULL);
  3722. } else if (NULL == atype->at_syntax_oid) {
  3723. schema_create_errormsg( errorbuf, errorbufsize,
  3724. schema_errprefix_at, first_attr_name,
  3725. "Missing parent attribute syntax OID");
  3726. status = invalid_syntax_error;
  3727. goto done;
  3728. }
  3729. if (NULL == atype->at_equality_oid) {
  3730. atype->at_equality_oid = slapi_ch_strdup(asi_parent->asi_mr_equality);
  3731. }
  3732. if (NULL == atype->at_substr_oid) {
  3733. atype->at_substr_oid = slapi_ch_strdup(asi_parent->asi_mr_substring);
  3734. }
  3735. if (NULL == atype->at_ordering_oid) {
  3736. atype->at_ordering_oid = slapi_ch_strdup(asi_parent->asi_mr_ordering);
  3737. }
  3738. attr_syntax_return( asi_parent );
  3739. }
  3740. }
  3741. /*
  3742. * Make sure we have a syntax oid set
  3743. */
  3744. if (NULL == atype->at_syntax_oid) {
  3745. schema_create_errormsg( errorbuf, errorbufsize, schema_errprefix_at,
  3746. first_attr_name, "Missing attribute syntax OID");
  3747. status = invalid_syntax_error;
  3748. goto done;
  3749. }
  3750. /*
  3751. * Make sure the OID is known
  3752. */
  3753. if (!status && (plugin_syntax_find ( atype->at_syntax_oid ) == NULL) ) {
  3754. schema_create_errormsg( errorbuf, errorbufsize, schema_errprefix_at,
  3755. first_attr_name, "Unknown attribute syntax OID \"%s\"",
  3756. atype->at_syntax_oid );
  3757. status = invalid_syntax_error;
  3758. goto done;
  3759. }
  3760. /*
  3761. * Check to make sure that the OID isn't being used by an objectclass
  3762. */
  3763. if (!(schema_flags & DSE_SCHEMA_LOCKED)){
  3764. oc_lock_read();
  3765. }
  3766. poc = oc_find_oid_nolock( atype->at_oid );
  3767. if ( poc != NULL) {
  3768. schema_create_errormsg( errorbuf, errorbufsize, schema_errprefix_at, first_attr_name,
  3769. "The OID \"%s\" is also used by the object class \"%s\"", atype->at_oid, poc->oc_name);
  3770. status = LDAP_TYPE_OR_VALUE_EXISTS;
  3771. }
  3772. if (!(schema_flags & DSE_SCHEMA_LOCKED)){
  3773. oc_unlock();
  3774. }
  3775. if(status){
  3776. goto done;
  3777. }
  3778. /*
  3779. * Walk the "at_extensions" and set the schema extensions
  3780. */
  3781. for(a = 0; atype->at_extensions && atype->at_extensions[a]; a++){
  3782. schemaext *newext = (schemaext *)slapi_ch_calloc(1, sizeof (schemaext));
  3783. newext->term = slapi_ch_strdup(atype->at_extensions[a]->lsei_name);
  3784. for (aa = 0; atype->at_extensions[a]->lsei_values && atype->at_extensions[a]->lsei_values[aa]; aa++){
  3785. charray_add(&newext->values, slapi_ch_strdup(atype->at_extensions[a]->lsei_values[aa]));
  3786. newext->value_count++;
  3787. }
  3788. if(extensions == NULL){
  3789. extensions = newext;
  3790. head = newext;
  3791. } else {
  3792. extensions->next = newext;
  3793. extensions = newext;
  3794. }
  3795. }
  3796. extensions = head; /* reset to the top of the list */
  3797. /*
  3798. * Make sure if we are user-defined, that the attr_origins represents it
  3799. */
  3800. if (!extension_is_user_defined( extensions )) {
  3801. if ( is_user_defined ) {
  3802. int added = 0;
  3803. /* see if we have a X-ORIGIN term already */
  3804. while(extensions){
  3805. if(strcmp(extensions->term, "X-ORIGIN") == 0){
  3806. charray_add(&extensions->values, slapi_ch_strdup(schema_user_defined_origin[0]));
  3807. extensions->value_count++;
  3808. added = 1;
  3809. break;
  3810. }
  3811. extensions = extensions->next;
  3812. }
  3813. if(!added){
  3814. /* X-ORIGIN is completely missing, add it */
  3815. extensions = head;
  3816. schemaext *newext = (schemaext *)slapi_ch_calloc(1, sizeof (schemaext));
  3817. newext->term = slapi_ch_strdup("X-ORIGIN");
  3818. charray_add(&newext->values, slapi_ch_strdup(schema_user_defined_origin[0]));
  3819. newext->value_count++;
  3820. while(extensions && extensions->next){
  3821. /* move to the end of the list */
  3822. extensions = extensions->next;
  3823. }
  3824. if(extensions == NULL){
  3825. extensions = newext;
  3826. head = extensions;
  3827. } else {
  3828. extensions->next = newext;
  3829. }
  3830. }
  3831. } else {
  3832. flags |= SLAPI_ATTR_FLAG_STD_ATTR;
  3833. }
  3834. }
  3835. extensions = head; /* reset to the top of the list */
  3836. /*
  3837. * Check to see if the attribute name is valid
  3838. */
  3839. if (!(schema_flags & DSE_SCHEMA_NO_CHECK)) {
  3840. for (a = 0; a < num_names; ++a) {
  3841. if ( schema_check_name(attr_names[a], PR_TRUE, errorbuf, errorbufsize) == 0 ) {
  3842. status = invalid_syntax_error;
  3843. goto done;
  3844. } else if (!(flags & SLAPI_ATTR_FLAG_OVERRIDE) && attr_syntax_exists(attr_names[a])) {
  3845. schema_create_errormsg( errorbuf, errorbufsize,
  3846. schema_errprefix_at, attr_names[a],
  3847. "Could not be added because it already exists" );
  3848. status = LDAP_TYPE_OR_VALUE_EXISTS;
  3849. goto done;
  3850. }
  3851. }
  3852. }
  3853. /*
  3854. * Check if the OID is valid
  3855. */
  3856. if (!(schema_flags & DSE_SCHEMA_NO_CHECK)) {
  3857. if ( schema_check_oid ( first_attr_name, atype->at_oid, PR_TRUE, errorbuf,
  3858. errorbufsize ) == 0 ) {
  3859. status = invalid_syntax_error;
  3860. goto done;
  3861. }
  3862. }
  3863. /*
  3864. * Check if the OID is already being used
  3865. */
  3866. if (!(flags & SLAPI_ATTR_FLAG_OVERRIDE) && ( NULL != (tmpasi = attr_syntax_get_by_oid(atype->at_oid)))) {
  3867. schema_create_errormsg( errorbuf, errorbufsize,
  3868. schema_errprefix_at, first_attr_name,
  3869. "Could not be added because the OID \"%s\" is already in use", atype->at_oid);
  3870. status = LDAP_TYPE_OR_VALUE_EXISTS;
  3871. attr_syntax_return( tmpasi );
  3872. goto done;
  3873. }
  3874. /*
  3875. * Finally create the attribute
  3876. */
  3877. status = attr_syntax_create( atype->at_oid, attr_names, atype->at_desc, atype->at_sup_oid,
  3878. atype->at_equality_oid, atype->at_ordering_oid, atype->at_substr_oid,
  3879. extensions, atype->at_syntax_oid, syntaxlength, flags, &tmpasip );
  3880. if (!status) {
  3881. if ( NULL != asipp ) {
  3882. *asipp = tmpasip; /* just return it */
  3883. } else {
  3884. /* add the new attribute to the global store */
  3885. status = attr_syntax_add( tmpasip );
  3886. if ( LDAP_SUCCESS != status ) {
  3887. if ( 0 != (flags & SLAPI_ATTR_FLAG_OVERRIDE) &&
  3888. LDAP_TYPE_OR_VALUE_EXISTS == status ) {
  3889. /*
  3890. * This can only occur if the name and OID don't match the
  3891. * attribute we are trying to override (all other cases of
  3892. * "type or value exists" were trapped above).
  3893. */
  3894. schema_create_errormsg( errorbuf, errorbufsize,
  3895. schema_errprefix_at, first_attr_name,
  3896. "Does not match the OID \"%s\". Another attribute "
  3897. "type is already using the name or OID.", atype->at_oid);
  3898. } else {
  3899. schema_create_errormsg( errorbuf, errorbufsize,
  3900. schema_errprefix_at, first_attr_name,
  3901. "Could not be added (OID is \"%s\")", atype->at_oid );
  3902. }
  3903. attr_syntax_free( tmpasip );
  3904. }
  3905. }
  3906. }
  3907. done:
  3908. /* free everything */
  3909. ldap_attributetype_free(atype);
  3910. charray_free(attr_names);
  3911. schema_free_extensions(extensions);
  3912. return status;
  3913. }
  3914. /*
  3915. * parse_objclass_str
  3916. *
  3917. * Read the value of the objectclasses attribute in cn=schema, convert it
  3918. * into an objectclass struct.
  3919. *
  3920. * Arguments:
  3921. *
  3922. * input : value of objectclasses attribute to read
  3923. * oc : pointer write the objectclass to
  3924. * errorbuf : buffer to write any errors to
  3925. * is_user_defined : if non-zero, force objectclass to be user defined
  3926. * schema_flags : Any or none of the following bits could be set
  3927. * DSE_SCHEMA_NO_CHECK -- schema won't be checked
  3928. * DSE_SCHEMA_NO_GLOCK -- don't lock global resources
  3929. * DSE_SCHEMA_LOCKED -- already locked with
  3930. * reload_schemafile_lock;
  3931. * no further lock needed
  3932. * schema_ds4x_compat: if non-zero, act like Netscape DS 4.x
  3933. *
  3934. * Returns: an LDAP error code
  3935. *
  3936. * LDAP_SUCCESS if the objectclass was sucessfully read, the new
  3937. * objectclass will be written to oc
  3938. *
  3939. * All others: there was an error, an error message will
  3940. * be written to errorbuf
  3941. */
  3942. static int
  3943. parse_objclass_str ( const char *input, struct objclass **oc, char *errorbuf,
  3944. size_t errorbufsize, PRUint32 schema_flags, int is_user_defined,
  3945. int schema_ds4x_compat, struct objclass *private_schema )
  3946. {
  3947. LDAPObjectClass *objClass;
  3948. struct objclass *pnew_oc = NULL, *psup_oc = NULL;
  3949. schemaext *extensions = NULL, *head = NULL;
  3950. const char *errp;
  3951. char **OrigRequiredAttrsArray, **OrigAllowedAttrsArray;
  3952. char *first_oc_name = NULL;
  3953. /* If we ever accept openldap schema directly, then make parser_flags configurable */
  3954. const int parser_flags = LDAP_SCHEMA_ALLOW_NONE | LDAP_SCHEMA_ALLOW_NO_OID;
  3955. PRUint8 flags = 0;
  3956. int invalid_syntax_error;
  3957. int i, j;
  3958. int rc = 0;
  3959. /*
  3960. * openLDAP Objectclass struct
  3961. *
  3962. * typedef struct ldap_objectclass {
  3963. * char *oc_oid; OID
  3964. * char **oc_names; Names
  3965. * char *oc_desc; Description
  3966. * int oc_obsolete; Is obsolete?
  3967. * char **oc_sup_oids; OIDs of superior classes
  3968. * int oc_kind; Kind
  3969. * char **oc_at_oids_must; OIDs of required attribute types
  3970. * char **oc_at_oids_may; OIDs of optional attribute types
  3971. * LDAPSchemaExtensionItem **oc_extensions; Extensions
  3972. * } LDAPObjectClass;
  3973. */
  3974. /*
  3975. * Set the appropriate error code
  3976. */
  3977. if ( schema_ds4x_compat ) {
  3978. invalid_syntax_error = LDAP_OPERATIONS_ERROR;
  3979. } else {
  3980. invalid_syntax_error = LDAP_INVALID_SYNTAX;
  3981. }
  3982. /*
  3983. * Verify we have input
  3984. */
  3985. if ( NULL == input || '\0' == input[0] ) {
  3986. schema_create_errormsg( errorbuf, errorbufsize, schema_errprefix_oc, NULL,
  3987. "One or more values are required for the objectClasses attribute" );
  3988. LDAPDebug ( LDAP_DEBUG_ANY, "NULL args passed to read_oc_ldif\n",0,0,0);
  3989. return LDAP_OPERATIONS_ERROR;
  3990. }
  3991. /*
  3992. * Parse the input and create the openLdap objectclass structure
  3993. */
  3994. while(isspace(*input)){
  3995. /* trim any leading spaces */
  3996. input++;
  3997. }
  3998. if((objClass = ldap_str2objectclass(input, &rc, &errp, parser_flags )) == NULL){
  3999. schema_create_errormsg( errorbuf, errorbufsize, schema_errprefix_oc, input,
  4000. "Failed to parse objectclass, error(%d) at (%s)", rc, errp );
  4001. return invalid_syntax_error;
  4002. }
  4003. /*
  4004. * Check the NAME and update our name list
  4005. */
  4006. if ( NULL != objClass->oc_names ) {
  4007. first_oc_name = objClass->oc_names[0];
  4008. } else { /* NAME followed by nothing violates syntax */
  4009. schema_create_errormsg( errorbuf, errorbufsize, schema_errprefix_oc, input,
  4010. "Missing or invalid objectclass name" );
  4011. rc = invalid_syntax_error;
  4012. goto done;
  4013. }
  4014. /*
  4015. * If the objectclass type doesn't have an OID, we'll make the oid objClass-oid.
  4016. */
  4017. if (NULL == objClass->oc_oid) {
  4018. objClass->oc_oid = slapi_ch_smprintf("%s-oid", first_oc_name );
  4019. }
  4020. /*
  4021. * Check to see if the objectclass name is valid
  4022. */
  4023. if (!(schema_flags & DSE_SCHEMA_NO_CHECK)) {
  4024. for (i = 0; objClass->oc_names[i]; ++i) {
  4025. if ( schema_check_name(objClass->oc_names[i], PR_TRUE, errorbuf, errorbufsize) == 0 ) {
  4026. rc = invalid_syntax_error;
  4027. goto done;
  4028. }
  4029. }
  4030. }
  4031. /*
  4032. * Check if the OID is valid
  4033. */
  4034. if (!(schema_flags & DSE_SCHEMA_NO_CHECK)) {
  4035. if ( schema_check_oid ( first_oc_name, objClass->oc_oid, PR_TRUE, errorbuf,
  4036. errorbufsize ) == 0 ) {
  4037. rc = invalid_syntax_error;
  4038. goto done;
  4039. }
  4040. }
  4041. /*
  4042. * Look for the superior objectclass. We first look for a parenthesized
  4043. * list and if not found we look for a simple OID.
  4044. *
  4045. * XXXmcs: Since we do not yet support multiple superior objectclasses, we
  4046. * just grab the first OID in a parenthesized list.
  4047. */
  4048. if (!(schema_flags & DSE_SCHEMA_NO_GLOCK)) {
  4049. /* needed because we access the superior oc */
  4050. oc_lock_read();
  4051. }
  4052. if(objClass->oc_sup_oids && objClass->oc_sup_oids[0]) {
  4053. if (schema_flags & DSE_SCHEMA_USE_PRIV_SCHEMA) {
  4054. /* We have built an objectclass list on a private variable
  4055. * This is used to check the schema of a remote consumer
  4056. */
  4057. psup_oc = oc_find_nolock(objClass->oc_sup_oids[0], private_schema, PR_TRUE);
  4058. } else {
  4059. psup_oc = oc_find_nolock(objClass->oc_sup_oids[0], NULL, PR_FALSE);
  4060. }
  4061. }
  4062. /*
  4063. * Walk the "oc_extensions" and set the schema extensions
  4064. */
  4065. for(i = 0; objClass->oc_extensions && objClass->oc_extensions[i]; i++){
  4066. schemaext *newext = (schemaext *)slapi_ch_calloc(1, sizeof (schemaext));
  4067. newext->term = slapi_ch_strdup(objClass->oc_extensions[i]->lsei_name);
  4068. for (j = 0; objClass->oc_extensions[i]->lsei_values && objClass->oc_extensions[i]->lsei_values[j]; j++){
  4069. charray_add(&newext->values, slapi_ch_strdup(objClass->oc_extensions[i]->lsei_values[j]));
  4070. newext->value_count++;
  4071. }
  4072. if(extensions == NULL){
  4073. extensions = newext;
  4074. head = extensions;
  4075. } else {
  4076. extensions->next = newext;
  4077. extensions = newext;
  4078. }
  4079. }
  4080. extensions = head; /* reset to the top of the list */
  4081. /*
  4082. * Set the remaining flags
  4083. */
  4084. if(objClass->oc_obsolete){
  4085. flags |= OC_FLAG_OBSOLETE;
  4086. }
  4087. if ( extension_is_user_defined( extensions )) {
  4088. flags |= OC_FLAG_USER_OC;
  4089. } else if ( is_user_defined ) {
  4090. int added = 0;
  4091. /* see if we have a X-ORIGIN term already */
  4092. while(extensions){
  4093. if(strcmp(extensions->term, "X-ORIGIN") == 0){
  4094. charray_add(&extensions->values, slapi_ch_strdup(schema_user_defined_origin[0]));
  4095. extensions->value_count++;
  4096. added = 1;
  4097. break;
  4098. }
  4099. extensions = extensions->next;
  4100. }
  4101. if(!added){
  4102. /* X-ORIGIN is completely missing, add it */
  4103. extensions = head;
  4104. schemaext *newext = (schemaext *)slapi_ch_calloc(1, sizeof (schemaext));
  4105. newext->term = slapi_ch_strdup("X-ORIGIN");
  4106. charray_add( &newext->values, slapi_ch_strdup( schema_user_defined_origin[0] ));
  4107. newext->value_count++;
  4108. while(extensions && extensions->next){
  4109. extensions = extensions->next;
  4110. }
  4111. if(extensions == NULL){
  4112. extensions = newext;
  4113. head = extensions;
  4114. } else {
  4115. extensions->next = newext;
  4116. }
  4117. }
  4118. flags |= OC_FLAG_USER_OC;
  4119. } else {
  4120. flags |= OC_FLAG_STANDARD_OC;
  4121. }
  4122. extensions = head; /* reset to the top of the list */
  4123. /*
  4124. * Generate OrigRequiredAttrsArray and OrigAllowedAttrsArray from the superior oc
  4125. */
  4126. if (psup_oc) {
  4127. int found_it;
  4128. OrigRequiredAttrsArray = (char **) slapi_ch_malloc (1 * sizeof(char *)) ;
  4129. OrigRequiredAttrsArray[0] = NULL;
  4130. OrigAllowedAttrsArray = (char **) slapi_ch_malloc (1 * sizeof(char *)) ;
  4131. OrigAllowedAttrsArray[0] = NULL;
  4132. if (psup_oc->oc_required && objClass->oc_at_oids_must) {
  4133. for (i = 0; objClass->oc_at_oids_must[i]; i++) {
  4134. found_it = 0;
  4135. for (j = 0; psup_oc->oc_required[j]; j++) {
  4136. if (strcasecmp (psup_oc->oc_required[j], objClass->oc_at_oids_must[i]) == 0) {
  4137. found_it = 1;
  4138. break;
  4139. }
  4140. }
  4141. if (!found_it) {
  4142. charray_add (&OrigRequiredAttrsArray, slapi_ch_strdup ( objClass->oc_at_oids_must[i] ) );
  4143. }
  4144. }
  4145. } else {
  4146. /* we still need to set the originals */
  4147. charray_free(OrigRequiredAttrsArray);
  4148. OrigRequiredAttrsArray = charray_dup(objClass->oc_at_oids_must);
  4149. }
  4150. if (psup_oc->oc_allowed && objClass->oc_at_oids_may) {
  4151. for (i = 0; objClass->oc_at_oids_may[i]; i++) {
  4152. found_it = 0;
  4153. for (j = 0; psup_oc->oc_allowed[j]; j++) {
  4154. if (strcasecmp (psup_oc->oc_allowed[j], objClass->oc_at_oids_may[i]) == 0) {
  4155. found_it = 1;
  4156. break;
  4157. }
  4158. }
  4159. if (!found_it) {
  4160. charray_add (&OrigAllowedAttrsArray, slapi_ch_strdup (objClass->oc_at_oids_may[i]) );
  4161. }
  4162. }
  4163. } else {
  4164. /* we still need to set the originals */
  4165. charray_free(OrigAllowedAttrsArray);
  4166. OrigAllowedAttrsArray = charray_dup(objClass->oc_at_oids_may);
  4167. }
  4168. } else {
  4169. /* if no parent oc */
  4170. OrigRequiredAttrsArray = charray_dup ( objClass->oc_at_oids_must );
  4171. OrigAllowedAttrsArray = charray_dup ( objClass->oc_at_oids_may );
  4172. }
  4173. if (!(schema_flags & DSE_SCHEMA_NO_GLOCK)) {
  4174. oc_unlock(); /* we are done accessing superior oc (psup_oc) */
  4175. }
  4176. /*
  4177. * Finally - create new the objectclass
  4178. */
  4179. pnew_oc = (struct objclass *) slapi_ch_calloc (1, sizeof (struct objclass));
  4180. pnew_oc->oc_name = slapi_ch_strdup ( objClass->oc_names[0] );
  4181. if(objClass->oc_sup_oids){
  4182. pnew_oc->oc_superior = slapi_ch_strdup( objClass->oc_sup_oids[0] );
  4183. }
  4184. pnew_oc->oc_oid = slapi_ch_strdup( objClass->oc_oid );
  4185. pnew_oc->oc_desc = slapi_ch_strdup( objClass->oc_desc );
  4186. pnew_oc->oc_required = charray_dup( objClass->oc_at_oids_must );
  4187. pnew_oc->oc_allowed = charray_dup( objClass->oc_at_oids_may );
  4188. pnew_oc->oc_orig_required = OrigRequiredAttrsArray;
  4189. pnew_oc->oc_orig_allowed = OrigAllowedAttrsArray;
  4190. pnew_oc->oc_kind = objClass->oc_kind;
  4191. pnew_oc->oc_extensions = extensions;
  4192. pnew_oc->oc_next = NULL;
  4193. pnew_oc->oc_flags = flags;
  4194. *oc = pnew_oc;
  4195. done:
  4196. ldap_objectclass_free(objClass);
  4197. return rc;
  4198. }
  4199. #endif
  4200. /*
  4201. * schema_check_oc_attrs:
  4202. * Check to see if the required and allowed attributes are valid attributes
  4203. *
  4204. * arguments: poc : pointer to the objectclass to check
  4205. * errorbuf : buffer to write any error messages to
  4206. * stripOptions: 1 if you want to silently strip any options
  4207. * 0 if options should cause an error
  4208. *
  4209. * Returns:
  4210. *
  4211. * 0 if there's a unknown attribute, and errorbuf will contain an
  4212. * error message.
  4213. *
  4214. * 1 if everything is ok
  4215. *
  4216. * Note: no locking of poc is needed because poc is always a newly allocated
  4217. * objclass struct (this function is only called by add_oc_internal).
  4218. */
  4219. static int
  4220. schema_check_oc_attrs ( struct objclass *poc,
  4221. char *errorbuf, size_t errorbufsize,
  4222. int stripOptions )
  4223. {
  4224. int i;
  4225. if ( errorbuf == NULL || poc == NULL || poc->oc_name == NULL) {
  4226. /* error */
  4227. LDAPDebug (LDAP_DEBUG_PARSE,
  4228. "Null args passed to schema_check_oc_attrs\n",
  4229. NULL, NULL, NULL);
  4230. return -1;
  4231. }
  4232. /* remove any options like ;binary from the oc's attributes */
  4233. if ( strip_oc_options( poc ) && !stripOptions) {
  4234. /* there were options present, this oc should be rejected */
  4235. schema_create_errormsg( errorbuf, errorbufsize, schema_errprefix_oc,
  4236. poc->oc_name, "Contains attribute options. "
  4237. "Attribute options, such as \";binary\" are not allowed in "
  4238. "object class definitions." );
  4239. return 0;
  4240. }
  4241. for ( i = 0; poc->oc_allowed && poc->oc_allowed[i]; i++ ) {
  4242. if ( attr_syntax_exists ( poc->oc_allowed[i] ) == 0 ) {
  4243. schema_create_errormsg( errorbuf, errorbufsize, schema_errprefix_oc,
  4244. poc->oc_name, "Unknown allowed attribute type \"%s\"",
  4245. poc->oc_allowed[i]);
  4246. return 0;
  4247. }
  4248. }
  4249. for ( i = 0; poc->oc_required && poc->oc_required[i]; i++ ) {
  4250. if ( attr_syntax_exists ( poc->oc_required[i] ) == 0 ) {
  4251. schema_create_errormsg( errorbuf, errorbufsize, schema_errprefix_oc,
  4252. poc->oc_name, "Unknown required attribute type \"%s\"",
  4253. poc->oc_required[i]);
  4254. return 0;
  4255. }
  4256. }
  4257. return 1;
  4258. }
  4259. /*
  4260. * schema_check_name:
  4261. * Check if the attribute or objectclass name is valid. Names can only contain
  4262. * characters, digits, and hyphens. In addition, names must begin with
  4263. * a character. If the nsslapd-attribute-name-exceptions attribute in cn=config
  4264. * is true, then we also allow underscores.
  4265. *
  4266. * XXX We're also supposed to allow semicolons, but we already use them to deal
  4267. * with attribute options XXX
  4268. *
  4269. * returns 1 if the attribute has a legal name
  4270. * 0 if not
  4271. *
  4272. * If the attribute name is invalid, an error message will be written to msg
  4273. */
  4274. static int
  4275. schema_check_name(char *name, PRBool isAttribute, char *errorbuf,
  4276. size_t errorbufsize )
  4277. {
  4278. int i;
  4279. /* allowed characters */
  4280. static char allowed[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-";
  4281. /* additional characters to allow if allow_exceptions is true */
  4282. static char allowedExceptions[] = "_";
  4283. int allow_exceptions = config_get_attrname_exceptions();
  4284. if ( name == NULL || errorbuf == NULL) {
  4285. /* this is bad */
  4286. return 0;
  4287. }
  4288. if (!strcasecmp(name, PSEUDO_ATTR_UNHASHEDUSERPASSWORD)) {
  4289. /* explicitly allow this badly named attribute */
  4290. return 1;
  4291. }
  4292. /* attribute names must begin with a letter */
  4293. if ( (isascii (name[0]) == 0) || (isalpha (name[0]) == 0)) {
  4294. if ( (strlen(name) + 80) < BUFSIZ ) {
  4295. schema_create_errormsg( errorbuf, errorbufsize, schema_errprefix_at,
  4296. name, "The name is invalid. Names must begin with a letter" );
  4297. }
  4298. else {
  4299. schema_create_errormsg( errorbuf, errorbufsize, schema_errprefix_at,
  4300. name, "The name is invalid, and probably too long. "
  4301. "Names must begin with a letter" );
  4302. }
  4303. return 0;
  4304. }
  4305. for (i = 1; name[i]; i++ ) {
  4306. if ( (NULL == strchr( allowed, name[i] )) &&
  4307. (!allow_exceptions ||
  4308. (NULL == strchr(allowedExceptions, name[i])) ) ) {
  4309. if ( (strlen(name) + 80) < BUFSIZ ) {
  4310. schema_create_errormsg( errorbuf, errorbufsize, schema_errprefix_at,
  4311. name, "The name contains the invalid character \"%c\"", name[i] );
  4312. }
  4313. else {
  4314. schema_create_errormsg( errorbuf, errorbufsize, schema_errprefix_at,
  4315. name, "The name contains the invalid character \"%c\". The name"
  4316. " is also probably too long.", name[i] );
  4317. }
  4318. return 0;
  4319. }
  4320. }
  4321. return 1;
  4322. }
  4323. /*
  4324. * schema_check_oid:
  4325. * Check if the oid is valid.
  4326. *
  4327. * returns 1 if the attribute has a legal oid
  4328. * 0 if not
  4329. *
  4330. * If the oid is invalid, an error message will be written to errorbuf
  4331. *
  4332. * Oids can either have the form <attr/oc name>-oid or
  4333. * start and end with a digit, and contain only digits and periods
  4334. */
  4335. static int
  4336. schema_check_oid( const char *name, const char *oid, PRBool isAttribute,
  4337. char *errorbuf, size_t errorbufsize ) {
  4338. int i = 0, length_oid = 0, rc = 0;
  4339. char *namePlusOid = NULL;
  4340. if ( name == NULL || oid == NULL) {
  4341. /* this is bad */
  4342. LDAPDebug (LDAP_DEBUG_ANY, "NULL passed to schema_check_oid\n",0,0,0);
  4343. return 0;
  4344. }
  4345. /* check to see if the OID is <name>-oid */
  4346. namePlusOid = slapi_ch_smprintf("%s-oid", name );
  4347. rc = strcasecmp( oid, namePlusOid );
  4348. slapi_ch_free( (void **) &namePlusOid );
  4349. if ( 0 == rc ) {
  4350. return 1;
  4351. }
  4352. /* If not, the OID must begin and end with a digit, and contain only
  4353. digits and dots */
  4354. /* check to see that it begins and ends with a digit */
  4355. length_oid = strlen(oid);
  4356. if ( !isdigit(oid[0]) ||
  4357. !isdigit(oid[length_oid-1]) ) {
  4358. schema_create_errormsg( errorbuf, errorbufsize,
  4359. isAttribute ? schema_errprefix_at : schema_errprefix_oc,
  4360. name,
  4361. "The OID \"%s\" must begin and end with a digit, or be \"%s-oid\"",
  4362. oid, name );
  4363. return 0;
  4364. }
  4365. /* check to see that it contains only digits and dots */
  4366. for ( i = 0; i < length_oid; i++ ) {
  4367. if ( !isdigit(oid[i]) && oid[i] != '.' ){
  4368. schema_create_errormsg( errorbuf, errorbufsize,
  4369. isAttribute ? schema_errprefix_at : schema_errprefix_oc,
  4370. name,
  4371. "The OID \"%s\" contains an invalid character: \"%c\"; the"
  4372. " OID must contain only digits and periods, or be \"%s-oid\"",
  4373. oid, oid[i], name );
  4374. return 0;
  4375. }
  4376. }
  4377. /* The oid is OK if we're here */
  4378. return 1;
  4379. }
  4380. /*
  4381. * Some utility functions for dealing with a dynamically
  4382. * allocated buffer.
  4383. */
  4384. static struct sizedbuffer *sizedbuffer_construct(size_t size)
  4385. {
  4386. struct sizedbuffer *p= (struct sizedbuffer *)slapi_ch_malloc(sizeof(struct sizedbuffer));
  4387. p->size= size;
  4388. if(size>0)
  4389. {
  4390. p->buffer= (char*)slapi_ch_malloc(size);
  4391. p->buffer[0]= '\0';
  4392. }
  4393. else
  4394. {
  4395. p->buffer= NULL;
  4396. }
  4397. return p;
  4398. }
  4399. static void sizedbuffer_destroy(struct sizedbuffer *p)
  4400. {
  4401. if(p!=NULL)
  4402. {
  4403. slapi_ch_free((void**)&p->buffer);
  4404. }
  4405. slapi_ch_free((void**)&p);
  4406. }
  4407. static void sizedbuffer_allocate(struct sizedbuffer *p, size_t sizeneeded)
  4408. {
  4409. if(p!=NULL)
  4410. {
  4411. if(sizeneeded>p->size)
  4412. {
  4413. if(p->buffer!=NULL)
  4414. {
  4415. slapi_ch_free((void**)&p->buffer);
  4416. }
  4417. p->buffer= (char*)slapi_ch_malloc(sizeneeded);
  4418. p->buffer[0]= '\0';
  4419. p->size= sizeneeded;
  4420. }
  4421. }
  4422. }
  4423. /*
  4424. * Check if the object class is extensible
  4425. */
  4426. static int isExtensibleObjectclass(const char *objectclass)
  4427. {
  4428. if ( strcasecmp( objectclass, "extensibleobject" ) == 0 ) {
  4429. return( 1 );
  4430. }
  4431. /* The Easter Egg is based on a special object class */
  4432. if ( strcasecmp( objectclass, EGG_OBJECT_CLASS ) == 0 ) {
  4433. return( 1 );
  4434. }
  4435. return 0;
  4436. }
  4437. /*
  4438. * strip_oc_options: strip any attribute options from the objectclass'
  4439. * attributes (remove things like ;binary from the attrs)
  4440. *
  4441. * argument: pointer to an objectclass, attributes will have their
  4442. * options removed in place
  4443. *
  4444. * returns: number of options removed
  4445. *
  4446. * Note: no locking of poc is needed because poc is always a newly allocated
  4447. * objclass struct (this function is only called by schema_check_oc_attrs,
  4448. * which is only called by add_oc_internal).
  4449. */
  4450. static int
  4451. strip_oc_options( struct objclass *poc ) {
  4452. int i, numRemoved = 0;
  4453. char *mod = NULL;
  4454. for ( i = 0; poc->oc_allowed && poc->oc_allowed[i]; i++ ) {
  4455. if ( (mod = stripOption( poc->oc_allowed[i] )) != NULL ){
  4456. LDAPDebug (LDAP_DEBUG_ANY,
  4457. "Removed option \"%s\" from allowed attribute type "
  4458. "\"%s\" in object class \"%s\".\n",
  4459. mod, poc->oc_allowed[i], poc->oc_name );
  4460. numRemoved++;
  4461. }
  4462. }
  4463. for ( i = 0; poc->oc_required && poc->oc_required[i]; i++ ) {
  4464. if ( (mod = stripOption( poc->oc_required[i] )) != NULL ){
  4465. LDAPDebug (LDAP_DEBUG_ANY,
  4466. "Removed option \"%s\" from required attribute type "
  4467. "\"%s\" in object class \"%s\".\n",
  4468. mod, poc->oc_required[i], poc->oc_name );
  4469. numRemoved++;
  4470. }
  4471. }
  4472. return numRemoved;
  4473. }
  4474. /*
  4475. * stripOption:
  4476. * removes options such as ";binary" from attribute names
  4477. *
  4478. * argument: pointer to an attribute name, such as "userCertificate;binary"
  4479. *
  4480. * returns: pointer to the option, such as "binary"
  4481. * NULL if there's no option
  4482. *
  4483. */
  4484. static char *
  4485. stripOption(char *attr) {
  4486. char *pSemiColon = strchr( attr, ';' );
  4487. if (pSemiColon) {
  4488. *pSemiColon = '\0';
  4489. }
  4490. return pSemiColon ? pSemiColon + 1 : NULL;
  4491. }
  4492. /*
  4493. * load_schema_dse: called by dse_read_file() when target is cn=schema
  4494. *
  4495. * Initialize attributes and objectclasses from the schema
  4496. *
  4497. * Note that this function removes all values for `attributetypes'
  4498. * and `objectclasses' attributes from the entry `e'.
  4499. *
  4500. * returntext is always at least SLAPI_DSE_RETURNTEXT_SIZE bytes in size.
  4501. */
  4502. int
  4503. load_schema_dse(Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Entry *ignored,
  4504. int *returncode, char *returntext, void *arg)
  4505. {
  4506. Slapi_Attr *attr = 0;
  4507. int primary_file = 0; /* this is the primary (writeable) schema file */
  4508. int schema_ds4x_compat = config_get_ds4_compatible_schema();
  4509. PRUint32 flags = *(PRUint32 *)arg;
  4510. *returncode = 0;
  4511. /*
  4512. * Note: there is no need to call schema_lock_write() here because this
  4513. * function is only called during server startup.
  4514. */
  4515. slapi_pblock_get( pb, SLAPI_DSE_IS_PRIMARY_FILE, &primary_file );
  4516. if (!slapi_entry_attr_find(e, "attributetypes", &attr) && attr)
  4517. {
  4518. /* enumerate the values in attr */
  4519. Slapi_Value *v = 0;
  4520. int index = 0;
  4521. for (index = slapi_attr_first_value(attr, &v);
  4522. v && (index != -1);
  4523. index = slapi_attr_next_value(attr, index, &v))
  4524. {
  4525. const char *s = slapi_value_get_string(v);
  4526. if (!s)
  4527. continue;
  4528. if (flags & DSE_SCHEMA_NO_LOAD)
  4529. {
  4530. struct asyntaxinfo *tmpasip = NULL;
  4531. if ((*returncode = parse_at_str(s, &tmpasip, returntext,
  4532. SLAPI_DSE_RETURNTEXT_SIZE, flags,
  4533. primary_file /* force user defined? */,
  4534. schema_ds4x_compat, 0)) != 0)
  4535. break;
  4536. attr_syntax_free( tmpasip ); /* trash it */
  4537. }
  4538. else
  4539. {
  4540. if ((*returncode = parse_at_str(s, NULL, returntext,
  4541. SLAPI_DSE_RETURNTEXT_SIZE, flags,
  4542. primary_file /* force user defined? */,
  4543. schema_ds4x_compat, 0)) != 0)
  4544. break;
  4545. }
  4546. }
  4547. slapi_entry_attr_delete(e, "attributetypes");
  4548. }
  4549. if (*returncode)
  4550. return SLAPI_DSE_CALLBACK_ERROR;
  4551. flags |= DSE_SCHEMA_NO_GLOCK; /* don't lock global resources
  4552. during initialization */
  4553. if (!slapi_entry_attr_find(e, "objectclasses", &attr) && attr)
  4554. {
  4555. /* enumerate the values in attr */
  4556. Slapi_Value *v = 0;
  4557. int index = 0;
  4558. for (index = slapi_attr_first_value(attr, &v);
  4559. v && (index != -1);
  4560. index = slapi_attr_next_value(attr, index, &v))
  4561. {
  4562. struct objclass *oc = 0;
  4563. const char *s = slapi_value_get_string(v);
  4564. if (!s)
  4565. continue;
  4566. if ( LDAP_SUCCESS != (*returncode = parse_oc_str(s, &oc, returntext,
  4567. SLAPI_DSE_RETURNTEXT_SIZE, flags,
  4568. primary_file /* force user defined? */,
  4569. schema_ds4x_compat, NULL)))
  4570. {
  4571. oc_free( &oc );
  4572. break;
  4573. }
  4574. if (flags & DSE_SCHEMA_NO_LOAD)
  4575. {
  4576. /* we don't load the objectclase; free it */
  4577. oc_free( &oc );
  4578. }
  4579. else
  4580. {
  4581. if ( LDAP_SUCCESS !=
  4582. (*returncode = add_oc_internal(oc, returntext,
  4583. SLAPI_DSE_RETURNTEXT_SIZE, schema_ds4x_compat,
  4584. flags))) {
  4585. oc_free( &oc );
  4586. break;
  4587. }
  4588. }
  4589. }
  4590. slapi_entry_attr_delete(e, "objectclasses");
  4591. }
  4592. /* Set the schema CSN */
  4593. if (!(flags & DSE_SCHEMA_NO_LOAD) &&
  4594. !slapi_entry_attr_find(e, "nsschemacsn", &attr) && attr)
  4595. {
  4596. Slapi_Value *v = NULL;
  4597. slapi_attr_first_value(attr, &v);
  4598. if (NULL != v) {
  4599. const char *s = slapi_value_get_string(v);
  4600. if (NULL != s) {
  4601. CSN *csn = csn_new_by_string(s);
  4602. g_set_global_schema_csn(csn);
  4603. }
  4604. }
  4605. }
  4606. return (*returncode == LDAP_SUCCESS) ? SLAPI_DSE_CALLBACK_OK
  4607. : SLAPI_DSE_CALLBACK_ERROR;
  4608. }
  4609. /*
  4610. * Try to initialize the schema from the LDIF file. Read
  4611. * the file and convert it to the avl tree of DSEs. If the
  4612. * file doesn't exist, we try to create it and put a minimal
  4613. * schema entry into it.
  4614. *
  4615. * Returns 1 for OK, 0 for Fail.
  4616. *
  4617. * schema_flags:
  4618. * DSE_SCHEMA_NO_LOAD -- schema won't get loaded
  4619. * DSE_SCHEMA_NO_CHECK -- schema won't be checked
  4620. * DSE_SCHEMA_NO_BACKEND -- don't add as backend
  4621. * DSE_SCHEMA_LOCKED -- already locked; no further lock needed
  4622. */
  4623. static int
  4624. init_schema_dse_ext(char *schemadir, Slapi_Backend *be,
  4625. struct dse **local_pschemadse, PRUint32 schema_flags)
  4626. {
  4627. int rc= 1; /* OK */
  4628. char *userschemafile = 0;
  4629. char *userschematmpfile = 0;
  4630. char **filelist = 0;
  4631. char *myschemadir = NULL;
  4632. Slapi_DN schema;
  4633. if (NULL == local_pschemadse)
  4634. {
  4635. return 0; /* cannot proceed; return failure */
  4636. }
  4637. *local_pschemadse = NULL;
  4638. slapi_sdn_init_ndn_byref(&schema,"cn=schema");
  4639. /* get schemadir if not given */
  4640. if (NULL == schemadir)
  4641. {
  4642. myschemadir = config_get_schemadir();
  4643. if (NULL == myschemadir)
  4644. {
  4645. return 0; /* cannot proceed; return failure */
  4646. }
  4647. }
  4648. else
  4649. {
  4650. myschemadir = schemadir;
  4651. }
  4652. filelist = get_priority_filelist(myschemadir, ".*ldif$");
  4653. if (!filelist || !*filelist)
  4654. {
  4655. slapi_log_error(SLAPI_LOG_FATAL, "schema",
  4656. "No schema files were found in the directory %s\n", myschemadir);
  4657. free_filelist(filelist);
  4658. rc = 0;
  4659. }
  4660. else
  4661. {
  4662. /* figure out the last file in the list; it is the user schema */
  4663. int ii = 0;
  4664. while (filelist[ii]) ++ii;
  4665. userschemafile = filelist[ii-1];
  4666. userschematmpfile = slapi_ch_smprintf("%s.tmp", userschemafile);
  4667. }
  4668. if(rc)
  4669. {
  4670. *local_pschemadse = dse_new_with_filelist(userschemafile,
  4671. userschematmpfile, NULL, NULL, myschemadir, filelist);
  4672. }
  4673. PR_ASSERT(*local_pschemadse);
  4674. if ((rc = (*local_pschemadse != NULL)) != 0)
  4675. {
  4676. /* pass schema_flags as arguments */
  4677. dse_register_callback(*local_pschemadse,
  4678. DSE_OPERATION_READ, DSE_FLAG_PREOP, &schema,
  4679. LDAP_SCOPE_BASE, NULL,
  4680. load_schema_dse, (void *)&schema_flags);
  4681. }
  4682. slapi_ch_free_string(&userschematmpfile);
  4683. if (NULL == schemadir)
  4684. slapi_ch_free_string(&myschemadir); /* allocated in this function */
  4685. if(rc)
  4686. {
  4687. char errorbuf[SLAPI_DSE_RETURNTEXT_SIZE] = {0};
  4688. char *attr_str;
  4689. int dont_write = 1;
  4690. int merge = 1;
  4691. int dont_dup_check = 1;
  4692. Slapi_PBlock pb;
  4693. memset(&pb, 0, sizeof(pb));
  4694. /* don't write out the file when reading */
  4695. slapi_pblock_set(&pb, SLAPI_DSE_DONT_WRITE_WHEN_ADDING, (void*)&dont_write);
  4696. /* duplicate entries are allowed */
  4697. slapi_pblock_set(&pb, SLAPI_DSE_MERGE_WHEN_ADDING, (void*)&merge);
  4698. /* use the non duplicate checking str2entry */
  4699. slapi_pblock_set(&pb, SLAPI_DSE_DONT_CHECK_DUPS, (void*)&dont_dup_check);
  4700. /* borrow the task flag space */
  4701. slapi_pblock_set(&pb, SLAPI_SCHEMA_FLAGS, (void*)&schema_flags);
  4702. /* add the objectclass attribute so we can do some basic schema
  4703. checking during initialization; this will be overridden when
  4704. its "real" definition is read from the schema conf files */
  4705. #ifdef USE_OPENLDAP
  4706. attr_str = "( 2.5.4.0 NAME 'objectClass' "
  4707. "DESC 'Standard schema for LDAP' SYNTAX "
  4708. "1.3.6.1.4.1.1466.115.121.1.15 X-ORIGIN 'RFC 2252' )";
  4709. #else
  4710. attr_str = "attributeTypes: ( 2.5.4.0 NAME 'objectClass' "
  4711. "DESC 'Standard schema for LDAP' SYNTAX "
  4712. "1.3.6.1.4.1.1466.115.121.1.15 X-ORIGIN 'RFC 2252' )";
  4713. #endif
  4714. if (schema_flags & DSE_SCHEMA_NO_LOAD)
  4715. {
  4716. struct asyntaxinfo *tmpasip = NULL;
  4717. rc = parse_at_str(attr_str, &tmpasip, errorbuf, SLAPI_DSE_RETURNTEXT_SIZE,
  4718. DSE_SCHEMA_NO_GLOCK|schema_flags, 0, 0, 0);
  4719. attr_syntax_free( tmpasip ); /* trash it */
  4720. }
  4721. else
  4722. {
  4723. rc = parse_at_str(attr_str, NULL, errorbuf, SLAPI_DSE_RETURNTEXT_SIZE,
  4724. schema_flags, 0, 0, 0);
  4725. }
  4726. if (rc)
  4727. {
  4728. slapi_log_error(SLAPI_LOG_FATAL, "schema", "Could not add"
  4729. " attribute type \"objectClass\" to the schema: %s\n",
  4730. errorbuf);
  4731. }
  4732. rc = dse_read_file(*local_pschemadse, &pb);
  4733. }
  4734. if (rc && !(schema_flags & DSE_SCHEMA_NO_BACKEND))
  4735. {
  4736. /* make sure the schema is normalized */
  4737. if (schema_flags & DSE_SCHEMA_LOCKED)
  4738. normalize_oc_nolock();
  4739. else
  4740. normalize_oc();
  4741. /* register callbacks */
  4742. dse_register_callback(*local_pschemadse, SLAPI_OPERATION_SEARCH,
  4743. DSE_FLAG_PREOP,&schema, LDAP_SCOPE_BASE,
  4744. NULL, read_schema_dse, NULL);
  4745. dse_register_callback(*local_pschemadse, SLAPI_OPERATION_MODIFY,
  4746. DSE_FLAG_PREOP,&schema, LDAP_SCOPE_BASE,
  4747. NULL, modify_schema_dse, NULL);
  4748. dse_register_callback(*local_pschemadse, SLAPI_OPERATION_DELETE,
  4749. DSE_FLAG_PREOP, &schema, LDAP_SCOPE_BASE,
  4750. NULL,dont_allow_that,NULL);
  4751. dse_register_callback(*local_pschemadse, DSE_OPERATION_WRITE,
  4752. DSE_FLAG_PREOP, &schema, LDAP_SCOPE_BASE,
  4753. NULL, refresh_user_defined_schema, NULL);
  4754. if (rc) {
  4755. if (NULL == be) { /* be is not given. select it */
  4756. be = slapi_be_select_by_instance_name( DSE_SCHEMA );
  4757. }
  4758. if (NULL == be) { /* first time */
  4759. /* add as a backend */
  4760. be = be_new_internal(*local_pschemadse, "DSE", DSE_SCHEMA);
  4761. be_addsuffix(be, &schema);
  4762. } else { /* schema file reload */
  4763. struct slapdplugin *backend_plugin = NULL;
  4764. be_replace_dse_internal(be, *local_pschemadse);
  4765. /* ldbm has some internal attributes to be added */
  4766. backend_plugin = plugin_get_by_name("ldbm database");
  4767. if (backend_plugin) {
  4768. if (backend_plugin->plg_add_schema) {
  4769. (backend_plugin->plg_add_schema)( NULL );
  4770. } else {
  4771. slapi_log_error( SLAPI_LOG_FATAL, "init_schema_dse",
  4772. "backend has not set internal schema\n" );
  4773. }
  4774. }
  4775. }
  4776. }
  4777. }
  4778. slapi_sdn_done(&schema);
  4779. return rc;
  4780. }
  4781. int
  4782. init_schema_dse(const char *configdir)
  4783. {
  4784. char *schemadir = config_get_schemadir();
  4785. int rc = 0;
  4786. if (NULL == schemadir)
  4787. {
  4788. schemadir = slapi_ch_smprintf("%s/%s", configdir, SCHEMA_SUBDIR_NAME);
  4789. }
  4790. rc = init_schema_dse_ext(schemadir, NULL, &pschemadse, DSE_SCHEMA_NO_GLOCK);
  4791. slapi_ch_free_string(&schemadir);
  4792. return rc;
  4793. }
  4794. #if !defined (USE_OPENLDAP)
  4795. static char **
  4796. parse_xstring_list( const char *schema_value, const char *name, size_t namelen, int *num_xvalsp,
  4797. char **default_list )
  4798. {
  4799. char *start, *end, *xval_tmp;
  4800. char **xvals = NULL;
  4801. if (( start = PL_strstr ( schema_value, name )) != NULL) {
  4802. start += namelen+1; /* +1 for space */
  4803. xval_tmp = slapi_ch_strdup( start );
  4804. if ( *start == '(' ) {
  4805. end = strchr( xval_tmp, ')' );
  4806. } else {
  4807. end = strchr( xval_tmp + 1, '\'' );
  4808. }
  4809. if (end) {
  4810. *(end+1) = 0;
  4811. }
  4812. xvals = parse_qdstrings( xval_tmp, num_xvalsp );
  4813. slapi_ch_free( (void **)&xval_tmp );
  4814. } else {
  4815. xvals = NULL;
  4816. *num_xvalsp = 0;
  4817. }
  4818. if ( NULL == xvals && NULL != default_list ) {
  4819. int i;
  4820. for ( i = 0; default_list[i] != NULL; ++i ) {
  4821. ;
  4822. }
  4823. *num_xvalsp = i;
  4824. xvals = (char **)slapi_ch_malloc( (i+1) * sizeof(char *));
  4825. for ( i = 0; default_list[i] != NULL; ++i ) {
  4826. xvals[i] = slapi_ch_strdup( default_list[i] );
  4827. }
  4828. xvals[i] = NULL;
  4829. }
  4830. /* for debugging
  4831. if ( xvals == NULL || xvals[0] == NULL ) {
  4832. LDAPDebug( LDAP_DEBUG_ANY, "no xstring values for xstring (%s) in (%s)\n", name, schema_value, 0 );
  4833. }
  4834. */
  4835. return xvals;
  4836. }
  4837. /* used by mozldap read_at_ldif() & read_oc_ldif() */
  4838. static schemaext*
  4839. parse_extensions( const char *schema_value, char **default_list )
  4840. {
  4841. schemaext *extensions = NULL, *head = NULL;
  4842. char *input, *token, *iter = NULL;
  4843. int i;
  4844. /*
  4845. * First make a copy of the input, then grab all the "X-" words,
  4846. * and extract the values.
  4847. */
  4848. input = slapi_ch_strdup(schema_value);
  4849. token = ldap_utf8strtok_r(input, " ", &iter);
  4850. while(token){
  4851. if(strncmp(token,"X-", 2) == 0){
  4852. /* we have a new extension */
  4853. schemaext *newext;
  4854. newext = (schemaext *)slapi_ch_calloc(1, sizeof (schemaext));
  4855. newext->term = slapi_ch_strdup(token);
  4856. newext->values = parse_xstring_list(schema_value, token, strlen(token), &newext->value_count, default_list);
  4857. if(extensions == NULL){
  4858. extensions = newext;
  4859. head = newext;
  4860. } else {
  4861. extensions->next = newext;
  4862. extensions = extensions->next;
  4863. }
  4864. }
  4865. token = ldap_utf8strtok_r(iter, " ", &iter);
  4866. }
  4867. extensions = head;
  4868. /*
  4869. * Ok, if X-ORIGIN was not specified, then add it with the default values
  4870. */
  4871. if ( !extension_is_user_defined(extensions) && default_list ) {
  4872. int added = 0;
  4873. while(extensions){
  4874. if(strcmp(extensions->term, "X-ORIGIN") == 0){
  4875. charray_add(&extensions->values, slapi_ch_strdup(default_list[0]) );
  4876. extensions->value_count++;
  4877. added = 1;
  4878. extensions = head;
  4879. break;
  4880. }
  4881. extensions = extensions->next;
  4882. }
  4883. if(!added){
  4884. schemaext *newext = (schemaext *)slapi_ch_calloc(1, sizeof (schemaext));
  4885. newext->term = slapi_ch_strdup("X-ORIGIN");
  4886. for ( i = 0; default_list[i]; ++i ){
  4887. newext->value_count++;
  4888. charray_add(&newext->values, slapi_ch_strdup(default_list[i]) );
  4889. }
  4890. extensions = head;
  4891. while(extensions && extensions->next){
  4892. /* move to the end of the list */
  4893. extensions = extensions->next;
  4894. }
  4895. if(extensions == NULL){
  4896. extensions = newext;
  4897. } else {
  4898. extensions->next = newext;
  4899. }
  4900. }
  4901. }
  4902. slapi_ch_free_string(&input);
  4903. return extensions;
  4904. }
  4905. /*
  4906. * Look for `keyword' within `*inputp' and return the flag_value if found
  4907. * (zero if returned if not found).
  4908. *
  4909. * If the keyword is found, `*inputp' is set to point just beyond the end of
  4910. * the keyword. If the keyword is not found, `*inputp' is not changed.
  4911. *
  4912. * The `strstr_fn' function pointer is used to search for `keyword', e.g., it
  4913. * could be PL_strcasestr().
  4914. *
  4915. * The string passed in `keyword' MUST include a trailing space, e.g.,
  4916. *
  4917. * flag |= get_flag_keyword( " COLLECTIVE ", SLAPI_ATTR_FLAG_COLLECTIVE,
  4918. * &input, PL_strcasestr );
  4919. *
  4920. */
  4921. static int
  4922. get_flag_keyword( const char *keyword, int flag_value, const char **inputp,
  4923. schema_strstr_fn_t strstr_fn )
  4924. {
  4925. const char *kw;
  4926. PR_ASSERT( NULL != inputp );
  4927. PR_ASSERT( NULL != *inputp );
  4928. PR_ASSERT( ' ' == keyword[ strlen( keyword ) - 1 ] );
  4929. if ( NULL == strstr_fn ) {
  4930. strstr_fn = PL_strcasestr;
  4931. }
  4932. kw = (*strstr_fn)( *inputp, keyword );
  4933. if ( NULL == kw ) {
  4934. flag_value = 0; /* not found -- return no value */
  4935. } else {
  4936. *inputp = kw + strlen( keyword ) - 1; /* advance input */
  4937. }
  4938. return flag_value;
  4939. }
  4940. /*
  4941. * Look for `tag' within `*inputp' and return the OID string following `tag'.
  4942. * If the OID has single quotes around it they are removed (they are allowed
  4943. * for compatibility with DS 3.x and 4.x).
  4944. *
  4945. * If the tag is found, `*inputp' is set to point just beyond the end of
  4946. * the OID that was extracted and returned. If the tag is not found,
  4947. * `*inputp' is not changed.
  4948. *
  4949. * The `strstr_fn' function pointer is used to search for `tag', e.g., it
  4950. * could be PL_strcasestr().
  4951. *
  4952. * The string passed in `tag' SHOULD generally include a trailing space, e.g.,
  4953. *
  4954. * pSuperior = get_tagged_oid( "SUP ", &input, PL_strcasestr );
  4955. *
  4956. * The exception to this is when the tag contains '(' as a trailing character.
  4957. * This is used to process lists of oids, such as the following:
  4958. *
  4959. * SUP (inetOrgPerson $ testUser)
  4960. *
  4961. * A malloc'd string is returned if `tag; is found and NULL if not.
  4962. */
  4963. static char *
  4964. get_tagged_oid( const char *tag, const char **inputp,
  4965. schema_strstr_fn_t strstr_fn )
  4966. {
  4967. const char *startp, *endp;
  4968. char *oid;
  4969. PR_ASSERT( NULL != inputp );
  4970. PR_ASSERT( NULL != *inputp );
  4971. PR_ASSERT( NULL != tag );
  4972. PR_ASSERT( '\0' != tag[ 0 ] );
  4973. if('(' !=tag[0])
  4974. PR_ASSERT((' ' == tag[ strlen( tag ) - 1 ]) || ('(' == tag[ strlen( tag ) - 1 ]));
  4975. if ( NULL == strstr_fn ) {
  4976. strstr_fn = PL_strcasestr;
  4977. }
  4978. oid = NULL;
  4979. if ( NULL != ( startp = (*strstr_fn)( *inputp, tag ))) {
  4980. startp += strlen( tag );
  4981. /* skip past any extra white space */
  4982. if ( NULL == ( startp = skipWS( startp ))) {
  4983. return( NULL );
  4984. }
  4985. /* skip past the leading single quote, if present */
  4986. if ( *startp == '\'' ) {
  4987. ++startp;
  4988. /* skip past any extra white space */
  4989. startp = skipWS( startp );
  4990. }
  4991. /* locate the end of the OID */
  4992. if ((NULL != ( endp = strchr( startp, ' '))) ||
  4993. (NULL != (endp = strchr( startp, ')'))) ) {
  4994. if ( '\'' == *(endp-1) && endp > startp ) {
  4995. --endp; /* ignore trailing quote */
  4996. }
  4997. } else {
  4998. endp = startp + strlen( startp ); /* remainder of input */
  4999. }
  5000. oid = slapi_ch_malloc( endp - startp + 1 );
  5001. memcpy( oid, startp, endp - startp );
  5002. oid[ endp - startp ] = '\0';
  5003. *inputp = endp;
  5004. }
  5005. return( oid );
  5006. }
  5007. #endif
  5008. /*
  5009. * sprintf to `outp' the contents of `tag' followed by `oid' followed by a
  5010. * trailing space. If enquote is non-zero, single quotes are included
  5011. * around the `oid' string. If `suffix' is not NULL, it is output directly
  5012. * after the `oid' (before the trailing space).
  5013. * Note that `tag' should typically include a trailing space, e.g.,
  5014. *
  5015. * outp += put_tagged_oid( outp, "SUP ", "1.2.3.4", NULL, enquote_oids );
  5016. *
  5017. * Returns the number of bytes copied to `outp' or 0 if `oid' is NULL.
  5018. */
  5019. static int
  5020. put_tagged_oid( char *outp, const char *tag, const char *oid,
  5021. const char *suffix, int enquote )
  5022. {
  5023. int count = 0;
  5024. if ( NULL == suffix ) {
  5025. suffix = "";
  5026. }
  5027. if ( NULL != oid ) {
  5028. if ( enquote ) {
  5029. count = sprintf( outp, "%s'%s%s' ", tag, oid, suffix );
  5030. } else {
  5031. count = sprintf( outp, "%s%s%s ", tag, oid, suffix );
  5032. }
  5033. }
  5034. return( count );
  5035. }
  5036. /*
  5037. * Add to `buf' a string of the form:
  5038. *
  5039. * prefix SPACE ( oid1 $ oid2 ... ) SPACE
  5040. * OR
  5041. * prefix SPACE oid SPACE
  5042. *
  5043. * The part after <prefix> matches the `oids' definition
  5044. * from RFC 2252 section 4.1.
  5045. *
  5046. * If oids is NULL or an empty array, `buf' is not touched.
  5047. */
  5048. static void
  5049. strcat_oids( char *buf, char *prefix, char **oids, int schema_ds4x_compat )
  5050. {
  5051. char *p;
  5052. int i;
  5053. if ( NULL != oids && NULL != oids[0] ) {
  5054. p = buf + strlen(buf); /* skip past existing content */
  5055. if ( NULL == oids[1] && !schema_ds4x_compat ) {
  5056. sprintf( p, "%s %s ", prefix, oids[0] ); /* just one oid */
  5057. } else {
  5058. sprintf( p, "%s ( ", prefix ); /* oidlist */
  5059. for ( i = 0; oids[i] != NULL; ++i ) {
  5060. if ( i > 0 ) {
  5061. strcat( p, " $ " );
  5062. }
  5063. strcat( p, oids[i] );
  5064. }
  5065. strcat( p, " ) " );
  5066. }
  5067. }
  5068. }
  5069. /*
  5070. * Add to `buf' a string of the form:
  5071. *
  5072. * prefix SPACE ( 's1' 's2' ... ) SPACE
  5073. * OR
  5074. * prefix SPACE 's1' SPACE
  5075. *
  5076. * The part after <prefix> matches the qdescs definition
  5077. * from RFC 2252 section 4.1.
  5078. *
  5079. * A count of the number of bytes added to buf or needed is returned.
  5080. *
  5081. * If buf is NULL, no copying is done but the number of bytes needed
  5082. * is calculated and returned. This is useful if you need to allocate
  5083. * space before calling this function will a buffer.
  5084. *
  5085. */
  5086. static size_t
  5087. strcat_qdlist( char *buf, char *prefix, char **qdlist )
  5088. {
  5089. int i;
  5090. char *start, *p;
  5091. size_t len = 0;
  5092. if ( NULL != qdlist && NULL != qdlist[0] ) {
  5093. if ( NULL == buf ) { /* calculate length only */
  5094. len += strlen( prefix );
  5095. if ( NULL != qdlist[1] ) {
  5096. len += 4; /* surrounding spaces and '(' and ')' */
  5097. }
  5098. for ( i = 0; NULL != qdlist[i]; ++i ) {
  5099. len += 3; /* leading space and quote marks */
  5100. len += strlen(qdlist[i]);
  5101. }
  5102. ++len; /* trailing space */
  5103. } else {
  5104. p = start = buf + strlen(buf); /* skip past existing content */
  5105. if ( NULL == qdlist[1] ) { /* just one string */
  5106. p += sprintf( p, "%s '%s' ", prefix, qdlist[0] );
  5107. } else { /* a list of strings */
  5108. p += sprintf( p, "%s (", prefix );
  5109. for ( i = 0; qdlist[i] != NULL; ++i ) {
  5110. p += sprintf( p, " '%s'", qdlist[i] );
  5111. }
  5112. *p++ = ' ';
  5113. *p++ = ')';
  5114. *p++ = ' ';
  5115. *p = '\0';
  5116. }
  5117. len = p - start;
  5118. }
  5119. }
  5120. return( len );
  5121. }
  5122. /*
  5123. * Loop over the extensions calling strcat_qdlist for each one.
  5124. */
  5125. static size_t
  5126. strcat_extensions( char *buf, schemaext *extension )
  5127. {
  5128. size_t len = 0;
  5129. while(extension){
  5130. len += strcat_qdlist(buf, extension->term, extension->values);
  5131. extension = extension->next;
  5132. }
  5133. return( len );
  5134. }
  5135. /*
  5136. * Just like strlen() except that 0 is returned if `s' is NULL.
  5137. */
  5138. static size_t
  5139. strlen_null_ok(const char *s)
  5140. {
  5141. if ( NULL == s ) {
  5142. return( 0 );
  5143. }
  5144. return( strlen( s ));
  5145. }
  5146. /*
  5147. * Like strcpy() except a count of the number of bytes copied is returned.
  5148. */
  5149. static int
  5150. strcpy_count( char *dst, const char *src )
  5151. {
  5152. char *p;
  5153. p = dst;
  5154. while ( *src != '\0' ) {
  5155. *p++ = *src++;
  5156. }
  5157. *p = '\0';
  5158. return( p - dst );
  5159. }
  5160. static int
  5161. extension_is_user_defined( schemaext *extensions )
  5162. {
  5163. while(extensions){
  5164. if(strcasecmp(extensions->term, "X-ORIGIN") == 0 ){
  5165. int i = 0;
  5166. while(extensions->values && extensions->values[i]){
  5167. if(strcasecmp(schema_user_defined_origin[0], extensions->values[i]) == 0) {
  5168. return 1;
  5169. }
  5170. i++;
  5171. }
  5172. }
  5173. extensions = extensions->next;
  5174. }
  5175. return 0;
  5176. }
  5177. /*
  5178. * Return PR_TRUE if the attribute type named 'type' is one of those that
  5179. * we handle directly in this file (in the scheme DSE callbacks).
  5180. * Other types are handled by the generic DSE code in dse.c.
  5181. */
  5182. /* subschema DSE attribute types we handle within the DSE callback */
  5183. static char *schema_interesting_attr_types[] = {
  5184. "dITStructureRules",
  5185. "nameForms",
  5186. "dITContentRules",
  5187. "objectClasses",
  5188. "attributeTypes",
  5189. "matchingRules",
  5190. "matchingRuleUse",
  5191. "ldapSyntaxes",
  5192. "nsschemacsn",
  5193. NULL
  5194. };
  5195. static PRBool
  5196. schema_type_is_interesting( const char *type )
  5197. {
  5198. int i;
  5199. for ( i = 0; schema_interesting_attr_types[i] != NULL; ++i ) {
  5200. if ( 0 == strcasecmp( type, schema_interesting_attr_types[i] )) {
  5201. return PR_TRUE;
  5202. }
  5203. }
  5204. return PR_FALSE;
  5205. }
  5206. static void
  5207. schema_create_errormsg(
  5208. char *errorbuf,
  5209. size_t errorbufsize,
  5210. const char *prefix,
  5211. const char *name,
  5212. const char *fmt,
  5213. ...
  5214. )
  5215. {
  5216. if ( NULL != errorbuf ) {
  5217. va_list ap;
  5218. int rc = 0;
  5219. va_start( ap, fmt );
  5220. if ( NULL != name ) {
  5221. rc = PR_snprintf( errorbuf, errorbufsize, prefix, name );
  5222. }
  5223. /* ok to cast here because rc is positive */
  5224. if ( (rc >= 0) && ((size_t)rc < errorbufsize) ) {
  5225. (void)PR_vsnprintf( errorbuf + rc, errorbufsize - rc, fmt, ap );
  5226. }
  5227. va_end( ap );
  5228. }
  5229. }
  5230. /*
  5231. * va_locate_oc_val finds an objectclass within the array of values in va.
  5232. * First oc_name is used, falling back to oc_oid. oc_oid can be NULL.
  5233. * oc_name and oc_oid should be official names (no trailing spaces). But
  5234. * trailing spaces within the va are ignored if appropriate.
  5235. *
  5236. * Returns >=0 if found (index into va) and -1 if not found.
  5237. */
  5238. static int
  5239. va_locate_oc_val( Slapi_Value **va, const char *oc_name, const char *oc_oid )
  5240. {
  5241. int i;
  5242. const char *strval;
  5243. if ( NULL == va || oc_name == NULL ) { /* nothing to look for */
  5244. return -1;
  5245. }
  5246. if ( !schema_ignore_trailing_spaces ) {
  5247. for ( i = 0; va[i] != NULL; i++ ) {
  5248. strval = slapi_value_get_string(va[i]);
  5249. if ( NULL != strval ) {
  5250. if ( 0 == strcasecmp(strval, oc_name)) {
  5251. return i;
  5252. }
  5253. if ( NULL != oc_oid
  5254. && 0 == strcasecmp( strval, oc_oid )) {
  5255. return i;
  5256. }
  5257. }
  5258. }
  5259. } else {
  5260. /*
  5261. * Ignore trailing spaces when comparing object class names.
  5262. */
  5263. size_t len;
  5264. const char *p;
  5265. for ( i = 0; va[i] != NULL; i++ ) {
  5266. strval = slapi_value_get_string(va[i]);
  5267. if ( NULL != strval ) {
  5268. for ( p = strval, len = 0; (*p != '\0') && (*p != ' ');
  5269. p++, len++ ) {
  5270. ; /* NULL */
  5271. }
  5272. if ( 0 == strncasecmp(oc_name, strval, len )
  5273. && ( len == strlen(oc_name))) {
  5274. return i;
  5275. }
  5276. if ( NULL != oc_oid
  5277. && ( 0 == strncasecmp( oc_oid, strval, len ))
  5278. && ( len == strlen(oc_oid))) {
  5279. return i;
  5280. }
  5281. }
  5282. }
  5283. }
  5284. return -1; /* not found */
  5285. }
  5286. /*
  5287. * va_expand_one_oc is used to add missing superclass values to the
  5288. * objectclass attribute when an entry is added or modified.
  5289. *
  5290. * missing values are always added to the end of the 'vap' array.
  5291. *
  5292. * Note: calls to this function MUST be bracketed by lock()/unlock(), i.e.,
  5293. *
  5294. * oc_lock_read();
  5295. * va_expand_one_oc( b, o );
  5296. * oc_unlock();
  5297. */
  5298. static void
  5299. va_expand_one_oc( const char *dn, const Slapi_Attr *a, Slapi_ValueSet *vs, const char *ocs )
  5300. {
  5301. struct objclass *this_oc, *sup_oc;
  5302. int p;
  5303. Slapi_Value **va = vs->va;
  5304. this_oc = oc_find_nolock( ocs, NULL, PR_FALSE );
  5305. if ( this_oc == NULL ) {
  5306. return; /* skip unknown object classes */
  5307. }
  5308. if ( this_oc->oc_superior == NULL ) {
  5309. return; /* no superior */
  5310. }
  5311. sup_oc = oc_find_nolock( this_oc->oc_superior, NULL, PR_FALSE );
  5312. if ( sup_oc == NULL ) {
  5313. return; /* superior is unknown -- ignore */
  5314. }
  5315. p = va_locate_oc_val( va, sup_oc->oc_name, sup_oc->oc_oid );
  5316. if ( p != -1 ) {
  5317. return; /* value already present -- done! */
  5318. }
  5319. if ( slapi_valueset_count(vs) > 1000 ) {
  5320. return;
  5321. }
  5322. slapi_valueset_add_attr_value_ext(a, vs, slapi_value_new_string(sup_oc->oc_name), SLAPI_VALUE_FLAG_PASSIN);
  5323. LDAPDebug( LDAP_DEBUG_TRACE,
  5324. "Entry \"%s\": added missing objectClass value %s\n",
  5325. dn, sup_oc->oc_name, 0 );
  5326. }
  5327. /*
  5328. * Expand the objectClass values in 'e' to take superior classes into account.
  5329. * All missing superior classes are added to the objectClass attribute, as
  5330. * is 'top' if it is missing.
  5331. */
  5332. static void
  5333. schema_expand_objectclasses_ext( Slapi_Entry *e, int lock)
  5334. {
  5335. Slapi_Attr *sa;
  5336. Slapi_Value *v;
  5337. Slapi_ValueSet *vs;
  5338. const char *dn = slapi_entry_get_dn_const( e );
  5339. int i;
  5340. if ( 0 != slapi_entry_attr_find( e, SLAPI_ATTR_OBJECTCLASS, &sa )) {
  5341. return; /* no OC values -- nothing to do */
  5342. }
  5343. vs = &sa->a_present_values;
  5344. if ( slapi_valueset_isempty(vs) ) {
  5345. return; /* no OC values -- nothing to do */
  5346. }
  5347. if (lock)
  5348. oc_lock_read();
  5349. /*
  5350. * This loop relies on the fact that bv_expand_one_oc()
  5351. * always adds to the end
  5352. */
  5353. i = slapi_valueset_first_value(vs,&v);
  5354. while ( v != NULL) {
  5355. if ( NULL != slapi_value_get_string(v) ) {
  5356. va_expand_one_oc( dn, sa, &sa->a_present_values, slapi_value_get_string(v) );
  5357. }
  5358. i = slapi_valueset_next_value(vs, i, &v);
  5359. }
  5360. /* top must always be present */
  5361. va_expand_one_oc( dn, sa, &sa->a_present_values, "top" );
  5362. if (lock)
  5363. oc_unlock();
  5364. }
  5365. void
  5366. slapi_schema_expand_objectclasses( Slapi_Entry *e )
  5367. {
  5368. schema_expand_objectclasses_ext( e, 1);
  5369. }
  5370. void
  5371. schema_expand_objectclasses_nolock( Slapi_Entry *e )
  5372. {
  5373. schema_expand_objectclasses_ext( e, 0);
  5374. }
  5375. /* lock to protect both objectclass and schema_dse */
  5376. static void
  5377. reload_schemafile_lock()
  5378. {
  5379. oc_lock_write();
  5380. schema_dse_lock_write();
  5381. }
  5382. static void
  5383. reload_schemafile_unlock()
  5384. {
  5385. schema_dse_unlock();
  5386. oc_unlock();
  5387. }
  5388. /* API to validate the schema files */
  5389. int
  5390. slapi_validate_schema_files(char *schemadir)
  5391. {
  5392. struct dse *my_pschemadse = NULL;
  5393. int rc = init_schema_dse_ext(schemadir, NULL, &my_pschemadse,
  5394. DSE_SCHEMA_NO_LOAD | DSE_SCHEMA_NO_BACKEND);
  5395. dse_destroy(my_pschemadse); /* my_pschemadse was created just to
  5396. validate the schema */
  5397. if (rc) {
  5398. return LDAP_SUCCESS;
  5399. } else {
  5400. slapi_log_error( SLAPI_LOG_FATAL, "schema_reload",
  5401. "schema file validation failed\n" );
  5402. return LDAP_OBJECT_CLASS_VIOLATION;
  5403. }
  5404. }
  5405. /*
  5406. * API to reload the schema files.
  5407. * Rule: this function is called when slapi_validate_schema_files is passed.
  5408. * Schema checking is skipped in this function.
  5409. */
  5410. int
  5411. slapi_reload_schema_files(char *schemadir)
  5412. {
  5413. int rc = LDAP_SUCCESS;
  5414. struct dse *my_pschemadse = NULL;
  5415. /* get be to lock */
  5416. Slapi_Backend *be = slapi_be_select_by_instance_name( DSE_SCHEMA );
  5417. if (NULL == be)
  5418. {
  5419. slapi_log_error( SLAPI_LOG_FATAL, "schema_reload",
  5420. "schema file reload failed\n" );
  5421. return LDAP_LOCAL_ERROR;
  5422. }
  5423. slapi_be_Wlock(be); /* be lock must be outer of schemafile lock */
  5424. reload_schemafile_lock();
  5425. /* Exclude attr_syntax not to grab from the hash table while cleaning up */
  5426. attr_syntax_write_lock();
  5427. attr_syntax_delete_all_for_schemareload(SLAPI_ATTR_FLAG_KEEP);
  5428. oc_delete_all_nolock();
  5429. attr_syntax_unlock_write();
  5430. rc = init_schema_dse_ext(schemadir, be, &my_pschemadse,
  5431. DSE_SCHEMA_NO_CHECK | DSE_SCHEMA_LOCKED);
  5432. if (rc) {
  5433. dse_destroy(pschemadse);
  5434. pschemadse = my_pschemadse;
  5435. reload_schemafile_unlock();
  5436. slapi_be_Unlock(be);
  5437. return LDAP_SUCCESS;
  5438. } else {
  5439. reload_schemafile_unlock();
  5440. slapi_be_Unlock(be);
  5441. slapi_log_error( SLAPI_LOG_FATAL, "schema_reload",
  5442. "schema file reload failed\n" );
  5443. return LDAP_LOCAL_ERROR;
  5444. }
  5445. }
  5446. /*
  5447. * slapi_schema_list_objectclass_attributes:
  5448. * Return the list of attributes belonging to the objectclass
  5449. *
  5450. * The caller is responsible to free the returned list with charray_free.
  5451. * flags: one of them or both:
  5452. * SLAPI_OC_FLAG_REQUIRED
  5453. * SLAPI_OC_FLAG_ALLOWED
  5454. */
  5455. char **
  5456. slapi_schema_list_objectclass_attributes(const char *ocname_or_oid,
  5457. PRUint32 flags)
  5458. {
  5459. struct objclass *oc = NULL;
  5460. char **attrs = NULL;
  5461. PRUint32 mask = SLAPI_OC_FLAG_REQUIRED | SLAPI_OC_FLAG_ALLOWED;
  5462. if (!flags) {
  5463. return attrs;
  5464. }
  5465. oc_lock_read();
  5466. oc = oc_find_nolock(ocname_or_oid, NULL, PR_FALSE);
  5467. if (oc) {
  5468. switch (flags & mask) {
  5469. case SLAPI_OC_FLAG_REQUIRED:
  5470. attrs = charray_dup(oc->oc_required);
  5471. break;
  5472. case SLAPI_OC_FLAG_ALLOWED:
  5473. attrs = charray_dup(oc->oc_allowed);
  5474. break;
  5475. case SLAPI_OC_FLAG_REQUIRED|SLAPI_OC_FLAG_ALLOWED:
  5476. attrs = charray_dup(oc->oc_required);
  5477. charray_merge(&attrs, oc->oc_allowed, 1/*copy_strs*/);
  5478. break;
  5479. default:
  5480. slapi_log_error( SLAPI_LOG_FATAL, "list objectclass attributes",
  5481. "flag 0x%x not supported\n", flags );
  5482. break;
  5483. }
  5484. }
  5485. oc_unlock();
  5486. return attrs;
  5487. }
  5488. /*
  5489. * slapi_schema_get_superior_name:
  5490. * Return the name of the superior objectclass
  5491. *
  5492. * The caller is responsible to free the returned name
  5493. */
  5494. char *
  5495. slapi_schema_get_superior_name(const char *ocname_or_oid)
  5496. {
  5497. struct objclass *oc = NULL;
  5498. char *superior = NULL;
  5499. oc_lock_read();
  5500. oc = oc_find_nolock(ocname_or_oid, NULL, PR_FALSE);
  5501. if (oc) {
  5502. superior = slapi_ch_strdup(oc->oc_superior);
  5503. }
  5504. oc_unlock();
  5505. return superior;
  5506. }
  5507. /* Check if the oc_list1 is a superset of oc_list2.
  5508. * oc_list1 is a superset if it exists objectclass in oc_list1 that
  5509. * do not exist in oc_list2. Or if a OC in oc_list1 required more attributes
  5510. * that the OC in oc_list2. Or if a OC in oc_list1 allowed more attributes
  5511. * that the OC in oc_list2.
  5512. *
  5513. * It returns 1 if oc_list1 is a superset of oc_list2, else it returns 0
  5514. *
  5515. * If oc_list1 or oc_list2 is global_oc, the caller must hold the oc_lock
  5516. */
  5517. static int
  5518. schema_oc_superset_check(struct objclass *oc_list1, struct objclass *oc_list2, char *message, int replica_role) {
  5519. struct objclass *oc_1, *oc_2;
  5520. char *description;
  5521. int debug_logging = 0;
  5522. int rc, i, j;
  5523. int repl_schema_policy;
  5524. int found;
  5525. PRBool moved_must_to_may;
  5526. if (message == NULL) {
  5527. description = "";
  5528. } else {
  5529. description = message;
  5530. }
  5531. /* by default assum oc_list1 == oc_list2 */
  5532. rc = 0;
  5533. /* Are we doing replication logging */
  5534. if(slapi_is_loglevel_set(SLAPI_LOG_REPL)){
  5535. debug_logging = 1;
  5536. }
  5537. /* Check if all objectclass in oc_list1
  5538. * - exists in oc_list2
  5539. * - required attributes are also required in oc_2
  5540. * - allowed attributes are also allowed in oc_2
  5541. */
  5542. slapi_rwlock_rdlock( schema_policy_lock );
  5543. for (oc_1 = oc_list1; oc_1 != NULL; oc_1 = oc_1->oc_next) {
  5544. /* Check if there is a specific policy for that objectclass */
  5545. repl_schema_policy = schema_check_policy(replica_role, REPL_SCHEMA_OBJECTCLASS, oc_1->oc_name, oc_1->oc_oid);
  5546. if (repl_schema_policy == REPL_SCHEMA_UPDATE_ACCEPT_VALUE) {
  5547. /* We are skipping the superset checking for that objectclass */
  5548. slapi_log_error(SLAPI_LOG_REPL, "schema", "Do not check if this OBJECTCLASS is missing on local/remote schema [%s or %s]\n", oc_1->oc_name, oc_1->oc_oid);
  5549. continue;
  5550. } else if (repl_schema_policy == REPL_SCHEMA_UPDATE_REJECT_VALUE) {
  5551. /* This objectclass being present, we need to fail as if it was a superset
  5552. * keep evaluating to have all the objectclass checking
  5553. */
  5554. slapi_log_error(SLAPI_LOG_REPL, "schema", "%s objectclass prevents replication of the schema\n", oc_1->oc_name);
  5555. rc = 1;
  5556. if(debug_logging){
  5557. /* we continue to check all the objectclasses so we log what is wrong */
  5558. continue;
  5559. } else {
  5560. break;
  5561. }
  5562. }
  5563. /* Retrieve the remote objectclass in our local schema */
  5564. oc_2 = oc_find_nolock(oc_1->oc_oid, oc_list2, PR_TRUE);
  5565. if (oc_2 == NULL) {
  5566. /* try to retrieve it with the name*/
  5567. oc_2 = oc_find_nolock(oc_1->oc_name, oc_list2, PR_TRUE);
  5568. }
  5569. if (oc_2 == NULL) {
  5570. slapi_log_error(SLAPI_LOG_REPL, "schema", "Fail to retrieve in the %s schema [%s or %s]\n",
  5571. description,
  5572. oc_1->oc_name,
  5573. oc_1->oc_oid);
  5574. /* The oc_1 objectclasses is supperset */
  5575. rc = 1;
  5576. if(debug_logging){
  5577. /* we continue to check all the objectclasses so we log what is wrong */
  5578. continue;
  5579. } else {
  5580. break;
  5581. }
  5582. }
  5583. /* First check the MUST */
  5584. if (oc_1->oc_orig_required) {
  5585. for (i = 0; oc_1->oc_orig_required[i] != NULL; i++) {
  5586. /* For each required attribute from the remote schema check that
  5587. * it is also required in the local schema
  5588. */
  5589. found = 0;
  5590. if (oc_2->oc_orig_required) {
  5591. for (j = 0; oc_2->oc_orig_required[j] != NULL; j++) {
  5592. if (strcasecmp(oc_2->oc_orig_required[j], oc_1->oc_orig_required[i]) == 0) {
  5593. found = 1;
  5594. break;
  5595. }
  5596. }
  5597. }
  5598. if (!found) {
  5599. /* Before stating that oc1 is a superset of oc2, we need to verify that the 'required'
  5600. * attribute (from oc1) is missing in 'required' oc2 because it is
  5601. * now 'allowed' in oc2
  5602. */
  5603. moved_must_to_may = PR_FALSE;
  5604. if (oc_2->oc_orig_allowed) {
  5605. for (j = 0; oc_2->oc_orig_allowed[j] != NULL; j++) {
  5606. if (strcasecmp(oc_2->oc_orig_allowed[j], oc_1->oc_orig_required[i]) == 0) {
  5607. moved_must_to_may = PR_TRUE;
  5608. break;
  5609. }
  5610. }
  5611. }
  5612. if (moved_must_to_may) {
  5613. /* This is a special case where oc1 is actually NOT a superset of oc2 */
  5614. slapi_log_error(SLAPI_LOG_REPL, "schema", "Attribute %s is no longer 'required' in '%s' of the %s schema but is now 'allowed'\n",
  5615. oc_1->oc_orig_required[i],
  5616. oc_1->oc_name,
  5617. description);
  5618. } else {
  5619. /* The required attribute in the remote protocol (remote_oc->oc_orig_required[i])
  5620. * is not required in the local protocol
  5621. */
  5622. slapi_log_error(SLAPI_LOG_REPL, "schema", "Attribute %s is not required in '%s' of the %s schema\n",
  5623. oc_1->oc_orig_required[i],
  5624. oc_1->oc_name,
  5625. description);
  5626. /* The oc_1 objectclasses is supperset */
  5627. rc = 1;
  5628. if (debug_logging) {
  5629. /* we continue to check all attributes so we log what is wrong */
  5630. continue;
  5631. } else {
  5632. break;
  5633. }
  5634. }
  5635. }
  5636. }
  5637. }
  5638. /* Second check the MAY */
  5639. if (oc_1->oc_orig_allowed) {
  5640. for (i = 0; oc_1->oc_orig_allowed[i] != NULL; i++) {
  5641. /* For each required attribute from the remote schema check that
  5642. * it is also required in the local schema
  5643. */
  5644. found = 0;
  5645. if (oc_2->oc_orig_allowed) {
  5646. for (j = 0; oc_2->oc_orig_allowed[j] != NULL; j++) {
  5647. if (strcasecmp(oc_2->oc_orig_allowed[j], oc_1->oc_orig_allowed[i]) == 0) {
  5648. found = 1;
  5649. break;
  5650. }
  5651. }
  5652. }
  5653. if (!found) {
  5654. /* The allowed attribute in the remote schema (remote_oc->oc_orig_allowed[i])
  5655. * is not allowed in the local schema
  5656. */
  5657. slapi_log_error(SLAPI_LOG_REPL, "schema", "Attribute %s is not allowed in '%s' of the %s schema\n",
  5658. oc_1->oc_orig_allowed[i],
  5659. oc_1->oc_name,
  5660. description);
  5661. /* The oc_1 objectclasses is superset */
  5662. rc = 1;
  5663. if (debug_logging) {
  5664. /* we continue to check all attributes so we log what is wrong */
  5665. continue;
  5666. } else {
  5667. break;
  5668. }
  5669. }
  5670. }
  5671. }
  5672. }
  5673. slapi_rwlock_unlock( schema_policy_lock );
  5674. return rc;
  5675. }
  5676. static int
  5677. schema_at_superset_check(struct asyntaxinfo *at_list1, struct asyntaxinfo *at_list2, char *message, int replica_role)
  5678. {
  5679. struct asyntaxinfo *at_1, *at_2;
  5680. char *info = NULL;
  5681. int debug_logging = 0;
  5682. int repl_schema_policy;
  5683. int rc = 0;
  5684. if(at_list1 == NULL || at_list2 == NULL){
  5685. return 0;
  5686. }
  5687. /* Are we doing replication logging */
  5688. if(slapi_is_loglevel_set(SLAPI_LOG_REPL)){
  5689. debug_logging = 1;
  5690. }
  5691. slapi_rwlock_rdlock( schema_policy_lock );
  5692. for (at_1 = at_list1; at_1 != NULL; at_1 = at_1->asi_next) {
  5693. /* Check if there is a specific policy for that objectclass */
  5694. repl_schema_policy = schema_check_policy(replica_role, REPL_SCHEMA_ATTRIBUTE, at_1->asi_name, at_1->asi_oid);
  5695. if (repl_schema_policy == REPL_SCHEMA_UPDATE_ACCEPT_VALUE) {
  5696. /* We are skipping the superset checking for that attribute */
  5697. slapi_log_error(SLAPI_LOG_REPL, "schema", "Do not check if this ATTRIBUTE is missing on local/remote schema [%s or %s]\n", at_1->asi_name, at_1->asi_oid);
  5698. continue;
  5699. } else if (repl_schema_policy == REPL_SCHEMA_UPDATE_REJECT_VALUE) {
  5700. /* This attribute being present, we need to fail as if it was a superset
  5701. * but keep evaluating to have all the attribute checking
  5702. */
  5703. slapi_log_error(SLAPI_LOG_REPL, "schema", "%s attribute prevents replication of the schema\n", at_1->asi_name);
  5704. rc = 1;
  5705. if (debug_logging) {
  5706. /* we continue to check all the objectclasses so we log what is wrong */
  5707. continue;
  5708. } else {
  5709. break;
  5710. }
  5711. }
  5712. /* check if at_1 exists in at_list2 */
  5713. if((at_2 = attr_syntax_find(at_1, at_list2))){
  5714. /*
  5715. * Check for single vs. multi value
  5716. */
  5717. if(!(at_1->asi_flags & SLAPI_ATTR_FLAG_SINGLE) && (at_2->asi_flags & SLAPI_ATTR_FLAG_SINGLE)){
  5718. /* at_list 1 is a superset */
  5719. rc = 1;
  5720. if(debug_logging){
  5721. slapi_log_error(SLAPI_LOG_REPL, "schema", "%s schema attribute [%s] is not "
  5722. "\"single-valued\" \n",message, at_1->asi_name);
  5723. continue;
  5724. } else {
  5725. break;
  5726. }
  5727. }
  5728. /*
  5729. * Check the syntaxes
  5730. */
  5731. if(schema_at_superset_check_syntax_oids(at_1->asi_syntax_oid, at_2->asi_syntax_oid)){
  5732. /* at_list 1 is a superset */
  5733. rc = 1;
  5734. if(debug_logging){
  5735. slapi_log_error(SLAPI_LOG_REPL, "schema", "%s schema attribute [%s] syntax "
  5736. "can not be overwritten\n",message, at_1->asi_name);
  5737. continue;
  5738. } else {
  5739. break;
  5740. }
  5741. }
  5742. /*
  5743. * Check some matching rules - not finished yet...
  5744. *
  5745. * For now, skip the matching rule check (rc is never equal to -1)
  5746. */
  5747. if(rc == -1 && schema_at_superset_check_mr(at_1, at_2, info)){
  5748. rc = 1;
  5749. if(debug_logging){
  5750. slapi_log_error(SLAPI_LOG_REPL, "schema", "%s schema attribute [%s] matching "
  5751. "rule can not be overwritten\n",message, at_1->asi_name);
  5752. continue;
  5753. } else {
  5754. break;
  5755. }
  5756. }
  5757. } else {
  5758. rc = 1;
  5759. if(debug_logging){
  5760. /* we continue to check all attributes so we log what is wrong */
  5761. slapi_log_error(SLAPI_LOG_REPL, "schema", "Fail to retrieve in the %s schema [%s or %s]\n",
  5762. message, at_1->asi_name, at_1->asi_oid);
  5763. continue;
  5764. } else {
  5765. break;
  5766. }
  5767. }
  5768. }
  5769. slapi_rwlock_unlock( schema_policy_lock );
  5770. return rc;
  5771. }
  5772. /*
  5773. * Return 1 if a1's matching rules are superset(not to be overwritten). If just one of
  5774. * the matching rules should not be overwritten, even if one should, we can not allow it.
  5775. */
  5776. static int
  5777. schema_at_superset_check_mr(struct asyntaxinfo *a1, struct asyntaxinfo *a2, char *info)
  5778. {
  5779. char *a1_mrtype[3] = { a1->asi_mr_equality, a1->asi_mr_substring, a1->asi_mr_ordering };
  5780. char *a2_mrtype[3] = { a2->asi_mr_equality, a2->asi_mr_substring, a2->asi_mr_ordering };
  5781. int rc = 0, i;
  5782. /*
  5783. * Loop over the three matching rule types
  5784. */
  5785. for(i = 0; i < 3; i++){
  5786. if(a1_mrtype[i]){
  5787. if(a2_mrtype[i]){
  5788. /*
  5789. * Future action item - determine matching rule precedence:
  5790. *
  5791. ces
  5792. "caseExactIA5Match", "1.3.6.1.4.1.1466.109.114.1"
  5793. "caseExactMatch", "2.5.13.5"
  5794. "caseExactOrderingMatch", "2.5.13.6"
  5795. "caseExactSubstringsMatch", "2.5.13.7"
  5796. "caseExactIA5SubstringsMatch", "2.16.840.1.113730.3.3.1"
  5797. cis
  5798. "generalizedTimeMatch", "2.5.13.27"
  5799. "generalizedTimeOrderingMatch", "2.5.13.28"
  5800. "booleanMatch", "2.5.13.13"
  5801. "caseIgnoreIA5Match", "1.3.6.1.4.1.1466.109.114.2"
  5802. "caseIgnoreIA5SubstringsMatch", "1.3.6.1.4.1.1466.109.114.3"
  5803. "caseIgnoreListMatch", "2.5.13.11"
  5804. "caseIgnoreListSubstringsMatch", "2.5.13.12"
  5805. "caseIgnoreMatch", "2.5.13.2" -------------------------------
  5806. "caseIgnoreOrderingMatch", "2.5.13.3" -----------------------> can have lang options
  5807. "caseIgnoreSubstringsMatch", "2.5.13.4" --------------------- (as seen in the console)!
  5808. "directoryStringFirstComponentMatch", "2.5.13.31"
  5809. "objectIdentifierMatch", "2.5.13.0"
  5810. "objectIdentifierFirstComponentMatch", "2.5.13.30"
  5811. bitstring
  5812. "bitStringMatch", "2.5.13.16","2.16.840.1.113730.3.3.1"
  5813. bin
  5814. "octetStringMatch", "2.5.13.17"
  5815. "octetStringOrderingMatch", "2.5.13.18"
  5816. DN
  5817. "distinguishedNameMatch", "2.5.13.1"
  5818. Int
  5819. "integerMatch", "2.5.13.14"
  5820. "integerOrderingMatch", "2.5.13.15"
  5821. "integerFirstComponentMatch", "2.5.13.29"
  5822. NameAndOptUID
  5823. "uniqueMemberMatch", "2.5.13.23"
  5824. NumericString
  5825. "numericStringMatch", "2.5.13.8"
  5826. "numericStringOrderingMatch", "2.5.13.9"
  5827. "numericStringSubstringsMatch", "2.5.13.10"
  5828. Telephone
  5829. "telephoneNumberMatch", "2.5.13.20"
  5830. "telephoneNumberSubstringsMatch", "2.5.13.21"
  5831. */
  5832. }
  5833. }
  5834. }
  5835. return rc;
  5836. }
  5837. /*
  5838. * Return 1 if oid1 is a superset(oid1 is not to be overwritten)
  5839. */
  5840. static int
  5841. schema_at_superset_check_syntax_oids(char *oid1, char *oid2)
  5842. {
  5843. if(oid1 == NULL && oid2 == NULL){
  5844. return 0;
  5845. } else if (oid2 == NULL){
  5846. return 0;
  5847. } else if (oid1 == NULL){
  5848. return 1;
  5849. }
  5850. if(strcmp(oid1, BINARY_SYNTAX_OID) == 0){
  5851. if(strcmp(oid2, BINARY_SYNTAX_OID) &&
  5852. strcmp(oid2, INTEGER_SYNTAX_OID) &&
  5853. strcmp(oid2, NUMERICSTRING_SYNTAX_OID) &&
  5854. strcmp(oid2, IA5STRING_SYNTAX_OID) &&
  5855. strcmp(oid2, DIRSTRING_SYNTAX_OID) &&
  5856. strcmp(oid2, PRINTABLESTRING_SYNTAX_OID) &&
  5857. strcmp(oid2, SPACE_INSENSITIVE_STRING_SYNTAX_OID) &&
  5858. strcmp(oid2, FACSIMILE_SYNTAX_OID) &&
  5859. strcmp(oid2, PRINTABLESTRING_SYNTAX_OID) &&
  5860. strcmp(oid2, TELEPHONE_SYNTAX_OID) &&
  5861. strcmp(oid2, TELETEXTERMID_SYNTAX_OID) &&
  5862. strcmp(oid2, TELEXNUMBER_SYNTAX_OID))
  5863. {
  5864. return 1;
  5865. }
  5866. } else if(strcmp(oid1, BITSTRING_SYNTAX_OID) == 0){
  5867. if(strcmp(oid2, BINARY_SYNTAX_OID) &&
  5868. strcmp(oid2, BITSTRING_SYNTAX_OID) &&
  5869. strcmp(oid2, INTEGER_SYNTAX_OID) &&
  5870. strcmp(oid2, NUMERICSTRING_SYNTAX_OID) &&
  5871. strcmp(oid2, DIRSTRING_SYNTAX_OID) &&
  5872. strcmp(oid2, PRINTABLESTRING_SYNTAX_OID) &&
  5873. strcmp(oid2, IA5STRING_SYNTAX_OID) &&
  5874. strcmp(oid2, FACSIMILE_SYNTAX_OID) &&
  5875. strcmp(oid2, PRINTABLESTRING_SYNTAX_OID) &&
  5876. strcmp(oid2, SPACE_INSENSITIVE_STRING_SYNTAX_OID) &&
  5877. strcmp(oid2, TELEPHONE_SYNTAX_OID) &&
  5878. strcmp(oid2, TELETEXTERMID_SYNTAX_OID) &&
  5879. strcmp(oid2, TELEXNUMBER_SYNTAX_OID))
  5880. {
  5881. return 1;
  5882. }
  5883. } else if(strcmp(oid1, BOOLEAN_SYNTAX_OID) == 0){
  5884. if(strcmp(oid2, BOOLEAN_SYNTAX_OID) &&
  5885. strcmp(oid2, DIRSTRING_SYNTAX_OID) &&
  5886. strcmp(oid2, PRINTABLESTRING_SYNTAX_OID) &&
  5887. strcmp(oid2, SPACE_INSENSITIVE_STRING_SYNTAX_OID) &&
  5888. strcmp(oid2, IA5STRING_SYNTAX_OID))
  5889. {
  5890. return 1;
  5891. }
  5892. } else if(strcmp(oid1, COUNTRYSTRING_SYNTAX_OID) ==0){
  5893. if(strcmp(oid2, COUNTRYSTRING_SYNTAX_OID) &&
  5894. strcmp(oid2, DIRSTRING_SYNTAX_OID) &&
  5895. strcmp(oid2, PRINTABLESTRING_SYNTAX_OID) &&
  5896. strcmp(oid2, SPACE_INSENSITIVE_STRING_SYNTAX_OID) &&
  5897. strcmp(oid2, IA5STRING_SYNTAX_OID))
  5898. {
  5899. return 1;
  5900. }
  5901. } else if(strcmp(oid1, DN_SYNTAX_OID) == 0){
  5902. if(strcmp(oid2, DN_SYNTAX_OID) &&
  5903. strcmp(oid2, SPACE_INSENSITIVE_STRING_SYNTAX_OID) &&
  5904. strcmp(oid2, DIRSTRING_SYNTAX_OID) )
  5905. {
  5906. return 1;
  5907. }
  5908. } else if(strcmp(oid1, DELIVERYMETHOD_SYNTAX_OID) ==0){
  5909. if(strcmp(oid2, DELIVERYMETHOD_SYNTAX_OID) &&
  5910. strcmp(oid2, DIRSTRING_SYNTAX_OID) &&
  5911. strcmp(oid2, PRINTABLESTRING_SYNTAX_OID) &&
  5912. strcmp(oid2, SPACE_INSENSITIVE_STRING_SYNTAX_OID) &&
  5913. strcmp(oid2, IA5STRING_SYNTAX_OID))
  5914. {
  5915. return 1;
  5916. }
  5917. } else if(strcmp(oid1, DIRSTRING_SYNTAX_OID) == 0){
  5918. if(strcmp(oid2, DIRSTRING_SYNTAX_OID) &&
  5919. strcmp(oid2, SPACE_INSENSITIVE_STRING_SYNTAX_OID)){
  5920. return 1;
  5921. }
  5922. } else if(strcmp(oid1, ENHANCEDGUIDE_SYNTAX_OID) == 0){
  5923. if(strcmp(oid2, ENHANCEDGUIDE_SYNTAX_OID) &&
  5924. strcmp(oid2, DIRSTRING_SYNTAX_OID) &&
  5925. strcmp(oid2, PRINTABLESTRING_SYNTAX_OID) &&
  5926. strcmp(oid2, SPACE_INSENSITIVE_STRING_SYNTAX_OID) &&
  5927. strcmp(oid2, IA5STRING_SYNTAX_OID))
  5928. {
  5929. return 1;
  5930. }
  5931. } else if(strcmp(oid1, IA5STRING_SYNTAX_OID) == 0){
  5932. if(strcmp(oid2, IA5STRING_SYNTAX_OID) &&
  5933. strcmp(oid2, DIRSTRING_SYNTAX_OID) &&
  5934. strcmp(oid2, SPACE_INSENSITIVE_STRING_SYNTAX_OID) &&
  5935. strcmp(oid2, PRINTABLESTRING_SYNTAX_OID))
  5936. {
  5937. return 1;
  5938. }
  5939. } else if(strcmp(oid1, INTEGER_SYNTAX_OID) == 0){
  5940. if(strcmp(oid2, INTEGER_SYNTAX_OID) &&
  5941. strcmp(oid2, DIRSTRING_SYNTAX_OID) &&
  5942. strcmp(oid2, PRINTABLESTRING_SYNTAX_OID) &&
  5943. strcmp(oid2, NUMERICSTRING_SYNTAX_OID) &&
  5944. strcmp(oid2, TELEPHONE_SYNTAX_OID) &&
  5945. strcmp(oid2, TELETEXTERMID_SYNTAX_OID) &&
  5946. strcmp(oid2, TELEXNUMBER_SYNTAX_OID) &&
  5947. strcmp(oid2, SPACE_INSENSITIVE_STRING_SYNTAX_OID) &&
  5948. strcmp(oid2, IA5STRING_SYNTAX_OID) )
  5949. {
  5950. return 1;
  5951. }
  5952. } else if(strcmp(oid1, JPEG_SYNTAX_OID) == 0){
  5953. if(strcmp(oid2, JPEG_SYNTAX_OID) &&
  5954. strcmp(oid2, DIRSTRING_SYNTAX_OID) &&
  5955. strcmp(oid2, PRINTABLESTRING_SYNTAX_OID) &&
  5956. strcmp(oid2, SPACE_INSENSITIVE_STRING_SYNTAX_OID) &&
  5957. strcmp(oid2, IA5STRING_SYNTAX_OID))
  5958. {
  5959. return 1;
  5960. }
  5961. } else if(strcmp(oid1, NAMEANDOPTIONALUID_SYNTAX_OID) == 0){
  5962. if(strcmp(oid2, NAMEANDOPTIONALUID_SYNTAX_OID) &&
  5963. strcmp(oid2, NAMEANDOPTIONALUID_SYNTAX_OID) &&
  5964. strcmp(oid2, DIRSTRING_SYNTAX_OID) &&
  5965. strcmp(oid2, PRINTABLESTRING_SYNTAX_OID) &&
  5966. strcmp(oid2, SPACE_INSENSITIVE_STRING_SYNTAX_OID) &&
  5967. strcmp(oid2, IA5STRING_SYNTAX_OID))
  5968. {
  5969. return 1;
  5970. }
  5971. } else if(strcmp(oid1, NUMERICSTRING_SYNTAX_OID) == 0){
  5972. if(strcmp(oid2, NUMERICSTRING_SYNTAX_OID) &&
  5973. strcmp(oid2, DIRSTRING_SYNTAX_OID) &&
  5974. strcmp(oid2, PRINTABLESTRING_SYNTAX_OID) &&
  5975. strcmp(oid2, SPACE_INSENSITIVE_STRING_SYNTAX_OID) &&
  5976. strcmp(oid2, IA5STRING_SYNTAX_OID))
  5977. {
  5978. return 1;
  5979. }
  5980. } else if(strcmp(oid1, OID_SYNTAX_OID) == 0){
  5981. if(strcmp(oid2, OID_SYNTAX_OID) &&
  5982. strcmp(oid2, DIRSTRING_SYNTAX_OID) &&
  5983. strcmp(oid2, PRINTABLESTRING_SYNTAX_OID) &&
  5984. strcmp(oid2, SPACE_INSENSITIVE_STRING_SYNTAX_OID) &&
  5985. strcmp(oid2, IA5STRING_SYNTAX_OID))
  5986. {
  5987. return 1;
  5988. }
  5989. } else if (strcmp(oid1, OCTETSTRING_SYNTAX_OID) == 0){
  5990. if(strcmp(oid2, OCTETSTRING_SYNTAX_OID) &&
  5991. strcmp(oid2, DIRSTRING_SYNTAX_OID) &&
  5992. strcmp(oid2, PRINTABLESTRING_SYNTAX_OID) &&
  5993. strcmp(oid2, SPACE_INSENSITIVE_STRING_SYNTAX_OID) &&
  5994. strcmp(oid2, IA5STRING_SYNTAX_OID))
  5995. {
  5996. return 1;
  5997. }
  5998. } else if(strcmp(oid1, POSTALADDRESS_SYNTAX_OID) ==0){
  5999. if(strcmp(oid2, POSTALADDRESS_SYNTAX_OID) &&
  6000. strcmp(oid2, DIRSTRING_SYNTAX_OID) &&
  6001. strcmp(oid2, PRINTABLESTRING_SYNTAX_OID) &&
  6002. strcmp(oid2, SPACE_INSENSITIVE_STRING_SYNTAX_OID) &&
  6003. strcmp(oid2, IA5STRING_SYNTAX_OID))
  6004. {
  6005. return 1;
  6006. }
  6007. } else if(strcmp(oid1, PRINTABLESTRING_SYNTAX_OID) == 0){
  6008. if(strcmp(oid2, PRINTABLESTRING_SYNTAX_OID) &&
  6009. strcmp(oid2, SPACE_INSENSITIVE_STRING_SYNTAX_OID) &&
  6010. strcmp(oid2, DIRSTRING_SYNTAX_OID) &&
  6011. strcmp(oid2, IA5STRING_SYNTAX_OID))
  6012. {
  6013. return 1;
  6014. }
  6015. } else if(strcmp(oid1, TELEPHONE_SYNTAX_OID) == 0){
  6016. if(strcmp(oid2, PRINTABLESTRING_SYNTAX_OID) &&
  6017. strcmp(oid2, TELEPHONE_SYNTAX_OID) &&
  6018. strcmp(oid2, DIRSTRING_SYNTAX_OID) &&
  6019. strcmp(oid2, SPACE_INSENSITIVE_STRING_SYNTAX_OID) &&
  6020. strcmp(oid2, IA5STRING_SYNTAX_OID))
  6021. {
  6022. return 1;
  6023. }
  6024. } else if(strcmp(oid1, TELETEXTERMID_SYNTAX_OID) == 0){
  6025. if(strcmp(oid2, TELETEXTERMID_SYNTAX_OID) &&
  6026. strcmp(oid2, PRINTABLESTRING_SYNTAX_OID) &&
  6027. strcmp(oid2, DIRSTRING_SYNTAX_OID) &&
  6028. strcmp(oid2, SPACE_INSENSITIVE_STRING_SYNTAX_OID) &&
  6029. strcmp(oid2, IA5STRING_SYNTAX_OID))
  6030. {
  6031. return 1;
  6032. }
  6033. } else if(strcmp(oid1, TELEXNUMBER_SYNTAX_OID) == 0){
  6034. if(strcmp(oid2, TELEXNUMBER_SYNTAX_OID) &&
  6035. strcmp(oid2, PRINTABLESTRING_SYNTAX_OID) &&
  6036. strcmp(oid2, DIRSTRING_SYNTAX_OID) &&
  6037. strcmp(oid2, SPACE_INSENSITIVE_STRING_SYNTAX_OID) &&
  6038. strcmp(oid2, IA5STRING_SYNTAX_OID))
  6039. {
  6040. return 1;
  6041. }
  6042. } else if (strcmp(oid1, SPACE_INSENSITIVE_STRING_SYNTAX_OID) == 0){
  6043. if(strcmp(oid2, SPACE_INSENSITIVE_STRING_SYNTAX_OID) &&
  6044. strcmp(oid2, PRINTABLESTRING_SYNTAX_OID) &&
  6045. strcmp(oid2, DIRSTRING_SYNTAX_OID) &&
  6046. strcmp(oid2, SPACE_INSENSITIVE_STRING_SYNTAX_OID) &&
  6047. strcmp(oid2, IA5STRING_SYNTAX_OID))
  6048. {
  6049. return 1;
  6050. }
  6051. }
  6052. return 0;
  6053. }
  6054. static void
  6055. schema_oclist_free(struct objclass *oc_list)
  6056. {
  6057. struct objclass *oc, *oc_next;
  6058. for (oc = oc_list; oc != NULL; oc = oc_next) {
  6059. oc_next = oc->oc_next;
  6060. oc_free(&oc);
  6061. }
  6062. }
  6063. static void
  6064. schema_atlist_free(struct asyntaxinfo *at_list)
  6065. {
  6066. struct asyntaxinfo *at, *at_next;
  6067. for (at = at_list; at != NULL; at = at_next) {
  6068. at_next = at->asi_next;
  6069. attr_syntax_free(at);
  6070. }
  6071. }
  6072. static struct objclass *
  6073. schema_berval_to_oclist(struct berval **oc_berval)
  6074. {
  6075. struct objclass *oc, *oc_list, *oc_tail;
  6076. char errorbuf[BUFSIZ];
  6077. int schema_ds4x_compat, rc;
  6078. int i;
  6079. schema_ds4x_compat = config_get_ds4_compatible_schema();
  6080. rc = 0;
  6081. oc_list = NULL;
  6082. oc_tail = NULL;
  6083. if (oc_berval != NULL) {
  6084. for (i = 0; oc_berval[i] != NULL; i++) {
  6085. /* parse the objectclass value */
  6086. if (LDAP_SUCCESS != (rc = parse_oc_str(oc_berval[i]->bv_val, &oc,
  6087. errorbuf, sizeof (errorbuf), DSE_SCHEMA_NO_CHECK | DSE_SCHEMA_USE_PRIV_SCHEMA, 0,
  6088. schema_ds4x_compat, oc_list))) {
  6089. oc_free(&oc);
  6090. rc = 1;
  6091. break;
  6092. }
  6093. /* Add oc at the end of the oc_list */
  6094. oc->oc_next = NULL;
  6095. if (oc_list == NULL) {
  6096. oc_list = oc;
  6097. oc_tail = oc;
  6098. } else {
  6099. oc_tail->oc_next = oc;
  6100. oc_tail = oc;
  6101. }
  6102. }
  6103. }
  6104. if (rc) {
  6105. schema_oclist_free(oc_list);
  6106. oc_list = NULL;
  6107. }
  6108. return oc_list;
  6109. }
  6110. static struct asyntaxinfo *
  6111. schema_berval_to_atlist(struct berval **at_berval)
  6112. {
  6113. struct asyntaxinfo *at, *head = NULL, *at_list = NULL;
  6114. char errorbuf[BUFSIZ];
  6115. int schema_ds4x_compat, rc = 0, i;
  6116. schema_ds4x_compat = config_get_ds4_compatible_schema();
  6117. if (at_berval != NULL) {
  6118. for (i = 0; at_berval[i] != NULL; i++) {
  6119. /* parse the objectclass value */
  6120. rc = parse_at_str(at_berval[i]->bv_val, &at, errorbuf, sizeof (errorbuf),
  6121. DSE_SCHEMA_NO_CHECK | DSE_SCHEMA_USE_PRIV_SCHEMA, 0, schema_ds4x_compat, 0);
  6122. if(rc){
  6123. attr_syntax_free(at);
  6124. break;
  6125. }
  6126. if(!head){
  6127. head = at_list = at;
  6128. } else {
  6129. at_list->asi_next = at;
  6130. at->asi_prev = at_list;
  6131. at_list = at;
  6132. }
  6133. }
  6134. }
  6135. if (rc) {
  6136. schema_atlist_free(head);
  6137. }
  6138. return head;
  6139. }
  6140. int
  6141. schema_objectclasses_superset_check(struct berval **remote_schema, char *type)
  6142. {
  6143. int rc;
  6144. struct objclass *remote_oc_list;
  6145. rc = 0;
  6146. /* head is the future list of the objectclass of the remote schema */
  6147. remote_oc_list = NULL;
  6148. if (remote_schema != NULL) {
  6149. /* First build an objectclass list from the remote schema */
  6150. if ((remote_oc_list = schema_berval_to_oclist(remote_schema)) == NULL) {
  6151. rc = 1;
  6152. return rc;
  6153. }
  6154. /* Check that for each object from the remote schema
  6155. * - MUST attributes are also MUST in local schema
  6156. * - ALLOWED attributes are also ALLOWED in local schema
  6157. */
  6158. if (remote_oc_list) {
  6159. oc_lock_read();
  6160. if (strcmp(type, OC_SUPPLIER) == 0) {
  6161. /* Check if the remote_oc_list from a consumer are or not
  6162. * a superset of the objectclasses of the local supplier schema
  6163. */
  6164. rc = schema_oc_superset_check(remote_oc_list, g_get_global_oc_nolock(), "local supplier", REPL_SCHEMA_AS_SUPPLIER);
  6165. } else {
  6166. /* Check if the objectclasses of the local consumer schema are or not
  6167. * a superset of the remote_oc_list from a supplier
  6168. */
  6169. rc = schema_oc_superset_check(g_get_global_oc_nolock(), remote_oc_list, "remote supplier", REPL_SCHEMA_AS_CONSUMER);
  6170. }
  6171. oc_unlock();
  6172. }
  6173. /* Free the remote schema list*/
  6174. schema_oclist_free(remote_oc_list);
  6175. }
  6176. return rc;
  6177. }
  6178. int
  6179. schema_attributetypes_superset_check(struct berval **remote_schema, char *type)
  6180. {
  6181. struct asyntaxinfo *remote_at_list = NULL;
  6182. int rc = 0;
  6183. if (remote_schema != NULL) {
  6184. /* First build an attribute list from the remote schema */
  6185. if ((remote_at_list = schema_berval_to_atlist(remote_schema)) == NULL) {
  6186. rc = 1;
  6187. return rc;
  6188. }
  6189. /*
  6190. * Check that for each object from the remote schema
  6191. * - MUST attributes are also MUST in local schema
  6192. * - ALLOWED attributes are also ALLOWED in local schema
  6193. */
  6194. if (remote_at_list) {
  6195. attr_syntax_read_lock();
  6196. if (strcmp(type, OC_SUPPLIER) == 0) {
  6197. /*
  6198. * Check if the remote_at_list from a consumer are or not
  6199. * a superset of the attributetypes of the local supplier schema
  6200. */
  6201. rc = schema_at_superset_check(remote_at_list, attr_syntax_get_global_at(), "local supplier", REPL_SCHEMA_AS_SUPPLIER);
  6202. } else {
  6203. /*
  6204. * Check if the attributeypes of the local consumer schema are or not
  6205. * a superset of the remote_at_list from a supplier
  6206. */
  6207. rc = schema_at_superset_check(attr_syntax_get_global_at(), remote_at_list, "remote supplier", REPL_SCHEMA_AS_CONSUMER);
  6208. }
  6209. attr_syntax_unlock_read();
  6210. }
  6211. /* Free the remote schema list */
  6212. schema_atlist_free(remote_at_list);
  6213. }
  6214. return rc;
  6215. }