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