getdn.c 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334
  1. /* $OpenLDAP$ */
  2. /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  3. *
  4. * Copyright 1998-2022 The OpenLDAP Foundation.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted only as authorized by the OpenLDAP
  9. * Public License.
  10. *
  11. * A copy of this license is available in the file LICENSE in the
  12. * top-level directory of the distribution or, alternatively, at
  13. * <http://www.OpenLDAP.org/license.html>.
  14. */
  15. /* Portions Copyright (c) 1994 Regents of the University of Michigan.
  16. * All rights reserved.
  17. */
  18. #include "portable.h"
  19. #include <stdio.h>
  20. #include <ac/stdlib.h>
  21. #include <ac/socket.h>
  22. #include <ac/string.h>
  23. #include <ac/time.h>
  24. #include "ldap-int.h"
  25. #include "ldap_schema.h"
  26. #include "ldif.h"
  27. /* extension to UFN that turns trailing "dc=value" rdns in DNS style,
  28. * e.g. "ou=People,dc=openldap,dc=org" => "People, openldap.org" */
  29. #define DC_IN_UFN
  30. /* parsing/printing routines */
  31. static int str2strval( const char *str, ber_len_t stoplen, struct berval *val,
  32. const char **next, unsigned flags, int *retFlags, void *ctx );
  33. static int DCE2strval( const char *str, struct berval *val,
  34. const char **next, unsigned flags, void *ctx );
  35. static int IA52strval( const char *str, struct berval *val,
  36. const char **next, unsigned flags, void *ctx );
  37. static int quotedIA52strval( const char *str, struct berval *val,
  38. const char **next, unsigned flags, void *ctx );
  39. static int hexstr2binval( const char *str, struct berval *val,
  40. const char **next, unsigned flags, void *ctx );
  41. static int hexstr2bin( const char *str, char *c );
  42. static int byte2hexpair( const char *val, char *pair );
  43. static int binval2hexstr( struct berval *val, char *str );
  44. static int strval2strlen( struct berval *val, unsigned flags,
  45. ber_len_t *len );
  46. static int strval2str( struct berval *val, char *str, unsigned flags,
  47. ber_len_t *len );
  48. static int strval2IA5strlen( struct berval *val, unsigned flags,
  49. ber_len_t *len );
  50. static int strval2IA5str( struct berval *val, char *str, unsigned flags,
  51. ber_len_t *len );
  52. static int strval2DCEstrlen( struct berval *val, unsigned flags,
  53. ber_len_t *len );
  54. static int strval2DCEstr( struct berval *val, char *str, unsigned flags,
  55. ber_len_t *len );
  56. static int strval2ADstrlen( struct berval *val, unsigned flags,
  57. ber_len_t *len );
  58. static int strval2ADstr( struct berval *val, char *str, unsigned flags,
  59. ber_len_t *len );
  60. static int dn2domain( LDAPDN dn, struct berval *bv, int pos, int *iRDN );
  61. /* AVA helpers */
  62. static LDAPAVA * ldapava_new(
  63. const struct berval *attr, const struct berval *val, unsigned flags, void *ctx );
  64. /* Higher level helpers */
  65. static int rdn2strlen( LDAPRDN rdn, unsigned flags, ber_len_t *len,
  66. int ( *s2l )( struct berval *, unsigned, ber_len_t * ) );
  67. static int rdn2str( LDAPRDN rdn, char *str, unsigned flags, ber_len_t *len,
  68. int ( *s2s )( struct berval *, char *, unsigned, ber_len_t * ));
  69. static int rdn2UFNstrlen( LDAPRDN rdn, unsigned flags, ber_len_t *len );
  70. static int rdn2UFNstr( LDAPRDN rdn, char *str, unsigned flags, ber_len_t *len );
  71. static int rdn2DCEstrlen( LDAPRDN rdn, unsigned flags, ber_len_t *len );
  72. static int rdn2DCEstr( LDAPRDN rdn, char *str, unsigned flag, ber_len_t *len, int first );
  73. static int rdn2ADstrlen( LDAPRDN rdn, unsigned flags, ber_len_t *len );
  74. static int rdn2ADstr( LDAPRDN rdn, char *str, unsigned flags, ber_len_t *len, int first );
  75. /*
  76. * RFC 1823 ldap_get_dn
  77. */
  78. char *
  79. ldap_get_dn( LDAP *ld, LDAPMessage *entry )
  80. {
  81. char *dn;
  82. BerElement tmp;
  83. Debug0( LDAP_DEBUG_TRACE, "ldap_get_dn\n" );
  84. assert( ld != NULL );
  85. assert( LDAP_VALID(ld) );
  86. assert( entry != NULL );
  87. tmp = *entry->lm_ber; /* struct copy */
  88. if ( ber_scanf( &tmp, "{a" /*}*/, &dn ) == LBER_ERROR ) {
  89. ld->ld_errno = LDAP_DECODING_ERROR;
  90. return( NULL );
  91. }
  92. return( dn );
  93. }
  94. int
  95. ldap_get_dn_ber( LDAP *ld, LDAPMessage *entry, BerElement **berout,
  96. BerValue *dn )
  97. {
  98. BerElement tmp, *ber;
  99. ber_len_t len = 0;
  100. int rc = LDAP_SUCCESS;
  101. Debug0( LDAP_DEBUG_TRACE, "ldap_get_dn_ber\n" );
  102. assert( ld != NULL );
  103. assert( LDAP_VALID(ld) );
  104. assert( entry != NULL );
  105. assert( dn != NULL );
  106. dn->bv_val = NULL;
  107. dn->bv_len = 0;
  108. if ( berout ) {
  109. *berout = NULL;
  110. ber = ldap_alloc_ber_with_options( ld );
  111. if( ber == NULL ) {
  112. return LDAP_NO_MEMORY;
  113. }
  114. *berout = ber;
  115. } else {
  116. ber = &tmp;
  117. }
  118. *ber = *entry->lm_ber; /* struct copy */
  119. if ( ber_scanf( ber, "{ml{" /*}*/, dn, &len ) == LBER_ERROR ) {
  120. rc = ld->ld_errno = LDAP_DECODING_ERROR;
  121. }
  122. if ( rc == LDAP_SUCCESS ) {
  123. /* set the length to avoid overrun */
  124. rc = ber_set_option( ber, LBER_OPT_REMAINING_BYTES, &len );
  125. if( rc != LBER_OPT_SUCCESS ) {
  126. rc = ld->ld_errno = LDAP_LOCAL_ERROR;
  127. }
  128. }
  129. if ( rc != LDAP_SUCCESS && berout ) {
  130. ber_free( ber, 0 );
  131. *berout = NULL;
  132. }
  133. return rc;
  134. }
  135. /*
  136. * RFC 1823 ldap_dn2ufn
  137. */
  138. char *
  139. ldap_dn2ufn( LDAP_CONST char *dn )
  140. {
  141. char *out = NULL;
  142. Debug0( LDAP_DEBUG_TRACE, "ldap_dn2ufn\n" );
  143. ( void )ldap_dn_normalize( dn, LDAP_DN_FORMAT_LDAP,
  144. &out, LDAP_DN_FORMAT_UFN );
  145. return( out );
  146. }
  147. /*
  148. * RFC 1823 ldap_explode_dn
  149. */
  150. char **
  151. ldap_explode_dn( LDAP_CONST char *dn, int notypes )
  152. {
  153. LDAPDN tmpDN;
  154. char **values = NULL;
  155. int iRDN;
  156. unsigned flag = notypes ? LDAP_DN_FORMAT_UFN : LDAP_DN_FORMAT_LDAPV3;
  157. Debug0( LDAP_DEBUG_TRACE, "ldap_explode_dn\n" );
  158. if ( ldap_str2dn( dn, &tmpDN, LDAP_DN_FORMAT_LDAP )
  159. != LDAP_SUCCESS ) {
  160. return NULL;
  161. }
  162. if( tmpDN == NULL ) {
  163. values = LDAP_MALLOC( sizeof( char * ) );
  164. if( values == NULL ) return NULL;
  165. values[0] = NULL;
  166. return values;
  167. }
  168. for ( iRDN = 0; tmpDN[ iRDN ]; iRDN++ );
  169. values = LDAP_MALLOC( sizeof( char * ) * ( 1 + iRDN ) );
  170. if ( values == NULL ) {
  171. ldap_dnfree( tmpDN );
  172. return NULL;
  173. }
  174. for ( iRDN = 0; tmpDN[ iRDN ]; iRDN++ ) {
  175. ldap_rdn2str( tmpDN[ iRDN ], &values[ iRDN ], flag );
  176. }
  177. ldap_dnfree( tmpDN );
  178. values[ iRDN ] = NULL;
  179. return values;
  180. }
  181. char **
  182. ldap_explode_rdn( LDAP_CONST char *rdn, int notypes )
  183. {
  184. LDAPRDN tmpRDN;
  185. char **values = NULL;
  186. const char *p;
  187. int iAVA;
  188. Debug0( LDAP_DEBUG_TRACE, "ldap_explode_rdn\n" );
  189. /*
  190. * we only parse the first rdn
  191. * FIXME: we prefer efficiency over checking if the _ENTIRE_
  192. * dn can be parsed
  193. */
  194. if ( ldap_str2rdn( rdn, &tmpRDN, (char **) &p, LDAP_DN_FORMAT_LDAP )
  195. != LDAP_SUCCESS ) {
  196. return( NULL );
  197. }
  198. for ( iAVA = 0; tmpRDN[ iAVA ]; iAVA++ ) ;
  199. values = LDAP_MALLOC( sizeof( char * ) * ( 1 + iAVA ) );
  200. if ( values == NULL ) {
  201. ldap_rdnfree( tmpRDN );
  202. return( NULL );
  203. }
  204. for ( iAVA = 0; tmpRDN[ iAVA ]; iAVA++ ) {
  205. ber_len_t l = 0, vl, al = 0;
  206. char *str;
  207. LDAPAVA *ava = tmpRDN[ iAVA ];
  208. if ( ava->la_flags & LDAP_AVA_BINARY ) {
  209. vl = 1 + 2 * ava->la_value.bv_len;
  210. } else {
  211. if ( strval2strlen( &ava->la_value,
  212. ava->la_flags, &vl ) ) {
  213. goto error_return;
  214. }
  215. }
  216. if ( !notypes ) {
  217. al = ava->la_attr.bv_len;
  218. l = vl + ava->la_attr.bv_len + 1;
  219. str = LDAP_MALLOC( l + 1 );
  220. if ( str == NULL ) {
  221. goto error_return;
  222. }
  223. AC_MEMCPY( str, ava->la_attr.bv_val,
  224. ava->la_attr.bv_len );
  225. str[ al++ ] = '=';
  226. } else {
  227. l = vl;
  228. str = LDAP_MALLOC( l + 1 );
  229. if ( str == NULL ) {
  230. goto error_return;
  231. }
  232. }
  233. if ( ava->la_flags & LDAP_AVA_BINARY ) {
  234. str[ al++ ] = '#';
  235. if ( binval2hexstr( &ava->la_value, &str[ al ] ) ) {
  236. goto error_return;
  237. }
  238. } else {
  239. if ( strval2str( &ava->la_value, &str[ al ],
  240. ava->la_flags, &vl ) ) {
  241. goto error_return;
  242. }
  243. }
  244. str[ l ] = '\0';
  245. values[ iAVA ] = str;
  246. }
  247. values[ iAVA ] = NULL;
  248. ldap_rdnfree( tmpRDN );
  249. return( values );
  250. error_return:;
  251. LBER_VFREE( values );
  252. ldap_rdnfree( tmpRDN );
  253. return( NULL );
  254. }
  255. char *
  256. ldap_dn2dcedn( LDAP_CONST char *dn )
  257. {
  258. char *out = NULL;
  259. Debug0( LDAP_DEBUG_TRACE, "ldap_dn2dcedn\n" );
  260. ( void )ldap_dn_normalize( dn, LDAP_DN_FORMAT_LDAP,
  261. &out, LDAP_DN_FORMAT_DCE );
  262. return( out );
  263. }
  264. char *
  265. ldap_dcedn2dn( LDAP_CONST char *dce )
  266. {
  267. char *out = NULL;
  268. Debug0( LDAP_DEBUG_TRACE, "ldap_dcedn2dn\n" );
  269. ( void )ldap_dn_normalize( dce, LDAP_DN_FORMAT_DCE, &out, LDAP_DN_FORMAT_LDAPV3 );
  270. return( out );
  271. }
  272. char *
  273. ldap_dn2ad_canonical( LDAP_CONST char *dn )
  274. {
  275. char *out = NULL;
  276. Debug0( LDAP_DEBUG_TRACE, "ldap_dn2ad_canonical\n" );
  277. ( void )ldap_dn_normalize( dn, LDAP_DN_FORMAT_LDAP,
  278. &out, LDAP_DN_FORMAT_AD_CANONICAL );
  279. return( out );
  280. }
  281. /*
  282. * function that changes the string representation of dnin
  283. * from ( fin & LDAP_DN_FORMAT_MASK ) to ( fout & LDAP_DN_FORMAT_MASK )
  284. *
  285. * fin can be one of:
  286. * LDAP_DN_FORMAT_LDAP (RFC 4514 liberal, plus some RFC 1779)
  287. * LDAP_DN_FORMAT_LDAPV3 (RFC 4514)
  288. * LDAP_DN_FORMAT_LDAPV2 (RFC 1779)
  289. * LDAP_DN_FORMAT_DCE (?)
  290. *
  291. * fout can be any of the above except
  292. * LDAP_DN_FORMAT_LDAP
  293. * plus:
  294. * LDAP_DN_FORMAT_UFN (RFC 1781, partial and with extensions)
  295. * LDAP_DN_FORMAT_AD_CANONICAL (?)
  296. */
  297. int
  298. ldap_dn_normalize( LDAP_CONST char *dnin,
  299. unsigned fin, char **dnout, unsigned fout )
  300. {
  301. int rc;
  302. LDAPDN tmpDN = NULL;
  303. Debug0( LDAP_DEBUG_TRACE, "ldap_dn_normalize\n" );
  304. assert( dnout != NULL );
  305. *dnout = NULL;
  306. if ( dnin == NULL ) {
  307. return( LDAP_SUCCESS );
  308. }
  309. rc = ldap_str2dn( dnin , &tmpDN, fin );
  310. if ( rc != LDAP_SUCCESS ) {
  311. return( rc );
  312. }
  313. rc = ldap_dn2str( tmpDN, dnout, fout );
  314. ldap_dnfree( tmpDN );
  315. return( rc );
  316. }
  317. /* States */
  318. #define B4AVA 0x0000
  319. /* #define B4ATTRTYPE 0x0001 */
  320. #define B4OIDATTRTYPE 0x0002
  321. #define B4STRINGATTRTYPE 0x0003
  322. #define B4AVAEQUALS 0x0100
  323. #define B4AVASEP 0x0200
  324. #define B4RDNSEP 0x0300
  325. #define GOTAVA 0x0400
  326. #define B4ATTRVALUE 0x0010
  327. #define B4STRINGVALUE 0x0020
  328. #define B4IA5VALUEQUOTED 0x0030
  329. #define B4IA5VALUE 0x0040
  330. #define B4BINARYVALUE 0x0050
  331. /*
  332. * Helpers (mostly from slap.h)
  333. * c is assumed to Unicode in an ASCII compatible format (UTF-8)
  334. * Macros assume "C" Locale (ASCII)
  335. */
  336. #define LDAP_DN_ASCII_SPACE(c) \
  337. ( (c) == ' ' || (c) == '\t' || (c) == '\n' || (c) == '\r' )
  338. #define LDAP_DN_ASCII_LOWER(c) LDAP_LOWER(c)
  339. #define LDAP_DN_ASCII_UPPER(c) LDAP_UPPER(c)
  340. #define LDAP_DN_ASCII_ALPHA(c) LDAP_ALPHA(c)
  341. #define LDAP_DN_ASCII_DIGIT(c) LDAP_DIGIT(c)
  342. #define LDAP_DN_ASCII_LCASE_HEXALPHA(c) LDAP_HEXLOWER(c)
  343. #define LDAP_DN_ASCII_UCASE_HEXALPHA(c) LDAP_HEXUPPER(c)
  344. #define LDAP_DN_ASCII_HEXDIGIT(c) LDAP_HEX(c)
  345. #define LDAP_DN_ASCII_ALNUM(c) LDAP_ALNUM(c)
  346. #define LDAP_DN_ASCII_PRINTABLE(c) ( (c) >= ' ' && (c) <= '~' )
  347. /* attribute type */
  348. #define LDAP_DN_OID_LEADCHAR(c) LDAP_DIGIT(c)
  349. #define LDAP_DN_DESC_LEADCHAR(c) LDAP_ALPHA(c)
  350. #define LDAP_DN_DESC_CHAR(c) LDAP_LDH(c)
  351. #define LDAP_DN_LANG_SEP(c) ( (c) == ';' )
  352. #define LDAP_DN_ATTRDESC_CHAR(c) \
  353. ( LDAP_DN_DESC_CHAR(c) || LDAP_DN_LANG_SEP(c) )
  354. /* special symbols */
  355. #define LDAP_DN_AVA_EQUALS(c) ( (c) == '=' )
  356. #define LDAP_DN_AVA_SEP(c) ( (c) == '+' )
  357. #define LDAP_DN_RDN_SEP(c) ( (c) == ',' )
  358. #define LDAP_DN_RDN_SEP_V2(c) ( LDAP_DN_RDN_SEP(c) || (c) == ';' )
  359. #define LDAP_DN_OCTOTHORPE(c) ( (c) == '#' )
  360. #define LDAP_DN_QUOTES(c) ( (c) == '\"' )
  361. #define LDAP_DN_ESCAPE(c) ( (c) == '\\' )
  362. #define LDAP_DN_VALUE_END(c) \
  363. ( LDAP_DN_RDN_SEP(c) || LDAP_DN_AVA_SEP(c) )
  364. /* NOTE: according to RFC 4514, '=' can be escaped and treated as special,
  365. * i.e. escaped both as "\<hexpair>" and * as "\=", but it is treated as
  366. * a regular char, i.e. it can also appear as '='.
  367. *
  368. * As such, in 2.2 we used to allow reading unescaped '=', but we always
  369. * produced escaped '\3D'; this changes since 2.3, if compatibility issues
  370. * do not arise
  371. */
  372. #define LDAP_DN_NE(c) \
  373. ( LDAP_DN_RDN_SEP_V2(c) || LDAP_DN_AVA_SEP(c) \
  374. || LDAP_DN_QUOTES(c) \
  375. || (c) == '<' || (c) == '>' )
  376. #define LDAP_DN_MAYESCAPE(c) \
  377. ( LDAP_DN_ESCAPE(c) || LDAP_DN_NE(c) \
  378. || LDAP_DN_AVA_EQUALS(c) \
  379. || LDAP_DN_ASCII_SPACE(c) || LDAP_DN_OCTOTHORPE(c) )
  380. #define LDAP_DN_SHOULDESCAPE(c) ( LDAP_DN_AVA_EQUALS(c) )
  381. #define LDAP_DN_NEEDESCAPE(c) \
  382. ( LDAP_DN_ESCAPE(c) || LDAP_DN_NE(c) )
  383. #define LDAP_DN_NEEDESCAPE_LEAD(c) LDAP_DN_MAYESCAPE(c)
  384. #define LDAP_DN_NEEDESCAPE_TRAIL(c) \
  385. ( LDAP_DN_ASCII_SPACE(c) || LDAP_DN_NEEDESCAPE(c) )
  386. #define LDAP_DN_WILLESCAPE_CHAR(c) \
  387. ( LDAP_DN_RDN_SEP(c) || LDAP_DN_AVA_SEP(c) || LDAP_DN_ESCAPE(c) )
  388. #define LDAP_DN_IS_PRETTY(f) ( (f) & LDAP_DN_PRETTY )
  389. #define LDAP_DN_WILLESCAPE_HEX(f, c) \
  390. ( ( !LDAP_DN_IS_PRETTY( f ) ) && LDAP_DN_WILLESCAPE_CHAR(c) )
  391. /* LDAPv2 */
  392. #define LDAP_DN_VALUE_END_V2(c) \
  393. ( LDAP_DN_RDN_SEP_V2(c) || LDAP_DN_AVA_SEP(c) )
  394. /* RFC 1779 */
  395. #define LDAP_DN_V2_SPECIAL(c) \
  396. ( LDAP_DN_RDN_SEP_V2(c) || LDAP_DN_AVA_EQUALS(c) \
  397. || LDAP_DN_AVA_SEP(c) || (c) == '<' || (c) == '>' \
  398. || LDAP_DN_OCTOTHORPE(c) )
  399. #define LDAP_DN_V2_PAIR(c) \
  400. ( LDAP_DN_V2_SPECIAL(c) || LDAP_DN_ESCAPE(c) || LDAP_DN_QUOTES(c) )
  401. /*
  402. * DCE (mostly from Luke Howard and IBM implementation for AIX)
  403. *
  404. * From: "Application Development Guide - Directory Services" (FIXME: add link?)
  405. * Here escapes and valid chars for GDS are considered; as soon as more
  406. * specific info is found, the macros will be updated.
  407. *
  408. * Chars: 'a'-'z', 'A'-'Z', '0'-'9',
  409. * '.', ':', ',', ''', '+', '-', '=', '(', ')', '?', '/', ' '.
  410. *
  411. * Metachars: '/', ',', '=', '\'.
  412. *
  413. * the '\' is used to escape other metachars.
  414. *
  415. * Assertion: '='
  416. * RDN separator: '/'
  417. * AVA separator: ','
  418. *
  419. * Attribute types must start with alphabetic chars and can contain
  420. * alphabetic chars and digits (FIXME: no '-'?). OIDs are allowed.
  421. */
  422. #define LDAP_DN_RDN_SEP_DCE(c) ( (c) == '/' )
  423. #define LDAP_DN_AVA_SEP_DCE(c) ( (c) == ',' )
  424. #define LDAP_DN_ESCAPE_DCE(c) ( LDAP_DN_ESCAPE(c) )
  425. #define LDAP_DN_VALUE_END_DCE(c) \
  426. ( LDAP_DN_RDN_SEP_DCE(c) || LDAP_DN_AVA_SEP_DCE(c) )
  427. #define LDAP_DN_NEEDESCAPE_DCE(c) \
  428. ( LDAP_DN_VALUE_END_DCE(c) || LDAP_DN_AVA_EQUALS(c) )
  429. /* AD Canonical */
  430. #define LDAP_DN_RDN_SEP_AD(c) ( (c) == '/' )
  431. #define LDAP_DN_ESCAPE_AD(c) ( LDAP_DN_ESCAPE(c) )
  432. #define LDAP_DN_AVA_SEP_AD(c) ( (c) == ',' ) /* assume same as DCE */
  433. #define LDAP_DN_VALUE_END_AD(c) \
  434. ( LDAP_DN_RDN_SEP_AD(c) || LDAP_DN_AVA_SEP_AD(c) )
  435. #define LDAP_DN_NEEDESCAPE_AD(c) \
  436. ( LDAP_DN_VALUE_END_AD(c) || LDAP_DN_AVA_EQUALS(c) )
  437. /* generics */
  438. #define LDAP_DN_HEXPAIR(s) \
  439. ( LDAP_DN_ASCII_HEXDIGIT((s)[0]) && LDAP_DN_ASCII_HEXDIGIT((s)[1]) )
  440. /* better look at the AttributeDescription? */
  441. /* FIXME: no composite rdn or non-"dc" types, right?
  442. * (what about "dc" in OID form?) */
  443. /* FIXME: we do not allow binary values in domain, right? */
  444. /* NOTE: use this macro only when ABSOLUTELY SURE rdn IS VALID! */
  445. /* NOTE: don't use strcasecmp() as it is locale specific! */
  446. #define LDAP_DC_ATTR "dc"
  447. #define LDAP_DC_ATTRU "DC"
  448. #define LDAP_DN_IS_RDN_DC( r ) \
  449. ( (r) && (r)[0] && !(r)[1] \
  450. && ((r)[0]->la_flags & LDAP_AVA_STRING) \
  451. && ((r)[0]->la_attr.bv_len == 2) \
  452. && (((r)[0]->la_attr.bv_val[0] == LDAP_DC_ATTR[0]) \
  453. || ((r)[0]->la_attr.bv_val[0] == LDAP_DC_ATTRU[0])) \
  454. && (((r)[0]->la_attr.bv_val[1] == LDAP_DC_ATTR[1]) \
  455. || ((r)[0]->la_attr.bv_val[1] == LDAP_DC_ATTRU[1])))
  456. /* Composite rules */
  457. #define LDAP_DN_ALLOW_ONE_SPACE(f) \
  458. ( LDAP_DN_LDAPV2(f) \
  459. || !( (f) & LDAP_DN_P_NOSPACEAFTERRDN ) )
  460. #define LDAP_DN_ALLOW_SPACES(f) \
  461. ( LDAP_DN_LDAPV2(f) \
  462. || !( (f) & ( LDAP_DN_P_NOLEADTRAILSPACES | LDAP_DN_P_NOSPACEAFTERRDN ) ) )
  463. #define LDAP_DN_LDAP(f) \
  464. ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_LDAP )
  465. #define LDAP_DN_LDAPV3(f) \
  466. ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_LDAPV3 )
  467. #define LDAP_DN_LDAPV2(f) \
  468. ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_LDAPV2 )
  469. #define LDAP_DN_DCE(f) \
  470. ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_DCE )
  471. #define LDAP_DN_UFN(f) \
  472. ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_UFN )
  473. #define LDAP_DN_ADC(f) \
  474. ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_AD_CANONICAL )
  475. #define LDAP_DN_FORMAT(f) ( (f) & LDAP_DN_FORMAT_MASK )
  476. /*
  477. * LDAPAVA helpers (will become part of the API for operations
  478. * on structural representations of DNs).
  479. */
  480. static LDAPAVA *
  481. ldapava_new( const struct berval *attr, const struct berval *val,
  482. unsigned flags, void *ctx )
  483. {
  484. LDAPAVA *ava;
  485. assert( attr != NULL );
  486. assert( val != NULL );
  487. ava = LDAP_MALLOCX( sizeof( LDAPAVA ) + attr->bv_len + 1, ctx );
  488. if ( ava ) {
  489. ava->la_attr.bv_len = attr->bv_len;
  490. ava->la_attr.bv_val = (char *)(ava+1);
  491. AC_MEMCPY( ava->la_attr.bv_val, attr->bv_val, attr->bv_len );
  492. ava->la_attr.bv_val[attr->bv_len] = '\0';
  493. ava->la_value = *val;
  494. ava->la_flags = flags | LDAP_AVA_FREE_VALUE;
  495. ava->la_private = NULL;
  496. }
  497. return( ava );
  498. }
  499. static void
  500. ldapava_free( LDAPAVA *ava, void *ctx )
  501. {
  502. assert( ava != NULL );
  503. #if 0
  504. /* ava's private must be freed by caller
  505. * (at present let's skip this check because la_private
  506. * basically holds static data) */
  507. assert( ava->la_private == NULL );
  508. #endif
  509. if (ava->la_flags & LDAP_AVA_FREE_VALUE)
  510. LDAP_FREEX( ava->la_value.bv_val, ctx );
  511. LDAP_FREEX( ava, ctx );
  512. }
  513. void
  514. ldap_rdnfree( LDAPRDN rdn )
  515. {
  516. ldap_rdnfree_x( rdn, NULL );
  517. }
  518. void
  519. ldap_rdnfree_x( LDAPRDN rdn, void *ctx )
  520. {
  521. int iAVA;
  522. if ( rdn == NULL ) {
  523. return;
  524. }
  525. for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
  526. ldapava_free( rdn[ iAVA ], ctx );
  527. }
  528. LDAP_FREEX( rdn, ctx );
  529. }
  530. void
  531. ldap_dnfree( LDAPDN dn )
  532. {
  533. ldap_dnfree_x( dn, NULL );
  534. }
  535. void
  536. ldap_dnfree_x( LDAPDN dn, void *ctx )
  537. {
  538. int iRDN;
  539. if ( dn == NULL ) {
  540. return;
  541. }
  542. for ( iRDN = 0; dn[ iRDN ]; iRDN++ ) {
  543. ldap_rdnfree_x( dn[ iRDN ], ctx );
  544. }
  545. LDAP_FREEX( dn, ctx );
  546. }
  547. /*
  548. * Converts a string representation of a DN (in LDAPv3, LDAPv2 or DCE)
  549. * into a structural representation of the DN, by separating attribute
  550. * types and values encoded in the more appropriate form, which is
  551. * string or OID for attribute types and binary form of the BER encoded
  552. * value or Unicode string. Formats different from LDAPv3 are parsed
  553. * according to their own rules and turned into the more appropriate
  554. * form according to LDAPv3.
  555. *
  556. * NOTE: I realize the code is getting spaghettish; it is rather
  557. * experimental and will hopefully turn into something more simple
  558. * and readable as soon as it works as expected.
  559. */
  560. /*
  561. * Default sizes of AVA and RDN static working arrays; if required
  562. * the are dynamically resized. The values can be tuned in case
  563. * of special requirements (e.g. very deep DN trees or high number
  564. * of AVAs per RDN).
  565. */
  566. #define TMP_AVA_SLOTS 8
  567. #define TMP_RDN_SLOTS 32
  568. int
  569. ldap_str2dn( LDAP_CONST char *str, LDAPDN *dn, unsigned flags )
  570. {
  571. struct berval bv;
  572. assert( str != NULL );
  573. bv.bv_len = strlen( str );
  574. bv.bv_val = (char *) str;
  575. return ldap_bv2dn_x( &bv, dn, flags, NULL );
  576. }
  577. int
  578. ldap_bv2dn( struct berval *bv, LDAPDN *dn, unsigned flags )
  579. {
  580. return ldap_bv2dn_x( bv, dn, flags, NULL );
  581. }
  582. int
  583. ldap_bv2dn_x( struct berval *bvin, LDAPDN *dn, unsigned flags, void *ctx )
  584. {
  585. const char *p;
  586. int rc = LDAP_DECODING_ERROR;
  587. int nrdns = 0;
  588. LDAPDN newDN = NULL;
  589. LDAPRDN newRDN = NULL, tmpDN_[TMP_RDN_SLOTS], *tmpDN = tmpDN_;
  590. int num_slots = TMP_RDN_SLOTS;
  591. char *str, *end;
  592. struct berval bvtmp, *bv = &bvtmp;
  593. assert( bvin != NULL );
  594. assert( bvin->bv_val != NULL );
  595. assert( dn != NULL );
  596. *bv = *bvin;
  597. str = bv->bv_val;
  598. end = str + bv->bv_len;
  599. Debug2( LDAP_DEBUG_ARGS, "=> ldap_bv2dn(%s,%u)\n", str, flags );
  600. *dn = NULL;
  601. switch ( LDAP_DN_FORMAT( flags ) ) {
  602. case LDAP_DN_FORMAT_LDAP:
  603. case LDAP_DN_FORMAT_LDAPV3:
  604. case LDAP_DN_FORMAT_DCE:
  605. break;
  606. /* allow DN enclosed in brackets */
  607. case LDAP_DN_FORMAT_LDAPV2:
  608. if ( str[0] == '<' ) {
  609. if ( bv->bv_len < 2 || end[ -1 ] != '>' ) {
  610. rc = LDAP_DECODING_ERROR;
  611. goto parsing_error;
  612. }
  613. bv->bv_val++;
  614. bv->bv_len -= 2;
  615. str++;
  616. end--;
  617. }
  618. break;
  619. /* unsupported in str2dn */
  620. case LDAP_DN_FORMAT_UFN:
  621. case LDAP_DN_FORMAT_AD_CANONICAL:
  622. return LDAP_PARAM_ERROR;
  623. case LDAP_DN_FORMAT_LBER:
  624. default:
  625. return LDAP_PARAM_ERROR;
  626. }
  627. if ( bv->bv_len == 0 ) {
  628. return LDAP_SUCCESS;
  629. }
  630. if( memchr( bv->bv_val, '\0', bv->bv_len ) != NULL ) {
  631. /* value must have embedded NULs */
  632. return LDAP_DECODING_ERROR;
  633. }
  634. p = str;
  635. if ( LDAP_DN_DCE( flags ) ) {
  636. /*
  637. * (from Luke Howard: thnx) A RDN separator is required
  638. * at the beginning of an (absolute) DN.
  639. */
  640. if ( !LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
  641. goto parsing_error;
  642. }
  643. p++;
  644. /*
  645. * actually we do not want to accept by default the DCE form,
  646. * we do not want to auto-detect it
  647. */
  648. #if 0
  649. } else if ( LDAP_DN_LDAP( flags ) ) {
  650. /*
  651. * if dn starts with '/' let's make it a DCE dn
  652. */
  653. if ( LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
  654. flags |= LDAP_DN_FORMAT_DCE;
  655. p++;
  656. }
  657. #endif
  658. }
  659. for ( ; p < end; p++ ) {
  660. int err;
  661. struct berval tmpbv;
  662. tmpbv.bv_len = bv->bv_len - ( p - str );
  663. tmpbv.bv_val = (char *)p;
  664. err = ldap_bv2rdn_x( &tmpbv, &newRDN, (char **) &p, flags,ctx);
  665. if ( err != LDAP_SUCCESS ) {
  666. goto parsing_error;
  667. }
  668. /*
  669. * We expect a rdn separator
  670. */
  671. if ( p < end && p[ 0 ] ) {
  672. switch ( LDAP_DN_FORMAT( flags ) ) {
  673. case LDAP_DN_FORMAT_LDAPV3:
  674. if ( !LDAP_DN_RDN_SEP( p[ 0 ] ) ) {
  675. rc = LDAP_DECODING_ERROR;
  676. goto parsing_error;
  677. }
  678. break;
  679. case LDAP_DN_FORMAT_LDAP:
  680. case LDAP_DN_FORMAT_LDAPV2:
  681. if ( !LDAP_DN_RDN_SEP_V2( p[ 0 ] ) ) {
  682. rc = LDAP_DECODING_ERROR;
  683. goto parsing_error;
  684. }
  685. break;
  686. case LDAP_DN_FORMAT_DCE:
  687. if ( !LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
  688. rc = LDAP_DECODING_ERROR;
  689. goto parsing_error;
  690. }
  691. break;
  692. }
  693. }
  694. tmpDN[nrdns++] = newRDN;
  695. newRDN = NULL;
  696. /*
  697. * make the static RDN array dynamically rescalable
  698. */
  699. if ( nrdns == num_slots ) {
  700. LDAPRDN *tmp;
  701. if ( tmpDN == tmpDN_ ) {
  702. tmp = LDAP_MALLOCX( num_slots * 2 * sizeof( LDAPRDN * ), ctx );
  703. if ( tmp == NULL ) {
  704. rc = LDAP_NO_MEMORY;
  705. goto parsing_error;
  706. }
  707. AC_MEMCPY( tmp, tmpDN, num_slots * sizeof( LDAPRDN * ) );
  708. } else {
  709. tmp = LDAP_REALLOCX( tmpDN, num_slots * 2 * sizeof( LDAPRDN * ), ctx );
  710. if ( tmp == NULL ) {
  711. rc = LDAP_NO_MEMORY;
  712. goto parsing_error;
  713. }
  714. }
  715. tmpDN = tmp;
  716. num_slots *= 2;
  717. }
  718. if ( p >= end || p[ 0 ] == '\0' ) {
  719. /*
  720. * the DN is over, phew
  721. */
  722. newDN = (LDAPDN)LDAP_MALLOCX( sizeof(LDAPRDN *) * (nrdns+1), ctx );
  723. if ( newDN == NULL ) {
  724. rc = LDAP_NO_MEMORY;
  725. goto parsing_error;
  726. } else {
  727. int i;
  728. if ( LDAP_DN_DCE( flags ) ) {
  729. /* add in reversed order */
  730. for ( i=0; i<nrdns; i++ )
  731. newDN[i] = tmpDN[nrdns-1-i];
  732. } else {
  733. for ( i=0; i<nrdns; i++ )
  734. newDN[i] = tmpDN[i];
  735. }
  736. newDN[nrdns] = NULL;
  737. rc = LDAP_SUCCESS;
  738. }
  739. goto return_result;
  740. }
  741. }
  742. parsing_error:;
  743. if ( newRDN ) {
  744. ldap_rdnfree_x( newRDN, ctx );
  745. }
  746. for ( nrdns-- ;nrdns >= 0; nrdns-- ) {
  747. ldap_rdnfree_x( tmpDN[nrdns], ctx );
  748. }
  749. return_result:;
  750. if ( tmpDN != tmpDN_ ) {
  751. LDAP_FREEX( tmpDN, ctx );
  752. }
  753. Debug3( LDAP_DEBUG_ARGS, "<= ldap_bv2dn(%s)=%d %s\n", str, rc,
  754. rc ? ldap_err2string( rc ) : "" );
  755. *dn = newDN;
  756. return( rc );
  757. }
  758. /*
  759. * ldap_str2rdn
  760. *
  761. * Parses a relative DN according to flags up to a rdn separator
  762. * or to the end of str.
  763. * Returns the rdn and a pointer to the string continuation, which
  764. * corresponds to the rdn separator or to '\0' in case the string is over.
  765. */
  766. int
  767. ldap_str2rdn( LDAP_CONST char *str, LDAPRDN *rdn,
  768. char **n_in, unsigned flags )
  769. {
  770. struct berval bv;
  771. assert( str != NULL );
  772. assert( str[ 0 ] != '\0' ); /* FIXME: is this required? */
  773. bv.bv_len = strlen( str );
  774. bv.bv_val = (char *) str;
  775. return ldap_bv2rdn_x( &bv, rdn, n_in, flags, NULL );
  776. }
  777. int
  778. ldap_bv2rdn( struct berval *bv, LDAPRDN *rdn,
  779. char **n_in, unsigned flags )
  780. {
  781. return ldap_bv2rdn_x( bv, rdn, n_in, flags, NULL );
  782. }
  783. int
  784. ldap_bv2rdn_x( struct berval *bv, LDAPRDN *rdn,
  785. char **n_in, unsigned flags, void *ctx )
  786. {
  787. const char **n = (const char **) n_in;
  788. const char *p;
  789. int navas = 0;
  790. int state = B4AVA;
  791. int rc = LDAP_DECODING_ERROR;
  792. int attrTypeEncoding = LDAP_AVA_STRING,
  793. attrValueEncoding = LDAP_AVA_STRING;
  794. struct berval attrType = BER_BVNULL;
  795. struct berval attrValue = BER_BVNULL;
  796. LDAPRDN newRDN = NULL;
  797. LDAPAVA *tmpRDN_[TMP_AVA_SLOTS], **tmpRDN = tmpRDN_;
  798. int num_slots = TMP_AVA_SLOTS;
  799. char *str;
  800. ber_len_t stoplen;
  801. assert( bv != NULL );
  802. assert( bv->bv_len != 0 );
  803. assert( bv->bv_val != NULL );
  804. assert( rdn || flags & LDAP_DN_SKIP );
  805. assert( n != NULL );
  806. str = bv->bv_val;
  807. stoplen = bv->bv_len;
  808. if ( rdn ) {
  809. *rdn = NULL;
  810. }
  811. *n = NULL;
  812. switch ( LDAP_DN_FORMAT( flags ) ) {
  813. case LDAP_DN_FORMAT_LDAP:
  814. case LDAP_DN_FORMAT_LDAPV3:
  815. case LDAP_DN_FORMAT_LDAPV2:
  816. case LDAP_DN_FORMAT_DCE:
  817. break;
  818. /* unsupported in str2dn */
  819. case LDAP_DN_FORMAT_UFN:
  820. case LDAP_DN_FORMAT_AD_CANONICAL:
  821. return LDAP_PARAM_ERROR;
  822. case LDAP_DN_FORMAT_LBER:
  823. default:
  824. return LDAP_PARAM_ERROR;
  825. }
  826. if ( bv->bv_len == 0 ) {
  827. return LDAP_SUCCESS;
  828. }
  829. if( memchr( bv->bv_val, '\0', bv->bv_len ) != NULL ) {
  830. /* value must have embedded NULs */
  831. return LDAP_DECODING_ERROR;
  832. }
  833. p = str;
  834. for ( ; p[ 0 ] || state == GOTAVA; ) {
  835. /*
  836. * The parser in principle advances one token a time,
  837. * or toggles state if preferable.
  838. */
  839. switch (state) {
  840. /*
  841. * an AttributeType can be encoded as:
  842. * - its string representation; in detail, implementations
  843. * MUST recognize AttributeType string type names listed
  844. * in Section 3 of RFC 4514, and MAY recognize other names.
  845. * - its numeric OID (a dotted decimal string)
  846. */
  847. case B4AVA:
  848. if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
  849. if ( !LDAP_DN_ALLOW_ONE_SPACE( flags ) ) {
  850. /* error */
  851. goto parsing_error;
  852. }
  853. p++;
  854. }
  855. if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
  856. if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
  857. /* error */
  858. goto parsing_error;
  859. }
  860. /* whitespace is allowed (and trimmed) */
  861. p++;
  862. while ( p[ 0 ] && LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
  863. p++;
  864. }
  865. if ( !p[ 0 ] ) {
  866. /* error: we expected an AVA */
  867. goto parsing_error;
  868. }
  869. }
  870. /* oid */
  871. if ( LDAP_DN_OID_LEADCHAR( p[ 0 ] ) ) {
  872. state = B4OIDATTRTYPE;
  873. break;
  874. }
  875. /* else must be alpha */
  876. if ( !LDAP_DN_DESC_LEADCHAR( p[ 0 ] ) ) {
  877. goto parsing_error;
  878. }
  879. /* LDAPv2 "oid." prefix */
  880. if ( LDAP_DN_LDAPV2( flags ) ) {
  881. /*
  882. * to be overly pedantic, we only accept
  883. * "OID." or "oid."
  884. */
  885. if ( flags & LDAP_DN_PEDANTIC ) {
  886. if ( !strncmp( p, "OID.", 4 )
  887. || !strncmp( p, "oid.", 4 ) ) {
  888. p += 4;
  889. state = B4OIDATTRTYPE;
  890. break;
  891. }
  892. } else {
  893. if ( !strncasecmp( p, "oid.", 4 ) ) {
  894. p += 4;
  895. state = B4OIDATTRTYPE;
  896. break;
  897. }
  898. }
  899. }
  900. state = B4STRINGATTRTYPE;
  901. break;
  902. case B4OIDATTRTYPE: {
  903. int err = LDAP_SUCCESS;
  904. attrType.bv_val = ldap_int_parse_numericoid( &p, &err,
  905. LDAP_SCHEMA_SKIP);
  906. if ( err != LDAP_SUCCESS ) {
  907. goto parsing_error;
  908. }
  909. attrType.bv_len = p - attrType.bv_val;
  910. attrTypeEncoding = LDAP_AVA_BINARY;
  911. state = B4AVAEQUALS;
  912. break;
  913. }
  914. case B4STRINGATTRTYPE: {
  915. const char *startPos, *endPos = NULL;
  916. ber_len_t len;
  917. /*
  918. * the starting char has been found to be
  919. * a LDAP_DN_DESC_LEADCHAR so we don't re-check it
  920. * FIXME: DCE attr types seem to have a more
  921. * restrictive syntax (no '-' ...)
  922. */
  923. for ( startPos = p++; p[ 0 ]; p++ ) {
  924. if ( LDAP_DN_DESC_CHAR( p[ 0 ] ) ) {
  925. continue;
  926. }
  927. if ( LDAP_DN_LANG_SEP( p[ 0 ] ) ) {
  928. /*
  929. * RFC 4514 explicitly does not allow attribute
  930. * description options, such as language tags.
  931. */
  932. if ( flags & LDAP_DN_PEDANTIC ) {
  933. goto parsing_error;
  934. }
  935. /*
  936. * we trim ';' and following lang
  937. * and so from attribute types
  938. */
  939. endPos = p;
  940. for ( ; LDAP_DN_ATTRDESC_CHAR( p[ 0 ] )
  941. || LDAP_DN_LANG_SEP( p[ 0 ] ); p++ ) {
  942. /* no op */ ;
  943. }
  944. break;
  945. }
  946. break;
  947. }
  948. len = ( endPos ? endPos : p ) - startPos;
  949. if ( len == 0 ) {
  950. goto parsing_error;
  951. }
  952. attrTypeEncoding = LDAP_AVA_STRING;
  953. /*
  954. * here we need to decide whether to use it as is
  955. * or turn it in OID form; as a consequence, we
  956. * need to decide whether to binary encode the value
  957. */
  958. state = B4AVAEQUALS;
  959. if ( flags & LDAP_DN_SKIP ) {
  960. break;
  961. }
  962. attrType.bv_val = (char *)startPos;
  963. attrType.bv_len = len;
  964. break;
  965. }
  966. case B4AVAEQUALS:
  967. /* spaces may not be allowed */
  968. if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
  969. if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
  970. goto parsing_error;
  971. }
  972. /* trim spaces */
  973. for ( p++; LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
  974. /* no op */
  975. }
  976. }
  977. /* need equal sign */
  978. if ( !LDAP_DN_AVA_EQUALS( p[ 0 ] ) ) {
  979. goto parsing_error;
  980. }
  981. p++;
  982. /* spaces may not be allowed */
  983. if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
  984. if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
  985. goto parsing_error;
  986. }
  987. /* trim spaces */
  988. for ( p++; LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
  989. /* no op */
  990. }
  991. }
  992. /*
  993. * octothorpe means a BER encoded value will follow
  994. * FIXME: I don't think DCE will allow it
  995. */
  996. if ( LDAP_DN_OCTOTHORPE( p[ 0 ] ) ) {
  997. p++;
  998. attrValueEncoding = LDAP_AVA_BINARY;
  999. state = B4BINARYVALUE;
  1000. break;
  1001. }
  1002. /* STRING value expected */
  1003. /*
  1004. * if we're pedantic, an attribute type in OID form
  1005. * SHOULD imply a BER encoded attribute value; we
  1006. * should at least issue a warning
  1007. */
  1008. if ( ( flags & LDAP_DN_PEDANTIC )
  1009. && ( attrTypeEncoding == LDAP_AVA_BINARY ) ) {
  1010. /* OID attrType SHOULD use binary encoding */
  1011. goto parsing_error;
  1012. }
  1013. attrValueEncoding = LDAP_AVA_STRING;
  1014. /*
  1015. * LDAPv2 allows the attribute value to be quoted;
  1016. * also, IA5 values are expected, in principle
  1017. */
  1018. if ( LDAP_DN_LDAPV2( flags ) || LDAP_DN_LDAP( flags ) ) {
  1019. if ( LDAP_DN_QUOTES( p[ 0 ] ) ) {
  1020. p++;
  1021. state = B4IA5VALUEQUOTED;
  1022. break;
  1023. }
  1024. if ( LDAP_DN_LDAPV2( flags ) ) {
  1025. state = B4IA5VALUE;
  1026. break;
  1027. }
  1028. }
  1029. /*
  1030. * here STRING means RFC 4514 string
  1031. * FIXME: what about DCE strings?
  1032. */
  1033. if ( !p[ 0 ] ) {
  1034. /* empty value */
  1035. state = GOTAVA;
  1036. } else {
  1037. state = B4STRINGVALUE;
  1038. }
  1039. break;
  1040. case B4BINARYVALUE:
  1041. if ( hexstr2binval( p, &attrValue, &p, flags, ctx ) ) {
  1042. goto parsing_error;
  1043. }
  1044. state = GOTAVA;
  1045. break;
  1046. case B4STRINGVALUE:
  1047. switch ( LDAP_DN_FORMAT( flags ) ) {
  1048. case LDAP_DN_FORMAT_LDAP:
  1049. case LDAP_DN_FORMAT_LDAPV3:
  1050. if ( str2strval( p, stoplen - ( p - str ),
  1051. &attrValue, &p, flags,
  1052. &attrValueEncoding, ctx ) ) {
  1053. goto parsing_error;
  1054. }
  1055. break;
  1056. case LDAP_DN_FORMAT_DCE:
  1057. if ( DCE2strval( p, &attrValue, &p, flags, ctx ) ) {
  1058. goto parsing_error;
  1059. }
  1060. break;
  1061. default:
  1062. assert( 0 );
  1063. }
  1064. state = GOTAVA;
  1065. break;
  1066. case B4IA5VALUE:
  1067. if ( IA52strval( p, &attrValue, &p, flags, ctx ) ) {
  1068. goto parsing_error;
  1069. }
  1070. state = GOTAVA;
  1071. break;
  1072. case B4IA5VALUEQUOTED:
  1073. /* lead quote already stripped */
  1074. if ( quotedIA52strval( p, &attrValue,
  1075. &p, flags, ctx ) ) {
  1076. goto parsing_error;
  1077. }
  1078. state = GOTAVA;
  1079. break;
  1080. case GOTAVA: {
  1081. int rdnsep = 0;
  1082. if ( !( flags & LDAP_DN_SKIP ) ) {
  1083. LDAPAVA *ava;
  1084. /*
  1085. * we accept empty values
  1086. */
  1087. ava = ldapava_new( &attrType, &attrValue,
  1088. attrValueEncoding, ctx );
  1089. if ( ava == NULL ) {
  1090. rc = LDAP_NO_MEMORY;
  1091. goto parsing_error;
  1092. }
  1093. tmpRDN[navas++] = ava;
  1094. attrValue.bv_val = NULL;
  1095. attrValue.bv_len = 0;
  1096. /*
  1097. * prepare room for new AVAs if needed
  1098. */
  1099. if (navas == num_slots) {
  1100. LDAPAVA **tmp;
  1101. if ( tmpRDN == tmpRDN_ ) {
  1102. tmp = LDAP_MALLOCX( num_slots * 2 * sizeof( LDAPAVA * ), ctx );
  1103. if ( tmp == NULL ) {
  1104. rc = LDAP_NO_MEMORY;
  1105. goto parsing_error;
  1106. }
  1107. AC_MEMCPY( tmp, tmpRDN, num_slots * sizeof( LDAPAVA * ) );
  1108. } else {
  1109. tmp = LDAP_REALLOCX( tmpRDN, num_slots * 2 * sizeof( LDAPAVA * ), ctx );
  1110. if ( tmp == NULL ) {
  1111. rc = LDAP_NO_MEMORY;
  1112. goto parsing_error;
  1113. }
  1114. }
  1115. tmpRDN = tmp;
  1116. num_slots *= 2;
  1117. }
  1118. }
  1119. /*
  1120. * if we got an AVA separator ('+', or ',' for DCE )
  1121. * we expect a new AVA for this RDN; otherwise
  1122. * we add the RDN to the DN
  1123. */
  1124. switch ( LDAP_DN_FORMAT( flags ) ) {
  1125. case LDAP_DN_FORMAT_LDAP:
  1126. case LDAP_DN_FORMAT_LDAPV3:
  1127. case LDAP_DN_FORMAT_LDAPV2:
  1128. if ( !LDAP_DN_AVA_SEP( p[ 0 ] ) ) {
  1129. rdnsep = 1;
  1130. }
  1131. break;
  1132. case LDAP_DN_FORMAT_DCE:
  1133. if ( !LDAP_DN_AVA_SEP_DCE( p[ 0 ] ) ) {
  1134. rdnsep = 1;
  1135. }
  1136. break;
  1137. }
  1138. if ( rdnsep ) {
  1139. /*
  1140. * the RDN is over, phew
  1141. */
  1142. *n = p;
  1143. if ( !( flags & LDAP_DN_SKIP ) ) {
  1144. newRDN = (LDAPRDN)LDAP_MALLOCX(
  1145. sizeof(LDAPAVA) * (navas+1), ctx );
  1146. if ( newRDN == NULL ) {
  1147. rc = LDAP_NO_MEMORY;
  1148. goto parsing_error;
  1149. } else {
  1150. AC_MEMCPY( newRDN, tmpRDN, sizeof(LDAPAVA *) * navas);
  1151. newRDN[navas] = NULL;
  1152. }
  1153. }
  1154. rc = LDAP_SUCCESS;
  1155. goto return_result;
  1156. }
  1157. /* they should have been used in an AVA */
  1158. attrType.bv_val = NULL;
  1159. attrValue.bv_val = NULL;
  1160. p++;
  1161. state = B4AVA;
  1162. break;
  1163. }
  1164. default:
  1165. assert( 0 );
  1166. goto parsing_error;
  1167. }
  1168. }
  1169. *n = p;
  1170. parsing_error:;
  1171. /* They are set to NULL after they're used in an AVA */
  1172. if ( attrValue.bv_val ) {
  1173. LDAP_FREEX( attrValue.bv_val, ctx );
  1174. }
  1175. for ( navas-- ; navas >= 0; navas-- ) {
  1176. ldapava_free( tmpRDN[navas], ctx );
  1177. }
  1178. return_result:;
  1179. if ( tmpRDN != tmpRDN_ ) {
  1180. LDAP_FREEX( tmpRDN, ctx );
  1181. }
  1182. if ( rdn ) {
  1183. *rdn = newRDN;
  1184. }
  1185. return( rc );
  1186. }
  1187. /*
  1188. * reads in a UTF-8 string value, unescaping stuff:
  1189. * '\' + LDAP_DN_NEEDESCAPE(c) -> 'c'
  1190. * '\' + HEXPAIR(p) -> unhex(p)
  1191. */
  1192. static int
  1193. str2strval( const char *str, ber_len_t stoplen, struct berval *val, const char **next, unsigned flags, int *retFlags, void *ctx )
  1194. {
  1195. const char *p, *end, *startPos, *endPos = NULL;
  1196. ber_len_t len, escapes;
  1197. assert( str != NULL );
  1198. assert( val != NULL );
  1199. assert( next != NULL );
  1200. *next = NULL;
  1201. end = str + stoplen;
  1202. for ( startPos = p = str, escapes = 0; p < end; p++ ) {
  1203. if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
  1204. p++;
  1205. if ( p[ 0 ] == '\0' ) {
  1206. return( 1 );
  1207. }
  1208. if ( LDAP_DN_MAYESCAPE( p[ 0 ] ) ) {
  1209. escapes++;
  1210. continue;
  1211. }
  1212. if ( LDAP_DN_HEXPAIR( p ) ) {
  1213. char c;
  1214. hexstr2bin( p, &c );
  1215. escapes += 2;
  1216. if ( !LDAP_DN_ASCII_PRINTABLE( c ) ) {
  1217. /*
  1218. * we assume the string is UTF-8
  1219. */
  1220. *retFlags = LDAP_AVA_NONPRINTABLE;
  1221. }
  1222. p++;
  1223. continue;
  1224. }
  1225. if ( LDAP_DN_PEDANTIC & flags ) {
  1226. return( 1 );
  1227. }
  1228. /*
  1229. * we do not allow escaping
  1230. * of chars that don't need
  1231. * to and do not belong to
  1232. * HEXDIGITS
  1233. */
  1234. return( 1 );
  1235. } else if ( !LDAP_DN_ASCII_PRINTABLE( p[ 0 ] ) ) {
  1236. if ( p[ 0 ] == '\0' ) {
  1237. return( 1 );
  1238. }
  1239. *retFlags = LDAP_AVA_NONPRINTABLE;
  1240. } else if ( ( LDAP_DN_LDAP( flags ) && LDAP_DN_VALUE_END_V2( p[ 0 ] ) )
  1241. || ( LDAP_DN_LDAPV3( flags ) && LDAP_DN_VALUE_END( p[ 0 ] ) ) ) {
  1242. break;
  1243. } else if ( LDAP_DN_NEEDESCAPE( p[ 0 ] ) ) {
  1244. /*
  1245. * FIXME: maybe we can add
  1246. * escapes if not pedantic?
  1247. */
  1248. return( 1 );
  1249. }
  1250. }
  1251. /*
  1252. * we do allow unescaped spaces at the end
  1253. * of the value only in non-pedantic mode
  1254. */
  1255. if ( p > startPos + 1 && LDAP_DN_ASCII_SPACE( p[ -1 ] ) &&
  1256. !LDAP_DN_ESCAPE( p[ -2 ] ) ) {
  1257. if ( flags & LDAP_DN_PEDANTIC ) {
  1258. return( 1 );
  1259. }
  1260. /* strip trailing (unescaped) spaces */
  1261. for ( endPos = p - 1;
  1262. endPos > startPos + 1 &&
  1263. LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
  1264. !LDAP_DN_ESCAPE( endPos[ -2 ] );
  1265. endPos-- ) {
  1266. /* no op */
  1267. }
  1268. }
  1269. *next = p;
  1270. if ( flags & LDAP_DN_SKIP ) {
  1271. return( 0 );
  1272. }
  1273. /*
  1274. * FIXME: test memory?
  1275. */
  1276. len = ( endPos ? endPos : p ) - startPos - escapes;
  1277. val->bv_len = len;
  1278. if ( escapes == 0 ) {
  1279. if ( *retFlags & LDAP_AVA_NONPRINTABLE ) {
  1280. val->bv_val = LDAP_MALLOCX( len + 1, ctx );
  1281. if ( val->bv_val == NULL ) {
  1282. return( 1 );
  1283. }
  1284. AC_MEMCPY( val->bv_val, startPos, len );
  1285. val->bv_val[ len ] = '\0';
  1286. } else {
  1287. val->bv_val = LDAP_STRNDUPX( startPos, len, ctx );
  1288. }
  1289. } else {
  1290. ber_len_t s, d;
  1291. val->bv_val = LDAP_MALLOCX( len + 1, ctx );
  1292. if ( val->bv_val == NULL ) {
  1293. return( 1 );
  1294. }
  1295. for ( s = 0, d = 0; d < len; ) {
  1296. if ( LDAP_DN_ESCAPE( startPos[ s ] ) ) {
  1297. s++;
  1298. if ( LDAP_DN_MAYESCAPE( startPos[ s ] ) ) {
  1299. val->bv_val[ d++ ] =
  1300. startPos[ s++ ];
  1301. } else if ( LDAP_DN_HEXPAIR( &startPos[ s ] ) ) {
  1302. char c;
  1303. hexstr2bin( &startPos[ s ], &c );
  1304. val->bv_val[ d++ ] = c;
  1305. s += 2;
  1306. } else {
  1307. /* we should never get here */
  1308. assert( 0 );
  1309. }
  1310. } else {
  1311. val->bv_val[ d++ ] = startPos[ s++ ];
  1312. }
  1313. }
  1314. val->bv_val[ d ] = '\0';
  1315. assert( d == len );
  1316. }
  1317. return( 0 );
  1318. }
  1319. static int
  1320. DCE2strval( const char *str, struct berval *val, const char **next, unsigned flags, void *ctx )
  1321. {
  1322. const char *p, *startPos, *endPos = NULL;
  1323. ber_len_t len, escapes;
  1324. assert( str != NULL );
  1325. assert( val != NULL );
  1326. assert( next != NULL );
  1327. *next = NULL;
  1328. for ( startPos = p = str, escapes = 0; p[ 0 ]; p++ ) {
  1329. if ( LDAP_DN_ESCAPE_DCE( p[ 0 ] ) ) {
  1330. p++;
  1331. if ( LDAP_DN_NEEDESCAPE_DCE( p[ 0 ] ) ) {
  1332. escapes++;
  1333. } else {
  1334. return( 1 );
  1335. }
  1336. } else if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
  1337. break;
  1338. }
  1339. /*
  1340. * FIXME: can we accept anything else? I guess we need
  1341. * to stop if a value is not legal
  1342. */
  1343. }
  1344. /*
  1345. * (unescaped) trailing spaces are trimmed must be silently ignored;
  1346. * so we eat them
  1347. */
  1348. if ( p > startPos + 1 && LDAP_DN_ASCII_SPACE( p[ -1 ] ) &&
  1349. !LDAP_DN_ESCAPE( p[ -2 ] ) ) {
  1350. if ( flags & LDAP_DN_PEDANTIC ) {
  1351. return( 1 );
  1352. }
  1353. /* strip trailing (unescaped) spaces */
  1354. for ( endPos = p - 1;
  1355. endPos > startPos + 1 &&
  1356. LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
  1357. !LDAP_DN_ESCAPE( endPos[ -2 ] );
  1358. endPos-- ) {
  1359. /* no op */
  1360. }
  1361. }
  1362. *next = p;
  1363. if ( flags & LDAP_DN_SKIP ) {
  1364. return( 0 );
  1365. }
  1366. len = ( endPos ? endPos : p ) - startPos - escapes;
  1367. val->bv_len = len;
  1368. if ( escapes == 0 ){
  1369. val->bv_val = LDAP_STRNDUPX( startPos, len, ctx );
  1370. } else {
  1371. ber_len_t s, d;
  1372. val->bv_val = LDAP_MALLOCX( len + 1, ctx );
  1373. if ( val->bv_val == NULL ) {
  1374. return( 1 );
  1375. }
  1376. for ( s = 0, d = 0; d < len; ) {
  1377. /*
  1378. * This point is reached only if escapes
  1379. * are properly used, so all we need to
  1380. * do is eat them
  1381. */
  1382. if ( LDAP_DN_ESCAPE_DCE( startPos[ s ] ) ) {
  1383. s++;
  1384. }
  1385. val->bv_val[ d++ ] = startPos[ s++ ];
  1386. }
  1387. val->bv_val[ d ] = '\0';
  1388. assert( strlen( val->bv_val ) == len );
  1389. }
  1390. return( 0 );
  1391. }
  1392. static int
  1393. IA52strval( const char *str, struct berval *val, const char **next, unsigned flags, void *ctx )
  1394. {
  1395. const char *p, *startPos, *endPos = NULL;
  1396. ber_len_t len, escapes;
  1397. assert( str != NULL );
  1398. assert( val != NULL );
  1399. assert( next != NULL );
  1400. *next = NULL;
  1401. /*
  1402. * LDAPv2 (RFC 1779)
  1403. */
  1404. for ( startPos = p = str, escapes = 0; p[ 0 ]; p++ ) {
  1405. if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
  1406. p++;
  1407. if ( p[ 0 ] == '\0' ) {
  1408. return( 1 );
  1409. }
  1410. if ( !LDAP_DN_NEEDESCAPE( p[ 0 ] )
  1411. && ( LDAP_DN_PEDANTIC & flags ) ) {
  1412. return( 1 );
  1413. }
  1414. escapes++;
  1415. } else if ( LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) {
  1416. break;
  1417. }
  1418. /*
  1419. * FIXME: can we accept anything else? I guess we need
  1420. * to stop if a value is not legal
  1421. */
  1422. }
  1423. /* strip trailing (unescaped) spaces */
  1424. for ( endPos = p;
  1425. endPos > startPos + 1 &&
  1426. LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
  1427. !LDAP_DN_ESCAPE( endPos[ -2 ] );
  1428. endPos-- ) {
  1429. /* no op */
  1430. }
  1431. *next = p;
  1432. if ( flags & LDAP_DN_SKIP ) {
  1433. return( 0 );
  1434. }
  1435. len = ( endPos ? endPos : p ) - startPos - escapes;
  1436. val->bv_len = len;
  1437. if ( escapes == 0 ) {
  1438. val->bv_val = LDAP_STRNDUPX( startPos, len, ctx );
  1439. } else {
  1440. ber_len_t s, d;
  1441. val->bv_val = LDAP_MALLOCX( len + 1, ctx );
  1442. if ( val->bv_val == NULL ) {
  1443. return( 1 );
  1444. }
  1445. for ( s = 0, d = 0; d < len; ) {
  1446. if ( LDAP_DN_ESCAPE( startPos[ s ] ) ) {
  1447. s++;
  1448. }
  1449. val->bv_val[ d++ ] = startPos[ s++ ];
  1450. }
  1451. val->bv_val[ d ] = '\0';
  1452. assert( strlen( val->bv_val ) == len );
  1453. }
  1454. return( 0 );
  1455. }
  1456. static int
  1457. quotedIA52strval( const char *str, struct berval *val, const char **next, unsigned flags, void *ctx )
  1458. {
  1459. const char *p, *startPos, *endPos = NULL;
  1460. ber_len_t len;
  1461. unsigned escapes = 0;
  1462. assert( str != NULL );
  1463. assert( val != NULL );
  1464. assert( next != NULL );
  1465. *next = NULL;
  1466. /* initial quote already eaten */
  1467. for ( startPos = p = str; p[ 0 ]; p++ ) {
  1468. /*
  1469. * According to RFC 1779, the quoted value can
  1470. * contain escaped as well as unescaped special values;
  1471. * as a consequence we tolerate escaped values
  1472. * (e.g. '"\,"' -> '\,') and escape unescaped specials
  1473. * (e.g. '","' -> '\,').
  1474. */
  1475. if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
  1476. if ( p[ 1 ] == '\0' ) {
  1477. return( 1 );
  1478. }
  1479. p++;
  1480. if ( !LDAP_DN_V2_PAIR( p[ 0 ] )
  1481. && ( LDAP_DN_PEDANTIC & flags ) ) {
  1482. /*
  1483. * do we allow to escape normal chars?
  1484. * LDAPv2 does not allow any mechanism
  1485. * for escaping chars with '\' and hex
  1486. * pair
  1487. */
  1488. return( 1 );
  1489. }
  1490. escapes++;
  1491. } else if ( LDAP_DN_QUOTES( p[ 0 ] ) ) {
  1492. endPos = p;
  1493. /* eat closing quotes */
  1494. p++;
  1495. break;
  1496. }
  1497. /*
  1498. * FIXME: can we accept anything else? I guess we need
  1499. * to stop if a value is not legal
  1500. */
  1501. }
  1502. if ( endPos == NULL ) {
  1503. return( 1 );
  1504. }
  1505. /* Strip trailing (unescaped) spaces */
  1506. for ( ; p[ 0 ] && LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
  1507. /* no op */
  1508. }
  1509. *next = p;
  1510. if ( flags & LDAP_DN_SKIP ) {
  1511. return( 0 );
  1512. }
  1513. len = endPos - startPos - escapes;
  1514. assert( endPos >= startPos + escapes );
  1515. val->bv_len = len;
  1516. if ( escapes == 0 ) {
  1517. val->bv_val = LDAP_STRNDUPX( startPos, len, ctx );
  1518. } else {
  1519. ber_len_t s, d;
  1520. val->bv_val = LDAP_MALLOCX( len + 1, ctx );
  1521. if ( val->bv_val == NULL ) {
  1522. return( 1 );
  1523. }
  1524. val->bv_len = len;
  1525. for ( s = d = 0; d < len; ) {
  1526. if ( LDAP_DN_ESCAPE( str[ s ] ) ) {
  1527. s++;
  1528. }
  1529. val->bv_val[ d++ ] = str[ s++ ];
  1530. }
  1531. val->bv_val[ d ] = '\0';
  1532. assert( strlen( val->bv_val ) == len );
  1533. }
  1534. return( 0 );
  1535. }
  1536. static int
  1537. hexstr2bin( const char *str, char *c )
  1538. {
  1539. char c1, c2;
  1540. assert( str != NULL );
  1541. assert( c != NULL );
  1542. c1 = str[ 0 ];
  1543. c2 = str[ 1 ];
  1544. if ( LDAP_DN_ASCII_DIGIT( c1 ) ) {
  1545. *c = c1 - '0';
  1546. } else {
  1547. if ( LDAP_DN_ASCII_UCASE_HEXALPHA( c1 ) ) {
  1548. *c = c1 - 'A' + 10;
  1549. } else {
  1550. assert( LDAP_DN_ASCII_LCASE_HEXALPHA( c1 ) );
  1551. *c = c1 - 'a' + 10;
  1552. }
  1553. }
  1554. *c <<= 4;
  1555. if ( LDAP_DN_ASCII_DIGIT( c2 ) ) {
  1556. *c += c2 - '0';
  1557. } else {
  1558. if ( LDAP_DN_ASCII_UCASE_HEXALPHA( c2 ) ) {
  1559. *c += c2 - 'A' + 10;
  1560. } else {
  1561. assert( LDAP_DN_ASCII_LCASE_HEXALPHA( c2 ) );
  1562. *c += c2 - 'a' + 10;
  1563. }
  1564. }
  1565. return( 0 );
  1566. }
  1567. static int
  1568. hexstr2binval( const char *str, struct berval *val, const char **next, unsigned flags, void *ctx )
  1569. {
  1570. const char *p, *startPos, *endPos = NULL;
  1571. ber_len_t len;
  1572. ber_len_t s, d;
  1573. assert( str != NULL );
  1574. assert( val != NULL );
  1575. assert( next != NULL );
  1576. *next = NULL;
  1577. for ( startPos = p = str; p[ 0 ]; p += 2 ) {
  1578. switch ( LDAP_DN_FORMAT( flags ) ) {
  1579. case LDAP_DN_FORMAT_LDAPV3:
  1580. if ( LDAP_DN_VALUE_END( p[ 0 ] ) ) {
  1581. goto end_of_value;
  1582. }
  1583. break;
  1584. case LDAP_DN_FORMAT_LDAP:
  1585. case LDAP_DN_FORMAT_LDAPV2:
  1586. if ( LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) {
  1587. goto end_of_value;
  1588. }
  1589. break;
  1590. case LDAP_DN_FORMAT_DCE:
  1591. if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
  1592. goto end_of_value;
  1593. }
  1594. break;
  1595. }
  1596. if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
  1597. if ( flags & LDAP_DN_PEDANTIC ) {
  1598. return( 1 );
  1599. }
  1600. endPos = p;
  1601. for ( ; p[ 0 ]; p++ ) {
  1602. switch ( LDAP_DN_FORMAT( flags ) ) {
  1603. case LDAP_DN_FORMAT_LDAPV3:
  1604. if ( LDAP_DN_VALUE_END( p[ 0 ] ) ) {
  1605. goto end_of_value;
  1606. }
  1607. break;
  1608. case LDAP_DN_FORMAT_LDAP:
  1609. case LDAP_DN_FORMAT_LDAPV2:
  1610. if ( LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) {
  1611. goto end_of_value;
  1612. }
  1613. break;
  1614. case LDAP_DN_FORMAT_DCE:
  1615. if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
  1616. goto end_of_value;
  1617. }
  1618. break;
  1619. }
  1620. }
  1621. break;
  1622. }
  1623. if ( !LDAP_DN_HEXPAIR( p ) ) {
  1624. return( 1 );
  1625. }
  1626. }
  1627. end_of_value:;
  1628. *next = p;
  1629. if ( flags & LDAP_DN_SKIP ) {
  1630. return( 0 );
  1631. }
  1632. len = ( ( endPos ? endPos : p ) - startPos ) / 2;
  1633. /* must be even! */
  1634. assert( 2 * len == (ber_len_t) (( endPos ? endPos : p ) - startPos ));
  1635. val->bv_len = len;
  1636. val->bv_val = LDAP_MALLOCX( len + 1, ctx );
  1637. if ( val->bv_val == NULL ) {
  1638. return( LDAP_NO_MEMORY );
  1639. }
  1640. for ( s = 0, d = 0; d < len; s += 2, d++ ) {
  1641. char c;
  1642. hexstr2bin( &startPos[ s ], &c );
  1643. val->bv_val[ d ] = c;
  1644. }
  1645. val->bv_val[ d ] = '\0';
  1646. return( 0 );
  1647. }
  1648. /*
  1649. * convert a byte in a hexadecimal pair
  1650. */
  1651. static int
  1652. byte2hexpair( const char *val, char *pair )
  1653. {
  1654. static const char hexdig[] = "0123456789ABCDEF";
  1655. assert( val != NULL );
  1656. assert( pair != NULL );
  1657. /*
  1658. * we assume the string has enough room for the hex encoding
  1659. * of the value
  1660. */
  1661. pair[ 0 ] = hexdig[ 0x0f & ( val[ 0 ] >> 4 ) ];
  1662. pair[ 1 ] = hexdig[ 0x0f & val[ 0 ] ];
  1663. return( 0 );
  1664. }
  1665. /*
  1666. * convert a binary value in hexadecimal pairs
  1667. */
  1668. static int
  1669. binval2hexstr( struct berval *val, char *str )
  1670. {
  1671. ber_len_t s, d;
  1672. assert( val != NULL );
  1673. assert( str != NULL );
  1674. if ( val->bv_len == 0 ) {
  1675. return( 0 );
  1676. }
  1677. /*
  1678. * we assume the string has enough room for the hex encoding
  1679. * of the value
  1680. */
  1681. for ( s = 0, d = 0; s < val->bv_len; s++, d += 2 ) {
  1682. byte2hexpair( &val->bv_val[ s ], &str[ d ] );
  1683. }
  1684. return( 0 );
  1685. }
  1686. /*
  1687. * Length of the string representation, accounting for escaped hex
  1688. * of UTF-8 chars
  1689. */
  1690. static int
  1691. strval2strlen( struct berval *val, unsigned flags, ber_len_t *len )
  1692. {
  1693. ber_len_t l, cl = 1;
  1694. char *p, *end;
  1695. int escaped_byte_len = LDAP_DN_IS_PRETTY( flags ) ? 1 : 3;
  1696. #ifdef PRETTY_ESCAPE
  1697. int escaped_ascii_len = LDAP_DN_IS_PRETTY( flags ) ? 2 : 3;
  1698. #endif /* PRETTY_ESCAPE */
  1699. assert( val != NULL );
  1700. assert( len != NULL );
  1701. *len = 0;
  1702. if ( val->bv_len == 0 ) {
  1703. return( 0 );
  1704. }
  1705. end = val->bv_val + val->bv_len - 1;
  1706. for ( l = 0, p = val->bv_val; p <= end; p += cl ) {
  1707. /*
  1708. * escape '%x00'
  1709. */
  1710. if ( p[ 0 ] == '\0' ) {
  1711. cl = 1;
  1712. l += 3;
  1713. continue;
  1714. }
  1715. cl = LDAP_UTF8_CHARLEN2( p, cl );
  1716. if ( cl == 0 ) {
  1717. /* illegal utf-8 char! */
  1718. return( -1 );
  1719. } else if ( cl > 1 ) {
  1720. ber_len_t cnt;
  1721. for ( cnt = 1; cnt < cl; cnt++ ) {
  1722. if ( ( p[ cnt ] & 0xc0 ) != 0x80 ) {
  1723. return( -1 );
  1724. }
  1725. }
  1726. l += escaped_byte_len * cl;
  1727. } else if ( LDAP_DN_NEEDESCAPE( p[ 0 ] )
  1728. || LDAP_DN_SHOULDESCAPE( p[ 0 ] )
  1729. || ( p == val->bv_val && LDAP_DN_NEEDESCAPE_LEAD( p[ 0 ] ) )
  1730. || ( p == end && LDAP_DN_NEEDESCAPE_TRAIL( p[ 0 ] ) ) ) {
  1731. #ifdef PRETTY_ESCAPE
  1732. #if 0
  1733. if ( LDAP_DN_WILLESCAPE_HEX( flags, p[ 0 ] ) ) {
  1734. #else
  1735. if ( LDAP_DN_WILLESCAPE_CHAR( p[ 0 ] ) ) {
  1736. #endif
  1737. /*
  1738. * there might be some chars we want
  1739. * to escape in form of a couple
  1740. * of hexdigits for optimization purposes
  1741. */
  1742. l += 3;
  1743. } else {
  1744. l += escaped_ascii_len;
  1745. }
  1746. #else /* ! PRETTY_ESCAPE */
  1747. l += 3;
  1748. #endif /* ! PRETTY_ESCAPE */
  1749. } else {
  1750. l++;
  1751. }
  1752. }
  1753. *len = l;
  1754. return( 0 );
  1755. }
  1756. /*
  1757. * convert to string representation, escaping with hex the UTF-8 stuff;
  1758. * assume the destination has enough room for escaping
  1759. */
  1760. static int
  1761. strval2str( struct berval *val, char *str, unsigned flags, ber_len_t *len )
  1762. {
  1763. ber_len_t s, d, end;
  1764. assert( val != NULL );
  1765. assert( str != NULL );
  1766. assert( len != NULL );
  1767. if ( val->bv_len == 0 ) {
  1768. *len = 0;
  1769. return( 0 );
  1770. }
  1771. /*
  1772. * we assume the string has enough room for the hex encoding
  1773. * of the value
  1774. */
  1775. for ( s = 0, d = 0, end = val->bv_len - 1; s < val->bv_len; ) {
  1776. ber_len_t cl;
  1777. /*
  1778. * escape '%x00'
  1779. */
  1780. if ( val->bv_val[ s ] == '\0' ) {
  1781. cl = 1;
  1782. str[ d++ ] = '\\';
  1783. str[ d++ ] = '0';
  1784. str[ d++ ] = '0';
  1785. s++;
  1786. continue;
  1787. }
  1788. /*
  1789. * The length was checked in strval2strlen();
  1790. */
  1791. cl = LDAP_UTF8_CHARLEN( &val->bv_val[ s ] );
  1792. /*
  1793. * there might be some chars we want to escape in form
  1794. * of a couple of hexdigits for optimization purposes
  1795. */
  1796. if ( ( cl > 1 && !LDAP_DN_IS_PRETTY( flags ) )
  1797. #ifdef PRETTY_ESCAPE
  1798. #if 0
  1799. || LDAP_DN_WILLESCAPE_HEX( flags, val->bv_val[ s ] )
  1800. #else
  1801. || LDAP_DN_WILLESCAPE_CHAR( val->bv_val[ s ] )
  1802. #endif
  1803. #else /* ! PRETTY_ESCAPE */
  1804. || LDAP_DN_NEEDESCAPE( val->bv_val[ s ] )
  1805. || LDAP_DN_SHOULDESCAPE( val->bv_val[ s ] )
  1806. || ( d == 0 && LDAP_DN_NEEDESCAPE_LEAD( val->bv_val[ s ] ) )
  1807. || ( s == end && LDAP_DN_NEEDESCAPE_TRAIL( val->bv_val[ s ] ) )
  1808. #endif /* ! PRETTY_ESCAPE */
  1809. ) {
  1810. for ( ; cl--; ) {
  1811. str[ d++ ] = '\\';
  1812. byte2hexpair( &val->bv_val[ s ], &str[ d ] );
  1813. s++;
  1814. d += 2;
  1815. }
  1816. } else if ( cl > 1 ) {
  1817. for ( ; cl--; ) {
  1818. str[ d++ ] = val->bv_val[ s++ ];
  1819. }
  1820. } else {
  1821. #ifdef PRETTY_ESCAPE
  1822. if ( LDAP_DN_NEEDESCAPE( val->bv_val[ s ] )
  1823. || LDAP_DN_SHOULDESCAPE( val->bv_val[ s ] )
  1824. || ( d == 0 && LDAP_DN_NEEDESCAPE_LEAD( val->bv_val[ s ] ) )
  1825. || ( s == end && LDAP_DN_NEEDESCAPE_TRAIL( val->bv_val[ s ] ) ) ) {
  1826. str[ d++ ] = '\\';
  1827. if ( !LDAP_DN_IS_PRETTY( flags ) ) {
  1828. byte2hexpair( &val->bv_val[ s ], &str[ d ] );
  1829. s++;
  1830. d += 2;
  1831. continue;
  1832. }
  1833. }
  1834. #endif /* PRETTY_ESCAPE */
  1835. str[ d++ ] = val->bv_val[ s++ ];
  1836. }
  1837. }
  1838. *len = d;
  1839. return( 0 );
  1840. }
  1841. /*
  1842. * Length of the IA5 string representation (no UTF-8 allowed)
  1843. */
  1844. static int
  1845. strval2IA5strlen( struct berval *val, unsigned flags, ber_len_t *len )
  1846. {
  1847. ber_len_t l;
  1848. char *p;
  1849. assert( val != NULL );
  1850. assert( len != NULL );
  1851. *len = 0;
  1852. if ( val->bv_len == 0 ) {
  1853. return( 0 );
  1854. }
  1855. if ( flags & LDAP_AVA_NONPRINTABLE ) {
  1856. /*
  1857. * Turn value into a binary encoded BER
  1858. */
  1859. return( -1 );
  1860. } else {
  1861. for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
  1862. if ( LDAP_DN_NEEDESCAPE( p[ 0 ] )
  1863. || LDAP_DN_SHOULDESCAPE( p[ 0 ] )
  1864. || ( p == val->bv_val && LDAP_DN_NEEDESCAPE_LEAD( p[ 0 ] ) )
  1865. || ( !p[ 1 ] && LDAP_DN_NEEDESCAPE_TRAIL( p[ 0 ] ) ) ) {
  1866. l += 2;
  1867. } else {
  1868. l++;
  1869. }
  1870. }
  1871. }
  1872. *len = l;
  1873. return( 0 );
  1874. }
  1875. /*
  1876. * convert to string representation (np UTF-8)
  1877. * assume the destination has enough room for escaping
  1878. */
  1879. static int
  1880. strval2IA5str( struct berval *val, char *str, unsigned flags, ber_len_t *len )
  1881. {
  1882. ber_len_t s, d, end;
  1883. assert( val != NULL );
  1884. assert( str != NULL );
  1885. assert( len != NULL );
  1886. if ( val->bv_len == 0 ) {
  1887. *len = 0;
  1888. return( 0 );
  1889. }
  1890. if ( flags & LDAP_AVA_NONPRINTABLE ) {
  1891. /*
  1892. * Turn value into a binary encoded BER
  1893. */
  1894. *len = 0;
  1895. return( -1 );
  1896. } else {
  1897. /*
  1898. * we assume the string has enough room for the hex encoding
  1899. * of the value
  1900. */
  1901. for ( s = 0, d = 0, end = val->bv_len - 1; s < val->bv_len; ) {
  1902. if ( LDAP_DN_NEEDESCAPE( val->bv_val[ s ] )
  1903. || LDAP_DN_SHOULDESCAPE( val->bv_val[ s ] )
  1904. || ( s == 0 && LDAP_DN_NEEDESCAPE_LEAD( val->bv_val[ s ] ) )
  1905. || ( s == end && LDAP_DN_NEEDESCAPE_TRAIL( val->bv_val[ s ] ) ) ) {
  1906. str[ d++ ] = '\\';
  1907. }
  1908. str[ d++ ] = val->bv_val[ s++ ];
  1909. }
  1910. }
  1911. *len = d;
  1912. return( 0 );
  1913. }
  1914. /*
  1915. * Length of the (supposedly) DCE string representation,
  1916. * accounting for escaped hex of UTF-8 chars
  1917. */
  1918. static int
  1919. strval2DCEstrlen( struct berval *val, unsigned flags, ber_len_t *len )
  1920. {
  1921. ber_len_t l;
  1922. char *p;
  1923. assert( val != NULL );
  1924. assert( len != NULL );
  1925. *len = 0;
  1926. if ( val->bv_len == 0 ) {
  1927. return( 0 );
  1928. }
  1929. if ( flags & LDAP_AVA_NONPRINTABLE ) {
  1930. /*
  1931. * FIXME: Turn the value into a binary encoded BER?
  1932. */
  1933. return( -1 );
  1934. } else {
  1935. for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
  1936. if ( LDAP_DN_NEEDESCAPE_DCE( p[ 0 ] ) ) {
  1937. l += 2;
  1938. } else {
  1939. l++;
  1940. }
  1941. }
  1942. }
  1943. *len = l;
  1944. return( 0 );
  1945. }
  1946. /*
  1947. * convert to (supposedly) DCE string representation,
  1948. * escaping with hex the UTF-8 stuff;
  1949. * assume the destination has enough room for escaping
  1950. */
  1951. static int
  1952. strval2DCEstr( struct berval *val, char *str, unsigned flags, ber_len_t *len )
  1953. {
  1954. ber_len_t s, d;
  1955. assert( val != NULL );
  1956. assert( str != NULL );
  1957. assert( len != NULL );
  1958. if ( val->bv_len == 0 ) {
  1959. *len = 0;
  1960. return( 0 );
  1961. }
  1962. if ( flags & LDAP_AVA_NONPRINTABLE ) {
  1963. /*
  1964. * FIXME: Turn the value into a binary encoded BER?
  1965. */
  1966. *len = 0;
  1967. return( -1 );
  1968. } else {
  1969. /*
  1970. * we assume the string has enough room for the hex encoding
  1971. * of the value
  1972. */
  1973. for ( s = 0, d = 0; s < val->bv_len; ) {
  1974. if ( LDAP_DN_NEEDESCAPE_DCE( val->bv_val[ s ] ) ) {
  1975. str[ d++ ] = '\\';
  1976. }
  1977. str[ d++ ] = val->bv_val[ s++ ];
  1978. }
  1979. }
  1980. *len = d;
  1981. return( 0 );
  1982. }
  1983. /*
  1984. * Length of the (supposedly) AD canonical string representation,
  1985. * accounting for chars that need to be escaped
  1986. */
  1987. static int
  1988. strval2ADstrlen( struct berval *val, unsigned flags, ber_len_t *len )
  1989. {
  1990. ber_len_t l, cl;
  1991. char *p;
  1992. assert( val != NULL );
  1993. assert( len != NULL );
  1994. *len = 0;
  1995. if ( val->bv_len == 0 ) {
  1996. return( 0 );
  1997. }
  1998. for ( l = 0, p = val->bv_val; p[ 0 ]; p += cl ) {
  1999. cl = LDAP_UTF8_CHARLEN2( p, cl );
  2000. if ( cl == 0 ) {
  2001. /* illegal utf-8 char */
  2002. return -1;
  2003. } else if ( (cl == 1) && LDAP_DN_NEEDESCAPE_AD( p[ 0 ] ) ) {
  2004. l += 2;
  2005. } else {
  2006. l += cl;
  2007. }
  2008. }
  2009. *len = l;
  2010. return( 0 );
  2011. }
  2012. /*
  2013. * convert to (supposedly) AD string representation,
  2014. * assume the destination has enough room for escaping
  2015. */
  2016. static int
  2017. strval2ADstr( struct berval *val, char *str, unsigned flags, ber_len_t *len )
  2018. {
  2019. ber_len_t s, d, cl;
  2020. assert( val != NULL );
  2021. assert( str != NULL );
  2022. assert( len != NULL );
  2023. if ( val->bv_len == 0 ) {
  2024. *len = 0;
  2025. return( 0 );
  2026. }
  2027. /*
  2028. * we assume the string has enough room for the escaping
  2029. * of the value
  2030. */
  2031. for ( s = 0, d = 0; s < val->bv_len; ) {
  2032. cl = LDAP_UTF8_CHARLEN2( val->bv_val+s, cl );
  2033. if ( cl == 0 ) {
  2034. /* illegal utf-8 char */
  2035. return -1;
  2036. } else if ( (cl == 1) && LDAP_DN_NEEDESCAPE_AD(val->bv_val[ s ]) ) {
  2037. str[ d++ ] = '\\';
  2038. }
  2039. for (; cl--;) {
  2040. str[ d++ ] = val->bv_val[ s++ ];
  2041. }
  2042. }
  2043. *len = d;
  2044. return( 0 );
  2045. }
  2046. /*
  2047. * If the DN is terminated by single-AVA RDNs with attribute type of "dc",
  2048. * the first part of the AD representation of the DN is written in DNS
  2049. * form, i.e. dot separated domain name components (as suggested
  2050. * by Luke Howard, http://www.padl.com/~lukeh)
  2051. */
  2052. static int
  2053. dn2domain( LDAPDN dn, struct berval *bv, int pos, int *iRDN )
  2054. {
  2055. int i;
  2056. int domain = 0, first = 1;
  2057. ber_len_t l = 1; /* we move the null also */
  2058. char *str;
  2059. /* we are guaranteed there's enough memory in str */
  2060. /* sanity */
  2061. assert( dn != NULL );
  2062. assert( bv != NULL );
  2063. assert( iRDN != NULL );
  2064. assert( *iRDN >= 0 );
  2065. str = bv->bv_val + pos;
  2066. for ( i = *iRDN; i >= 0; i-- ) {
  2067. LDAPRDN rdn;
  2068. LDAPAVA *ava;
  2069. assert( dn[ i ] != NULL );
  2070. rdn = dn[ i ];
  2071. assert( rdn[ 0 ] != NULL );
  2072. ava = rdn[ 0 ];
  2073. if ( !LDAP_DN_IS_RDN_DC( rdn ) ) {
  2074. break;
  2075. }
  2076. if ( ldif_is_not_printable( ava->la_value.bv_val, ava->la_value.bv_len ) ) {
  2077. domain = 0;
  2078. break;
  2079. }
  2080. domain = 1;
  2081. if ( first ) {
  2082. first = 0;
  2083. AC_MEMCPY( str, ava->la_value.bv_val,
  2084. ava->la_value.bv_len + 1);
  2085. l += ava->la_value.bv_len;
  2086. } else {
  2087. AC_MEMCPY( str + ava->la_value.bv_len + 1, bv->bv_val + pos, l);
  2088. AC_MEMCPY( str, ava->la_value.bv_val,
  2089. ava->la_value.bv_len );
  2090. str[ ava->la_value.bv_len ] = '.';
  2091. l += ava->la_value.bv_len + 1;
  2092. }
  2093. }
  2094. *iRDN = i;
  2095. bv->bv_len = pos + l - 1;
  2096. return( domain );
  2097. }
  2098. static int
  2099. rdn2strlen( LDAPRDN rdn, unsigned flags, ber_len_t *len,
  2100. int ( *s2l )( struct berval *v, unsigned f, ber_len_t *l ) )
  2101. {
  2102. int iAVA;
  2103. ber_len_t l = 0;
  2104. *len = 0;
  2105. for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
  2106. LDAPAVA *ava = rdn[ iAVA ];
  2107. /* len(type) + '=' + '+' | ',' */
  2108. l += ava->la_attr.bv_len + 2;
  2109. if ( ava->la_flags & LDAP_AVA_BINARY ) {
  2110. /* octothorpe + twice the length */
  2111. l += 1 + 2 * ava->la_value.bv_len;
  2112. } else {
  2113. ber_len_t vl;
  2114. unsigned f = flags | ava->la_flags;
  2115. if ( ( *s2l )( &ava->la_value, f, &vl ) ) {
  2116. return( -1 );
  2117. }
  2118. l += vl;
  2119. }
  2120. }
  2121. *len = l;
  2122. return( 0 );
  2123. }
  2124. static int
  2125. rdn2str( LDAPRDN rdn, char *str, unsigned flags, ber_len_t *len,
  2126. int ( *s2s ) ( struct berval *v, char * s, unsigned f, ber_len_t *l ) )
  2127. {
  2128. int iAVA;
  2129. ber_len_t l = 0;
  2130. for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
  2131. LDAPAVA *ava = rdn[ iAVA ];
  2132. AC_MEMCPY( &str[ l ], ava->la_attr.bv_val,
  2133. ava->la_attr.bv_len );
  2134. l += ava->la_attr.bv_len;
  2135. str[ l++ ] = '=';
  2136. if ( ava->la_flags & LDAP_AVA_BINARY ) {
  2137. str[ l++ ] = '#';
  2138. if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
  2139. return( -1 );
  2140. }
  2141. l += 2 * ava->la_value.bv_len;
  2142. } else {
  2143. ber_len_t vl;
  2144. unsigned f = flags | ava->la_flags;
  2145. if ( ( *s2s )( &ava->la_value, &str[ l ], f, &vl ) ) {
  2146. return( -1 );
  2147. }
  2148. l += vl;
  2149. }
  2150. str[ l++ ] = ( rdn[ iAVA + 1] ? '+' : ',' );
  2151. }
  2152. *len = l;
  2153. return( 0 );
  2154. }
  2155. static int
  2156. rdn2DCEstrlen( LDAPRDN rdn, unsigned flags, ber_len_t *len )
  2157. {
  2158. int iAVA;
  2159. ber_len_t l = 0;
  2160. *len = 0;
  2161. for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
  2162. LDAPAVA *ava = rdn[ iAVA ];
  2163. /* len(type) + '=' + ',' | '/' */
  2164. l += ava->la_attr.bv_len + 2;
  2165. if ( ava->la_flags & LDAP_AVA_BINARY ) {
  2166. /* octothorpe + twice the length */
  2167. l += 1 + 2 * ava->la_value.bv_len;
  2168. } else {
  2169. ber_len_t vl;
  2170. unsigned f = flags | ava->la_flags;
  2171. if ( strval2DCEstrlen( &ava->la_value, f, &vl ) ) {
  2172. return( -1 );
  2173. }
  2174. l += vl;
  2175. }
  2176. }
  2177. *len = l;
  2178. return( 0 );
  2179. }
  2180. static int
  2181. rdn2DCEstr( LDAPRDN rdn, char *str, unsigned flags, ber_len_t *len, int first )
  2182. {
  2183. int iAVA;
  2184. ber_len_t l = 0;
  2185. for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
  2186. LDAPAVA *ava = rdn[ iAVA ];
  2187. if ( first ) {
  2188. first = 0;
  2189. } else {
  2190. str[ l++ ] = ( iAVA ? ',' : '/' );
  2191. }
  2192. AC_MEMCPY( &str[ l ], ava->la_attr.bv_val,
  2193. ava->la_attr.bv_len );
  2194. l += ava->la_attr.bv_len;
  2195. str[ l++ ] = '=';
  2196. if ( ava->la_flags & LDAP_AVA_BINARY ) {
  2197. str[ l++ ] = '#';
  2198. if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
  2199. return( -1 );
  2200. }
  2201. l += 2 * ava->la_value.bv_len;
  2202. } else {
  2203. ber_len_t vl;
  2204. unsigned f = flags | ava->la_flags;
  2205. if ( strval2DCEstr( &ava->la_value, &str[ l ], f, &vl ) ) {
  2206. return( -1 );
  2207. }
  2208. l += vl;
  2209. }
  2210. }
  2211. *len = l;
  2212. return( 0 );
  2213. }
  2214. static int
  2215. rdn2UFNstrlen( LDAPRDN rdn, unsigned flags, ber_len_t *len )
  2216. {
  2217. int iAVA;
  2218. ber_len_t l = 0;
  2219. assert( rdn != NULL );
  2220. assert( len != NULL );
  2221. *len = 0;
  2222. for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
  2223. LDAPAVA *ava = rdn[ iAVA ];
  2224. /* ' + ' | ', ' */
  2225. l += ( rdn[ iAVA + 1 ] ? 3 : 2 );
  2226. /* FIXME: are binary values allowed in UFN? */
  2227. if ( ava->la_flags & LDAP_AVA_BINARY ) {
  2228. /* octothorpe + twice the value */
  2229. l += 1 + 2 * ava->la_value.bv_len;
  2230. } else {
  2231. ber_len_t vl;
  2232. unsigned f = flags | ava->la_flags;
  2233. if ( strval2strlen( &ava->la_value, f, &vl ) ) {
  2234. return( -1 );
  2235. }
  2236. l += vl;
  2237. }
  2238. }
  2239. *len = l;
  2240. return( 0 );
  2241. }
  2242. static int
  2243. rdn2UFNstr( LDAPRDN rdn, char *str, unsigned flags, ber_len_t *len )
  2244. {
  2245. int iAVA;
  2246. ber_len_t l = 0;
  2247. for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
  2248. LDAPAVA *ava = rdn[ iAVA ];
  2249. if ( ava->la_flags & LDAP_AVA_BINARY ) {
  2250. str[ l++ ] = '#';
  2251. if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
  2252. return( -1 );
  2253. }
  2254. l += 2 * ava->la_value.bv_len;
  2255. } else {
  2256. ber_len_t vl;
  2257. unsigned f = flags | ava->la_flags;
  2258. if ( strval2str( &ava->la_value, &str[ l ], f, &vl ) ) {
  2259. return( -1 );
  2260. }
  2261. l += vl;
  2262. }
  2263. if ( rdn[ iAVA + 1 ] ) {
  2264. AC_MEMCPY( &str[ l ], " + ", 3 );
  2265. l += 3;
  2266. } else {
  2267. AC_MEMCPY( &str[ l ], ", ", 2 );
  2268. l += 2;
  2269. }
  2270. }
  2271. *len = l;
  2272. return( 0 );
  2273. }
  2274. static int
  2275. rdn2ADstrlen( LDAPRDN rdn, unsigned flags, ber_len_t *len )
  2276. {
  2277. int iAVA;
  2278. ber_len_t l = 0;
  2279. assert( rdn != NULL );
  2280. assert( len != NULL );
  2281. *len = 0;
  2282. for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
  2283. LDAPAVA *ava = rdn[ iAVA ];
  2284. /* ',' | '/' */
  2285. l++;
  2286. /* FIXME: are binary values allowed in UFN? */
  2287. if ( ava->la_flags & LDAP_AVA_BINARY ) {
  2288. /* octothorpe + twice the value */
  2289. l += 1 + 2 * ava->la_value.bv_len;
  2290. } else {
  2291. ber_len_t vl;
  2292. unsigned f = flags | ava->la_flags;
  2293. if ( strval2ADstrlen( &ava->la_value, f, &vl ) ) {
  2294. return( -1 );
  2295. }
  2296. l += vl;
  2297. }
  2298. }
  2299. *len = l;
  2300. return( 0 );
  2301. }
  2302. static int
  2303. rdn2ADstr( LDAPRDN rdn, char *str, unsigned flags, ber_len_t *len, int first )
  2304. {
  2305. int iAVA;
  2306. ber_len_t l = 0;
  2307. for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
  2308. LDAPAVA *ava = rdn[ iAVA ];
  2309. if ( first ) {
  2310. first = 0;
  2311. } else {
  2312. str[ l++ ] = ( iAVA ? ',' : '/' );
  2313. }
  2314. if ( ava->la_flags & LDAP_AVA_BINARY ) {
  2315. str[ l++ ] = '#';
  2316. if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
  2317. return( -1 );
  2318. }
  2319. l += 2 * ava->la_value.bv_len;
  2320. } else {
  2321. ber_len_t vl;
  2322. unsigned f = flags | ava->la_flags;
  2323. if ( strval2ADstr( &ava->la_value, &str[ l ], f, &vl ) ) {
  2324. return( -1 );
  2325. }
  2326. l += vl;
  2327. }
  2328. }
  2329. *len = l;
  2330. return( 0 );
  2331. }
  2332. /*
  2333. * ldap_rdn2str
  2334. *
  2335. * Returns in str a string representation of rdn based on flags.
  2336. * There is some duplication of code between this and ldap_dn2str;
  2337. * this is wanted to reduce the allocation of temporary buffers.
  2338. */
  2339. int
  2340. ldap_rdn2str( LDAPRDN rdn, char **str, unsigned flags )
  2341. {
  2342. struct berval bv;
  2343. int rc;
  2344. assert( str != NULL );
  2345. if((flags & LDAP_DN_FORMAT_MASK) == LDAP_DN_FORMAT_LBER) {
  2346. return LDAP_PARAM_ERROR;
  2347. }
  2348. rc = ldap_rdn2bv_x( rdn, &bv, flags, NULL );
  2349. *str = bv.bv_val;
  2350. return rc;
  2351. }
  2352. int
  2353. ldap_rdn2bv( LDAPRDN rdn, struct berval *bv, unsigned flags )
  2354. {
  2355. return ldap_rdn2bv_x( rdn, bv, flags, NULL );
  2356. }
  2357. int
  2358. ldap_rdn2bv_x( LDAPRDN rdn, struct berval *bv, unsigned flags, void *ctx )
  2359. {
  2360. int rc, back;
  2361. ber_len_t l;
  2362. assert( bv != NULL );
  2363. bv->bv_len = 0;
  2364. bv->bv_val = NULL;
  2365. if ( rdn == NULL ) {
  2366. bv->bv_val = LDAP_STRDUPX( "", ctx );
  2367. return( LDAP_SUCCESS );
  2368. }
  2369. /*
  2370. * This routine wastes "back" bytes at the end of the string
  2371. */
  2372. switch ( LDAP_DN_FORMAT( flags ) ) {
  2373. case LDAP_DN_FORMAT_LDAPV3:
  2374. if ( rdn2strlen( rdn, flags, &l, strval2strlen ) ) {
  2375. return LDAP_DECODING_ERROR;
  2376. }
  2377. break;
  2378. case LDAP_DN_FORMAT_LDAPV2:
  2379. if ( rdn2strlen( rdn, flags, &l, strval2IA5strlen ) ) {
  2380. return LDAP_DECODING_ERROR;
  2381. }
  2382. break;
  2383. case LDAP_DN_FORMAT_UFN:
  2384. if ( rdn2UFNstrlen( rdn, flags, &l ) ) {
  2385. return LDAP_DECODING_ERROR;
  2386. }
  2387. break;
  2388. case LDAP_DN_FORMAT_DCE:
  2389. if ( rdn2DCEstrlen( rdn, flags, &l ) ) {
  2390. return LDAP_DECODING_ERROR;
  2391. }
  2392. break;
  2393. case LDAP_DN_FORMAT_AD_CANONICAL:
  2394. if ( rdn2ADstrlen( rdn, flags, &l ) ) {
  2395. return LDAP_DECODING_ERROR;
  2396. }
  2397. break;
  2398. default:
  2399. return LDAP_PARAM_ERROR;
  2400. }
  2401. bv->bv_val = LDAP_MALLOCX( l + 1, ctx );
  2402. if ( bv->bv_val == NULL ) {
  2403. return LDAP_NO_MEMORY;
  2404. }
  2405. switch ( LDAP_DN_FORMAT( flags ) ) {
  2406. case LDAP_DN_FORMAT_LDAPV3:
  2407. rc = rdn2str( rdn, bv->bv_val, flags, &l, strval2str );
  2408. back = 1;
  2409. break;
  2410. case LDAP_DN_FORMAT_LDAPV2:
  2411. rc = rdn2str( rdn, bv->bv_val, flags, &l, strval2IA5str );
  2412. back = 1;
  2413. break;
  2414. case LDAP_DN_FORMAT_UFN:
  2415. rc = rdn2UFNstr( rdn, bv->bv_val, flags, &l );
  2416. back = 2;
  2417. break;
  2418. case LDAP_DN_FORMAT_DCE:
  2419. rc = rdn2DCEstr( rdn, bv->bv_val, flags, &l, 1 );
  2420. back = 0;
  2421. break;
  2422. case LDAP_DN_FORMAT_AD_CANONICAL:
  2423. rc = rdn2ADstr( rdn, bv->bv_val, flags, &l, 1 );
  2424. back = 0;
  2425. break;
  2426. default:
  2427. /* need at least one of the previous */
  2428. return LDAP_PARAM_ERROR;
  2429. }
  2430. if ( rc ) {
  2431. LDAP_FREEX( bv->bv_val, ctx );
  2432. return rc;
  2433. }
  2434. bv->bv_len = l - back;
  2435. bv->bv_val[ bv->bv_len ] = '\0';
  2436. return LDAP_SUCCESS;
  2437. }
  2438. /*
  2439. * Very bulk implementation; many optimizations can be performed
  2440. * - a NULL dn results in an empty string ""
  2441. *
  2442. * FIXME: doubts
  2443. * a) what do we do if a UTF-8 string must be converted in LDAPv2?
  2444. * we must encode it in binary form ('#' + HEXPAIRs)
  2445. * b) does DCE/AD support UTF-8?
  2446. * no clue; don't think so.
  2447. * c) what do we do when binary values must be converted in UTF/DCE/AD?
  2448. * use binary encoded BER
  2449. */
  2450. int ldap_dn2str( LDAPDN dn, char **str, unsigned flags )
  2451. {
  2452. struct berval bv;
  2453. int rc;
  2454. assert( str != NULL );
  2455. if((flags & LDAP_DN_FORMAT_MASK) == LDAP_DN_FORMAT_LBER) {
  2456. return LDAP_PARAM_ERROR;
  2457. }
  2458. rc = ldap_dn2bv_x( dn, &bv, flags, NULL );
  2459. *str = bv.bv_val;
  2460. return rc;
  2461. }
  2462. int ldap_dn2bv( LDAPDN dn, struct berval *bv, unsigned flags )
  2463. {
  2464. return ldap_dn2bv_x( dn, bv, flags, NULL );
  2465. }
  2466. int ldap_dn2bv_x( LDAPDN dn, struct berval *bv, unsigned flags, void *ctx )
  2467. {
  2468. int iRDN;
  2469. int rc = LDAP_ENCODING_ERROR;
  2470. ber_len_t len, l;
  2471. /* stringifying helpers for LDAPv3/LDAPv2 */
  2472. int ( *sv2l ) ( struct berval *v, unsigned f, ber_len_t *l );
  2473. int ( *sv2s ) ( struct berval *v, char *s, unsigned f, ber_len_t *l );
  2474. assert( bv != NULL );
  2475. bv->bv_len = 0;
  2476. bv->bv_val = NULL;
  2477. Debug1( LDAP_DEBUG_ARGS, "=> ldap_dn2bv(%u)\n", flags );
  2478. /*
  2479. * a null dn means an empty dn string
  2480. * FIXME: better raise an error?
  2481. */
  2482. if ( dn == NULL || dn[0] == NULL ) {
  2483. bv->bv_val = LDAP_STRDUPX( "", ctx );
  2484. return( LDAP_SUCCESS );
  2485. }
  2486. switch ( LDAP_DN_FORMAT( flags ) ) {
  2487. case LDAP_DN_FORMAT_LDAPV3:
  2488. sv2l = strval2strlen;
  2489. sv2s = strval2str;
  2490. if( 0 ) {
  2491. case LDAP_DN_FORMAT_LDAPV2:
  2492. sv2l = strval2IA5strlen;
  2493. sv2s = strval2IA5str;
  2494. }
  2495. for ( iRDN = 0, len = 0; dn[ iRDN ]; iRDN++ ) {
  2496. ber_len_t rdnl;
  2497. if ( rdn2strlen( dn[ iRDN ], flags, &rdnl, sv2l ) ) {
  2498. goto return_results;
  2499. }
  2500. len += rdnl;
  2501. }
  2502. if ( ( bv->bv_val = LDAP_MALLOCX( len + 1, ctx ) ) == NULL ) {
  2503. rc = LDAP_NO_MEMORY;
  2504. break;
  2505. }
  2506. for ( l = 0, iRDN = 0; dn[ iRDN ]; iRDN++ ) {
  2507. ber_len_t rdnl;
  2508. if ( rdn2str( dn[ iRDN ], &bv->bv_val[ l ], flags,
  2509. &rdnl, sv2s ) ) {
  2510. LDAP_FREEX( bv->bv_val, ctx );
  2511. bv->bv_val = NULL;
  2512. goto return_results;
  2513. }
  2514. l += rdnl;
  2515. }
  2516. assert( l == len );
  2517. /*
  2518. * trim the last ',' (the allocated memory
  2519. * is one byte longer than required)
  2520. */
  2521. bv->bv_len = len - 1;
  2522. bv->bv_val[ bv->bv_len ] = '\0';
  2523. rc = LDAP_SUCCESS;
  2524. break;
  2525. case LDAP_DN_FORMAT_UFN: {
  2526. /*
  2527. * FIXME: quoting from RFC 1781:
  2528. *
  2529. To take a distinguished name, and generate a name of this format with
  2530. attribute types omitted, the following steps are followed.
  2531. 1. If the first attribute is of type CommonName, the type may be
  2532. omitted.
  2533. 2. If the last attribute is of type Country, the type may be
  2534. omitted.
  2535. 3. If the last attribute is of type Country, the last
  2536. Organisation attribute may have the type omitted.
  2537. 4. All attributes of type OrganisationalUnit may have the type
  2538. omitted, unless they are after an Organisation attribute or
  2539. the first attribute is of type OrganisationalUnit.
  2540. * this should be the pedantic implementation.
  2541. *
  2542. * Here the standard implementation reflects
  2543. * the one historically provided by OpenLDAP
  2544. * (and UMIch, I presume), with the variant
  2545. * of spaces and plusses (' + ') separating
  2546. * rdn components.
  2547. *
  2548. * A non-standard but nice implementation could
  2549. * be to turn the final "dc" attributes into a
  2550. * dot-separated domain.
  2551. *
  2552. * Other improvements could involve the use of
  2553. * friendly country names and so.
  2554. */
  2555. #ifdef DC_IN_UFN
  2556. int leftmost_dc = -1;
  2557. int last_iRDN = -1;
  2558. #endif /* DC_IN_UFN */
  2559. for ( iRDN = 0, len = 0; dn[ iRDN ]; iRDN++ ) {
  2560. ber_len_t rdnl;
  2561. if ( rdn2UFNstrlen( dn[ iRDN ], flags, &rdnl ) ) {
  2562. goto return_results;
  2563. }
  2564. len += rdnl;
  2565. #ifdef DC_IN_UFN
  2566. if ( LDAP_DN_IS_RDN_DC( dn[ iRDN ] ) ) {
  2567. if ( leftmost_dc == -1 ) {
  2568. leftmost_dc = iRDN;
  2569. }
  2570. } else {
  2571. leftmost_dc = -1;
  2572. }
  2573. #endif /* DC_IN_UFN */
  2574. }
  2575. if ( ( bv->bv_val = LDAP_MALLOCX( len + 1, ctx ) ) == NULL ) {
  2576. rc = LDAP_NO_MEMORY;
  2577. break;
  2578. }
  2579. #ifdef DC_IN_UFN
  2580. if ( leftmost_dc == -1 ) {
  2581. #endif /* DC_IN_UFN */
  2582. for ( l = 0, iRDN = 0; dn[ iRDN ]; iRDN++ ) {
  2583. ber_len_t vl;
  2584. if ( rdn2UFNstr( dn[ iRDN ], &bv->bv_val[ l ],
  2585. flags, &vl ) ) {
  2586. LDAP_FREEX( bv->bv_val, ctx );
  2587. bv->bv_val = NULL;
  2588. goto return_results;
  2589. }
  2590. l += vl;
  2591. }
  2592. /*
  2593. * trim the last ', ' (the allocated memory
  2594. * is two bytes longer than required)
  2595. */
  2596. bv->bv_len = len - 2;
  2597. bv->bv_val[ bv->bv_len ] = '\0';
  2598. #ifdef DC_IN_UFN
  2599. } else {
  2600. last_iRDN = iRDN - 1;
  2601. for ( l = 0, iRDN = 0; iRDN < leftmost_dc; iRDN++ ) {
  2602. ber_len_t vl;
  2603. if ( rdn2UFNstr( dn[ iRDN ], &bv->bv_val[ l ],
  2604. flags, &vl ) ) {
  2605. LDAP_FREEX( bv->bv_val, ctx );
  2606. bv->bv_val = NULL;
  2607. goto return_results;
  2608. }
  2609. l += vl;
  2610. }
  2611. if ( !dn2domain( dn, bv, l, &last_iRDN ) ) {
  2612. LDAP_FREEX( bv->bv_val, ctx );
  2613. bv->bv_val = NULL;
  2614. goto return_results;
  2615. }
  2616. /* the string is correctly terminated by dn2domain */
  2617. }
  2618. #endif /* DC_IN_UFN */
  2619. rc = LDAP_SUCCESS;
  2620. } break;
  2621. case LDAP_DN_FORMAT_DCE:
  2622. for ( iRDN = 0, len = 0; dn[ iRDN ]; iRDN++ ) {
  2623. ber_len_t rdnl;
  2624. if ( rdn2DCEstrlen( dn[ iRDN ], flags, &rdnl ) ) {
  2625. goto return_results;
  2626. }
  2627. len += rdnl;
  2628. }
  2629. if ( ( bv->bv_val = LDAP_MALLOCX( len + 1, ctx ) ) == NULL ) {
  2630. rc = LDAP_NO_MEMORY;
  2631. break;
  2632. }
  2633. for ( l = 0; iRDN--; ) {
  2634. ber_len_t rdnl;
  2635. if ( rdn2DCEstr( dn[ iRDN ], &bv->bv_val[ l ], flags,
  2636. &rdnl, 0 ) ) {
  2637. LDAP_FREEX( bv->bv_val, ctx );
  2638. bv->bv_val = NULL;
  2639. goto return_results;
  2640. }
  2641. l += rdnl;
  2642. }
  2643. assert( l == len );
  2644. bv->bv_len = len;
  2645. bv->bv_val[ bv->bv_len ] = '\0';
  2646. rc = LDAP_SUCCESS;
  2647. break;
  2648. case LDAP_DN_FORMAT_AD_CANONICAL: {
  2649. int trailing_slash = 1;
  2650. /*
  2651. * Sort of UFN for DCE DNs: a slash ('/') separated
  2652. * global->local DN with no types; strictly speaking,
  2653. * the naming context should be a domain, which is
  2654. * written in DNS-style, e.g. dot-separated.
  2655. *
  2656. * Example:
  2657. *
  2658. * "givenName=Bill+sn=Gates,ou=People,dc=microsoft,dc=com"
  2659. *
  2660. * will read
  2661. *
  2662. * "microsoft.com/People/Bill,Gates"
  2663. */
  2664. for ( iRDN = 0, len = -1; dn[ iRDN ]; iRDN++ ) {
  2665. ber_len_t rdnl;
  2666. if ( rdn2ADstrlen( dn[ iRDN ], flags, &rdnl ) ) {
  2667. goto return_results;
  2668. }
  2669. len += rdnl;
  2670. }
  2671. /* reserve room for trailing '/' in case the DN
  2672. * is exactly a domain */
  2673. if ( ( bv->bv_val = LDAP_MALLOCX( len + 1 + 1, ctx ) ) == NULL )
  2674. {
  2675. rc = LDAP_NO_MEMORY;
  2676. break;
  2677. }
  2678. iRDN--;
  2679. if ( iRDN && dn2domain( dn, bv, 0, &iRDN ) != 0 ) {
  2680. for ( l = bv->bv_len; iRDN >= 0 ; iRDN-- ) {
  2681. ber_len_t rdnl;
  2682. trailing_slash = 0;
  2683. if ( rdn2ADstr( dn[ iRDN ], &bv->bv_val[ l ],
  2684. flags, &rdnl, 0 ) ) {
  2685. LDAP_FREEX( bv->bv_val, ctx );
  2686. bv->bv_val = NULL;
  2687. goto return_results;
  2688. }
  2689. l += rdnl;
  2690. }
  2691. } else {
  2692. int first = 1;
  2693. /*
  2694. * Strictly speaking, AD canonical requires
  2695. * a DN to be in the form "..., dc=smtg",
  2696. * i.e. terminated by a domain component
  2697. */
  2698. if ( flags & LDAP_DN_PEDANTIC ) {
  2699. LDAP_FREEX( bv->bv_val, ctx );
  2700. bv->bv_val = NULL;
  2701. rc = LDAP_ENCODING_ERROR;
  2702. break;
  2703. }
  2704. for ( l = 0; iRDN >= 0 ; iRDN-- ) {
  2705. ber_len_t rdnl;
  2706. if ( rdn2ADstr( dn[ iRDN ], &bv->bv_val[ l ],
  2707. flags, &rdnl, first ) ) {
  2708. LDAP_FREEX( bv->bv_val, ctx );
  2709. bv->bv_val = NULL;
  2710. goto return_results;
  2711. }
  2712. if ( first ) {
  2713. first = 0;
  2714. }
  2715. l += rdnl;
  2716. }
  2717. }
  2718. if ( trailing_slash ) {
  2719. /* the DN is exactly a domain -- need a trailing
  2720. * slash; room was reserved in advance */
  2721. bv->bv_val[ len ] = '/';
  2722. len++;
  2723. }
  2724. bv->bv_len = len;
  2725. bv->bv_val[ bv->bv_len ] = '\0';
  2726. rc = LDAP_SUCCESS;
  2727. } break;
  2728. default:
  2729. return LDAP_PARAM_ERROR;
  2730. }
  2731. Debug3( LDAP_DEBUG_ARGS, "<= ldap_dn2bv(%s)=%d %s\n",
  2732. bv->bv_val, rc, rc ? ldap_err2string( rc ) : "" );
  2733. return_results:;
  2734. return( rc );
  2735. }