]> sipb.mit.edu Git - ikiwiki.git/blob - doc/plugins/contrib/unixauth.mdwn
2de6fc51fdda01402400a67146304449c1054c4d
[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 => "formbuilder_setup", id => "unixauth",
51                 call => \&formbuilder_setup);
52             hook(type => "formbuilder", id => "unixauth",
53                 call => \&formbuilder);
54         hook(type => "sessioncgi", id => "unixauth", call => \&sessioncgi);
55     } # }}}
56     
57     # Checks if a string matches a user's password, and returns true or false.
58     sub checkpassword ($$;$) { #{{{
59         my $user=shift;
60         my $password=shift;
61         my $field=shift || "password";
62     
63         # It's very important that the user not be allowed to log in with
64         # an empty password!
65         if (! length $password) {
66                 return 0;
67         }
68     
69         my $ret=0;
70         if (! exists $config{unixauth_type}) {
71                 # admin needs to carefully think over his configuration
72                 return 0;
73         }
74         elsif ($config{unixauth_type} eq "checkpassword") {
75                 open UNIXAUTH, "|$config{unixauth_command} true 3<&0" or die("Could not run $config{unixauth_type}");
76                 print UNIXAUTH "$user\0$password\0Y123456\0";
77                 close UNIXAUTH;
78                 $ret=!($?>>8);
79         }
80         elsif ($config{unixauth_type} eq "pwauth") {
81                 open UNIXAUTH, "|$config{unixauth_command}" or die("Could not run $config{unixauth_type}");
82                 print UNIXAUTH "$user\n$password\n";
83                 close UNIXAUTH;
84                 $ret=!($?>>8);
85         }
86         else {
87                 # no such authentication type
88                 return 0;
89         }
90     
91         if ($ret) {
92             my $userinfo=IkiWiki::userinfo_retrieve();
93             if (! length $user || ! defined $userinfo ||
94                 ! exists $userinfo->{$user} || ! ref $userinfo->{$user}) {
95                     IkiWiki::userinfo_setall($user, {
96                         'email' => '',
97                         'regdate' => time,
98                     });
99             }
100         }
101     
102         return $ret;
103     } #}}}
104     
105     sub formbuilder_setup (@) { #{{{
106         my %params=@_;
107     
108         my $form=$params{form};
109         my $session=$params{session};
110         my $cgi=$params{cgi};
111     
112         # if not under SSL, die before even showing a login form,
113         # unless the admin explicitly says it's fine
114         if (! exists $config{unixauth_requiressl}) {
115                 $config{unixauth_requiressl} = 1;
116         }
117         if ($config{unixauth_requiressl}) {
118             if ((! $config{sslcookie}) || (! exists $ENV{'HTTPS'})) {
119                 die("SSL required to login. Contact your administrator.<br>");
120             }
121         }
122     
123         if ($form->title eq "signin") {
124                 $form->field(name => "name", required => 0);
125                 $form->field(name => "password", type => "password", required => 0);
126                 
127                 if ($form->submitted) {
128                         my $submittype=$form->submitted;
129                         # Set required fields based on how form was submitted.
130                         my %required=(
131                                 "Login" => [qw(name password)],
132                         );
133                         foreach my $opt (@{$required{$submittype}}) {
134                                 $form->field(name => $opt, required => 1);
135                         }
136         
137                         # Validate password against name for Login.
138                         if ($submittype eq "Login") {
139                                 $form->field(
140                                         name => "password",
141                                         validate => sub {
142                                                 checkpassword($form->field("name"), shift);
143                                         },
144                                 );
145                         }
146                         
147                         # XXX is this reachable? looks like no
148                         elsif ($submittype eq "Login") {
149                                 $form->field( 
150                                         name => "name",
151                                         validate => sub {
152                                                 my $name=shift;
153                                                 length $name &&
154                                                 IkiWiki::userinfo_get($name, "regdate");
155                                         },
156                                 );
157                         }
158                 }
159                 else {
160                         # First time settings.
161                         $form->field(name => "name");
162                         if ($session->param("name")) {
163                                 $form->field(name => "name", value => $session->param("name"));
164                         }
165                 }
166         }
167         elsif ($form->title eq "preferences") {
168                 $form->field(name => "name", disabled => 1, 
169                         value => $session->param("name"), force => 1,
170                         fieldset => "login");
171                 $form->field(name => "password", disabled => 1, type => "password",
172                         fieldset => "login"),
173         }
174     }
175     
176     sub formbuilder (@) { #{{{
177         my %params=@_;
178     
179         my $form=$params{form};
180         my $session=$params{session};
181         my $cgi=$params{cgi};
182         my $buttons=$params{buttons};
183     
184         if ($form->title eq "signin") {
185                 if ($form->submitted && $form->validate) {
186                         if ($form->submitted eq 'Login') {
187                                 $session->param("name", $form->field("name"));
188                                 IkiWiki::cgi_postsignin($cgi, $session);
189                         }
190                 }
191         }
192         elsif ($form->title eq "preferences") {
193                 if ($form->submitted eq "Save Preferences" && $form->validate) {
194                         my $user_name=$form->field('name');
195                 }
196         }
197     } #}}}
198     
199     sub sessioncgi ($$) { #{{{
200         my $q=shift;
201         my $session=shift;
202     } #}}}
203     
204     1
205
206 """]]