6 use Digest
::MD5
qw(md5);
25 require Digest
::SHA
::PurePerl
;
26 Digest
::SHA
::PurePerl
->import(
29 die "One of Digest::SHA or Digest::SHA1 or Digest::SHA::PurePerl "
30 . "must be available\n";
36 $self->{uuid
} = '' unless $self->{uuid
};
40 @md5 = unpack('C*', md5
(time . $$ . rand() . join(':',%$self)));
42 $md5[6] = 0x40 | ($md5[6] & 0x0F); # Version 4 -- random
43 $md5[8] = 0x80 | ($md5[8] & 0x3F); # RFC 4122 specification
45 '%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x',
49 sub _remove_ssh_leftovers
{
51 system "rm -f '$Girocco::Config::chroot/etc/sshkeys/$self->{name}'";
52 system "rm -f '$Girocco::Config::chroot/etc/sshcerts/${Girocco::Config::nickname}_$self->{name}'_user_*.pem";
57 my (undef, undef, $gid) = getgrnam($Girocco::Config
::owning_group
||'');
58 my $owngroupid = $gid ?
$gid : 65534;
59 Girocco
::User
->load($self->{name
}) and die "User $self->{name} already exists";
60 $self->{uuid
} = $self->_gen_uuid;
61 my $email_uuid = join ',', $self->{email
}, $self->{uuid
};
62 filedb_atomic_append
(jailed_file
('/etc/passwd'),
63 join(':', $self->{name
}, 'x', '\i', $owngroupid, $email_uuid, '/', '/bin/git-shell-verify'),
65 $self->_remove_ssh_leftovers;
70 filedb_atomic_edit
(jailed_file
('/etc/passwd'),
74 if ($self->{name
} eq (split /:/)[0]) {
75 # preserve all but login name and comment field
76 my @fields=split(/:/, $_, -1);
77 $fields[0] = $self->{name
};
78 $self->{uuid
} = (split(',', $fields[4]))[1] || '';
79 $self->{uuid
} or $self->{uuid
} = $self->_gen_uuid;
80 $fields[4] = join(',', $self->{email
}, $self->{uuid
});
81 return join(':', @fields)."\n";
92 $self->_remove_ssh_leftovers;
93 filedb_atomic_edit
(jailed_file
('/etc/passwd'),
95 $self->{name
} ne (split /:/)[0] and return $_;
103 '/etc/sshkeys/'.$self->{name
};
108 open F
, '<', jailed_file
($self->_sshkey_path) or die "sshkey load failed: $!";
114 if (/^ssh-(?:dss|rsa) /) {
116 } elsif (/^# ([A-Z]+)AUTH ([0-9a-f]+) (\d+)/) {
118 $auth = $2 unless (time >= $expire);
119 $authtype = $1 if $auth;
123 my $keys = join("\n", @keys); chomp $keys;
124 ($keys, $auth, $authtype);
130 foreach (split /\r\n|\r|\n/, $keys) {
131 next if /^[ \t]*$/ || /^[ \t]*#/;
134 return join("\n", @lines);
139 $self->{keys} = _trimkeys
($self->{keys} || '');
140 open F
, '>', jailed_file
($self->_sshkey_path) or die "sshkey save failed: $!";
141 if (defined($self->{auth
}) && $self->{auth
}) {
142 my $expire = time + 24 * 3600;
143 my $typestr = $self->{authtype
} ?
uc($self->{authtype
}) : 'REPO';
144 print F
"# ${typestr}AUTH $self->{auth} $expire\n";
146 print F
$self->{keys};
147 print F
"\n" if $self->{keys};
149 chmod 0664, jailed_file
($self->_sshkey_path);
152 # private constructor, do not use
156 Girocco
::User
::valid_name
($name) or die "refusing to create user with invalid name ($name)!";
157 my $proj = { name
=> $name };
162 # public constructor #0
163 # creates a virtual user not connected to disk record
164 # you can conjure() it later to disk
168 my $self = $class->_new($name);
172 # public constructor #1
177 open F
, '<', jailed_file
("/etc/passwd") or die "user load failed: $!";
181 next unless (shift eq $name);
183 my $self = $class->_new($name);
186 (undef, $self->{uid
}, undef, $email_uuid) = @_;
187 ($self->{keys}, $self->{auth
}, $self->{authtype
}) = $self->_sshkey_load;
188 ($self->{email
}, $self->{uuid
}) = split ',', $email_uuid;
191 $self->{uuid
} or $self->_passwd_update;
198 # public constructor #2
203 open F
, '<', jailed_file
("/etc/passwd") or die "user load failed: $!";
207 next unless ($_[2] eq $uid);
209 my $self = $class->_new($_[0]);
212 (undef, undef, $self->{uid
}, undef, $email_uuid) = @_;
213 ($self->{keys}, $self->{auth
}, $self->{authtype
}) = $self->_sshkey_load;
214 ($self->{email
}, $self->{uuid
}) = split ',', $email_uuid;
217 $self->{uuid
} or $self->_passwd_update;
224 # $user may not be in sane state if this returns false!
228 my $cgi = $gcgi->cgi;
230 $self->{name
} = $gcgi->wparam('name');
231 Girocco
::User
::valid_name
($self->{name
})
232 or $gcgi->err("Name contains invalid characters.");
234 length($self->{name
}) <= 64
235 or $gcgi->err("Your user name is longer than 64 characters. Do you really need that much?");
237 $self->{email
} = $gcgi->wparam('email');
238 valid_email
($self->{email
})
239 or $gcgi->err("Your email sure looks weird...?");
240 length($self->{email
}) <= 96
241 or $gcgi->err("Your email is longer than 96 characters. Do you really need that much?");
243 $self->keys_fill($gcgi);
249 my $email = shift || '';
251 if (valid_email
($email)) {
252 $self->{email
} = $email;
253 $self->_passwd_update;
255 $gcgi->err("Your email sure looks weird...?");
258 not $gcgi->err_check;
263 my ($type, $bits, $fingerprint, $comment) = sshpub_validate
($key);
264 return $type ?
1 : 0;
270 my $cgi = $gcgi->cgi;
272 $self->{keys} = _trimkeys
($cgi->param('keys'));
273 length($self->{keys}) <= 4096
274 or $gcgi->err("The list of keys is more than 4kb. Do you really need that much?");
275 foreach my $key (split /\r?\n/, $self->{keys}) {
276 my ($type, $bits, $fingerprint, $comment);
277 ($type, $bits, $fingerprint, $comment) = sshpub_validate
($key)
278 if $key =~ /^ssh-(?:dss|rsa) [0-9A-Za-z+\/=]+ \S
+@\S
+$/;
280 my $keyval = CGI
::escapeHTML
($key);
282 $dsablurb = ' or ssh-dss' unless $Girocco::Config
::disable_dsa
;
284 Your ssh key ("$keyval") appears to have an invalid format
285 (does not start with ssh-rsa$dsablurb or does not end with <tt>\@</tt>-identifier) -
286 maybe your browser has split a single key onto multiple lines?
288 } elsif ($Girocco::Config
::disable_dsa
&& $type eq 'ssh-dss') {
289 my $keyval = CGI
::escapeHTML
($key);
291 Your ssh key ("$keyval") appears to be of type dsa but only rsa keys are
292 supported - please generate an rsa key (starts with ssh-rsa) and try again
294 } elsif ($Girocco::Config
::min_key_length
&& $bits < $Girocco::Config
::min_key_length
) {
295 my $keyval = CGI
::escapeHTML
($key);
297 Your ssh key ("$keyval") appears to have only $bits bit(s) but at least
298 $Girocco::Config::min_key_length are required - please generate a longer key
303 not $gcgi->err_check;
314 my @keys = split(/\r?\n/, $self->{keys});
317 my %types = ('ssh-dss' => 'DSA', 'ssh-rsa' => 'RSA');
321 my ($type, $bits, $fingerprint, $comment) = sshpub_validate
($_);
322 next unless $type && $types{$type};
323 my $euser = CGI
::escapeHTML
(CGI
::Util
::escape
($self->{name
}));
324 $html .= "<li>$bits <tt>$fingerprint</tt> ($types{$type}) $comment";
325 $html .= "<br /><a target=\"_blank\" ".
326 "href=\"@{[url_path($Girocco::Config::webadmurl)]}/usercert.cgi/$euser/$line/".
327 $Girocco::Config
::nickname
."_${euser}_user_$line.pem\">".
328 "download https push user authentication certificate</a> <sup>".
329 "<a target=\"_blank\" href=\"@{[url_path($Girocco::Config::htmlurl)]}/httpspush.html\">".
330 "(learn more)</a></sup>"
331 if $type eq 'ssh-rsa' && $Girocco::Config
::httpspushurl
&&
332 $Girocco::Config
::clientcert
&&
333 $Girocco::Config
::clientkey
;
343 $type = 'REPO' unless $type && $type =~ /^[A-Z]+$/;
345 $self->{authtype
} = $type;
346 $self->{auth
} = sha1_hex
(time . $$ . rand() . $self->{keys});
354 delete $self->{auth
};
355 delete $self->{authtype
};
361 return @
{$self->{projects
}} if defined($self->{projects
});
362 my @projects = filedb_atomic_grep
(jailed_file
('/etc/group'),
366 my ($group, $users) = (split /:/)[0,3];
367 $group if $users && $users =~ /(^|,)\Q$self->{name}\E(,|$)/;
370 $self->{projects
} = \
@projects;
371 @
{$self->{projects
}};
384 require Girocco
::Project
;
385 foreach ($self->get_projects) {
386 if (Girocco
::Project
::does_exist
($_)) {
387 my $project = Girocco
::Project
->load($_);
388 $project->update if $project->remove_user($self->{name
});
392 $self->_passwd_remove;
399 /^[a-zA-Z0-9][a-zA-Z0-9+._-]*$/;
404 Girocco
::User
::valid_name
($name) or die "tried to query for user with invalid name $name!";
405 (-e jailed_file
("/etc/sshkeys/$name"));
410 $Girocco::Config
::chrooted
and undef; # TODO for ACLs within chroot
411 scalar(getpwnam($name));