summaryrefslogtreecommitdiff
path: root/IkiWiki/CGI.pm
blob: 9277223f00e8e8d0f30071e7b47462db54cd5e55 (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki;
  3. use warnings;
  4. use strict;
  5. use IkiWiki;
  6. use IkiWiki::UserInfo;
  7. use open qw{:utf8 :std};
  8. use Encode;
  9. sub printheader ($) {
  10. my $session=shift;
  11. if ($config{sslcookie}) {
  12. print $session->header(-charset => 'utf-8',
  13. -cookie => $session->cookie(-httponly => 1, -secure => 1));
  14. } else {
  15. print $session->header(-charset => 'utf-8',
  16. -cookie => $session->cookie(-httponly => 1));
  17. }
  18. }
  19. sub showform ($$$$;@) {
  20. my $form=shift;
  21. my $buttons=shift;
  22. my $session=shift;
  23. my $cgi=shift;
  24. if (exists $hooks{formbuilder}) {
  25. run_hooks(formbuilder => sub {
  26. shift->(form => $form, cgi => $cgi, session => $session,
  27. buttons => $buttons);
  28. });
  29. }
  30. printheader($session);
  31. print misctemplate($form->title, $form->render(submit => $buttons), @_);
  32. }
  33. sub redirect ($$) {
  34. my $q=shift;
  35. eval q{use URI};
  36. my $url=URI->new(shift);
  37. if (! $config{w3mmode}) {
  38. print $q->redirect($url);
  39. }
  40. else {
  41. print "Content-type: text/plain\n";
  42. print "W3m-control: GOTO $url\n\n";
  43. }
  44. }
  45. sub decode_cgi_utf8 ($) {
  46. # decode_form_utf8 method is needed for 5.10
  47. if ($] < 5.01) {
  48. my $cgi = shift;
  49. foreach my $f ($cgi->param) {
  50. $cgi->param($f, map { decode_utf8 $_ } $cgi->param($f));
  51. }
  52. }
  53. }
  54. sub decode_form_utf8 ($) {
  55. if ($] >= 5.01) {
  56. my $form = shift;
  57. foreach my $f ($form->field) {
  58. $form->field(name => $f,
  59. value => decode_utf8($form->field($f)),
  60. force => 1,
  61. );
  62. }
  63. }
  64. }
  65. # Check if the user is signed in. If not, redirect to the signin form and
  66. # save their place to return to later.
  67. sub needsignin ($$) {
  68. my $q=shift;
  69. my $session=shift;
  70. if (! defined $session->param("name") ||
  71. ! userinfo_get($session->param("name"), "regdate")) {
  72. $session->param(postsignin => $ENV{QUERY_STRING});
  73. cgi_signin($q, $session);
  74. cgi_savesession($session);
  75. exit;
  76. }
  77. }
  78. sub cgi_signin ($$) {
  79. my $q=shift;
  80. my $session=shift;
  81. decode_cgi_utf8($q);
  82. eval q{use CGI::FormBuilder};
  83. error($@) if $@;
  84. my $form = CGI::FormBuilder->new(
  85. title => "signin",
  86. name => "signin",
  87. charset => "utf-8",
  88. method => 'POST',
  89. required => 'NONE',
  90. javascript => 0,
  91. params => $q,
  92. action => $config{cgiurl},
  93. header => 0,
  94. template => {type => 'div'},
  95. stylesheet => baseurl()."style.css",
  96. );
  97. my $buttons=["Login"];
  98. if ($q->param("do") ne "signin" && !$form->submitted) {
  99. $form->text(gettext("You need to log in first."));
  100. }
  101. $form->field(name => "do", type => "hidden", value => "signin",
  102. force => 1);
  103. decode_form_utf8($form);
  104. run_hooks(formbuilder_setup => sub {
  105. shift->(form => $form, cgi => $q, session => $session,
  106. buttons => $buttons);
  107. });
  108. decode_form_utf8($form);
  109. if ($form->submitted) {
  110. $form->validate;
  111. }
  112. showform($form, $buttons, $session, $q);
  113. }
  114. sub cgi_postsignin ($$) {
  115. my $q=shift;
  116. my $session=shift;
  117. # Continue with whatever was being done before the signin process.
  118. if (defined $session->param("postsignin")) {
  119. my $postsignin=CGI->new($session->param("postsignin"));
  120. $session->clear("postsignin");
  121. cgi($postsignin, $session);
  122. cgi_savesession($session);
  123. exit;
  124. }
  125. else {
  126. if ($config{sslcookie} && ! $q->https()) {
  127. error(gettext("probable misconfiguration: sslcookie is set, but you are attempting to login via http, not https"));
  128. }
  129. else {
  130. error(gettext("login failed, perhaps you need to turn on cookies?"));
  131. }
  132. }
  133. }
  134. sub cgi_prefs ($$) {
  135. my $q=shift;
  136. my $session=shift;
  137. needsignin($q, $session);
  138. decode_cgi_utf8($q);
  139. # The session id is stored on the form and checked to
  140. # guard against CSRF.
  141. my $sid=$q->param('sid');
  142. if (! defined $sid) {
  143. $q->delete_all;
  144. }
  145. elsif ($sid ne $session->id) {
  146. error(gettext("Your login session has expired."));
  147. }
  148. eval q{use CGI::FormBuilder};
  149. error($@) if $@;
  150. my $form = CGI::FormBuilder->new(
  151. title => "preferences",
  152. name => "preferences",
  153. header => 0,
  154. charset => "utf-8",
  155. method => 'POST',
  156. validate => {
  157. email => 'EMAIL',
  158. },
  159. required => 'NONE',
  160. javascript => 0,
  161. params => $q,
  162. action => $config{cgiurl},
  163. template => {type => 'div'},
  164. stylesheet => baseurl()."style.css",
  165. fieldsets => [
  166. [login => gettext("Login")],
  167. [preferences => gettext("Preferences")],
  168. [admin => gettext("Admin")]
  169. ],
  170. );
  171. my $buttons=["Save Preferences", "Logout", "Cancel"];
  172. decode_form_utf8($form);
  173. run_hooks(formbuilder_setup => sub {
  174. shift->(form => $form, cgi => $q, session => $session,
  175. buttons => $buttons);
  176. });
  177. decode_form_utf8($form);
  178. $form->field(name => "do", type => "hidden", value => "prefs",
  179. force => 1);
  180. $form->field(name => "sid", type => "hidden", value => $session->id,
  181. force => 1);
  182. $form->field(name => "email", size => 50, fieldset => "preferences");
  183. my $user_name=$session->param("name");
  184. if (! $form->submitted) {
  185. $form->field(name => "email", force => 1,
  186. value => userinfo_get($user_name, "email"));
  187. }
  188. if ($form->submitted eq 'Logout') {
  189. $session->delete();
  190. redirect($q, $config{url});
  191. return;
  192. }
  193. elsif ($form->submitted eq 'Cancel') {
  194. redirect($q, $config{url});
  195. return;
  196. }
  197. elsif ($form->submitted eq 'Save Preferences' && $form->validate) {
  198. if (defined $form->field('email')) {
  199. userinfo_set($user_name, 'email', $form->field('email')) ||
  200. error("failed to set email");
  201. }
  202. $form->text(gettext("Preferences saved."));
  203. }
  204. showform($form, $buttons, $session, $q);
  205. }
  206. sub cgi_custom_failure ($$) {
  207. my $header=shift;
  208. my $message=shift;
  209. print $header;
  210. print $message;
  211. # Internet Explod^Hrer won't show custom 404 responses
  212. # unless they're >= 512 bytes
  213. print ' ' x 512;
  214. exit;
  215. }
  216. sub check_banned ($$) {
  217. my $q=shift;
  218. my $session=shift;
  219. my $banned=0;
  220. my $name=$session->param("name");
  221. if (defined $name &&
  222. grep { $name eq $_ } @{$config{banned_users}}) {
  223. $banned=1;
  224. }
  225. foreach my $b (@{$config{banned_users}}) {
  226. if (pagespec_match("", $b,
  227. ip => $ENV{REMOTE_ADDR},
  228. name => defined $name ? $name : "",
  229. )) {
  230. $banned=1;
  231. last;
  232. }
  233. }
  234. if ($banned) {
  235. $session->delete();
  236. cgi_savesession($session);
  237. cgi_custom_failure(
  238. $q->header(-status => "403 Forbidden"),
  239. gettext("You are banned."));
  240. }
  241. }
  242. sub cgi_getsession ($) {
  243. my $q=shift;
  244. eval q{use CGI::Session; use HTML::Entities};
  245. error($@) if $@;
  246. CGI::Session->name("ikiwiki_session_".encode_entities($config{wikiname}));
  247. my $oldmask=umask(077);
  248. my $session = eval {
  249. CGI::Session->new("driver:DB_File", $q,
  250. { FileName => "$config{wikistatedir}/sessions.db" })
  251. };
  252. if (! $session || $@) {
  253. error($@." ".CGI::Session->errstr());
  254. }
  255. umask($oldmask);
  256. return $session;
  257. }
  258. # To guard against CSRF, the user's session id (sid)
  259. # can be stored on a form. This function will check
  260. # (for logged in users) that the sid on the form matches
  261. # the session id in the cookie.
  262. sub checksessionexpiry ($$) {
  263. my $q=shift;
  264. my $session = shift;
  265. if (defined $session->param("name")) {
  266. my $sid=$q->param('sid');
  267. if (! defined $sid || $sid ne $session->id) {
  268. error(gettext("Your login session has expired."));
  269. }
  270. }
  271. }
  272. sub cgi_savesession ($) {
  273. my $session=shift;
  274. # Force session flush with safe umask.
  275. my $oldmask=umask(077);
  276. $session->flush;
  277. umask($oldmask);
  278. }
  279. sub cgi (;$$) {
  280. my $q=shift;
  281. my $session=shift;
  282. eval q{use CGI};
  283. error($@) if $@;
  284. $CGI::DISABLE_UPLOADS=$config{cgi_disable_uploads};
  285. if (! $q) {
  286. binmode(STDIN);
  287. $q=CGI->new;
  288. binmode(STDIN, ":utf8");
  289. run_hooks(cgi => sub { shift->($q) });
  290. }
  291. my $do=$q->param('do');
  292. if (! defined $do || ! length $do) {
  293. my $error = $q->cgi_error;
  294. if ($error) {
  295. error("Request not processed: $error");
  296. }
  297. else {
  298. error("\"do\" parameter missing");
  299. }
  300. }
  301. # Need to lock the wiki before getting a session.
  302. lockwiki();
  303. loadindex();
  304. if (! $session) {
  305. $session=cgi_getsession($q);
  306. }
  307. # Auth hooks can sign a user in.
  308. if ($do ne 'signin' && ! defined $session->param("name")) {
  309. run_hooks(auth => sub {
  310. shift->($q, $session)
  311. });
  312. if (defined $session->param("name")) {
  313. # Make sure whatever user was authed is in the
  314. # userinfo db.
  315. if (! userinfo_get($session->param("name"), "regdate")) {
  316. userinfo_setall($session->param("name"), {
  317. email => "",
  318. password => "",
  319. regdate => time,
  320. }) || error("failed adding user");
  321. }
  322. }
  323. }
  324. check_banned($q, $session);
  325. run_hooks(sessioncgi => sub { shift->($q, $session) });
  326. if ($do eq 'signin') {
  327. cgi_signin($q, $session);
  328. cgi_savesession($session);
  329. }
  330. elsif ($do eq 'prefs') {
  331. cgi_prefs($q, $session);
  332. }
  333. elsif (defined $session->param("postsignin") || $do eq 'postsignin') {
  334. cgi_postsignin($q, $session);
  335. }
  336. else {
  337. error("unknown do parameter");
  338. }
  339. }
  340. # Does not need to be called directly; all errors will go through here.
  341. sub cgierror ($) {
  342. my $message=shift;
  343. print "Content-type: text/html\n\n";
  344. print misctemplate(gettext("Error"),
  345. "<p class=\"error\">".gettext("Error").": $message</p>");
  346. die $@;
  347. }
  348. 1