1 [[!template id=plugin name=unixauth core=0 author="[[schmonz]]"]]
4 This plugin authenticates users against the Unix user database. It presents a similar UI to [[plugins/passwordauth]], but simpler, as there's no need to be able to register or change one's password.
6 [pwauth](http://www.unixpapa.com/pwauth/) must be installed and working. In particular, it must be configured to recognize the UID of the calling web server, or authentication will always fail. Set `pwauth_path` to the full path of your pwauth binary.
8 As [with passwordauth](/security/#index14h2), be wary of sending usernames and passwords in cleartext. Unlike with passwordauth, sniffing these credentials can get an attacker much further than mere wiki access. SSL with this plugin is a __must__.
10 [[!toggle id="code" text="unixauth.pm"]]
12 [[!toggleable id="code" text="""
15 # Ikiwiki unixauth authentication.
16 package IkiWiki::Plugin::unixauth;
23 hook(type => "formbuilder_setup", id => "unixauth",
24 call => \&formbuilder_setup);
25 hook(type => "formbuilder", id => "unixauth",
26 call => \&formbuilder);
27 hook(type => "sessioncgi", id => "unixauth", call => \&sessioncgi);
30 # Checks if a string matches a user's password, and returns true or false.
31 sub checkpassword ($$;$) { #{{{
34 my $field=shift || "password";
36 # It's very important that the user not be allowed to log in with
38 if (! length $password) {
43 if (! exists $config{pwauth_path}) {
44 $config{pwauth_path}="/usr/libexec/pwauth";
46 open PWAUTH, "|$config{pwauth_path}" or die("Could not run pwauth");
47 print PWAUTH "$user\n$password\n";
52 my $userinfo=IkiWiki::userinfo_retrieve();
53 if (! length $user || ! defined $userinfo ||
54 ! exists $userinfo->{$user} || ! ref $userinfo->{$user}) {
55 IkiWiki::userinfo_setall($user, {
65 sub formbuilder_setup (@) { #{{{
68 my $form=$params{form};
69 my $session=$params{session};
72 if ($form->title eq "signin") {
73 $form->field(name => "name", required => 0);
74 $form->field(name => "password", type => "password", required => 0);
76 if ($form->submitted) {
77 my $submittype=$form->submitted;
78 # Set required fields based on how form was submitted.
80 "Login" => [qw(name password)],
82 foreach my $opt (@{$required{$submittype}}) {
83 $form->field(name => $opt, required => 1);
86 # Validate password against name for Login.
87 if ($submittype eq "Login") {
91 checkpassword($form->field("name"), shift);
96 elsif ($submittype eq "Login") {
102 IkiWiki::userinfo_get($name, "regdate");
108 # First time settings.
109 $form->field(name => "name");
110 if ($session->param("name")) {
111 $form->field(name => "name", value => $session->param("name"));
115 elsif ($form->title eq "preferences") {
116 $form->field(name => "name", disabled => 1,
117 value => $session->param("name"), force => 1,
118 fieldset => "login");
119 $form->field(name => "password", disabled => 1, type => "password",
120 fieldset => "login"),
124 sub formbuilder (@) { #{{{
127 my $form=$params{form};
128 my $session=$params{session};
129 my $cgi=$params{cgi};
130 my $buttons=$params{buttons};
132 if ($form->title eq "signin") {
133 if ($form->submitted && $form->validate) {
134 if ($form->submitted eq 'Login') {
135 $session->param("name", $form->field("name"));
136 IkiWiki::cgi_postsignin($cgi, $session);
140 elsif ($form->title eq "preferences") {
141 if ($form->submitted eq "Save Preferences" && $form->validate) {
142 my $user_name=$form->field('name');
147 sub sessioncgi ($$) { #{{{