dsalib_confs.c 5.9 KB

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