util.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089
  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 <libgen.h>
  52. #include <pk11func.h>
  53. #include "slap.h"
  54. #include "prtime.h"
  55. #include "prinrval.h"
  56. #include "snmp_collator.h"
  57. #define UTIL_ESCAPE_NONE 0
  58. #define UTIL_ESCAPE_HEX 1
  59. #define UTIL_ESCAPE_BACKSLASH 2
  60. #if defined( _WIN32 )
  61. #define _PSEP "\\"
  62. #define _PSEP2 "\\\\"
  63. #define _CSEP '\\'
  64. #else
  65. #define _PSEP "/"
  66. #define _PSEP2 "//"
  67. #define _CSEP '/'
  68. #endif
  69. static int special_np(unsigned char c)
  70. {
  71. if (c == '\\') {
  72. return UTIL_ESCAPE_BACKSLASH;
  73. }
  74. if (c < 32 || c > 126 || c == '"') {
  75. return UTIL_ESCAPE_HEX;
  76. }
  77. return UTIL_ESCAPE_NONE;
  78. }
  79. static int special_np_and_punct(unsigned char c)
  80. {
  81. if (c == '\\') {
  82. return UTIL_ESCAPE_BACKSLASH;
  83. }
  84. if (c < 32 || c > 126 || c == '"' || c == '*') {
  85. return UTIL_ESCAPE_HEX;
  86. }
  87. return UTIL_ESCAPE_NONE;
  88. }
  89. static int special_filter(unsigned char c)
  90. {
  91. /*
  92. * Escape all non-printing chars and double-quotes in addition
  93. * to those required by RFC 2254 so that we can use the string
  94. * in log files.
  95. */
  96. return (c < 32 ||
  97. c > 126 ||
  98. c == '*' ||
  99. c == '(' ||
  100. c == ')' ||
  101. c == '\\' ||
  102. c == '"') ? UTIL_ESCAPE_HEX : UTIL_ESCAPE_NONE;
  103. }
  104. static const char*
  105. do_escape_string (
  106. const char* str,
  107. int len, /* -1 means str is nul-terminated */
  108. char buf[BUFSIZ],
  109. int (*special)(unsigned char)
  110. )
  111. {
  112. const char* s;
  113. const char* last;
  114. int esc;
  115. if (str == NULL) {
  116. *buf = '\0';
  117. return buf;
  118. }
  119. if (len == -1) len = strlen (str);
  120. if (len == 0) return str;
  121. last = str + len - 1;
  122. for (s = str; s <= last; ++s) {
  123. if ( (esc = (*special)((unsigned char)*s))) {
  124. const char* first = str;
  125. char* bufNext = buf;
  126. int bufSpace = BUFSIZ - 4;
  127. while (1) {
  128. if (bufSpace < (s - first)) s = first + bufSpace - 1;
  129. if (s > first) {
  130. memcpy (bufNext, first, s - first);
  131. bufNext += (s - first);
  132. bufSpace -= (s - first);
  133. }
  134. if (s > last) {
  135. break;
  136. }
  137. do {
  138. if (esc == UTIL_ESCAPE_BACKSLASH) {
  139. /* *s is '\\' */
  140. /* If *(s+1) and *(s+2) are both hex digits,
  141. * the char is already escaped. */
  142. if (isxdigit(*(s+1)) && isxdigit(*(s+2))) {
  143. memcpy(bufNext, s, 3);
  144. bufNext += 3;
  145. bufSpace -= 3;
  146. s += 2;
  147. } else {
  148. *bufNext++ = *s; --bufSpace;
  149. }
  150. } else { /* UTIL_ESCAPE_HEX */
  151. *bufNext++ = '\\'; --bufSpace;
  152. if (bufSpace < 3) {
  153. memcpy(bufNext, "..", 2);
  154. bufNext += 2;
  155. goto bail;
  156. }
  157. PR_snprintf(bufNext, 3, "%02x", *(unsigned char*)s);
  158. bufNext += 2; bufSpace -= 2;
  159. }
  160. } while (++s <= last &&
  161. (esc = (*special)((unsigned char)*s)));
  162. if (s > last) break;
  163. first = s;
  164. while ( (esc = (*special)((unsigned char)*s)) == UTIL_ESCAPE_NONE && s <= last) ++s;
  165. }
  166. bail:
  167. *bufNext = '\0';
  168. return buf;
  169. }
  170. }
  171. return str;
  172. }
  173. /*
  174. * Function: escape_string
  175. * Arguments: str: string
  176. * buf: a char array of BUFSIZ length, in which the escaped string will
  177. * be returned.
  178. * Returns: a pointer to buf, if str==NULL or it needed to be escaped, or
  179. * str itself otherwise.
  180. *
  181. * This function should only be used for generating loggable strings.
  182. */
  183. const char*
  184. escape_string (const char* str, char buf[BUFSIZ])
  185. {
  186. return do_escape_string(str,-1,buf,special_np);
  187. }
  188. const char*
  189. escape_string_with_punctuation(const char* str, char buf[BUFSIZ])
  190. {
  191. return do_escape_string(str,-1,buf,special_np_and_punct);
  192. }
  193. const char*
  194. escape_filter_value(const char* str, int len, char buf[BUFSIZ])
  195. {
  196. return do_escape_string(str,len,buf,special_filter);
  197. }
  198. /*
  199. ** This function takes a quoted attribute value of the form "abc",
  200. ** and strips off the enclosing quotes. It also deals with quoted
  201. ** characters by removing the preceeding '\' character.
  202. **
  203. */
  204. void
  205. strcpy_unescape_value( char *d, const char *s )
  206. {
  207. int gotesc = 0;
  208. const char *end = s + strlen(s);
  209. for ( ; *s; s++ )
  210. {
  211. switch ( *s )
  212. {
  213. case '"':
  214. break;
  215. case '\\':
  216. gotesc = 1;
  217. if ( s+2 < end ) {
  218. int n = hexchar2int( s[1] );
  219. if ( n >= 0 && n < 16 ) {
  220. int n2 = hexchar2int( s[2] );
  221. if ( n2 >= 0 ) {
  222. n = (n << 4) + n2;
  223. if (n == 0) { /* don't change \00 */
  224. *d++ = *s++;
  225. *d++ = *s++;
  226. *d++ = *s;
  227. } else { /* change \xx to a single char */
  228. *d++ = (char)n;
  229. s += 2;
  230. }
  231. gotesc = 0;
  232. }
  233. }
  234. }
  235. /* This is an escaped single character (like \"), so
  236. * just copy the special character and not the escape.
  237. * We need to be careful to not go past the end of
  238. * the string when the loop increments s. */
  239. if (gotesc && (s+1 < end)) {
  240. s++;
  241. *d++ = *s;
  242. gotesc = 0;
  243. }
  244. break;
  245. default:
  246. *d++ = *s;
  247. break;
  248. }
  249. }
  250. *d = '\0';
  251. }
  252. /* functions to convert between an entry and a set of mods */
  253. int slapi_mods2entry (Slapi_Entry **e, const char *idn, LDAPMod **iattrs)
  254. {
  255. int i, rc = LDAP_SUCCESS;
  256. LDAPMod **attrs= NULL;
  257. PR_ASSERT (idn);
  258. PR_ASSERT (iattrs);
  259. PR_ASSERT (e);
  260. attrs = normalize_mods2bvals((const LDAPMod **)iattrs);
  261. PR_ASSERT (attrs);
  262. /* Construct an entry */
  263. *e = slapi_entry_alloc();
  264. PR_ASSERT (*e);
  265. slapi_entry_init(*e, slapi_ch_strdup(idn), NULL);
  266. for (i = 0; rc==LDAP_SUCCESS && attrs[ i ]!=NULL; i++)
  267. {
  268. char *normtype;
  269. Slapi_Value **vals;
  270. normtype = slapi_attr_syntax_normalize(attrs[ i ]->mod_type);
  271. valuearray_init_bervalarray(attrs[ i ]->mod_bvalues, &vals);
  272. if (strcasecmp(normtype, SLAPI_USERPWD_ATTR) == 0)
  273. {
  274. pw_encodevals(vals);
  275. }
  276. /* set entry uniqueid - also adds attribute to the list */
  277. if (strcasecmp(normtype, SLAPI_ATTR_UNIQUEID) == 0) {
  278. if (vals) {
  279. slapi_entry_set_uniqueid (*e,
  280. slapi_ch_strdup (slapi_value_get_string(vals[0])));
  281. } else {
  282. rc = LDAP_NO_SUCH_ATTRIBUTE;
  283. }
  284. } else {
  285. rc = slapi_entry_add_values_sv(*e, normtype, vals);
  286. }
  287. valuearray_free(&vals);
  288. if (rc != LDAP_SUCCESS)
  289. {
  290. LDAPDebug2Args(LDAP_DEBUG_ANY,
  291. "slapi_add_internal: add_values for type %s failed (rc: %d)\n",
  292. normtype, rc );
  293. slapi_entry_free (*e);
  294. *e = NULL;
  295. }
  296. slapi_ch_free((void **) &normtype);
  297. }
  298. freepmods(attrs);
  299. return rc;
  300. }
  301. int slapi_entry2mods (const Slapi_Entry *e, char **dn, LDAPMod ***attrs)
  302. {
  303. Slapi_Mods smods;
  304. Slapi_Attr *attr;
  305. Slapi_Value **va;
  306. char *type;
  307. int rc;
  308. PR_ASSERT (e && attrs);
  309. if (dn)
  310. *dn = slapi_ch_strdup (slapi_entry_get_dn ((Slapi_Entry *)e));
  311. slapi_mods_init (&smods, 0);
  312. rc = slapi_entry_first_attr(e, &attr);
  313. while (rc == 0)
  314. {
  315. if ( NULL != ( va = attr_get_present_values( attr ))) {
  316. slapi_attr_get_type(attr, &type);
  317. slapi_mods_add_mod_values(&smods, LDAP_MOD_ADD, type, va );
  318. }
  319. rc = slapi_entry_next_attr(e, attr, &attr);
  320. }
  321. *attrs = slapi_mods_get_ldapmods_passout (&smods);
  322. slapi_mods_done (&smods);
  323. return 0;
  324. }
  325. /******************************************************************************
  326. *
  327. * normalize_mods2bvals
  328. *
  329. * Return value: normalized mods
  330. * The values/bvals are all duplicated in this function since
  331. * the normalized mods are freed with ldap_mods_free by the caller.
  332. *
  333. *******************************************************************************/
  334. LDAPMod **
  335. normalize_mods2bvals(const LDAPMod **mods)
  336. {
  337. int w, x, vlen, num_values, num_mods;
  338. LDAPMod **normalized_mods;
  339. if (mods == NULL)
  340. {
  341. return NULL;
  342. }
  343. /* first normalize the mods so they are bvalues */
  344. /* count the number of mods -- sucks but should be small */
  345. num_mods = 1;
  346. for (w=0; mods[w] != NULL; w++) num_mods++;
  347. normalized_mods = (LDAPMod **) slapi_ch_calloc(num_mods, sizeof(LDAPMod *));
  348. for (w = 0; mods[w] != NULL; w++)
  349. {
  350. Slapi_Attr a = {0};
  351. slapi_attr_init(&a, mods[w]->mod_type);
  352. int is_dn_syntax = 0;
  353. struct berval **normmbvp = NULL;
  354. /* Check if the type of the to-be-added values has DN syntax
  355. * or not. */
  356. if (slapi_attr_is_dn_syntax_attr(&a)) {
  357. is_dn_syntax = 1;
  358. }
  359. attr_done(&a);
  360. /* copy each mod into a normalized modbvalue */
  361. normalized_mods[w] = (LDAPMod *) slapi_ch_calloc(1, sizeof(LDAPMod));
  362. normalized_mods[w]->mod_op = mods[w]->mod_op | LDAP_MOD_BVALUES;
  363. normalized_mods[w]->mod_type = slapi_ch_strdup(mods[w]->mod_type);
  364. /*
  365. * count the number of values -- kinda sucks but probably
  366. * less expensive then reallocing, and num_values
  367. * should typically be very small
  368. */
  369. num_values = 0;
  370. if (mods[w]->mod_op & LDAP_MOD_BVALUES)
  371. {
  372. for (x = 0; mods[w]->mod_bvalues != NULL &&
  373. mods[w]->mod_bvalues[x] != NULL; x++)
  374. {
  375. num_values++;
  376. }
  377. } else {
  378. for (x = 0; mods[w]->mod_values[x] != NULL &&
  379. mods[w]->mod_values[x] != NULL; x++)
  380. {
  381. num_values++;
  382. }
  383. }
  384. if (num_values > 0)
  385. {
  386. normalized_mods[w]->mod_bvalues = (struct berval **)
  387. slapi_ch_calloc(num_values + 1, sizeof(struct berval *));
  388. } else {
  389. normalized_mods[w]->mod_bvalues = NULL;
  390. }
  391. if (mods[w]->mod_op & LDAP_MOD_BVALUES)
  392. {
  393. struct berval **mbvp = NULL;
  394. for (mbvp = mods[w]->mod_bvalues,
  395. normmbvp = normalized_mods[w]->mod_bvalues;
  396. mbvp && *mbvp; mbvp++, normmbvp++)
  397. {
  398. if (is_dn_syntax) {
  399. Slapi_DN *sdn = slapi_sdn_new_dn_byref((*mbvp)->bv_val);
  400. if (slapi_sdn_get_dn(sdn)) {
  401. *normmbvp =
  402. (struct berval *)slapi_ch_malloc(sizeof(struct berval));
  403. (*normmbvp)->bv_val =
  404. slapi_ch_strdup(slapi_sdn_get_dn(sdn));
  405. (*normmbvp)->bv_len = slapi_sdn_get_ndn_len(sdn);
  406. } else {
  407. /* normalization failed; use the original */
  408. *normmbvp = ber_bvdup(*mbvp);
  409. }
  410. slapi_sdn_free(&sdn);
  411. } else {
  412. *normmbvp = ber_bvdup(*mbvp);
  413. }
  414. }
  415. } else {
  416. char **mvp = NULL;
  417. for (mvp = mods[w]->mod_values,
  418. normmbvp = normalized_mods[w]->mod_bvalues;
  419. mvp && *mvp; mvp++, normmbvp++)
  420. {
  421. *normmbvp =
  422. (struct berval *)slapi_ch_malloc(sizeof(struct berval));
  423. vlen = strlen(*mvp);
  424. if (is_dn_syntax) {
  425. Slapi_DN *sdn = slapi_sdn_new_dn_byref(*mvp);
  426. if (slapi_sdn_get_dn(sdn)) {
  427. *normmbvp =
  428. (struct berval *)slapi_ch_malloc(sizeof(struct berval));
  429. (*normmbvp)->bv_val =
  430. slapi_ch_strdup(slapi_sdn_get_dn(sdn));
  431. (*normmbvp)->bv_len = slapi_sdn_get_ndn_len(sdn);
  432. } else {
  433. /* normalization failed; use the original */
  434. (*normmbvp)->bv_val = slapi_ch_malloc(vlen + 1);
  435. memcpy((*normmbvp)->bv_val, *mvp, vlen);
  436. (*normmbvp)->bv_val[vlen] = '\0';
  437. (*normmbvp)->bv_len = vlen;
  438. }
  439. slapi_sdn_free(&sdn);
  440. } else {
  441. (*normmbvp)->bv_val = slapi_ch_malloc(vlen + 1);
  442. memcpy((*normmbvp)->bv_val, *mvp, vlen);
  443. (*normmbvp)->bv_val[vlen] = '\0';
  444. (*normmbvp)->bv_len = vlen;
  445. }
  446. }
  447. }
  448. PR_ASSERT(normmbvp - normalized_mods[w]->mod_bvalues <= num_values);
  449. /* don't forget to null terminate it */
  450. if (num_values > 0)
  451. {
  452. *normmbvp = NULL;
  453. }
  454. }
  455. /* don't forget to null terminate the normalize list of mods */
  456. normalized_mods[w] = NULL;
  457. return(normalized_mods);
  458. }
  459. /*
  460. * Return true if the given path is an absolute path, false otherwise
  461. */
  462. int
  463. is_abspath(const char *path)
  464. {
  465. if (path == NULL || *path == '\0') {
  466. return 0; /* empty path is not absolute? */
  467. }
  468. #if defined( XP_WIN32 )
  469. if (path[0] == '/' || path[0] == '\\' ||
  470. (isalpha(path[0]) && (path[1] == ':'))) {
  471. return 1; /* Windows abs path */
  472. }
  473. #else
  474. if (path[0] == '/') {
  475. return 1; /* unix abs path */
  476. }
  477. #endif
  478. return 0; /* not an abs path */
  479. }
  480. static void
  481. clean_path(char **norm_path)
  482. {
  483. char **np;
  484. for (np = norm_path; np && *np; np++)
  485. slapi_ch_free_string(np);
  486. slapi_ch_free((void **)&norm_path);
  487. }
  488. static char **
  489. normalize_path(char *path)
  490. {
  491. char *dname = NULL;
  492. char *dnamep = NULL;
  493. char **dirs = NULL;
  494. char **rdirs = NULL;
  495. char **dp = NULL;
  496. char **rdp;
  497. int elimdots = 0;
  498. if (NULL == path || '\0' == *path) {
  499. return NULL;
  500. }
  501. dirs = (char **)slapi_ch_calloc(strlen(path), sizeof(char *));
  502. rdirs = (char **)slapi_ch_calloc(strlen(path), sizeof(char *));
  503. dp = dirs;
  504. dname = slapi_ch_strdup(path);
  505. do {
  506. dnamep = strrchr(dname, _CSEP);
  507. if (NULL == dnamep) {
  508. dnamep = dname;
  509. } else {
  510. *dnamep = '\0';
  511. dnamep++;
  512. }
  513. if (0 != strcmp(dnamep, ".") && strlen(dnamep) > 0) {
  514. *dp++ = slapi_ch_strdup(dnamep); /* rm "/./" and "//" in the path */
  515. }
  516. } while ( dnamep > dname /* == -> no more _CSEP */ );
  517. slapi_ch_free_string(&dname);
  518. /* remove "xxx/.." in the path */
  519. for (dp = dirs, rdp = rdirs; dp && *dp; dp++) {
  520. while (*dp && 0 == strcmp(*dp, "..")) {
  521. dp++;
  522. elimdots++;
  523. }
  524. if (elimdots > 0) {
  525. elimdots--;
  526. } else if (*dp) {
  527. *rdp++ = slapi_ch_strdup(*dp);
  528. }
  529. }
  530. /* reverse */
  531. for (--rdp, dp = rdirs; rdp >= dp && rdp >= rdirs; --rdp, dp++) {
  532. char *tmpp = *dp;
  533. *dp = *rdp;
  534. *rdp = tmpp;
  535. }
  536. clean_path(dirs);
  537. return rdirs;
  538. }
  539. /*
  540. * Take "relpath" and prepend the current working directory to it
  541. * if it isn't an absolute pathname already. The caller is responsible
  542. * for freeing the returned string.
  543. */
  544. char *
  545. rel2abspath( char *relpath )
  546. {
  547. return rel2abspath_ext( relpath, NULL );
  548. }
  549. char *
  550. rel2abspath_ext( char *relpath, char *cwd )
  551. {
  552. char abspath[ MAXPATHLEN + 1 ];
  553. char *retpath = NULL;
  554. #if defined( _WIN32 )
  555. CHAR szDrive[_MAX_DRIVE];
  556. CHAR szDir[_MAX_DIR];
  557. CHAR szFname[_MAX_FNAME];
  558. CHAR szExt[_MAX_EXT];
  559. #endif
  560. if ( relpath == NULL ) {
  561. return NULL;
  562. }
  563. #if defined( _WIN32 )
  564. memset (&szDrive, 0, sizeof (szDrive));
  565. memset (&szDir, 0, sizeof (szDir));
  566. memset (&szFname, 0, sizeof (szFname));
  567. memset (&szExt, 0, sizeof (szExt));
  568. _splitpath( relpath, szDrive, szDir, szFname, szExt );
  569. if( szDrive[0] && szDir[0] )
  570. return( slapi_ch_strdup( relpath ));
  571. #endif
  572. if ( relpath[ 0 ] == _CSEP ) { /* absolute path */
  573. PR_snprintf(abspath, sizeof(abspath), "%s", relpath);
  574. } else { /* relative path */
  575. if ( NULL == cwd ) {
  576. if ( getcwd( abspath, MAXPATHLEN ) == NULL ) {
  577. perror( "getcwd" );
  578. LDAPDebug( LDAP_DEBUG_ANY, "Cannot determine current directory\n",
  579. 0, 0, 0 );
  580. exit( 1 );
  581. }
  582. } else {
  583. PR_snprintf(abspath, sizeof(abspath), "%s", cwd);
  584. }
  585. if ( strlen( relpath ) + strlen( abspath ) + 1 > MAXPATHLEN ) {
  586. LDAPDebug( LDAP_DEBUG_ANY, "Pathname \"%s" _PSEP "%s\" too long\n",
  587. abspath, relpath, 0 );
  588. exit( 1 );
  589. }
  590. if ( strcmp( relpath, "." )) {
  591. if ( abspath[ 0 ] != '\0' &&
  592. abspath[ strlen( abspath ) - 1 ] != _CSEP )
  593. {
  594. PL_strcatn( abspath, sizeof(abspath), _PSEP );
  595. }
  596. PL_strcatn( abspath, sizeof(abspath), relpath );
  597. }
  598. }
  599. retpath = slapi_ch_strdup(abspath);
  600. /* if there's no '.' or separators, no need to call normalize_path */
  601. if (NULL != strchr(abspath, '.') || NULL != strstr(abspath, _PSEP))
  602. {
  603. char **norm_path = normalize_path(abspath);
  604. char **np, *rp;
  605. int pathlen = strlen(abspath) + 1;
  606. int usedlen = 0;
  607. for (np = norm_path, rp = retpath; np && *np; np++) {
  608. int thislen = strlen(*np) + 1;
  609. if (0 != strcmp(*np, _PSEP))
  610. PR_snprintf(rp, pathlen - usedlen, "%c%s", _CSEP, *np);
  611. rp += thislen;
  612. usedlen += thislen;
  613. }
  614. clean_path(norm_path);
  615. }
  616. return retpath;
  617. }
  618. /*
  619. * Allocate a buffer large enough to hold a berval's
  620. * value and a terminating null byte. The returned buffer
  621. * is null-terminated. Returns NULL if bval is NULL or if
  622. * bval->bv_val is NULL.
  623. */
  624. char *
  625. slapi_berval_get_string_copy(const struct berval *bval)
  626. {
  627. char *return_value = NULL;
  628. if (NULL != bval && NULL != bval->bv_val)
  629. {
  630. return_value = slapi_ch_malloc(bval->bv_len + 1);
  631. memcpy(return_value, bval->bv_val, bval->bv_len);
  632. return_value[bval->bv_len] = '\0';
  633. }
  634. return return_value;
  635. }
  636. /* Takes a return code supposed to be errno or from a plugin
  637. which we don't expect to see and prints a handy log message */
  638. void slapd_nasty(char* str, int c, int err)
  639. {
  640. char *msg = NULL;
  641. char buffer[100];
  642. PR_snprintf(buffer,sizeof(buffer), "%s BAD %d",str,c);
  643. LDAPDebug(LDAP_DEBUG_ANY,"%s, err=%d %s\n",buffer,err,(msg = strerror( err )) ? msg : "");
  644. }
  645. /* ***************************************************
  646. Random function (very similar to rand_r())
  647. *************************************************** */
  648. int
  649. slapi_rand_r(unsigned int *randx)
  650. {
  651. if (*randx)
  652. {
  653. PK11_RandomUpdate(randx, sizeof(*randx));
  654. }
  655. PK11_GenerateRandom((unsigned char *)randx, (int)sizeof(*randx));
  656. return (int)(*randx & 0x7FFFFFFF);
  657. }
  658. /* ***************************************************
  659. Random function (very similar to rand_r() but takes and returns an array)
  660. Note: there is an identical function in plugins/pwdstorage/ssha_pwd.c.
  661. That module can't use a libslapd function because the module is included
  662. in libds_admin, which doesn't link to libslapd. Eventually, shared
  663. functions should be moved to a shared library.
  664. *************************************************** */
  665. void
  666. slapi_rand_array(void *randx, size_t len)
  667. {
  668. PK11_RandomUpdate(randx, len);
  669. PK11_GenerateRandom((unsigned char *)randx, (int)len);
  670. }
  671. /* ***************************************************
  672. Random function (very similar to rand()...)
  673. *************************************************** */
  674. int
  675. slapi_rand()
  676. {
  677. unsigned int randx = 0;
  678. return slapi_rand_r(&randx);
  679. }
  680. /************************************************************************
  681. Function: DS_Sleep(PRIntervalTime ticks)
  682. Purpose: To replace PR_Sleep()
  683. Author: Scott Hopson <[email protected]>
  684. Description:
  685. Causes the current thread to wait for ticks number of intervals.
  686. In UNIX this is accomplished by using select()
  687. which should be supported across all UNIX platforms.
  688. In WIN32 we simply use the Sleep() function which yields
  689. for the number of milliseconds specified.
  690. ************************************************************************/
  691. #if defined(_WIN32)
  692. #include "windows.h"
  693. void DS_Sleep(PRIntervalTime ticks)
  694. {
  695. DWORD mSecs = PR_IntervalToMilliseconds(ticks);
  696. Sleep(mSecs);
  697. }
  698. #else /*** UNIX ***/
  699. #include <sys/time.h>
  700. void DS_Sleep(PRIntervalTime ticks)
  701. {
  702. long mSecs = PR_IntervalToMilliseconds(ticks);
  703. struct timeval tm;
  704. tm.tv_sec = mSecs / 1000;
  705. tm.tv_usec = (mSecs % 1000) * 1000;
  706. select(0,NULL,NULL,NULL,&tm);
  707. }
  708. #endif
  709. /*****************************************************************************
  710. * strarray2str(): convert the array of strings in "a" into a single
  711. * space-separated string like:
  712. * str1 str2 str3
  713. * If buf is too small, the result will be truncated and end with "...".
  714. * If include_quotes is non-zero, double quote marks are included around
  715. * the string, e.g.,
  716. * "str2 str2 str3"
  717. *
  718. * Returns: 0 if completely successful and -1 if result is truncated.
  719. */
  720. int
  721. strarray2str( char **a, char *buf, size_t buflen, int include_quotes )
  722. {
  723. int rc = 0; /* optimistic */
  724. char *p = buf;
  725. size_t totlen = 0;
  726. if ( include_quotes ) {
  727. if ( buflen < 3 ) {
  728. return -1; /* not enough room for the quote marks! */
  729. }
  730. *p++ = '"';
  731. ++totlen;
  732. }
  733. if ( NULL != a ) {
  734. int ii;
  735. size_t len = 0;
  736. for ( ii = 0; a[ ii ] != NULL; ii++ ) {
  737. if ( ii > 0 ) {
  738. *p++ = ' ';
  739. totlen++;
  740. }
  741. len = strlen( a[ ii ]);
  742. if ( totlen + len > buflen - 5 ) {
  743. strcpy ( p, "..." );
  744. p += 3;
  745. totlen += 3;
  746. rc = -1;
  747. break; /* result truncated */
  748. } else {
  749. strcpy( p, a[ ii ]);
  750. p += len;
  751. totlen += len;
  752. }
  753. }
  754. }
  755. if ( include_quotes ) {
  756. *p++ = '"';
  757. ++totlen;
  758. }
  759. buf[ totlen ] = '\0';
  760. return( rc );
  761. }
  762. /*****************************************************************************/
  763. /* Changes the ownership of the given file/directory if not
  764. already the owner
  765. Returns 0 upon success or non-zero otherwise, usually -1 if
  766. some system error occurred
  767. */
  768. #ifndef _WIN32
  769. int
  770. slapd_chown_if_not_owner(const char *filename, uid_t uid, gid_t gid)
  771. {
  772. struct stat statbuf;
  773. int result = 1;
  774. if (!filename)
  775. return result;
  776. memset(&statbuf, '\0', sizeof(statbuf));
  777. if (!(result = stat(filename, &statbuf)))
  778. {
  779. if (((uid != -1) && (uid != statbuf.st_uid)) ||
  780. ((gid != -1) && (gid != statbuf.st_gid)))
  781. {
  782. result = chown(filename, uid, gid);
  783. }
  784. }
  785. return result;
  786. }
  787. #endif
  788. /*
  789. * Compare 2 pathes
  790. * Paths could contain ".", "..", "//" in the path, thus normalize them first.
  791. * One or two of the paths could be a relative path.
  792. */
  793. int
  794. slapd_comp_path(char *p0, char *p1)
  795. {
  796. int rval = 0;
  797. char *norm_p0 = rel2abspath(p0);
  798. char *norm_p1 = rel2abspath(p1);
  799. rval = strcmp(norm_p0, norm_p1);
  800. slapi_ch_free_string(&norm_p0);
  801. slapi_ch_free_string(&norm_p1);
  802. return rval;
  803. }
  804. /*
  805. Takes an unsigned char value and converts it to a hex string.
  806. The string s is written, and the caller must ensure s has enough
  807. space. For hex numbers, the upper argument says to use a-f or A-F.
  808. The return value is the address of the next char after the last one written.
  809. */
  810. char *
  811. slapi_u8_to_hex(uint8_t val, char *s, uint8_t upper) {
  812. static char ldigits[] = "0123456789abcdef";
  813. static char udigits[] = "0123456789ABCDEF";
  814. char *digits;
  815. if (upper) {
  816. digits = udigits;
  817. } else {
  818. digits = ldigits;
  819. }
  820. s[0] = digits[val >> 4];
  821. s[1] = digits[val & 0xf];
  822. return &s[2];
  823. }
  824. char *
  825. slapi_u16_to_hex(uint16_t val, char *s, uint8_t upper) {
  826. static char ldigits[] = "0123456789abcdef";
  827. static char udigits[] = "0123456789ABCDEF";
  828. char *digits;
  829. if (upper) {
  830. digits = udigits;
  831. } else {
  832. digits = ldigits;
  833. }
  834. s[0] = digits[val >> 12];
  835. s[1] = digits[(val >> 8) & 0xf];
  836. s[2] = digits[(val >> 4) & 0xf];
  837. s[3] = digits[val & 0xf];
  838. return &s[4];
  839. }
  840. char *
  841. slapi_u32_to_hex(uint32_t val, char *s, uint8_t upper) {
  842. static char ldigits[] = "0123456789abcdef";
  843. static char udigits[] = "0123456789ABCDEF";
  844. char *digits;
  845. if (upper) {
  846. digits = udigits;
  847. } else {
  848. digits = ldigits;
  849. }
  850. s[0] = digits[val >> 28];
  851. s[1] = digits[(val >> 24) & 0xf];
  852. s[2] = digits[(val >> 20) & 0xf];
  853. s[3] = digits[(val >> 16) & 0xf];
  854. s[4] = digits[(val >> 12) & 0xf];
  855. s[5] = digits[(val >> 8) & 0xf];
  856. s[6] = digits[(val >> 4) & 0xf];
  857. s[7] = digits[val & 0xf];
  858. return &s[8];
  859. }
  860. char *
  861. slapi_u64_to_hex(uint64_t val, char *s, uint8_t upper) {
  862. static char ldigits[] = "0123456789abcdef";
  863. static char udigits[] = "0123456789ABCDEF";
  864. char *digits;
  865. if (upper) {
  866. digits = udigits;
  867. } else {
  868. digits = ldigits;
  869. }
  870. s[0] = digits[val >> 60];
  871. s[1] = digits[(val >> 56) & 0xf];
  872. s[2] = digits[(val >> 52) & 0xf];
  873. s[3] = digits[(val >> 48) & 0xf];
  874. s[4] = digits[(val >> 44) & 0xf];
  875. s[5] = digits[(val >> 40) & 0xf];
  876. s[6] = digits[(val >> 36) & 0xf];
  877. s[7] = digits[(val >> 32) & 0xf];
  878. s[8] = digits[(val >> 28) & 0xf];
  879. s[9] = digits[(val >> 24) & 0xf];
  880. s[10] = digits[(val >> 20) & 0xf];
  881. s[11] = digits[(val >> 16) & 0xf];
  882. s[12] = digits[(val >> 12) & 0xf];
  883. s[13] = digits[(val >> 8) & 0xf];
  884. s[14] = digits[(val >> 4) & 0xf];
  885. s[15] = digits[val & 0xf];
  886. return &s[16];
  887. }
  888. static const int char2intarray[] = {
  889. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  890. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  891. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  892. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
  893. -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  894. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  895. -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  896. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  897. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  898. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  899. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  900. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  901. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  902. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  903. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  904. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
  905. };
  906. static const int char2intarray_size = sizeof(char2intarray)/sizeof(char2intarray[0]);
  907. int
  908. slapi_hexchar2int(char c)
  909. {
  910. return char2intarray[(unsigned char)c];
  911. }
  912. uint8_t
  913. slapi_str_to_u8(const char *s)
  914. {
  915. uint8_t v0 = (uint8_t)slapi_hexchar2int(s[0]);
  916. uint8_t v1 = (uint8_t)slapi_hexchar2int(s[1]);
  917. return (v0 << 4) | v1;
  918. }
  919. uint16_t
  920. slapi_str_to_u16(const char *s)
  921. {
  922. uint16_t v0 = (uint16_t)slapi_hexchar2int(s[0]);
  923. uint16_t v1 = (uint16_t)slapi_hexchar2int(s[1]);
  924. uint16_t v2 = (uint16_t)slapi_hexchar2int(s[2]);
  925. uint16_t v3 = (uint16_t)slapi_hexchar2int(s[3]);
  926. return (v0 << 12) | (v1 << 8) | (v2 << 4) | v3;
  927. }
  928. uint32_t
  929. slapi_str_to_u32(const char *s)
  930. {
  931. uint32_t v0 = (uint32_t)slapi_hexchar2int(s[0]);
  932. uint32_t v1 = (uint32_t)slapi_hexchar2int(s[1]);
  933. uint32_t v2 = (uint32_t)slapi_hexchar2int(s[2]);
  934. uint32_t v3 = (uint32_t)slapi_hexchar2int(s[3]);
  935. uint32_t v4 = (uint32_t)slapi_hexchar2int(s[4]);
  936. uint32_t v5 = (uint32_t)slapi_hexchar2int(s[5]);
  937. uint32_t v6 = (uint32_t)slapi_hexchar2int(s[6]);
  938. uint32_t v7 = (uint32_t)slapi_hexchar2int(s[7]);
  939. return (v0 << 28) | (v1 << 24) | (v2 << 20) | (v3 << 16) | (v4 << 12) | (v5 << 8) | (v6 << 4) | v7;
  940. }
  941. uint64_t
  942. slapi_str_to_u64(const char *s)
  943. {
  944. uint64_t v0 = (uint64_t)slapi_hexchar2int(s[0]);
  945. uint64_t v1 = (uint64_t)slapi_hexchar2int(s[1]);
  946. uint64_t v2 = (uint64_t)slapi_hexchar2int(s[2]);
  947. uint64_t v3 = (uint64_t)slapi_hexchar2int(s[3]);
  948. uint64_t v4 = (uint64_t)slapi_hexchar2int(s[4]);
  949. uint64_t v5 = (uint64_t)slapi_hexchar2int(s[5]);
  950. uint64_t v6 = (uint64_t)slapi_hexchar2int(s[6]);
  951. uint64_t v7 = (uint64_t)slapi_hexchar2int(s[7]);
  952. uint64_t v8 = (uint64_t)slapi_hexchar2int(s[8]);
  953. uint64_t v9 = (uint64_t)slapi_hexchar2int(s[9]);
  954. uint64_t v10 = (uint64_t)slapi_hexchar2int(s[10]);
  955. uint64_t v11 = (uint64_t)slapi_hexchar2int(s[11]);
  956. uint64_t v12 = (uint64_t)slapi_hexchar2int(s[12]);
  957. uint64_t v13 = (uint64_t)slapi_hexchar2int(s[13]);
  958. uint64_t v14 = (uint64_t)slapi_hexchar2int(s[14]);
  959. uint64_t v15 = (uint64_t)slapi_hexchar2int(s[15]);
  960. return (v0 << 60) | (v1 << 56) | (v2 << 52) | (v3 << 48) | (v4 << 44) | (v5 << 40) |
  961. (v6 << 36) | (v7 << 32) | (v8 << 28) | (v9 << 24) | (v10 << 20) | (v11 << 16) |
  962. (v12 << 12) | (v13 << 8) | (v14 << 4) | v15;
  963. }