util.c 31 KB

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