auth.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /** --- BEGIN COPYRIGHT BLOCK ---
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  4. * All rights reserved.
  5. --- END COPYRIGHT BLOCK --- */
  6. /*
  7. * auth.c -- CGI authentication form generator -- HTTP gateway
  8. */
  9. #include "dsgw.h"
  10. #include "dbtdsgw.h"
  11. static void post_request();
  12. static void get_request(char *binddn);
  13. int main(
  14. int argc,
  15. char **argv
  16. #ifdef DSGW_DEBUG
  17. ,char *env[]
  18. #endif
  19. ) {
  20. int reqmethod;
  21. char *binddn = NULL;
  22. char *qs = NULL;
  23. if (( qs = getenv( "QUERY_STRING" )) != NULL && *qs != '\0' ) {
  24. /* parse the query string: */
  25. auto char *p, *iter = NULL;
  26. qs = dsgw_ch_strdup( qs );
  27. for ( p = ldap_utf8strtok_r( qs, "&", &iter ); p != NULL;
  28. p = ldap_utf8strtok_r( NULL, "&", &iter )) {
  29. /*Get the context.*/
  30. if ( !strncasecmp( p, "context=", 8 )) {
  31. context = dsgw_ch_strdup( p + 8 );
  32. dsgw_form_unescape( context );
  33. continue;
  34. }
  35. /*Get the dn*/
  36. if ( !strncasecmp( p, "dn=", 3 )) {
  37. binddn = dsgw_ch_strdup( p + 3 );
  38. dsgw_form_unescape( binddn );
  39. continue;
  40. }
  41. }
  42. free( qs ); qs = NULL;
  43. }
  44. reqmethod = dsgw_init( argc, argv, DSGW_METHOD_POST | DSGW_METHOD_GET );
  45. #ifdef DSGW_DEBUG
  46. dsgw_logstringarray( "env", env );
  47. #endif
  48. if ( reqmethod == DSGW_METHOD_POST ) {
  49. post_request();
  50. } else {
  51. get_request(binddn);
  52. }
  53. exit( 0 );
  54. }
  55. static void
  56. get_request(char *binddn)
  57. {
  58. dsgw_send_header();
  59. if ( binddn != NULL ) {
  60. if ( !strcmp( binddn, MGRDNSTR )) {
  61. if ( gc->gc_rootdn == NULL ) {
  62. dsgw_error( DSGW_ERR_NO_MGRDN,
  63. XP_GetClientStr (DBT_noDirMgrIsDefined_),
  64. DSGW_ERROPT_EXIT, 0, NULL );
  65. }
  66. binddn = dsgw_ch_strdup( gc->gc_rootdn );
  67. } else if ( *binddn == '\0' ) {
  68. binddn = NULL;
  69. } else {
  70. binddn = dsgw_ch_strdup( binddn );
  71. dsgw_form_unescape( binddn );
  72. }
  73. }
  74. dsgw_emit_auth_form( binddn );
  75. if ( binddn != NULL ) {
  76. free( binddn );
  77. }
  78. }
  79. static void
  80. post_request()
  81. {
  82. char *binddn = NULL;
  83. char *dorootbind = NULL;
  84. dsgw_send_header();
  85. /*
  86. * If the "authasrootdn" CGI variable is present and has the value
  87. * "true" then the user clicked on the "authenticate as directory
  88. * manager" button. In that case, try to bind as the root dn given
  89. * in the dsgw config file.
  90. */
  91. dorootbind = dsgw_get_cgi_var( "authasrootdn", DSGW_CGIVAR_OPTIONAL );
  92. if ( dorootbind != NULL && !strcasecmp( dorootbind, "true" )) {
  93. binddn = dsgw_ch_strdup( gc->gc_rootdn );
  94. } else {
  95. binddn = dsgw_get_escaped_cgi_var( "escapedbinddn", "binddn",
  96. DSGW_CGIVAR_OPTIONAL );
  97. }
  98. dsgw_emit_auth_form( binddn );
  99. }