dsalib_conf.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. #if defined( XP_WIN32 )
  39. #include <windows.h>
  40. #include <process.h>
  41. #endif
  42. #include <sys/types.h>
  43. #include <stdio.h>
  44. #include <stdlib.h>
  45. #include <string.h>
  46. #include "dsalib.h"
  47. #include <ldaplog.h>
  48. #include "portable.h"
  49. #include <ctype.h>
  50. #include "nspr.h"
  51. #define CONF_FILE_NAME "dse.ldif"
  52. #define CONF_SUFFIX "cn=config"
  53. DS_EXPORT_SYMBOL char *
  54. ds_get_var_name(int varnum)
  55. {
  56. if ( (varnum >= DS_CFG_MAX) || (varnum < 0) )
  57. return(NULL); /* failure */
  58. return(ds_cfg_info[varnum].dci_varname);
  59. }
  60. /*
  61. * Get config info.
  62. */
  63. DS_EXPORT_SYMBOL char **
  64. ds_get_config(int type)
  65. {
  66. char conffile[PATH_MAX];
  67. char *configdir;
  68. FILE *sf = NULL;
  69. char **conf_list = NULL;
  70. if ( (type != DS_REAL_CONFIG) && (type != DS_TMP_CONFIG) ) {
  71. ds_send_error("Invalid config file type.", 0);
  72. return(NULL);
  73. }
  74. if ( (configdir = ds_get_config_dir()) == NULL ) {
  75. ds_send_error("Cannot find configuration directory.", 0);
  76. return(NULL);
  77. }
  78. PR_snprintf(conffile, PATH_MAX, "%s/%s", configdir, CONF_FILE_NAME);
  79. if ( !(sf = fopen(conffile, "r")) ) {
  80. ds_send_error("could not read config file.", 1);
  81. return(NULL);
  82. }
  83. conf_list = ds_get_conf_from_file(sf);
  84. fclose(sf);
  85. if (!conf_list) {
  86. ds_send_error("failed to read the config file successfully.", 0);
  87. return(NULL);
  88. }
  89. return(conf_list);
  90. }
  91. /*
  92. * NOTE: the ordering of the following array elements must be kept in sync
  93. * with the ordering of the #defines in ../include/dsalib.h.
  94. */
  95. struct ds_cfg_info ds_cfg_info[] = {
  96. {"nsslapd-errorlog-level" },
  97. {"nsslapd-referral" },
  98. {"nsslapd-auditlog" },
  99. {"nsslapd-localhost" },
  100. {"nsslapd-port" },
  101. {"nsslapd-security" },
  102. {"nsslapd-secureport" },
  103. {"nsslapd-ssl3ciphers"},
  104. {"passwordstoragescheme"},
  105. {"nsslapd-accesslog"},
  106. {"nsslapd-errorlog"},
  107. {"nsslapd-rootdn"},
  108. {"nsslapd-rootpwstoragescheme"},
  109. {"nsslapd-suffix"},
  110. {"nsslapd-localuser"},
  111. {0}
  112. };
  113. /*
  114. * Open the config file and look for option "option". Return its
  115. * value, or NULL if the option was not found.
  116. */
  117. DS_EXPORT_SYMBOL char *
  118. ds_get_config_value( int option )
  119. {
  120. char **all, *value;
  121. int i;
  122. char *attr = ds_get_var_name(option);
  123. if (attr == NULL)
  124. return NULL;
  125. all = ds_get_config( DS_REAL_CONFIG );
  126. if ( all == NULL ) {
  127. return NULL;
  128. }
  129. for ( i = 0; all[ i ] != NULL; i++ ) {
  130. if (( value = strchr( all[ i ], ':' )) != NULL ) {
  131. *value = '\0';
  132. ++value;
  133. while (*value && isspace(*value))
  134. ++value;
  135. }
  136. if ( !strcasecmp( attr, all[ i ] )) {
  137. return strdup( value );
  138. }
  139. }
  140. return NULL;
  141. }
  142. static size_t
  143. count_quotes (const char* s)
  144. {
  145. size_t count = 0;
  146. const char* t = s;
  147. if (t) while ((t = strpbrk (t, "\"\\")) != NULL) {
  148. ++count;
  149. ++t;
  150. }
  151. return count;
  152. }
  153. DS_EXPORT_SYMBOL char*
  154. ds_enquote_config_value (int paramnum, char* s)
  155. {
  156. char* result;
  157. char* brkcharset = "\"\\ \t\r\n";
  158. char *encoded_quote = "22"; /* replace quote with \22 */
  159. int encoded_quote_len = strlen(encoded_quote);
  160. char *begin = s;
  161. if (*s && ! strpbrk (s, brkcharset) &&
  162. ! (paramnum == DS_AUDITFILE || paramnum == DS_ACCESSLOG ||
  163. #if defined( XP_WIN32 )
  164. paramnum == DS_SUFFIX ||
  165. #endif
  166. paramnum == DS_ERRORLOG)) {
  167. result = s;
  168. } else {
  169. char* t = malloc (strlen (s) + count_quotes (s) + 3);
  170. result = t;
  171. *t++ = '"';
  172. while (*s) {
  173. switch (*s) {
  174. case '"':
  175. /* convert escaped quotes by replacing the quote with
  176. escape code e.g. 22 so that \" is converted to \22 "*/
  177. if ((s > begin) && (*(s - 1) == '\\'))
  178. {
  179. strcpy(t, encoded_quote);
  180. t += encoded_quote_len;
  181. }
  182. else /* unescaped ", just replace with \22 "*/
  183. {
  184. *t++ = '\\';
  185. strcpy(t, encoded_quote);
  186. t += encoded_quote_len;
  187. }
  188. ++s;
  189. break;
  190. default:
  191. *t++ = *s++; /* just copy it */
  192. break;
  193. }
  194. }
  195. *t++ = '"';
  196. *t = '\0';
  197. }
  198. return result;
  199. }
  200. DS_EXPORT_SYMBOL char*
  201. ds_DNS_to_DN (char* DNS)
  202. {
  203. static const char* const RDN = "dc=";
  204. char* DN;
  205. char* dot;
  206. size_t components;
  207. if (DNS == NULL || *DNS == '\0') {
  208. return strdup ("");
  209. }
  210. components = 1;
  211. for (dot = strchr (DNS, '.'); dot != NULL; dot = strchr (dot + 1, '.')) {
  212. ++components;
  213. }
  214. DN = malloc (strlen (DNS) + (components * strlen(RDN)) + 1);
  215. strcpy (DN, RDN);
  216. for (dot = strchr (DNS, '.'); dot != NULL; dot = strchr (dot + 1, '.')) {
  217. *dot = '\0';
  218. strcat (DN, DNS);
  219. strcat (DN, ",");
  220. strcat (DN, RDN);
  221. DNS = dot + 1;
  222. *dot = '.';
  223. }
  224. strcat (DN, DNS);
  225. dn_normalize (DN);
  226. return DN;
  227. }