unauth.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. * unauth.c -- CGI to discard cookies -- HTTP gateway
  40. */
  41. #include "dsgw.h"
  42. #include "dbtdsgw.h"
  43. char *get_auth_cookie( char *cookie );
  44. void generate_message( int type );
  45. #define CKEXP_SUCCESS 1
  46. #define CKEXP_FAILURE 2
  47. int main( int argc, char **argv )
  48. {
  49. int reqmethod;
  50. char *expck;
  51. char *authck;
  52. int rc;
  53. char *qs = NULL;
  54. /* Parse out the context=blah.html */
  55. if (( qs = getenv( "QUERY_STRING" )) != NULL && *qs != '\0' ) {
  56. /* parse the query string: */
  57. auto char *p, *iter = NULL;
  58. qs = dsgw_ch_strdup( qs );
  59. for ( p = ldap_utf8strtok_r( qs, "&", &iter ); p != NULL;
  60. p = ldap_utf8strtok_r( NULL, "&", &iter )) {
  61. /*
  62. * Get the conf file name. It'll be translated
  63. * into /dsgw/context/CONTEXT.conf if
  64. * CONTEXT is all alphanumeric (no slahes,
  65. * or dots). CONTEXT is passed into the cgi.
  66. * if context=CONTEXT is not there, or PATH_INFO
  67. * was used, then use dsgw.conf
  68. */
  69. if ( !strncasecmp( p, "context=", 8 )) {
  70. context = dsgw_ch_strdup( p + 8 );
  71. dsgw_form_unescape( context );
  72. continue;
  73. }
  74. }
  75. free( qs ); qs = NULL;
  76. }
  77. reqmethod = dsgw_init( argc, argv, DSGW_METHOD_GET );
  78. authck = dsgw_get_auth_cookie();
  79. if ( authck == NULL ) {
  80. /* No cookie. Generate an informational message. */
  81. generate_message( CKEXP_SUCCESS );
  82. free( authck );
  83. exit( 0 );
  84. }
  85. /* Remove the cookie from the cookie database */
  86. rc = dsgw_delcookie( authck );
  87. /* Generate a cookie header with the cookie set to [unauthenticated] */
  88. expck = dsgw_ch_malloc( strlen( DSGW_CKHDR ) + strlen( DSGW_AUTHCKNAME ) +
  89. strlen( DSGW_UNAUTHSTR ) + strlen( "=; path=/" ) + 2 );
  90. sprintf( expck, "%s%s=%s; path=/", DSGW_CKHDR, DSGW_AUTHCKNAME, DSGW_UNAUTHSTR );
  91. dsgw_add_header( expck );
  92. generate_message( CKEXP_SUCCESS );
  93. free( authck );
  94. free( expck );
  95. exit( 0 );
  96. }
  97. /*
  98. * It's quite likely that there will be more than one cookie in the
  99. * Cookie: header. See if we've got an authentication cookie, and if
  100. * so, parse it out and return a pointer to it. If no auth cookie
  101. * is present, return NULL.
  102. */
  103. char *
  104. get_auth_cookie( char *cookie )
  105. {
  106. char *p, *e;
  107. if ( cookie == NULL ) {
  108. return NULL;
  109. }
  110. if (( p = strstr( cookie, DSGW_AUTHCKNAME )) == NULL ) {
  111. return NULL;
  112. }
  113. if (( e = strchr( p, ';' )) != NULL ) {
  114. *e = '\0';
  115. }
  116. return p;
  117. }
  118. void
  119. generate_message( int type )
  120. {
  121. dsgw_send_header();
  122. dsgw_emits( "<HTML>" );
  123. dsgw_head_begin();
  124. dsgw_emits( "\n<TITLE>" );
  125. if ( type == CKEXP_SUCCESS ) {
  126. dsgw_emits( "Success" );
  127. } else if ( type == CKEXP_FAILURE ) {
  128. dsgw_emits( "Error" );
  129. }
  130. dsgw_emits( "</TITLE>\n</HEAD>\n" );
  131. dsgw_emitf( "<BODY %s>\n", dsgw_html_body_colors );
  132. dsgw_emitf( "<CENTER>\n"
  133. "<FONT SIZE=+2>\n"
  134. "%s"
  135. "</FONT>\n"
  136. "</CENTER>\n"
  137. "<P>\n"
  138. "%s",
  139. XP_GetClientStr( DBT_Success_ ),
  140. XP_GetClientStr( DBT_YouAreNoLongerAuthenticated_ ));
  141. if ( type != CKEXP_SUCCESS ) {
  142. /*
  143. * Something went wrong, so generate some JavaScript to
  144. * discard the cookie.
  145. */
  146. dsgw_emits( "<SCRIPT LANGUAGE=\"JavaScript\">\n" );
  147. dsgw_emitf( "document.cookie = '%s=%s; path=/';\n", DSGW_AUTHCKNAME,
  148. DSGW_UNAUTHSTR );
  149. dsgw_emits( "</SCRIPT>\n" );
  150. }
  151. dsgw_form_begin (NULL, NULL);
  152. dsgw_emits( "\n"
  153. "<TABLE BORDER=2 WIDTH=100%>\n"
  154. "<TR>\n"
  155. "<TD ALIGN=CENTER WIDTH=50%>\n");
  156. dsgw_emitf(
  157. "<INPUT TYPE=BUTTON VALUE=\"%s\"", XP_GetClientStr( DBT_GoBack_ ));
  158. dsgw_emits(
  159. " onClick=\"window.location.href=");
  160. dsgw_quote_emitf(QUOTATION_JAVASCRIPT, "auth?context=%s", context);
  161. dsgw_emits(";\"></TD>\n"
  162. "<TD ALIGN=CENTER WIDTH=50%>\n" );
  163. dsgw_emit_helpbutton( "UNAUTH" );
  164. dsgw_emits( "</TABLE></FORM>\n"
  165. "</BODY></HTML>\n" );
  166. }