dsalib_conf.c 6.7 KB

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