util.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. /* util.c -- utility functions -- functions available form libslapd */
  42. #ifdef _WIN32
  43. #include <direct.h> /* for getcwd */
  44. #else
  45. #include <sys/socket.h>
  46. #include <sys/param.h>
  47. #include <unistd.h>
  48. #include <pwd.h>
  49. #include <stdint.h>
  50. #endif
  51. #include <fcntl.h>
  52. #include <libgen.h>
  53. #include <pk11func.h>
  54. #include "slap.h"
  55. #include "prtime.h"
  56. #include "prinrval.h"
  57. #include "snmp_collator.h"
  58. #define UTIL_ESCAPE_NONE 0
  59. #define UTIL_ESCAPE_HEX 1
  60. #define UTIL_ESCAPE_BACKSLASH 2
  61. #if defined( _WIN32 )
  62. #define _PSEP "\\"
  63. #define _PSEP2 "\\\\"
  64. #define _CSEP '\\'
  65. #else
  66. #define _PSEP "/"
  67. #define _PSEP2 "//"
  68. #define _CSEP '/'
  69. #endif
  70. /* slapi_filter_sprintf macros */
  71. #define ATTRSIZE 256 /* size allowed for an attr name */
  72. #define FILTER_BUF 128 /* initial buffer size for attr value */
  73. #define BUF_INCR 16 /* the amount to increase the FILTER_BUF once it fills up */
  74. static int special_np(unsigned char c)
  75. {
  76. if (c == '\\') {
  77. return UTIL_ESCAPE_BACKSLASH;
  78. }
  79. if (c < 32 || c > 126 || c == '"') {
  80. return UTIL_ESCAPE_HEX;
  81. }
  82. return UTIL_ESCAPE_NONE;
  83. }
  84. static int special_np_and_punct(unsigned char c)
  85. {
  86. if (c == '\\') {
  87. return UTIL_ESCAPE_BACKSLASH;
  88. }
  89. if (c < 32 || c > 126 || c == '"' || c == '*') {
  90. return UTIL_ESCAPE_HEX;
  91. }
  92. return UTIL_ESCAPE_NONE;
  93. }
  94. #ifndef USE_OPENLDAP
  95. static int special_filter(unsigned char c)
  96. {
  97. /*
  98. * Escape all non-printing chars and double-quotes in addition
  99. * to those required by RFC 2254 so that we can use the string
  100. * in log files.
  101. */
  102. return (c < 32 ||
  103. c > 126 ||
  104. c == '*' ||
  105. c == '(' ||
  106. c == ')' ||
  107. c == '\\' ||
  108. c == '"') ? UTIL_ESCAPE_HEX : UTIL_ESCAPE_NONE;
  109. }
  110. #endif
  111. /*
  112. * Used by filter_stuff_func to help extract an attribute so we know
  113. * how to normalize the value.
  114. */
  115. static int
  116. special_attr_char(unsigned char c)
  117. {
  118. return (c < 32 ||
  119. c > 126 ||
  120. c == '*' ||
  121. c == '|' ||
  122. c == '&' ||
  123. c == '!' ||
  124. c == '(' ||
  125. c == ')' ||
  126. c == '\\' ||
  127. c == '=' ||
  128. c == '"');
  129. }
  130. static const char*
  131. do_escape_string (
  132. const char* str,
  133. int len, /* -1 means str is nul-terminated */
  134. char buf[BUFSIZ],
  135. int (*special)(unsigned char)
  136. )
  137. {
  138. const char* s;
  139. const char* last;
  140. int esc;
  141. if (str == NULL) {
  142. *buf = '\0';
  143. return buf;
  144. }
  145. if (len == -1) len = strlen (str);
  146. if (len == 0) return str;
  147. last = str + len - 1;
  148. for (s = str; s <= last; ++s) {
  149. if ( (esc = (*special)((unsigned char)*s))) {
  150. const char* first = str;
  151. char* bufNext = buf;
  152. int bufSpace = BUFSIZ - 4;
  153. while (1) {
  154. if (bufSpace < (s - first)) s = first + bufSpace - 1;
  155. if (s > first) {
  156. memcpy (bufNext, first, s - first);
  157. bufNext += (s - first);
  158. bufSpace -= (s - first);
  159. }
  160. if (s > last) {
  161. break;
  162. }
  163. do {
  164. if (esc == UTIL_ESCAPE_BACKSLASH) {
  165. /* *s is '\\' */
  166. /* If *(s+1) and *(s+2) are both hex digits,
  167. * the char is already escaped. */
  168. if (isxdigit(*(s+1)) && isxdigit(*(s+2))) {
  169. memcpy(bufNext, s, 3);
  170. bufNext += 3;
  171. bufSpace -= 3;
  172. s += 2;
  173. } else {
  174. *bufNext++ = *s; --bufSpace;
  175. }
  176. } else { /* UTIL_ESCAPE_HEX */
  177. *bufNext++ = '\\'; --bufSpace;
  178. if (bufSpace < 3) {
  179. memcpy(bufNext, "..", 2);
  180. bufNext += 2;
  181. goto bail;
  182. }
  183. PR_snprintf(bufNext, 3, "%02x", *(unsigned char*)s);
  184. bufNext += 2; bufSpace -= 2;
  185. }
  186. } while (++s <= last &&
  187. (esc = (*special)((unsigned char)*s)));
  188. if (s > last) break;
  189. first = s;
  190. while ( (esc = (*special)((unsigned char)*s)) == UTIL_ESCAPE_NONE && s <= last) ++s;
  191. }
  192. bail:
  193. *bufNext = '\0';
  194. return buf;
  195. }
  196. }
  197. return str;
  198. }
  199. /*
  200. * Function: escape_string
  201. * Arguments: str: string
  202. * buf: a char array of BUFSIZ length, in which the escaped string will
  203. * be returned.
  204. * Returns: a pointer to buf, if str==NULL or it needed to be escaped, or
  205. * str itself otherwise.
  206. *
  207. * This function should only be used for generating loggable strings.
  208. */
  209. const char*
  210. escape_string (const char* str, char buf[BUFSIZ])
  211. {
  212. return do_escape_string(str,-1,buf,special_np);
  213. }
  214. const char*
  215. escape_string_with_punctuation(const char* str, char buf[BUFSIZ])
  216. {
  217. return do_escape_string(str,-1,buf,special_np_and_punct);
  218. }
  219. #define ESCAPE_FILTER 1
  220. #define NORM_FILTER 2
  221. struct filter_ctx {
  222. char *buf;
  223. char attr[ATTRSIZE];
  224. int attr_position;
  225. int attr_found;
  226. int buf_size;
  227. int buf_len;
  228. int next_arg_needs_esc_norm;
  229. int skip_escape;
  230. };
  231. /*
  232. * This function is called by slapi_filter_sprintf to escape/normalize certain values
  233. */
  234. static PRIntn
  235. filter_stuff_func(void *arg, const char *val, PRUint32 slen)
  236. {
  237. struct filter_ctx *ctx = (struct filter_ctx *)arg;
  238. struct berval escaped_filter;
  239. struct berval raw_filter;
  240. char *buf = (char *)val;
  241. int extra_space;
  242. int filter_len = slen;
  243. /* look at val - if val is one of our special keywords, and make a note of it for the next pass */
  244. if (strcmp(val, ESC_NEXT_VAL) == 0){
  245. ctx->next_arg_needs_esc_norm |= ESCAPE_FILTER;
  246. return 0;
  247. }
  248. if (strcmp(val, NORM_NEXT_VAL) == 0){
  249. ctx->next_arg_needs_esc_norm |= NORM_FILTER;
  250. return 0;
  251. }
  252. if (strcmp(val, ESC_AND_NORM_NEXT_VAL) == 0){
  253. ctx->next_arg_needs_esc_norm = NORM_FILTER | ESCAPE_FILTER;
  254. return 0;
  255. }
  256. /*
  257. * Start collecting the attribute name so we can use the correct
  258. * syntax normalization func.
  259. */
  260. if(ctx->attr_found == 0 && ctx->attr_position < (ATTRSIZE - 1)){
  261. if(ctx->attr[0] == '\0'){
  262. if(strstr(val,"=")){
  263. /* we have an attr we need to record */
  264. if(!special_attr_char(val[0])){
  265. memcpy(ctx->attr, val, 1);
  266. ctx->attr_position++;
  267. }
  268. } else {
  269. /*
  270. * We have passed in an attribute as a arg - so we can just set the
  271. * attr with val. The next pass should be '=', otherwise we will
  272. * reset it.
  273. */
  274. memcpy(ctx->attr, val, slen);
  275. ctx->attr_position = slen;
  276. }
  277. } else {
  278. if(val[0] == '='){ /* hit the end of the attribute name */
  279. ctx->attr_found = 1;
  280. } else {
  281. if(special_attr_char(val[0])){
  282. /* this is not an attribute, we should not be collecting this, reset everything */
  283. memset(ctx->attr, '\0', ATTRSIZE);
  284. ctx->attr_position = 0;
  285. } else {
  286. memcpy(ctx->attr + ctx->attr_position, val, 1);
  287. ctx->attr_position++;
  288. }
  289. }
  290. }
  291. }
  292. if (ctx->next_arg_needs_esc_norm && !ctx->skip_escape){
  293. /*
  294. * Normalize the filter value first
  295. */
  296. if(ctx->next_arg_needs_esc_norm & NORM_FILTER){
  297. char *norm_val = NULL;
  298. if(ctx->attr_found){
  299. slapi_attr_value_normalize(NULL, NULL, ctx->attr , buf, 1, &norm_val );
  300. if(norm_val){
  301. buf = norm_val;
  302. filter_len = strlen(buf);
  303. }
  304. }
  305. }
  306. /*
  307. * Escape the filter value
  308. */
  309. if(ctx->next_arg_needs_esc_norm & ESCAPE_FILTER){
  310. #if defined (USE_OPENLDAP)
  311. raw_filter.bv_val = (char *)buf;
  312. raw_filter.bv_len = filter_len;
  313. if(ldap_bv2escaped_filter_value(&raw_filter, &escaped_filter) != 0){
  314. LDAPDebug(LDAP_DEBUG_TRACE, "slapi_filter_sprintf: failed to escape filter value(%s)\n",val,0,0);
  315. ctx->next_arg_needs_esc_norm = 0;
  316. return -1;
  317. } else {
  318. filter_len = escaped_filter.bv_len;
  319. buf = escaped_filter.bv_val;
  320. }
  321. #else
  322. char *val2 = NULL;
  323. buf = slapi_ch_calloc(sizeof(char), filter_len*3 + 1);
  324. val2 = do_escape_string(val, filter_len, buf, special_filter);
  325. if(val2 == NULL){
  326. LDAPDebug(LDAP_DEBUG_TRACE, "slapi_filter_sprintf: failed to escape filter value(%s)\n",val,0,0);
  327. ctx->next_arg_needs_esc_norm = 0;
  328. slapi_ch_free_string(&buf);
  329. return -1;
  330. } else if (val == val2) { /* value did not need escaping and was just returned */
  331. strcpy(buf, val); /* just use value as-is - len did not change */
  332. } else {
  333. filter_len = strlen(buf);
  334. }
  335. #endif
  336. }
  337. /*
  338. * Now add the new value to the buffer, and allocate more memory if needed
  339. */
  340. if (ctx->buf_size + filter_len >= ctx->buf_len){
  341. /* increase buffer for this filter */
  342. extra_space = (ctx->buf_len + filter_len + BUF_INCR);
  343. ctx->buf = slapi_ch_realloc(ctx->buf, sizeof(char) * extra_space);
  344. ctx->buf_len = extra_space;
  345. }
  346. /* append the escaped value */
  347. memcpy(ctx->buf + ctx->buf_size, buf, filter_len);
  348. ctx->buf_size += filter_len;
  349. /* done with the value, reset everything */
  350. ctx->next_arg_needs_esc_norm = 0;
  351. ctx->attr_found = 0;
  352. ctx->attr_position = 0;
  353. memset(ctx->attr, '\0', ATTRSIZE);
  354. slapi_ch_free_string(&buf);
  355. return filter_len;
  356. } else { /* process arg as is */
  357. /* check if we have enough room in our buffer */
  358. if (ctx->buf_size + slen >= ctx->buf_len){
  359. /* increase buffer for this filter */
  360. extra_space = (ctx->buf_len + slen + BUF_INCR);
  361. ctx->buf = slapi_ch_realloc((char *)ctx->buf, sizeof(char) * extra_space);
  362. ctx->buf_len = extra_space;
  363. }
  364. memcpy(ctx->buf + ctx->buf_size, buf, slen);
  365. ctx->buf_size += slen;
  366. return slen;
  367. }
  368. }
  369. /*
  370. * This is basically like slapi_ch_smprintf() except it can handle special
  371. * keywords that will cause the next value to be escaped and/or normalized.
  372. *
  373. * ESC_NEXT_VAL - escape the next value
  374. * NORM_NEXT_VAL - normalize the next value
  375. * ESC_AND_NORM_NEXT_VAL - escape and normalize the next value
  376. *
  377. * Example:
  378. *
  379. * slapi_filter_sprintf("cn=%s%s", ESC_NEXT_VAL, value);
  380. * slapi_filter_sprintf("(|(cn=%s%s)(sn=%s%s))", ESC_NEXT_VAL, value, NORM_NEXT_VAL, value);
  381. *
  382. * Note: you need a string format specifier(%s) for each keyword
  383. */
  384. char*
  385. slapi_filter_sprintf(const char *fmt, ...)
  386. {
  387. struct filter_ctx ctx;
  388. va_list args;
  389. char *buf;
  390. int rc;
  391. buf = slapi_ch_calloc(sizeof(char), FILTER_BUF + 1);
  392. ctx.buf = buf;
  393. memset(ctx.attr,'\0', ATTRSIZE);
  394. ctx.attr_position = 0;
  395. ctx.attr_found = 0;
  396. ctx.buf_len = FILTER_BUF;
  397. ctx.buf_size = 0;
  398. ctx.next_arg_needs_esc_norm = 0;
  399. ctx.skip_escape = 0;
  400. va_start(args, fmt);
  401. rc = PR_vsxprintf(filter_stuff_func, &ctx, fmt, args);
  402. if(rc == -1){
  403. /* transformation failed, just return non-normalized/escaped string */
  404. ctx.skip_escape = 1;
  405. PR_vsxprintf(filter_stuff_func, &ctx, fmt, args);
  406. }
  407. va_end(args);
  408. return ctx.buf;
  409. }
  410. /*
  411. * escape special characters in values used in search filters
  412. *
  413. * caller must free the returned value
  414. */
  415. char*
  416. slapi_escape_filter_value(char* filter_str, int len)
  417. {
  418. struct berval escaped_filter;
  419. struct berval raw_filter;
  420. int filter_len;
  421. /*
  422. * Check the length for special cases
  423. */
  424. if(len == -1){
  425. /* filter str is null terminated */
  426. filter_len = strlen(filter_str);
  427. } else if (len == 0){
  428. /* return the filter as is */
  429. return slapi_ch_strdup(filter_str);
  430. } else {
  431. /* the len is the length */
  432. filter_len = len;
  433. }
  434. #if defined (USE_OPENLDAP)
  435. /*
  436. * Construct the berval and escape it
  437. */
  438. raw_filter.bv_val = filter_str;
  439. raw_filter.bv_len = filter_len;
  440. if(ldap_bv2escaped_filter_value(&raw_filter, &escaped_filter) != 0){
  441. LDAPDebug(LDAP_DEBUG_TRACE, "slapi_escape_filter_value: failed to escape filter value(%s)\n",filter_str,0,0);
  442. return NULL;
  443. } else {
  444. return escaped_filter.bv_val;
  445. }
  446. #else
  447. char *buf = slapi_ch_calloc(sizeof(char), filter_len*3+1);
  448. return do_escape_string(filter_str, filter_len, buf, special_filter);
  449. #endif
  450. }
  451. /*
  452. ** This function takes a quoted attribute value of the form "abc",
  453. ** and strips off the enclosing quotes. It also deals with quoted
  454. ** characters by removing the preceeding '\' character.
  455. **
  456. */
  457. void
  458. strcpy_unescape_value( char *d, const char *s )
  459. {
  460. int gotesc = 0;
  461. const char *end = s + strlen(s);
  462. for ( ; *s; s++ )
  463. {
  464. switch ( *s )
  465. {
  466. case '"':
  467. break;
  468. case '\\':
  469. gotesc = 1;
  470. if ( s+2 < end ) {
  471. int n = slapi_hexchar2int( s[1] );
  472. if ( n >= 0 && n < 16 ) {
  473. int n2 = slapi_hexchar2int( s[2] );
  474. if ( n2 >= 0 ) {
  475. n = (n << 4) + n2;
  476. if (n == 0) { /* don't change \00 */
  477. *d++ = *s++;
  478. *d++ = *s++;
  479. *d++ = *s;
  480. } else { /* change \xx to a single char */
  481. *d++ = (char)n;
  482. s += 2;
  483. }
  484. gotesc = 0;
  485. }
  486. }
  487. }
  488. /* This is an escaped single character (like \"), so
  489. * just copy the special character and not the escape.
  490. * We need to be careful to not go past the end of
  491. * the string when the loop increments s. */
  492. if (gotesc && (s+1 < end)) {
  493. s++;
  494. *d++ = *s;
  495. gotesc = 0;
  496. }
  497. break;
  498. default:
  499. *d++ = *s;
  500. break;
  501. }
  502. }
  503. *d = '\0';
  504. }
  505. /* functions to convert between an entry and a set of mods */
  506. int slapi_mods2entry (Slapi_Entry **e, const char *idn, LDAPMod **iattrs)
  507. {
  508. int i, rc = LDAP_SUCCESS;
  509. LDAPMod **attrs= NULL;
  510. PR_ASSERT (idn);
  511. PR_ASSERT (iattrs);
  512. PR_ASSERT (e);
  513. attrs = normalize_mods2bvals((const LDAPMod **)iattrs);
  514. PR_ASSERT (attrs);
  515. /* Construct an entry */
  516. *e = slapi_entry_alloc();
  517. PR_ASSERT (*e);
  518. slapi_entry_init(*e, slapi_ch_strdup(idn), NULL);
  519. for (i = 0; rc==LDAP_SUCCESS && attrs[ i ]!=NULL; i++)
  520. {
  521. char *normtype;
  522. Slapi_Value **vals;
  523. /*
  524. * slapi_entry_apply_mod_extension applys mod and stores
  525. * the result in the extension
  526. * return value: 1 - mod is applied and stored in extension
  527. * -1 - mod is applied and failed
  528. * 0 - mod is nothing to do with extension
  529. */
  530. rc = slapi_entry_apply_mod_extension(*e, attrs[i], -1);
  531. if (rc) {
  532. if (1 == rc) {
  533. rc = LDAP_SUCCESS;
  534. } else {
  535. rc = LDAP_OPERATIONS_ERROR;
  536. }
  537. #if !defined(USE_OLD_UNHASHED)
  538. /* In case USE_OLD_UNHASHED,
  539. * unhashed pw needs to be in attr, too. */
  540. continue;
  541. #endif
  542. }
  543. normtype = slapi_attr_syntax_normalize(attrs[ i ]->mod_type);
  544. valuearray_init_bervalarray(attrs[ i ]->mod_bvalues, &vals);
  545. if (strcasecmp(normtype, SLAPI_USERPWD_ATTR) == 0)
  546. {
  547. pw_encodevals(vals);
  548. }
  549. /* set entry uniqueid - also adds attribute to the list */
  550. if (strcasecmp(normtype, SLAPI_ATTR_UNIQUEID) == 0) {
  551. if (vals) {
  552. slapi_entry_set_uniqueid (*e,
  553. slapi_ch_strdup (slapi_value_get_string(vals[0])));
  554. } else {
  555. rc = LDAP_NO_SUCH_ATTRIBUTE;
  556. }
  557. } else {
  558. rc = slapi_entry_add_values_sv(*e, normtype, vals);
  559. }
  560. valuearray_free(&vals);
  561. if (rc != LDAP_SUCCESS)
  562. {
  563. LDAPDebug2Args(LDAP_DEBUG_ANY,
  564. "slapi_add_internal: add_values for type %s failed (rc: %d)\n",
  565. normtype, rc );
  566. slapi_entry_free (*e);
  567. *e = NULL;
  568. }
  569. slapi_ch_free((void **) &normtype);
  570. }
  571. freepmods(attrs);
  572. return rc;
  573. }
  574. int
  575. slapi_entry2mods (const Slapi_Entry *e, char **dn, LDAPMod ***attrs)
  576. {
  577. Slapi_Mods smods;
  578. Slapi_Attr *attr;
  579. Slapi_Value **va;
  580. char *type;
  581. int rc;
  582. PR_ASSERT (e && attrs);
  583. if (dn)
  584. *dn = slapi_ch_strdup (slapi_entry_get_dn ((Slapi_Entry *)e));
  585. slapi_mods_init (&smods, 0);
  586. rc = slapi_entry_first_attr(e, &attr);
  587. while (rc == 0)
  588. {
  589. if ( NULL != ( va = attr_get_present_values( attr ))) {
  590. slapi_attr_get_type(attr, &type);
  591. slapi_mods_add_mod_values(&smods, LDAP_MOD_ADD, type, va );
  592. }
  593. rc = slapi_entry_next_attr(e, attr, &attr);
  594. }
  595. #if !defined(USE_OLD_UNHASHED)
  596. if (SLAPD_UNHASHED_PW_ON == config_get_unhashed_pw_switch()) {
  597. /* store unhashed passwd is enabled */
  598. /* In case USE_OLD_UNHASHED, unhashed pw is already in mods */
  599. /* add extension to mods */
  600. rc = slapi_pw_get_entry_ext((Slapi_Entry *)e, &va);
  601. if (LDAP_SUCCESS == rc) {
  602. /* va is copied and set to smods */
  603. slapi_mods_add_mod_values(&smods, LDAP_MOD_ADD,
  604. PSEUDO_ATTR_UNHASHEDUSERPASSWORD, va);
  605. }
  606. }
  607. #endif
  608. *attrs = slapi_mods_get_ldapmods_passout (&smods);
  609. slapi_mods_done (&smods);
  610. return 0;
  611. }
  612. /******************************************************************************
  613. *
  614. * normalize_mods2bvals
  615. *
  616. * Return value: normalized mods
  617. * The values/bvals are all duplicated in this function since
  618. * the normalized mods are freed with ldap_mods_free by the caller.
  619. *
  620. *******************************************************************************/
  621. LDAPMod **
  622. normalize_mods2bvals(const LDAPMod **mods)
  623. {
  624. int w, x, vlen, num_values, num_mods;
  625. LDAPMod **normalized_mods;
  626. if (mods == NULL)
  627. {
  628. return NULL;
  629. }
  630. /* first normalize the mods so they are bvalues */
  631. /* count the number of mods -- sucks but should be small */
  632. num_mods = 1;
  633. for (w=0; mods[w] != NULL; w++) num_mods++;
  634. normalized_mods = (LDAPMod **) slapi_ch_calloc(num_mods, sizeof(LDAPMod *));
  635. for (w = 0; mods[w] != NULL; w++)
  636. {
  637. Slapi_Attr a = {0};
  638. slapi_attr_init(&a, mods[w]->mod_type);
  639. int is_dn_syntax = 0;
  640. struct berval **normmbvp = NULL;
  641. /* Check if the type of the to-be-added values has DN syntax
  642. * or not. */
  643. if (slapi_attr_is_dn_syntax_attr(&a)) {
  644. is_dn_syntax = 1;
  645. }
  646. attr_done(&a);
  647. /* copy each mod into a normalized modbvalue */
  648. normalized_mods[w] = (LDAPMod *) slapi_ch_calloc(1, sizeof(LDAPMod));
  649. normalized_mods[w]->mod_op = mods[w]->mod_op | LDAP_MOD_BVALUES;
  650. normalized_mods[w]->mod_type = slapi_ch_strdup(mods[w]->mod_type);
  651. /*
  652. * count the number of values -- kinda sucks but probably
  653. * less expensive then reallocing, and num_values
  654. * should typically be very small
  655. */
  656. num_values = 0;
  657. if (mods[w]->mod_op & LDAP_MOD_BVALUES)
  658. {
  659. for (x = 0; mods[w]->mod_bvalues != NULL &&
  660. mods[w]->mod_bvalues[x] != NULL; x++)
  661. {
  662. num_values++;
  663. }
  664. } else {
  665. for (x = 0; mods[w]->mod_values != NULL &&
  666. mods[w]->mod_values[x] != NULL; x++)
  667. {
  668. num_values++;
  669. }
  670. }
  671. if (num_values > 0)
  672. {
  673. normalized_mods[w]->mod_bvalues = (struct berval **)
  674. slapi_ch_calloc(num_values + 1, sizeof(struct berval *));
  675. } else {
  676. normalized_mods[w]->mod_bvalues = NULL;
  677. }
  678. if (mods[w]->mod_op & LDAP_MOD_BVALUES)
  679. {
  680. struct berval **mbvp = NULL;
  681. for (mbvp = mods[w]->mod_bvalues,
  682. normmbvp = normalized_mods[w]->mod_bvalues;
  683. mbvp && *mbvp; mbvp++, normmbvp++)
  684. {
  685. if (is_dn_syntax) {
  686. Slapi_DN *sdn = slapi_sdn_new_dn_byref((*mbvp)->bv_val);
  687. if (slapi_sdn_get_dn(sdn)) {
  688. *normmbvp =
  689. (struct berval *)slapi_ch_malloc(sizeof(struct berval));
  690. (*normmbvp)->bv_val =
  691. slapi_ch_strdup(slapi_sdn_get_dn(sdn));
  692. (*normmbvp)->bv_len = slapi_sdn_get_ndn_len(sdn);
  693. } else {
  694. /* normalization failed; use the original */
  695. *normmbvp = ber_bvdup(*mbvp);
  696. }
  697. slapi_sdn_free(&sdn);
  698. } else {
  699. *normmbvp = ber_bvdup(*mbvp);
  700. }
  701. }
  702. } else {
  703. char **mvp = NULL;
  704. for (mvp = mods[w]->mod_values,
  705. normmbvp = normalized_mods[w]->mod_bvalues;
  706. mvp && *mvp; mvp++, normmbvp++)
  707. {
  708. vlen = strlen(*mvp);
  709. *normmbvp =
  710. (struct berval *)slapi_ch_malloc(sizeof(struct berval));
  711. if (is_dn_syntax) {
  712. Slapi_DN *sdn = slapi_sdn_new_dn_byref(*mvp);
  713. if (slapi_sdn_get_dn(sdn)) {
  714. (*normmbvp)->bv_val =
  715. slapi_ch_strdup(slapi_sdn_get_dn(sdn));
  716. (*normmbvp)->bv_len = slapi_sdn_get_ndn_len(sdn);
  717. } else {
  718. /* normalization failed; use the original */
  719. (*normmbvp)->bv_val = slapi_ch_malloc(vlen + 1);
  720. memcpy((*normmbvp)->bv_val, *mvp, vlen);
  721. (*normmbvp)->bv_val[vlen] = '\0';
  722. (*normmbvp)->bv_len = vlen;
  723. }
  724. slapi_sdn_free(&sdn);
  725. } else {
  726. (*normmbvp)->bv_val = slapi_ch_malloc(vlen + 1);
  727. memcpy((*normmbvp)->bv_val, *mvp, vlen);
  728. (*normmbvp)->bv_val[vlen] = '\0';
  729. (*normmbvp)->bv_len = vlen;
  730. }
  731. }
  732. }
  733. PR_ASSERT(normmbvp - normalized_mods[w]->mod_bvalues <= num_values);
  734. /* don't forget to null terminate it */
  735. if (num_values > 0)
  736. {
  737. *normmbvp = NULL;
  738. }
  739. }
  740. /* don't forget to null terminate the normalize list of mods */
  741. normalized_mods[w] = NULL;
  742. return(normalized_mods);
  743. }
  744. /*
  745. * Return true if the given path is an absolute path, false otherwise
  746. */
  747. int
  748. is_abspath(const char *path)
  749. {
  750. if (path == NULL || *path == '\0') {
  751. return 0; /* empty path is not absolute? */
  752. }
  753. #if defined( XP_WIN32 )
  754. if (path[0] == '/' || path[0] == '\\' ||
  755. (isalpha(path[0]) && (path[1] == ':'))) {
  756. return 1; /* Windows abs path */
  757. }
  758. #else
  759. if (path[0] == '/') {
  760. return 1; /* unix abs path */
  761. }
  762. #endif
  763. return 0; /* not an abs path */
  764. }
  765. static void
  766. clean_path(char **norm_path)
  767. {
  768. char **np;
  769. for (np = norm_path; np && *np; np++)
  770. slapi_ch_free_string(np);
  771. slapi_ch_free((void **)&norm_path);
  772. }
  773. static char **
  774. normalize_path(char *path)
  775. {
  776. char *dname = NULL;
  777. char *dnamep = NULL;
  778. char **dirs = NULL;
  779. char **rdirs = NULL;
  780. char **dp = NULL;
  781. char **rdp;
  782. int elimdots = 0;
  783. if (NULL == path || '\0' == *path) {
  784. return NULL;
  785. }
  786. dirs = (char **)slapi_ch_calloc(strlen(path), sizeof(char *));
  787. rdirs = (char **)slapi_ch_calloc(strlen(path), sizeof(char *));
  788. dp = dirs;
  789. dname = slapi_ch_strdup(path);
  790. do {
  791. dnamep = strrchr(dname, _CSEP);
  792. if (NULL == dnamep) {
  793. dnamep = dname;
  794. } else {
  795. *dnamep = '\0';
  796. dnamep++;
  797. }
  798. if (0 != strcmp(dnamep, ".") && strlen(dnamep) > 0) {
  799. *dp++ = slapi_ch_strdup(dnamep); /* rm "/./" and "//" in the path */
  800. }
  801. } while ( dnamep > dname /* == -> no more _CSEP */ );
  802. slapi_ch_free_string(&dname);
  803. /* remove "xxx/.." in the path */
  804. for (dp = dirs, rdp = rdirs; dp && *dp; dp++) {
  805. while (*dp && 0 == strcmp(*dp, "..")) {
  806. dp++;
  807. elimdots++;
  808. }
  809. if (elimdots > 0) {
  810. elimdots--;
  811. } else if (*dp) {
  812. *rdp++ = slapi_ch_strdup(*dp);
  813. }
  814. }
  815. /* reverse */
  816. for (--rdp, dp = rdirs; rdp >= dp && rdp >= rdirs; --rdp, dp++) {
  817. char *tmpp = *dp;
  818. *dp = *rdp;
  819. *rdp = tmpp;
  820. }
  821. clean_path(dirs);
  822. return rdirs;
  823. }
  824. /*
  825. * Take "relpath" and prepend the current working directory to it
  826. * if it isn't an absolute pathname already. The caller is responsible
  827. * for freeing the returned string.
  828. */
  829. char *
  830. rel2abspath( char *relpath )
  831. {
  832. return rel2abspath_ext( relpath, NULL );
  833. }
  834. char *
  835. rel2abspath_ext( char *relpath, char *cwd )
  836. {
  837. char abspath[ MAXPATHLEN + 1 ];
  838. char *retpath = NULL;
  839. #if defined( _WIN32 )
  840. CHAR szDrive[_MAX_DRIVE];
  841. CHAR szDir[_MAX_DIR];
  842. CHAR szFname[_MAX_FNAME];
  843. CHAR szExt[_MAX_EXT];
  844. #endif
  845. if ( relpath == NULL ) {
  846. return NULL;
  847. }
  848. #if defined( _WIN32 )
  849. memset (&szDrive, 0, sizeof (szDrive));
  850. memset (&szDir, 0, sizeof (szDir));
  851. memset (&szFname, 0, sizeof (szFname));
  852. memset (&szExt, 0, sizeof (szExt));
  853. _splitpath( relpath, szDrive, szDir, szFname, szExt );
  854. if( szDrive[0] && szDir[0] )
  855. return( slapi_ch_strdup( relpath ));
  856. #endif
  857. if ( relpath[ 0 ] == _CSEP ) { /* absolute path */
  858. PR_snprintf(abspath, sizeof(abspath), "%s", relpath);
  859. } else { /* relative path */
  860. if ( NULL == cwd ) {
  861. if ( getcwd( abspath, MAXPATHLEN ) == NULL ) {
  862. perror( "getcwd" );
  863. LDAPDebug( LDAP_DEBUG_ANY, "Cannot determine current directory\n",
  864. 0, 0, 0 );
  865. exit( 1 );
  866. }
  867. } else {
  868. PR_snprintf(abspath, sizeof(abspath), "%s", cwd);
  869. }
  870. if ( strlen( relpath ) + strlen( abspath ) + 1 > MAXPATHLEN ) {
  871. LDAPDebug( LDAP_DEBUG_ANY, "Pathname \"%s" _PSEP "%s\" too long\n",
  872. abspath, relpath, 0 );
  873. exit( 1 );
  874. }
  875. if ( strcmp( relpath, "." )) {
  876. if ( abspath[ 0 ] != '\0' &&
  877. abspath[ strlen( abspath ) - 1 ] != _CSEP )
  878. {
  879. PL_strcatn( abspath, sizeof(abspath), _PSEP );
  880. }
  881. PL_strcatn( abspath, sizeof(abspath), relpath );
  882. }
  883. }
  884. retpath = slapi_ch_strdup(abspath);
  885. /* if there's no '.' or separators, no need to call normalize_path */
  886. if (NULL != strchr(abspath, '.') || NULL != strstr(abspath, _PSEP))
  887. {
  888. char **norm_path = normalize_path(abspath);
  889. char **np, *rp;
  890. int pathlen = strlen(abspath) + 1;
  891. int usedlen = 0;
  892. for (np = norm_path, rp = retpath; np && *np; np++) {
  893. int thislen = strlen(*np) + 1;
  894. if (0 != strcmp(*np, _PSEP))
  895. PR_snprintf(rp, pathlen - usedlen, "%c%s", _CSEP, *np);
  896. rp += thislen;
  897. usedlen += thislen;
  898. }
  899. clean_path(norm_path);
  900. }
  901. return retpath;
  902. }
  903. /*
  904. * Allocate a buffer large enough to hold a berval's
  905. * value and a terminating null byte. The returned buffer
  906. * is null-terminated. Returns NULL if bval is NULL or if
  907. * bval->bv_val is NULL.
  908. */
  909. char *
  910. slapi_berval_get_string_copy(const struct berval *bval)
  911. {
  912. char *return_value = NULL;
  913. if (NULL != bval && NULL != bval->bv_val)
  914. {
  915. return_value = slapi_ch_malloc(bval->bv_len + 1);
  916. memcpy(return_value, bval->bv_val, bval->bv_len);
  917. return_value[bval->bv_len] = '\0';
  918. }
  919. return return_value;
  920. }
  921. /* Takes a return code supposed to be errno or from a plugin
  922. which we don't expect to see and prints a handy log message */
  923. void slapd_nasty(char* str, int c, int err)
  924. {
  925. char *msg = NULL;
  926. char buffer[100];
  927. PR_snprintf(buffer,sizeof(buffer), "%s BAD %d",str,c);
  928. LDAPDebug(LDAP_DEBUG_ANY,"%s, err=%d %s\n",buffer,err,(msg = strerror( err )) ? msg : "");
  929. }
  930. /* ***************************************************
  931. Random function (very similar to rand_r())
  932. *************************************************** */
  933. int
  934. slapi_rand_r(unsigned int *randx)
  935. {
  936. if (*randx)
  937. {
  938. PK11_RandomUpdate(randx, sizeof(*randx));
  939. }
  940. PK11_GenerateRandom((unsigned char *)randx, (int)sizeof(*randx));
  941. return (int)(*randx & 0x7FFFFFFF);
  942. }
  943. /* ***************************************************
  944. Random function (very similar to rand_r() but takes and returns an array)
  945. Note: there is an identical function in plugins/pwdstorage/ssha_pwd.c.
  946. That module can't use a libslapd function because the module is included
  947. in libds_admin, which doesn't link to libslapd. Eventually, shared
  948. functions should be moved to a shared library.
  949. *************************************************** */
  950. void
  951. slapi_rand_array(void *randx, size_t len)
  952. {
  953. PK11_RandomUpdate(randx, len);
  954. PK11_GenerateRandom((unsigned char *)randx, (int)len);
  955. }
  956. /* ***************************************************
  957. Random function (very similar to rand()...)
  958. *************************************************** */
  959. int
  960. slapi_rand()
  961. {
  962. unsigned int randx = 0;
  963. return slapi_rand_r(&randx);
  964. }
  965. /************************************************************************
  966. Function: DS_Sleep(PRIntervalTime ticks)
  967. Purpose: To replace PR_Sleep()
  968. Author: Scott Hopson <[email protected]>
  969. Description:
  970. Causes the current thread to wait for ticks number of intervals.
  971. In UNIX this is accomplished by using select()
  972. which should be supported across all UNIX platforms.
  973. In WIN32 we simply use the Sleep() function which yields
  974. for the number of milliseconds specified.
  975. ************************************************************************/
  976. #if defined(_WIN32)
  977. #include "windows.h"
  978. void DS_Sleep(PRIntervalTime ticks)
  979. {
  980. DWORD mSecs = PR_IntervalToMilliseconds(ticks);
  981. Sleep(mSecs);
  982. }
  983. #else /*** UNIX ***/
  984. #include <sys/time.h>
  985. void DS_Sleep(PRIntervalTime ticks)
  986. {
  987. long mSecs = PR_IntervalToMilliseconds(ticks);
  988. struct timeval tm;
  989. tm.tv_sec = mSecs / 1000;
  990. tm.tv_usec = (mSecs % 1000) * 1000;
  991. select(0,NULL,NULL,NULL,&tm);
  992. }
  993. #endif
  994. /*****************************************************************************
  995. * strarray2str(): convert the array of strings in "a" into a single
  996. * space-separated string like:
  997. * str1 str2 str3
  998. * If buf is too small, the result will be truncated and end with "...".
  999. * If include_quotes is non-zero, double quote marks are included around
  1000. * the string, e.g.,
  1001. * "str2 str2 str3"
  1002. *
  1003. * Returns: 0 if completely successful and -1 if result is truncated.
  1004. */
  1005. int
  1006. strarray2str( char **a, char *buf, size_t buflen, int include_quotes )
  1007. {
  1008. int rc = 0; /* optimistic */
  1009. char *p = buf;
  1010. size_t totlen = 0;
  1011. if ( include_quotes ) {
  1012. if ( buflen < 3 ) {
  1013. return -1; /* not enough room for the quote marks! */
  1014. }
  1015. *p++ = '"';
  1016. ++totlen;
  1017. }
  1018. if ( NULL != a ) {
  1019. int ii;
  1020. size_t len = 0;
  1021. for ( ii = 0; a[ ii ] != NULL; ii++ ) {
  1022. if ( ii > 0 ) {
  1023. *p++ = ' ';
  1024. totlen++;
  1025. }
  1026. len = strlen( a[ ii ]);
  1027. if ( totlen + len > buflen - 5 ) {
  1028. strcpy ( p, "..." );
  1029. p += 3;
  1030. totlen += 3;
  1031. rc = -1;
  1032. break; /* result truncated */
  1033. } else {
  1034. strcpy( p, a[ ii ]);
  1035. p += len;
  1036. totlen += len;
  1037. }
  1038. }
  1039. }
  1040. if ( include_quotes ) {
  1041. *p++ = '"';
  1042. ++totlen;
  1043. }
  1044. buf[ totlen ] = '\0';
  1045. return( rc );
  1046. }
  1047. /*****************************************************************************/
  1048. /* Changes the ownership of the given file/directory if not
  1049. already the owner
  1050. Returns 0 upon success or non-zero otherwise, usually -1 if
  1051. some system error occurred
  1052. */
  1053. #ifndef _WIN32
  1054. int
  1055. slapd_chown_if_not_owner(const char *filename, uid_t uid, gid_t gid)
  1056. {
  1057. int fd = -1;
  1058. struct stat statbuf;
  1059. int result = 1;
  1060. if (!filename)
  1061. return result;
  1062. fd = open(filename, O_RDONLY);
  1063. if (fd == -1) {
  1064. return result;
  1065. }
  1066. memset(&statbuf, '\0', sizeof(statbuf));
  1067. if (!(result = fstat(fd, &statbuf)))
  1068. {
  1069. if (((uid != -1) && (uid != statbuf.st_uid)) ||
  1070. ((gid != -1) && (gid != statbuf.st_gid)))
  1071. {
  1072. result = fchown(fd, uid, gid);
  1073. }
  1074. }
  1075. close(fd);
  1076. return result;
  1077. }
  1078. #endif
  1079. /*
  1080. * Compare 2 pathes
  1081. * Paths could contain ".", "..", "//" in the path, thus normalize them first.
  1082. * One or two of the paths could be a relative path.
  1083. */
  1084. int
  1085. slapd_comp_path(char *p0, char *p1)
  1086. {
  1087. int rval = 0;
  1088. char *norm_p0 = rel2abspath(p0);
  1089. char *norm_p1 = rel2abspath(p1);
  1090. rval = strcmp(norm_p0, norm_p1);
  1091. slapi_ch_free_string(&norm_p0);
  1092. slapi_ch_free_string(&norm_p1);
  1093. return rval;
  1094. }
  1095. /*
  1096. Takes an unsigned char value and converts it to a hex string.
  1097. The string s is written, and the caller must ensure s has enough
  1098. space. For hex numbers, the upper argument says to use a-f or A-F.
  1099. The return value is the address of the next char after the last one written.
  1100. */
  1101. char *
  1102. slapi_u8_to_hex(uint8_t val, char *s, uint8_t upper) {
  1103. static char ldigits[] = "0123456789abcdef";
  1104. static char udigits[] = "0123456789ABCDEF";
  1105. char *digits;
  1106. if (upper) {
  1107. digits = udigits;
  1108. } else {
  1109. digits = ldigits;
  1110. }
  1111. s[0] = digits[val >> 4];
  1112. s[1] = digits[val & 0xf];
  1113. return &s[2];
  1114. }
  1115. char *
  1116. slapi_u16_to_hex(uint16_t val, char *s, uint8_t upper) {
  1117. static char ldigits[] = "0123456789abcdef";
  1118. static char udigits[] = "0123456789ABCDEF";
  1119. char *digits;
  1120. if (upper) {
  1121. digits = udigits;
  1122. } else {
  1123. digits = ldigits;
  1124. }
  1125. s[0] = digits[val >> 12];
  1126. s[1] = digits[(val >> 8) & 0xf];
  1127. s[2] = digits[(val >> 4) & 0xf];
  1128. s[3] = digits[val & 0xf];
  1129. return &s[4];
  1130. }
  1131. char *
  1132. slapi_u32_to_hex(uint32_t val, char *s, uint8_t upper) {
  1133. static char ldigits[] = "0123456789abcdef";
  1134. static char udigits[] = "0123456789ABCDEF";
  1135. char *digits;
  1136. if (upper) {
  1137. digits = udigits;
  1138. } else {
  1139. digits = ldigits;
  1140. }
  1141. s[0] = digits[val >> 28];
  1142. s[1] = digits[(val >> 24) & 0xf];
  1143. s[2] = digits[(val >> 20) & 0xf];
  1144. s[3] = digits[(val >> 16) & 0xf];
  1145. s[4] = digits[(val >> 12) & 0xf];
  1146. s[5] = digits[(val >> 8) & 0xf];
  1147. s[6] = digits[(val >> 4) & 0xf];
  1148. s[7] = digits[val & 0xf];
  1149. return &s[8];
  1150. }
  1151. char *
  1152. slapi_u64_to_hex(uint64_t val, char *s, uint8_t upper) {
  1153. static char ldigits[] = "0123456789abcdef";
  1154. static char udigits[] = "0123456789ABCDEF";
  1155. char *digits;
  1156. if (upper) {
  1157. digits = udigits;
  1158. } else {
  1159. digits = ldigits;
  1160. }
  1161. s[0] = digits[val >> 60];
  1162. s[1] = digits[(val >> 56) & 0xf];
  1163. s[2] = digits[(val >> 52) & 0xf];
  1164. s[3] = digits[(val >> 48) & 0xf];
  1165. s[4] = digits[(val >> 44) & 0xf];
  1166. s[5] = digits[(val >> 40) & 0xf];
  1167. s[6] = digits[(val >> 36) & 0xf];
  1168. s[7] = digits[(val >> 32) & 0xf];
  1169. s[8] = digits[(val >> 28) & 0xf];
  1170. s[9] = digits[(val >> 24) & 0xf];
  1171. s[10] = digits[(val >> 20) & 0xf];
  1172. s[11] = digits[(val >> 16) & 0xf];
  1173. s[12] = digits[(val >> 12) & 0xf];
  1174. s[13] = digits[(val >> 8) & 0xf];
  1175. s[14] = digits[(val >> 4) & 0xf];
  1176. s[15] = digits[val & 0xf];
  1177. return &s[16];
  1178. }
  1179. static const int char2intarray[] = {
  1180. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  1181. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  1182. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  1183. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
  1184. -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  1185. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  1186. -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  1187. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  1188. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  1189. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  1190. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  1191. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  1192. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  1193. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  1194. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  1195. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
  1196. };
  1197. int
  1198. slapi_hexchar2int(char c)
  1199. {
  1200. return char2intarray[(unsigned char)c];
  1201. }
  1202. uint8_t
  1203. slapi_str_to_u8(const char *s)
  1204. {
  1205. uint8_t v0 = (uint8_t)slapi_hexchar2int(s[0]);
  1206. uint8_t v1 = (uint8_t)slapi_hexchar2int(s[1]);
  1207. return (v0 << 4) | v1;
  1208. }
  1209. uint16_t
  1210. slapi_str_to_u16(const char *s)
  1211. {
  1212. uint16_t v0 = (uint16_t)slapi_hexchar2int(s[0]);
  1213. uint16_t v1 = (uint16_t)slapi_hexchar2int(s[1]);
  1214. uint16_t v2 = (uint16_t)slapi_hexchar2int(s[2]);
  1215. uint16_t v3 = (uint16_t)slapi_hexchar2int(s[3]);
  1216. return (v0 << 12) | (v1 << 8) | (v2 << 4) | v3;
  1217. }
  1218. uint32_t
  1219. slapi_str_to_u32(const char *s)
  1220. {
  1221. uint32_t v0 = (uint32_t)slapi_hexchar2int(s[0]);
  1222. uint32_t v1 = (uint32_t)slapi_hexchar2int(s[1]);
  1223. uint32_t v2 = (uint32_t)slapi_hexchar2int(s[2]);
  1224. uint32_t v3 = (uint32_t)slapi_hexchar2int(s[3]);
  1225. uint32_t v4 = (uint32_t)slapi_hexchar2int(s[4]);
  1226. uint32_t v5 = (uint32_t)slapi_hexchar2int(s[5]);
  1227. uint32_t v6 = (uint32_t)slapi_hexchar2int(s[6]);
  1228. uint32_t v7 = (uint32_t)slapi_hexchar2int(s[7]);
  1229. return (v0 << 28) | (v1 << 24) | (v2 << 20) | (v3 << 16) | (v4 << 12) | (v5 << 8) | (v6 << 4) | v7;
  1230. }
  1231. uint64_t
  1232. slapi_str_to_u64(const char *s)
  1233. {
  1234. uint64_t v0 = (uint64_t)slapi_hexchar2int(s[0]);
  1235. uint64_t v1 = (uint64_t)slapi_hexchar2int(s[1]);
  1236. uint64_t v2 = (uint64_t)slapi_hexchar2int(s[2]);
  1237. uint64_t v3 = (uint64_t)slapi_hexchar2int(s[3]);
  1238. uint64_t v4 = (uint64_t)slapi_hexchar2int(s[4]);
  1239. uint64_t v5 = (uint64_t)slapi_hexchar2int(s[5]);
  1240. uint64_t v6 = (uint64_t)slapi_hexchar2int(s[6]);
  1241. uint64_t v7 = (uint64_t)slapi_hexchar2int(s[7]);
  1242. uint64_t v8 = (uint64_t)slapi_hexchar2int(s[8]);
  1243. uint64_t v9 = (uint64_t)slapi_hexchar2int(s[9]);
  1244. uint64_t v10 = (uint64_t)slapi_hexchar2int(s[10]);
  1245. uint64_t v11 = (uint64_t)slapi_hexchar2int(s[11]);
  1246. uint64_t v12 = (uint64_t)slapi_hexchar2int(s[12]);
  1247. uint64_t v13 = (uint64_t)slapi_hexchar2int(s[13]);
  1248. uint64_t v14 = (uint64_t)slapi_hexchar2int(s[14]);
  1249. uint64_t v15 = (uint64_t)slapi_hexchar2int(s[15]);
  1250. return (v0 << 60) | (v1 << 56) | (v2 << 52) | (v3 << 48) | (v4 << 44) | (v5 << 40) |
  1251. (v6 << 36) | (v7 << 32) | (v8 << 28) | (v9 << 24) | (v10 << 20) | (v11 << 16) |
  1252. (v12 << 12) | (v13 << 8) | (v14 << 4) | v15;
  1253. }
  1254. /* PR_GetLibraryName does almost everything we need, and unfortunately
  1255. a little bit more - it adds "lib" to be beginning of the library
  1256. name if the library name does not end with the current platform
  1257. DLL suffix - so
  1258. foo.so -> /path/foo.so
  1259. libfoo.so -> /path/libfoo.so
  1260. BUT
  1261. foo -> /path/libfoo.so
  1262. libfoo -> /path/liblibfoo.so
  1263. /path/libfoo -> lib/path/libfoo.so
  1264. */
  1265. char *
  1266. slapi_get_plugin_name(const char *path, const char *lib)
  1267. {
  1268. const char *libstr = "/lib";
  1269. size_t libstrlen = 4;
  1270. char *fullname = PR_GetLibraryName(path, lib);
  1271. char *ptr = PL_strrstr(fullname, lib);
  1272. /* see if /lib was added */
  1273. if (ptr && ((ptr - fullname) >= libstrlen)) {
  1274. /* ptr is at the libname in fullname, and there is something before it */
  1275. ptr -= libstrlen; /* ptr now points at the "/" in "/lib" if it is there */
  1276. if (0 == PL_strncmp(ptr, libstr, libstrlen)) {
  1277. /* just copy the remainder of the string on top of here */
  1278. ptr++; /* ptr now points at the "l" in "/lib" - keep the "/" */
  1279. memmove(ptr, ptr+3, strlen(ptr+3)+1);
  1280. }
  1281. } else if ((NULL == path) && (0 == strncmp(fullname, "lib", 3))) {
  1282. /*
  1283. * case: /path/libfoo -> lib/path/libfoo.so
  1284. * remove "lib".
  1285. */
  1286. memmove(fullname, fullname+3, strlen(fullname)-2);
  1287. }
  1288. return fullname;
  1289. }