dsalib_html.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. #endif
  44. #include "dsalib.h"
  45. #include "prprf.h"
  46. #include <sys/types.h>
  47. #include <string.h>
  48. #include <stdio.h>
  49. #include <stdlib.h>
  50. #include <errno.h>
  51. /* holds the contents of the POSTed data from stdin as an array
  52. of key/value pairs parsed from the key=value input */
  53. static char **input = 0;
  54. /* This variable is true if the program should assume stdout is connected
  55. to an HTML context e.g. if this is being run as a normal CGI. It is
  56. false to indicate that output should not contain HTML formatting,
  57. Javascript, etc. put plain ol' ASCII only
  58. */
  59. static int formattedOutput = 1;
  60. DS_EXPORT_SYMBOL int
  61. ds_get_formatted_output(void)
  62. {
  63. return formattedOutput;
  64. }
  65. DS_EXPORT_SYMBOL void
  66. ds_set_formatted_output(int val)
  67. {
  68. formattedOutput = val;
  69. }
  70. DS_EXPORT_SYMBOL void
  71. ds_print_file_name(char *fileptr)
  72. {
  73. char *meaning;
  74. fprintf(stdout, "%s", fileptr);
  75. if ( (meaning = ds_get_file_meaning(fileptr)) != NULL ) {
  76. fprintf(stdout, " (%s)", meaning);
  77. }
  78. }
  79. /*
  80. * Get a CGI variable.
  81. */
  82. DS_EXPORT_SYMBOL char *
  83. ds_get_cgi_var(char *cgi_var_name)
  84. {
  85. char *cgi_var_value;
  86. cgi_var_value = (char *) ds_a_get_cgi_var(cgi_var_name, NULL, NULL);
  87. if ( cgi_var_value == NULL ) {
  88. /*
  89. * The ds_a_get_cgi_var() lies! It gives a NULL even if the
  90. * value is "". So assume the variable is there and
  91. * return an empty string.
  92. */
  93. return("");
  94. }
  95. return(cgi_var_value);
  96. }
  97. /* parse POST input to a CGI program */
  98. DS_EXPORT_SYMBOL int
  99. ds_post_begin(FILE *in)
  100. {
  101. char *vars = NULL, *tmp = NULL, *decoded_vars = NULL;
  102. int cl;
  103. if(!(tmp = getenv("CONTENT_LENGTH")))
  104. {
  105. ds_report_error(DS_INCORRECT_USAGE, "Browser Error", "Your browser"
  106. " sent no content length with a POST command."
  107. " Please be sure to use a fully compliant browser.");
  108. return 1;
  109. }
  110. cl = atoi(tmp);
  111. vars = (char *)malloc(cl+1);
  112. if( !(fread(vars, 1, cl, in)) )
  113. {
  114. ds_report_error(DS_SYSTEM_ERROR, "CGI error",
  115. "The POST variables could not be read from stdin.");
  116. return 1;
  117. }
  118. vars[cl] = '\0';
  119. decoded_vars = ds_URL_decode(vars);
  120. free(vars);
  121. input = ds_string_to_vec(decoded_vars);
  122. free(decoded_vars);
  123. /*
  124. for (cl = 0; input[cl]; ++cl)
  125. printf("ds_post_begin: read cgi var=[%s]\n", input[cl]);
  126. */
  127. return 0;
  128. }
  129. /* parse GET input to a CGI program */
  130. DS_EXPORT_SYMBOL void
  131. ds_get_begin(char *query_string)
  132. {
  133. char *decoded_input = ds_URL_decode(query_string);
  134. input = ds_string_to_vec(decoded_input);
  135. free(decoded_input);
  136. }
  137. /*
  138. Borrowed from libadmin/form_post.c
  139. */
  140. DS_EXPORT_SYMBOL char *
  141. ds_a_get_cgi_var(char *varname, char *elem_id, char *bongmsg)
  142. {
  143. register int x = 0;
  144. int len = strlen(varname);
  145. char *ans = NULL;
  146. while(input[x]) {
  147. /* We want to get rid of the =, so len, len+1 */
  148. if((!strncmp(input[x], varname, len)) && (*(input[x]+len) == '=')) {
  149. ans = strdup(input[x] + len + 1);
  150. if(!strcmp(ans, ""))
  151. ans = NULL;
  152. break;
  153. } else
  154. x++;
  155. }
  156. if(ans == NULL) {
  157. if ((bongmsg) && strlen(bongmsg))
  158. {
  159. /* prefix error with varname so output interpreters can determine */
  160. /* which parameter is in error */
  161. char *msg;
  162. if (!ds_get_formatted_output() && (varname != NULL))
  163. {
  164. msg = PR_smprintf("%s.error: %s %s", varname, elem_id, bongmsg);
  165. }
  166. else
  167. {
  168. msg = PR_smprintf("error: %s %s", elem_id, bongmsg);
  169. }
  170. ds_show_message(msg);
  171. PR_smprintf_free(msg);
  172. }
  173. else
  174. return NULL;
  175. }
  176. else
  177. return(ans);
  178. /* shut up gcc */
  179. return NULL;
  180. }
  181. DS_EXPORT_SYMBOL char **
  182. ds_string_to_vec(char *in)
  183. {
  184. char **ans;
  185. int vars = 0;
  186. register int x = 0;
  187. char *tmp;
  188. in = strdup(in);
  189. while(in[x])
  190. if(in[x++]=='=')
  191. vars++;
  192. ans = (char **)calloc(vars+1, sizeof(char *));
  193. x=0;
  194. /* strtok() is not MT safe, but it is okay to call here because it is used in monothreaded env */
  195. tmp = strtok(in, "&");
  196. ans[x++]=strdup(tmp);
  197. while((tmp = strtok(NULL, "&"))) {
  198. ans[x++] = strdup(tmp);
  199. }
  200. free(in);
  201. return(ans);
  202. }