dsalib_confs.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. /*
  42. * Some of the simple conf stuff here. Must not call any
  43. * libadmin functions! This is needed by ds_config.c
  44. */
  45. #if defined( XP_WIN32 )
  46. #include <windows.h>
  47. #endif
  48. #include "dsalib.h"
  49. #include <sys/types.h>
  50. #include <stdio.h>
  51. #include <stdlib.h>
  52. #include <string.h>
  53. #include <ldif.h>
  54. #include <ctype.h>
  55. #include "nspr.h"
  56. #include "plstr.h"
  57. /*
  58. * Read the configuration info into a null-terminated list of strings.
  59. */
  60. DS_EXPORT_SYMBOL char **
  61. ds_get_conf_from_file(FILE *conf)
  62. {
  63. static char config_entry[] = "dn: cn=config";
  64. static int cfg_ent_len = sizeof(config_entry)-1;
  65. int listsize = 0;
  66. char **conf_list = NULL;
  67. char *entry = 0;
  68. int lineno = 0;
  69. while ((entry = ldif_get_entry(conf, &lineno))) {
  70. char *begin = entry;
  71. if (!PL_strncasecmp(entry, config_entry, cfg_ent_len)) {
  72. char *line = entry;
  73. while ((line = ldif_getline(&entry))) {
  74. char *type, *value;
  75. int vlen = 0;
  76. int rc;
  77. char *errmsg = NULL;
  78. if ( *line == '\n' || *line == '\0' ) {
  79. break;
  80. }
  81. /* this call modifies line */
  82. rc = ldif_parse_line(line, &type, &value, &vlen, &errmsg);
  83. if (rc != 0)
  84. {
  85. if ( errmsg != NULL ) {
  86. ds_send_error(errmsg, 0);
  87. PR_smprintf_free(errmsg);
  88. } else {
  89. ds_send_error("Unknown error processing config file", 0);
  90. }
  91. free(begin);
  92. return NULL;
  93. }
  94. listsize++;
  95. conf_list = (char **) realloc(conf_list,
  96. ((listsize + 1) * sizeof(char *)));
  97. /* this is the format expected by ds_get_config_value */
  98. conf_list[listsize - 1] = PR_smprintf("%s:%s", type, value);
  99. conf_list[listsize] = NULL; /* always null terminated */
  100. }
  101. }
  102. free(begin);
  103. }
  104. return(conf_list);
  105. }
  106. /*
  107. * Returns 1 if parm is in confline else 0
  108. */
  109. static int
  110. ds_parm_in_line(char *confline, char *parm)
  111. {
  112. int parm_size;
  113. if ( confline == NULL )
  114. return(0);
  115. if ( parm == NULL )
  116. return(0);
  117. parm_size = strlen(parm);
  118. if ( parm_size == (int)NULL )
  119. return(0);
  120. if ( PL_strncasecmp(confline, parm, parm_size) == 0 )
  121. if ( ((int) strlen(confline)) > parm_size )
  122. if ( confline[parm_size] == ':' )
  123. return(1);
  124. return(0);
  125. }
  126. /*
  127. * Gets the string that corresponds to the parameter supplied from the
  128. * list of config lines. Returns a malloc'd string.
  129. */
  130. DS_EXPORT_SYMBOL char *
  131. ds_get_value(char **ds_config, char *parm, int phase, int occurance)
  132. {
  133. char *line;
  134. int line_num = 0;
  135. int cur_phase = 0;
  136. int cur_occurance = 0;
  137. if ( (parm == NULL) || (ds_config == NULL) )
  138. return(NULL);
  139. if ( (phase < 0) || (occurance < 1) )
  140. return(NULL);
  141. line = ds_config[line_num];
  142. while ( line != NULL ) {
  143. if ( ds_parm_in_line(line, "database") )
  144. cur_phase++;
  145. if ( ds_parm_in_line(line, parm) ) { /* found it */
  146. if ( phase == cur_phase )
  147. if ( ++cur_occurance == occurance ) {
  148. /*
  149. * Use ldif_parse_line() so continuation markers are
  150. * handled correctly, etc.
  151. */
  152. char *errmsg, *type = NULL, *value = NULL, *tmpvalue = NULL;
  153. int ldif_rc, tmpvlen = 0;
  154. char *tmpline = strdup(line);
  155. if ( NULL == tmpline ) {
  156. ds_send_error(
  157. "ds_get_value() failed: strdup() returned NULL\n",
  158. 1 /* print errno */ );
  159. return(NULL);
  160. }
  161. ldif_rc = ldif_parse_line( tmpline, &type, &tmpvalue,
  162. &tmpvlen, &errmsg );
  163. if (ldif_rc < 0) {
  164. ds_send_error(errmsg, 0 /* do not print errno */);
  165. } else if (ldif_rc == 0) { /* value returned in place */
  166. value = strdup(tmpvalue);
  167. } else { /* malloc'd value */
  168. value = tmpvalue;
  169. }
  170. free(tmpline);
  171. if (errmsg) {
  172. PR_smprintf_free(errmsg);
  173. }
  174. return value;
  175. }
  176. }
  177. line_num++;
  178. line = ds_config[line_num];
  179. }
  180. return(NULL);
  181. }