dsalib_conf.c 6.7 KB

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