summaryrefslogtreecommitdiff
path: root/IkiWiki/CGI.pm
blob: 866711a719478cd7bde8a0fb8ecdfc44e8678ef9 (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 $q=shift;
  208. my $httpstatus=shift;
  209. my $message=shift;
  210. print $q->header(
  211. -status => $httpstatus,
  212. -charset => 'utf-8',
  213. );
  214. print $message;
  215. # Internet Explod^Hrer won't show custom 404 responses
  216. # unless they're >= 512 bytes
  217. print ' ' x 512;
  218. exit;
  219. }
  220. sub check_banned ($$) {
  221. my $q=shift;
  222. my $session=shift;
  223. my $banned=0;
  224. my $name=$session->param("name");
  225. if (defined $name &&
  226. grep { $name eq $_ } @{$config{banned_users}}) {
  227. $banned=1;
  228. }
  229. foreach my $b (@{$config{banned_users}}) {
  230. if (pagespec_match("", $b,
  231. ip => $ENV{REMOTE_ADDR},
  232. name => defined $name ? $name : "",
  233. )) {
  234. $banned=1;
  235. last;
  236. }
  237. }
  238. if ($banned) {
  239. $session->delete();
  240. cgi_savesession($session);
  241. cgi_custom_failure(
  242. $q, "403 Forbidden",
  243. gettext("You are banned."));
  244. }
  245. }
  246. sub cgi_getsession ($) {
  247. my $q=shift;
  248. eval q{use CGI::Session; use HTML::Entities};
  249. error($@) if $@;
  250. CGI::Session->name("ikiwiki_session_".encode_entities($config{wikiname}));
  251. my $oldmask=umask(077);
  252. my $session = eval {
  253. CGI::Session->new("driver:DB_File", $q,
  254. { FileName => "$config{wikistatedir}/sessions.db" })
  255. };
  256. if (! $session || $@) {
  257. error($@." ".CGI::Session->errstr());
  258. }
  259. umask($oldmask);
  260. return $session;
  261. }
  262. # To guard against CSRF, the user's session id (sid)
  263. # can be stored on a form. This function will check
  264. # (for logged in users) that the sid on the form matches
  265. # the session id in the cookie.
  266. sub checksessionexpiry ($$) {
  267. my $q=shift;
  268. my $session = shift;
  269. if (defined $session->param("name")) {
  270. my $sid=$q->param('sid');
  271. if (! defined $sid || $sid ne $session->id) {
  272. error(gettext("Your login session has expired."));
  273. }
  274. }
  275. }
  276. sub cgi_savesession ($) {
  277. my $session=shift;
  278. # Force session flush with safe umask.
  279. my $oldmask=umask(077);
  280. $session->flush;
  281. umask($oldmask);
  282. }
  283. sub cgi (;$$) {
  284. my $q=shift;
  285. my $session=shift;
  286. eval q{use CGI};
  287. error($@) if $@;
  288. $CGI::DISABLE_UPLOADS=$config{cgi_disable_uploads};
  289. if (! $q) {
  290. binmode(STDIN);
  291. $q=CGI->new;
  292. binmode(STDIN, ":utf8");
  293. run_hooks(cgi => sub { shift->($q) });
  294. }
  295. my $do=$q->param('do');
  296. if (! defined $do || ! length $do) {
  297. my $error = $q->cgi_error;
  298. if ($error) {
  299. error("Request not processed: $error");
  300. }
  301. else {
  302. error("\"do\" parameter missing");
  303. }
  304. }
  305. # Need to lock the wiki before getting a session.
  306. lockwiki();
  307. loadindex();
  308. if (! $session) {
  309. $session=cgi_getsession($q);
  310. }
  311. # Auth hooks can sign a user in.
  312. if ($do ne 'signin' && ! defined $session->param("name")) {
  313. run_hooks(auth => sub {
  314. shift->($q, $session)
  315. });
  316. if (defined $session->param("name")) {
  317. # Make sure whatever user was authed is in the
  318. # userinfo db.
  319. if (! userinfo_get($session->param("name"), "regdate")) {
  320. userinfo_setall($session->param("name"), {
  321. email => "",
  322. password => "",
  323. regdate => time,
  324. }) || error("failed adding user");
  325. }
  326. }
  327. }
  328. check_banned($q, $session);
  329. run_hooks(sessioncgi => sub { shift->($q, $session) });
  330. if ($do eq 'signin') {
  331. cgi_signin($q, $session);
  332. cgi_savesession($session);
  333. }
  334. elsif ($do eq 'prefs') {
  335. cgi_prefs($q, $session);
  336. }
  337. elsif (defined $session->param("postsignin") || $do eq 'postsignin') {
  338. cgi_postsignin($q, $session);
  339. }
  340. else {
  341. error("unknown do parameter");
  342. }
  343. }
  344. # Does not need to be called directly; all errors will go through here.
  345. sub cgierror ($) {
  346. my $message=shift;
  347. print "Content-type: text/html\n\n";
  348. print misctemplate(gettext("Error"),
  349. "<p class=\"error\">".gettext("Error").": $message</p>");
  350. die $@;
  351. }
  352. 1