dsalib_html.c 6.3 KB

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