]> sipb.mit.edu Git - ikiwiki.git/blob - doc/plugins/contrib/unixauth.mdwn
76a8477446f0d9d43b7db33e203604139a795c4c
[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 To authenticate, either [checkpassword](http://cr.yp.to/checkpwd.html) or [pwauth](http://www.unixpapa.com/pwauth/) must be installed and configured. `checkpassword` is strongly preferred. If your web server runs as an unprivileged user -- as it darn well should! -- then `checkpassword` needs to be setuid root. (Or your ikiwiki CGI wrapper, I guess, but don't do that.) Other checkpassword implementations are available, notably [checkpassword-pam](http://checkpasswd-pam.sourceforge.net/).
7
8 Config variables that affect the behavior of `unixauth`:
9
10 * `unixauth_type`: defaults to unset, can be "checkpassword" or "pwauth"
11 * `unixauth_command`: defaults to unset, should contain the full path and any arguments
12 * `unixauth_requiressl`: defaults to 1, can be 0
13 * `sslcookie`: needs to be 1 if `unixauth_requiressl` is 1 (perhaps this should be done automatically?)
14
15 __Security__: [As with passwordauth](/security/#index14h2), be wary of sending usernames and passwords in cleartext. Unlike passwordauth, sniffing `unixauth` credentials can get an attacker much further than mere wiki access. Therefore, this plugin defaults to not even _displaying_ the login form fields unless we're running under SSL. Nobody should be able to do anything remotely dumb until the admin has done at least a little thinking. After that, dumb things are always possible. ;-)
16
17 `unixauth` tests for the presence of the `HTTPS` environment variable. `Wrapper.pm` needs to be tweaked to pass it through; without that, the plugin fails closed.
18
19 [[!toggle id="diff" text="Wrapper.pm.diff"]]
20
21 [[!toggleable id="diff" text="""
22
23     --- Wrapper.pm.orig 2008-07-29 00:09:10.000000000 -0400
24     +++ Wrapper.pm
25     @@ -28,7 +28,7 @@ sub gen_wrapper () {
26         my @envsave;
27         push @envsave, qw{REMOTE_ADDR QUERY_STRING REQUEST_METHOD REQUEST_URI
28                        CONTENT_TYPE CONTENT_LENGTH GATEWAY_INTERFACE
29     -                  HTTP_COOKIE REMOTE_USER} if $config{cgi};
30     +                  HTTP_COOKIE REMOTE_USER HTTPS} if $config{cgi};
31         my $envsave="";
32         foreach my $var (@envsave) {
33                 $envsave.=<<"EOF"
34
35 """]]
36
37 [[!toggle id="code" text="unixauth.pm"]]
38
39 [[!toggleable id="code" text="""
40
41     #!/usr/bin/perl
42     # Ikiwiki unixauth authentication.
43     package IkiWiki::Plugin::unixauth;
44     
45     use warnings;
46     use strict;
47     use IkiWiki 2.00;
48     
49     sub import {
50         hook(type => "getsetup", id => "unixauth", call => \&getsetup);
51             hook(type => "formbuilder_setup", id => "unixauth",
52                 call => \&formbuilder_setup);
53             hook(type => "formbuilder", id => "unixauth",
54                 call => \&formbuilder);
55         hook(type => "sessioncgi", id => "unixauth", call => \&sessioncgi);
56     }
57     
58     sub getsetup () {
59         return
60         unixauth_type => {
61                 type => "string",
62                 example => "checkpassword",
63                 description => "type of authenticator; can be 'checkpassword' or 'pwauth'",
64                 safe => 0,
65                 rebuild => 1,
66         },
67         unixauth_command => {
68                 type => "string",
69                 example => "/path/to/checkpassword",
70                 description => "full path and any arguments",
71                 safe => 0,
72                 rebuild => 1,
73         },
74         unixauth_requiressl => {
75                 type => "boolean",
76                 example => "1",
77                 description => "require SSL? strongly recommended",
78                 safe => 0,
79                 rebuild => 1,
80         },
81         plugin => {
82                 description => "Unix user authentication",
83                 safe => 0,
84                 rebuild => 1,
85         },
86     }
87     
88     # Checks if a string matches a user's password, and returns true or false.
89     sub checkpassword ($$;$) {
90         my $user=shift;
91         my $password=shift;
92         my $field=shift || "password";
93     
94         # It's very important that the user not be allowed to log in with
95         # an empty password!
96         if (! length $password) {
97                 return 0;
98         }
99     
100         my $ret=0;
101         if (! exists $config{unixauth_type}) {
102                 # admin needs to carefully think over his configuration
103                 return 0;
104         }
105         elsif ($config{unixauth_type} eq "checkpassword") {
106                 open UNIXAUTH, "|$config{unixauth_command} true 3<&0" or die("Could not run $config{unixauth_type}");
107                 print UNIXAUTH "$user\0$password\0Y123456\0";
108                 close UNIXAUTH;
109                 $ret=!($?>>8);
110         }
111         elsif ($config{unixauth_type} eq "pwauth") {
112                 open UNIXAUTH, "|$config{unixauth_command}" or die("Could not run $config{unixauth_type}");
113                 print UNIXAUTH "$user\n$password\n";
114                 close UNIXAUTH;
115                 $ret=!($?>>8);
116         }
117         else {
118                 # no such authentication type
119                 return 0;
120         }
121     
122         if ($ret) {
123             my $userinfo=IkiWiki::userinfo_retrieve();
124             if (! length $user || ! defined $userinfo ||
125                 ! exists $userinfo->{$user} || ! ref $userinfo->{$user}) {
126                     IkiWiki::userinfo_setall($user, {
127                         'email' => '',
128                         'regdate' => time,
129                     });
130             }
131         }
132     
133         return $ret;
134     }
135     
136     sub formbuilder_setup (@) {
137         my %params=@_;
138     
139         my $form=$params{form};
140         my $session=$params{session};
141         my $cgi=$params{cgi};
142     
143         # if not under SSL, die before even showing a login form,
144         # unless the admin explicitly says it's fine
145         if (! exists $config{unixauth_requiressl}) {
146                 $config{unixauth_requiressl} = 1;
147         }
148         if ($config{unixauth_requiressl}) {
149             if ((! $config{sslcookie}) || (! exists $ENV{'HTTPS'})) {
150                 die("SSL required to login. Contact your administrator.<br>");
151             }
152         }
153     
154         if ($form->title eq "signin") {
155                 $form->field(name => "name", required => 0);
156                 $form->field(name => "password", type => "password", required => 0);
157                 
158                 if ($form->submitted) {
159                         my $submittype=$form->submitted;
160                         # Set required fields based on how form was submitted.
161                         my %required=(
162                                 "Login" => [qw(name password)],
163                         );
164                         foreach my $opt (@{$required{$submittype}}) {
165                                 $form->field(name => $opt, required => 1);
166                         }
167         
168                         # Validate password against name for Login.
169                         if ($submittype eq "Login") {
170                                 $form->field(
171                                         name => "password",
172                                         validate => sub {
173                                                 checkpassword($form->field("name"), shift);
174                                         },
175                                 );
176                         }
177                         
178                         # XXX is this reachable? looks like no
179                         elsif ($submittype eq "Login") {
180                                 $form->field( 
181                                         name => "name",
182                                         validate => sub {
183                                                 my $name=shift;
184                                                 length $name &&
185                                                 IkiWiki::userinfo_get($name, "regdate");
186                                         },
187                                 );
188                         }
189                 }
190                 else {
191                         # First time settings.
192                         $form->field(name => "name");
193                         if ($session->param("name")) {
194                                 $form->field(name => "name", value => $session->param("name"));
195                         }
196                 }
197         }
198         elsif ($form->title eq "preferences") {
199                 $form->field(name => "name", disabled => 1, 
200                         value => $session->param("name"), force => 1,
201                         fieldset => "login");
202                 $form->field(name => "password", disabled => 1, type => "password",
203                         fieldset => "login"),
204         }
205     }
206     
207     sub formbuilder (@) {
208         my %params=@_;
209     
210         my $form=$params{form};
211         my $session=$params{session};
212         my $cgi=$params{cgi};
213         my $buttons=$params{buttons};
214     
215         if ($form->title eq "signin") {
216                 if ($form->submitted && $form->validate) {
217                         if ($form->submitted eq 'Login') {
218                                 $session->param("name", $form->field("name"));
219                                 IkiWiki::cgi_postsignin($cgi, $session);
220                         }
221                 }
222         }
223         elsif ($form->title eq "preferences") {
224                 if ($form->submitted eq "Save Preferences" && $form->validate) {
225                         my $user_name=$form->field('name');
226                 }
227         }
228     }
229     
230     sub sessioncgi ($$) {
231         my $q=shift;
232         my $session=shift;
233     }
234     
235     1
236
237 """]]