schema.c 213 KB

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