Merge branch 'tova' into autoconfig
[ikiwiki.git] / doc / plugins / contrib / unixauth.mdwn
1 [[!template id=plugin name=unixauth core=0 author="[[schmonz]]"]]
2 [[!tag type/auth]]
3
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.
5
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.
7
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__.
9
10 [[!toggle id="code" text="unixauth.pm"]]
11
12 [[!toggleable id="code" text="""
13
14     #!/usr/bin/perl
15     # Ikiwiki unixauth authentication.
16     package IkiWiki::Plugin::unixauth;
17     
18     use warnings;
19     use strict;
20     use IkiWiki 2.00;
21     
22     sub import { #{{{
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);
28     } # }}}
29     
30     # Checks if a string matches a user's password, and returns true or false.
31     sub checkpassword ($$;$) { #{{{
32         my $user=shift;
33         my $password=shift;
34         my $field=shift || "password";
35     
36         # It's very important that the user not be allowed to log in with
37         # an empty password!
38         if (! length $password) {
39                 return 0;
40         }
41     
42         my $ret=0;
43         if (! exists $config{pwauth_path}) {
44                 $config{pwauth_path}="/usr/libexec/pwauth";
45         }
46         open PWAUTH, "|$config{pwauth_path}" or die("Could not run pwauth");
47         print PWAUTH "$user\n$password\n";
48         close PWAUTH;
49         $ret=!($?>>8);
50     
51         if ($ret) {
52             my $userinfo=IkiWiki::userinfo_retrieve();
53             if (! length $user || ! defined $userinfo ||
54                 ! exists $userinfo->{$user} || ! ref $userinfo->{$user}) {
55                     IkiWiki::userinfo_setall($user, {
56                         'email' => '',
57                         'regdate' => time,
58                     });
59             }
60         }
61     
62         return $ret;
63     } #}}}
64     
65     sub formbuilder_setup (@) { #{{{
66         my %params=@_;
67     
68         my $form=$params{form};
69         my $session=$params{session};
70         my $cgi=$params{cgi};
71     
72         if ($form->title eq "signin") {
73                 $form->field(name => "name", required => 0);
74                 $form->field(name => "password", type => "password", required => 0);
75                 
76                 if ($form->submitted) {
77                         my $submittype=$form->submitted;
78                         # Set required fields based on how form was submitted.
79                         my %required=(
80                                 "Login" => [qw(name password)],
81                         );
82                         foreach my $opt (@{$required{$submittype}}) {
83                                 $form->field(name => $opt, required => 1);
84                         }
85         
86                         # Validate password against name for Login.
87                         if ($submittype eq "Login") {
88                                 $form->field(
89                                         name => "password",
90                                         validate => sub {
91                                                 checkpassword($form->field("name"), shift);
92                                         },
93                                 );
94                         }
95                         
96                         elsif ($submittype eq "Login") {
97                                 $form->field( 
98                                         name => "name",
99                                         validate => sub {
100                                                 my $name=shift;
101                                                 length $name &&
102                                                 IkiWiki::userinfo_get($name, "regdate");
103                                         },
104                                 );
105                         }
106                 }
107                 else {
108                         # First time settings.
109                         $form->field(name => "name");
110                         if ($session->param("name")) {
111                                 $form->field(name => "name", value => $session->param("name"));
112                         }
113                 }
114         }
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"),
121         }
122     }
123     
124     sub formbuilder (@) { #{{{
125         my %params=@_;
126     
127         my $form=$params{form};
128         my $session=$params{session};
129         my $cgi=$params{cgi};
130         my $buttons=$params{buttons};
131     
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);
137                         }
138                 }
139         }
140         elsif ($form->title eq "preferences") {
141                 if ($form->submitted eq "Save Preferences" && $form->validate) {
142                         my $user_name=$form->field('name');
143                 }
144         }
145     } #}}}
146     
147     sub sessioncgi ($$) { #{{{
148         my $q=shift;
149         my $session=shift;
150     } #}}}
151     
152     1
153
154 """]]