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'));
64 $self->_remove_ssh_leftovers;
69 filedb_atomic_edit
(jailed_file
('/etc/passwd'),
73 if ($self->{name
} eq (split /:/)[0]) {
74 # preserve all but login name and comment field
75 my @fields=split(/:/, $_, -1);
76 $fields[0] = $self->{name
};
77 $self->{uuid
} = (split(',', $fields[4]))[1] || '';
78 $self->{uuid
} or $self->{uuid
} = $self->_gen_uuid;
79 $fields[4] = join(',', $self->{email
}, $self->{uuid
});
80 return join(':', @fields)."\n";
90 $self->_remove_ssh_leftovers;
91 filedb_atomic_edit
(jailed_file
('/etc/passwd'),
93 $self->{name
} ne (split /:/)[0] and return $_;
100 '/etc/sshkeys/'.$self->{name
};
105 open F
, '<', jailed_file
($self->_sshkey_path) or die "sshkey load failed: $!";
111 if (/^ssh-(?:dss|rsa) /) {
113 } elsif (/^# ([A-Z]+)AUTH ([0-9a-f]+) (\d+)/) {
115 $auth = $2 unless (time >= $expire);
116 $authtype = $1 if $auth;
120 my $keys = join("\n", @keys); chomp $keys;
121 ($keys, $auth, $authtype);
127 foreach (split /\r\n|\r|\n/, $keys) {
128 next if /^[ \t]*$/ || /^[ \t]*#/;
131 return join("\n", @lines);
136 $self->{keys} = _trimkeys
($self->{keys} || '');
137 open F
, '>', jailed_file
($self->_sshkey_path) or die "sshkey save failed: $!";
138 if (defined($self->{auth
}) && $self->{auth
}) {
139 my $expire = time + 24 * 3600;
140 my $typestr = $self->{authtype
} ?
uc($self->{authtype
}) : 'REPO';
141 print F
"# ${typestr}AUTH $self->{auth} $expire\n";
143 print F
$self->{keys};
144 print F
"\n" if $self->{keys};
146 chmod 0664, jailed_file
($self->_sshkey_path);
149 # private constructor, do not use
153 Girocco
::User
::valid_name
($name) or die "refusing to create user with invalid name ($name)!";
154 my $proj = { name
=> $name };
159 # public constructor #0
160 # creates a virtual user not connected to disk record
161 # you can conjure() it later to disk
165 my $self = $class->_new($name);
169 # public constructor #1
174 open F
, '<', jailed_file
("/etc/passwd") or die "user load failed: $!";
178 next unless (shift eq $name);
180 my $self = $class->_new($name);
183 (undef, $self->{uid
}, undef, $email_uuid) = @_;
184 ($self->{keys}, $self->{auth
}, $self->{authtype
}) = $self->_sshkey_load;
185 ($self->{email
}, $self->{uuid
}) = split ',', $email_uuid;
188 $self->{uuid
} or $self->_passwd_update;
195 # public constructor #2
200 open F
, '<', jailed_file
("/etc/passwd") or die "user load failed: $!";
204 next unless ($_[2] eq $uid);
206 my $self = $class->_new($_[0]);
209 (undef, undef, $self->{uid
}, undef, $email_uuid) = @_;
210 ($self->{keys}, $self->{auth
}, $self->{authtype
}) = $self->_sshkey_load;
211 ($self->{email
}, $self->{uuid
}) = split ',', $email_uuid;
214 $self->{uuid
} or $self->_passwd_update;
221 # $user may not be in sane state if this returns false!
225 my $cgi = $gcgi->cgi;
227 $self->{name
} = $gcgi->wparam('name');
228 Girocco
::User
::valid_name
($self->{name
})
229 or $gcgi->err("Name contains invalid characters.");
231 $self->{email
} = $gcgi->wparam('email');
232 valid_email
($self->{email
})
233 or $gcgi->err("Your email sure looks weird...?");
235 $self->keys_fill($gcgi);
241 my $email = shift || '';
243 if (valid_email
($email)) {
244 $self->{email
} = $email;
245 $self->_passwd_update;
247 $gcgi->err("Your email sure looks weird...?");
250 not $gcgi->err_check;
255 my ($type, $bits, $fingerprint, $comment) = sshpub_validate
($key);
256 return $type ?
1 : 0;
262 my $cgi = $gcgi->cgi;
264 $self->{keys} = _trimkeys
($cgi->param('keys'));
265 length($self->{keys}) <= 4096
266 or $gcgi->err("The list of keys is more than 4kb. Do you really need that much?");
267 foreach my $key (split /\r?\n/, $self->{keys}) {
268 my ($type, $bits, $fingerprint, $comment);
269 ($type, $bits, $fingerprint, $comment) = sshpub_validate
($key)
270 if $key =~ /^ssh-(?:dss|rsa) [0-9A-Za-z+\/=]+ \S
+@\S
+$/;
272 my $keyval = CGI
::escapeHTML
($key);
274 $dsablurb = ' or ssh-dss' unless $Girocco::Config
::disable_dsa
;
276 Your ssh key ("$keyval") appears to have an invalid format
277 (does not start with ssh-rsa$dsablurb or does not end with <tt>\@</tt>-identifier) -
278 maybe your browser has split a single key onto multiple lines?
280 } elsif ($Girocco::Config
::disable_dsa
&& $type eq 'ssh-dss') {
281 my $keyval = CGI
::escapeHTML
($key);
283 Your ssh key ("$keyval") appears to be of type dsa but only rsa keys are
284 supported - please generate an rsa key (starts with ssh-rsa) and try again
286 } elsif ($Girocco::Config
::min_key_length
&& $bits < $Girocco::Config
::min_key_length
) {
287 my $keyval = CGI
::escapeHTML
($key);
289 Your ssh key ("$keyval") appears to have only $bits bit(s) but at least
290 $Girocco::Config::min_key_length are required - please generate a longer key
295 not $gcgi->err_check;
306 my @keys = split(/\r?\n/, $self->{keys});
309 my %types = ('ssh-dss' => 'DSA', 'ssh-rsa' => 'RSA');
313 my ($type, $bits, $fingerprint, $comment) = sshpub_validate
($_);
314 next unless $type && $types{$type};
315 my $euser = CGI
::escapeHTML
(CGI
::Util
::escape
($self->{name
}));
316 $html .= "<li>$bits <tt>$fingerprint</tt> ($types{$type}) $comment";
317 $html .= "<br /><a target=\"_blank\" ".
318 "href=\"@{[url_path($Girocco::Config::webadmurl)]}/usercert.cgi/$euser/$line/".
319 $Girocco::Config
::nickname
."_${euser}_user_$line.pem\">".
320 "download https push user authentication certificate</a> <sup>".
321 "<a target=\"_blank\" href=\"@{[url_path($Girocco::Config::htmlurl)]}/httpspush.html\">".
322 "(learn more)</a></sup>"
323 if $type eq 'ssh-rsa' && $Girocco::Config
::httpspushurl
&&
324 $Girocco::Config
::clientcert
&&
325 $Girocco::Config
::clientkey
;
335 $type = 'REPO' unless $type && $type =~ /^[A-Z]+$/;
337 $self->{authtype
} = $type;
338 $self->{auth
} = sha1_hex
(time . $$ . rand() . $self->{keys});
346 delete $self->{auth
};
347 delete $self->{authtype
};
353 return @
{$self->{projects
}} if defined($self->{projects
});
354 my @projects = filedb_atomic_grep
(jailed_file
('/etc/group'),
358 my ($group, $users) = (split /:/)[0,3];
359 $group if $users && $users =~ /(^|,)\Q$self->{name}\E(,|$)/;
362 $self->{projects
} = \
@projects;
363 @
{$self->{projects
}};
376 require Girocco
::Project
;
377 foreach ($self->get_projects) {
378 if (Girocco
::Project
::does_exist
($_)) {
379 my $project = Girocco
::Project
->load($_);
380 $project->update if $project->remove_user($self->{name
});
384 $self->_passwd_remove;
391 /^[a-zA-Z0-9+._-]+$/;
396 Girocco
::User
::valid_name
($name) or die "tried to query for user with invalid name $name!";
397 (-e jailed_file
("/etc/sshkeys/$name"));
402 $Girocco::Config
::chrooted
and undef; # TODO for ACLs within chroot
403 scalar(getpwnam($name));