schema.c 180 KB

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