emitauth.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. * emitauth.c -- generate authentication form -- HTTP gateway
  40. */
  41. #include "dsgw.h"
  42. #include "dbtdsgw.h"
  43. static int isexp = 0; /* Why is this static? */
  44. static void
  45. emit_authinfo( int isEditing, int isPwForm, char *binddn )
  46. {
  47. char *cookie, *dn, *rndstr, *pw;
  48. int rc;
  49. int isauth = 0;
  50. /* try to get the DN the user is bound as, and determine if
  51. * authentication credentials have expired.
  52. */
  53. if (( cookie = dsgw_get_auth_cookie()) != NULL ) {
  54. if ( dsgw_parse_cookie( cookie, &rndstr, &dn ) == 0 ) {
  55. if ( dn == NULL ) {
  56. isauth = 0;
  57. } else {
  58. if (( rc = dsgw_ckdn2passwd( rndstr, dn, &pw )) == 0 ) {
  59. isauth = 1;
  60. } else {
  61. isauth = 0;
  62. if ( rc == DSGW_CKDB_EXPIRED ) {
  63. isexp = 1;
  64. }
  65. }
  66. }
  67. } else {
  68. isauth = 0;
  69. }
  70. } else {
  71. isauth = 0;
  72. }
  73. dsgw_emitf( "<CENTER>\n"
  74. "<FONT SIZE=+2>%s</FONT>\n"
  75. "</CENTER>\n"
  76. "<p>", XP_GetClientStr(DBT_authenticateLogInToTheDirectory_) );
  77. if ( isPwForm ) {
  78. #ifdef NOTFORNOW
  79. /* ldap_dn2ufn currently gobble up 'dc' so don't use it for */
  80. /* now */
  81. auto char *ufn = ldap_dn2ufn( binddn );
  82. dsgw_emitf( XP_GetClientStr(DBT_youAreAboutToAuthenticate_), ufn);
  83. free( ufn );
  84. #else
  85. dsgw_emitf( XP_GetClientStr(DBT_youAreAboutToAuthenticate_), binddn);
  86. #endif
  87. } else if ( isEditing ) {
  88. dsgw_emits( XP_GetClientStr(DBT_beforeYouCanEditOrAddEntriesYouM_) );
  89. } else {
  90. dsgw_emits( XP_GetClientStr(DBT_fromThisScreenYouMayAuthenticate_) );
  91. }
  92. if ( isEditing ) {
  93. return;
  94. }
  95. dsgw_emitf( "<HR>\n"
  96. "<CENTER>\n"
  97. "<FONT SIZE=+2>%s</FONT>\n"
  98. "</CENTER>\n"
  99. "<P>\n", XP_GetClientStr(DBT_authenticationStatus_) );
  100. if ( isauth ) {
  101. auto char *ufn;
  102. dsgw_emits( XP_GetClientStr(DBT_FormNyouAreCurrentlyAuthenticate_) );
  103. ufn = ldap_dn2ufn( dn );
  104. dsgw_emitf( "<b>%s</b>\n", ufn );
  105. free( ufn );
  106. dsgw_emitf( "%s<BR>"
  107. "<CENTER>\n"
  108. "<INPUT TYPE=BUTTON "
  109. "VALUE=\"%s\""
  110. "onClick=\"doUnauth();\">\n"
  111. "</FORM>\n"
  112. "</CENTER>\n"
  113. "<HR>\n",
  114. XP_GetClientStr(DBT_NifYouWishToDiscardYourAuthentic_),
  115. XP_GetClientStr(DBT_discardAuthenticationCredentials_2) );
  116. } else if ( isexp ) {
  117. dsgw_emits( XP_GetClientStr(DBT_yourAuthenticationCredentialsFor_) );
  118. dsgw_emitf( "<b>%s</b> ", dn );
  119. dsgw_emits( XP_GetClientStr(DBT_haveExpiredN_) );
  120. } else {
  121. dsgw_emits( XP_GetClientStr(DBT_currentlyYouAreNotAuthenticatedT_) );
  122. }
  123. }
  124. static void
  125. emit_file (char* filename, char* authdesturl, char *user )
  126. {
  127. auto FILE* html = dsgw_open_html_file( filename, DSGW_ERROPT_EXIT );
  128. auto char line[ BIG_LINE ];
  129. auto int argc;
  130. auto char **argv, *escaped_dn;
  131. if ( user != NULL ) {
  132. escaped_dn = dsgw_strdup_escaped( user );
  133. } else {
  134. escaped_dn = "";
  135. }
  136. while ( dsgw_next_html_line( html, line )) {
  137. if ( dsgw_parse_line( line, &argc, &argv, 0, dsgw_simple_cond_is_true, NULL )) {
  138. if ( dsgw_directive_is( line, DRCT_HEAD )) {
  139. dsgw_head_begin();
  140. dsgw_emits ("\n");
  141. } else if ( dsgw_directive_is( line, "DS_AUTH_SEARCH_SCRIPT" )) {
  142. dsgw_emits ("<SCRIPT NAME=\"JavaScript\">\n"
  143. "<!-- Hide from non-JavaScript browsers\n"
  144. "function doUnauth()\n"
  145. "{\n");
  146. dsgw_emits (" if ( confirm( ");
  147. dsgw_quote_emits (QUOTATION_JAVASCRIPT,
  148. XP_GetClientStr(DBT_discardAuthenticationCredentials_));
  149. dsgw_emits (" )) {\n"
  150. " window.location.href='unauth?context=");
  151. dsgw_emits(context);
  152. dsgw_emits("';\n"
  153. " }\n"
  154. "}\n");
  155. #if 0 /* This doesn't work with Navigator 2.x */
  156. dsgw_emits ("function checkSS(sform)\n"
  157. "{\n"
  158. " if (sform.searchstring.value == null || sform.searchstring.value == \"\") {\n");
  159. dsgw_emit_alert (NULL, NULL, XP_GetClientStr(DBT_youDidNotSupplyASearchString_));
  160. dsgw_emits (" return false;\n"
  161. " }\n"
  162. "}\n");
  163. #endif
  164. dsgw_emits ("function init()\n"
  165. "{\n"
  166. " document.authSearchForm.searchstring.select();\n"
  167. " document.authSearchForm.searchstring.focus();\n"
  168. " if (top.history.length == 1 && top.opener != null && top.opener.location.href != "
  169. "top.location.href) {\n"
  170. " if (top.closewin == true) {\n"
  171. " top.opener.document.clear();\n"
  172. " top.opener.document.open();\n"
  173. " top.opener.document.write('');\n"
  174. " top.opener.document.close();\n"
  175. " }\n"
  176. " }\n"
  177. " top.closewin = false;\n"
  178. "}\n"
  179. "// End hiding -->\n"
  180. "</SCRIPT>\n");
  181. } else if ( dsgw_directive_is( line, "DS_AUTH_SEARCH_BODY" )) {
  182. dsgw_emitf ("<BODY onLoad=\"setTimeout('init()', 10);\" %s>\n",
  183. dsgw_html_body_colors);
  184. dsgw_emit_alertForm();
  185. } else if ( dsgw_directive_is( line, "DS_AUTH_SEARCH_INFO" )) {
  186. emit_authinfo( authdesturl != NULL, 0, NULL );
  187. } else if ( dsgw_directive_is( line, "DS_AUTH_SEARCH_FORM" )) {
  188. dsgw_form_begin ("authSearchForm", "action=\"dosearch\""
  189. #if 0 /* This doesn't work with Navigator 2.x */
  190. " onSubmit=\"return checkSS(this)\""
  191. #endif
  192. );
  193. dsgw_emits ("\n<INPUT TYPE=hidden NAME=mode VALUE=\"auth\">\n");
  194. if ( authdesturl != NULL ) {
  195. dsgw_emitf ("<INPUT TYPE=hidden NAME=authdesturl VALUE=\"%s\">\n",
  196. authdesturl);
  197. }
  198. } else if ( dsgw_directive_is( line, "DS_AUTH_SEARCH_NAME" )) {
  199. dsgw_emitf ("<INPUT NAME=\"searchstring\" VALUE=\"%s\" SIZE=40>\n",
  200. ( user == NULL ) ? "" : user );
  201. } else if ( dsgw_directive_is( line, "DS_AUTH_SEARCH_BUTTONS" )) {
  202. if ( authdesturl == NULL ) {
  203. dsgw_emitf ("<TD ALIGN=CENTER WIDTH=50%%>\n"
  204. "<INPUT TYPE=\"submit\" VALUE=\"%s\">\n"
  205. "<TD ALIGN=CENTER WIDTH=50%%>\n",
  206. XP_GetClientStr(DBT_continue_) );
  207. } else {
  208. dsgw_emitf ("<TD ALIGN=CENTER WIDTH=33%%>\n"
  209. "<INPUT TYPE=\"submit\" VALUE=\"%s\">\n"
  210. "<TD ALIGN=CENTER WIDTH=33%%>\n"
  211. "<INPUT TYPE=\"button\" VALUE=\"%s\" "
  212. "onClick=\"parent.close();\">\n"
  213. "<TD ALIGN=CENTER WIDTH=34%%>\n",
  214. XP_GetClientStr(DBT_continue_1), XP_GetClientStr(DBT_cancel_) );
  215. }
  216. dsgw_emit_helpbutton ("AUTHHELP_ID" );
  217. } else if ( dsgw_directive_is( line, "DS_AUTH_AS_ROOT_FORM" )) {
  218. dsgw_form_begin ("AuthAsRootDNForm", "action=\"auth\"");
  219. dsgw_emits ("\n");
  220. dsgw_emits ("<INPUT TYPE=hidden NAME=authasrootdn VALUE=\"true\">\n");
  221. if ( authdesturl != NULL ) {
  222. dsgw_emitf ("<INPUT TYPE=hidden NAME=authdesturl VALUE=\"%s\">\n",
  223. authdesturl );
  224. }
  225. } else if ( dsgw_directive_is( line, "DS_AUTH_PASSWORD_SCRIPT" )) {
  226. dsgw_emits ("<SCRIPT NAME=\"JavaScript\">\n"
  227. "<!-- Hide from non-JavaScript browsers\n");
  228. /* doUnauth function - invoke CGI which tosses cookies. */
  229. dsgw_emitf ("function doUnauth()\n"
  230. "{\n"
  231. " if ( confirm( '%s' )) {\n"
  232. " window.location.href='unauth?context=%s';\n"
  233. " }\n"
  234. "}\n"
  235. "// End hiding -->\n"
  236. "</SCRIPT>\n\n",
  237. XP_GetClientStr (DBT_discardAuthenticationCredentials_1), context);
  238. } else if ( dsgw_directive_is( line, "DS_AUTH_PASSWORD_BODY" )) {
  239. dsgw_emitf ("<BODY onLoad=\"document.authPwForm.password.select();document.authPwForm.password.focus();\" %s>\n",
  240. dsgw_html_body_colors );
  241. } else if ( dsgw_directive_is( line, "DS_AUTH_PASSWORD_INFO" )) {
  242. emit_authinfo( authdesturl != NULL, 1, user );
  243. } else if ( dsgw_directive_is( line, "DS_AUTH_PASSWORD_FORM" )) {
  244. dsgw_form_begin( "authPwForm", "action=\"doauth\"" );
  245. dsgw_emits ("\n" );
  246. dsgw_emitf (
  247. "<INPUT type=hidden name=escapedbinddn value=\"%s\">\n",
  248. escaped_dn );
  249. if ( authdesturl != NULL ) {
  250. dsgw_emitf ("<INPUT type=hidden name=authdesturl value=\"%s\">\n",
  251. authdesturl );
  252. }
  253. } else if ( dsgw_directive_is( line, "DS_AUTH_PASSWORD_NAME" )) {
  254. auto char** xdn = ldap_explode_dn( user, 1 );
  255. dsgw_emits( xdn[ 0 ] );
  256. ldap_value_free( xdn );
  257. } else if ( dsgw_directive_is( line, "DS_AUTH_PASSWORD_BUTTONS" )) {
  258. if ( authdesturl == NULL ) {
  259. dsgw_emitf ("<TD ALIGN=CENTER WIDTH=50%%>\n"
  260. "<INPUT TYPE=\"submit\" VALUE=\"%s\">\n"
  261. "<TD ALIGN=CENTER WIDTH=50%%>\n",
  262. XP_GetClientStr(DBT_continue_2) );
  263. } else {
  264. dsgw_emitf ("<TD ALIGN=CENTER WIDTH=33%%>\n"
  265. "<INPUT TYPE=\"submit\" VALUE=\"%s\">\n"
  266. "<TD ALIGN=CENTER WIDTH=33%%>\n"
  267. "<INPUT TYPE=\"button\" VALUE=\"%s\" "
  268. "onClick=\"parent.close();\">\n"
  269. "<TD ALIGN=CENTER WIDTH=34%%>\n",
  270. XP_GetClientStr(DBT_continue_3), XP_GetClientStr(DBT_cancel_1) );
  271. }
  272. dsgw_emit_helpbutton ("AUTHHELP_PW" );
  273. } else if ( dsgw_directive_is( line, "DS_HELP_BUTTON" ) && argc > 0) {
  274. dsgw_emit_helpbutton (argv[0]);
  275. } else {
  276. dsgw_emits (line);
  277. }
  278. dsgw_argv_free( argv );
  279. }
  280. }
  281. fflush (stdout);
  282. fclose (html);
  283. }
  284. void
  285. dsgw_emit_auth_form( char *binddn )
  286. {
  287. dsgw_emit_auth_dest( binddn, dsgw_get_cgi_var( "authdesturl", DSGW_CGIVAR_OPTIONAL ));
  288. }
  289. void
  290. dsgw_emit_auth_dest( char *binddn, char* authdesturl )
  291. {
  292. /*
  293. * If dn is NULL, then we don't know who we want to bind as yet.
  294. * Generate a simplified search form. This form needs to post:
  295. * mode=auth
  296. * searchstring
  297. * authdesturl
  298. *
  299. * If dn was given, then prompt for the password. Needs to post:
  300. * password
  301. * authdesturl
  302. * binddn
  303. */
  304. if ( binddn == NULL ) {
  305. emit_file( "authSearch.html", authdesturl,
  306. dsgw_get_cgi_var( "authhint", DSGW_CGIVAR_OPTIONAL ));
  307. } else {
  308. emit_file( "authPassword.html", authdesturl, binddn );
  309. }
  310. }