schema.c 263 KB

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