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";
56 use POSIX
qw(strftime);
58 my (undef, undef, $gid) = getgrnam($Girocco::Config
::owning_group
||'');
59 my $owngroupid = $gid ?
$gid : 65534;
60 Girocco
::User
->load($self->{name
}) and die "User $self->{name} already exists";
61 $self->{uuid
} = $self->_gen_uuid;
62 my ($S,$M,$H,$d,$m,$y) = gmtime(time());
63 $self->{creationtime
} = strftime
("%Y%m%d_%H%M%S", $S, $M, $H, $d, $m, $y, -1, -1, -1);
64 my $email_uuid_etc = join ',', $self->{email
}, $self->{uuid
}, $self->{creationtime
};
65 filedb_atomic_append
(jailed_file
('/etc/passwd'),
66 join(':', $self->{name
}, 'x', '\i', $owngroupid, $email_uuid_etc, '/', '/bin/git-shell-verify'),
68 $self->_remove_ssh_leftovers;
73 filedb_atomic_edit
(jailed_file
('/etc/passwd'),
77 if ($self->{name
} eq (split /:/)[0]) {
78 # preserve all but login name and first 2 fields of comment field
79 # creating a uuid (field 2 of comment field) if one is not already present
80 my @fields=split(/:/, $_, -1);
81 $fields[0] = $self->{name
};
82 my @subfields = split(',', $fields[4]||'', -1);
83 $self->{uuid
} = $subfields[1] || '';
84 $self->{uuid
} or $self->{uuid
} = $self->_gen_uuid;
85 $subfields[0] = $self->{email
};
86 $subfields[1] = $self->{uuid
};
87 $fields[4] = join(',', @subfields);
88 return join(':', @fields)."\n";
99 $self->_remove_ssh_leftovers;
100 filedb_atomic_edit
(jailed_file
('/etc/passwd'),
102 $self->{name
} ne (split /:/)[0] and return $_;
110 '/etc/sshkeys/'.$self->{name
};
115 open F
, '<', jailed_file
($self->_sshkey_path) or die "sshkey load failed: $!";
121 if (/^ssh-(?:dss|rsa) /) {
123 } elsif (/^# ([A-Z]+)AUTH ([0-9a-f]+) (\d+)/) {
125 $auth = $2 unless (time >= $expire);
126 $authtype = $1 if $auth;
130 my $keys = join("\n", @keys); chomp $keys;
131 ($keys, $auth, $authtype);
137 foreach (split /\r\n|\r|\n/, $keys) {
138 next if /^[ \t]*$/ || /^[ \t]*#/;
141 return join("\n", @lines);
146 $self->{keys} = _trimkeys
($self->{keys} || '');
147 open F
, '>', jailed_file
($self->_sshkey_path) or die "sshkey save failed: $!";
148 if (defined($self->{auth
}) && $self->{auth
}) {
149 my $expire = time + 24 * 3600;
150 my $typestr = $self->{authtype
} ?
uc($self->{authtype
}) : 'REPO';
151 print F
"# ${typestr}AUTH $self->{auth} $expire\n";
153 print F
$self->{keys};
154 print F
"\n" if $self->{keys};
156 chmod 0664, jailed_file
($self->_sshkey_path);
159 # private constructor, do not use
163 Girocco
::User
::valid_name
($name) or die "refusing to create user with invalid name ($name)!";
164 my $proj = { name
=> $name };
169 # public constructor #0
170 # creates a virtual user not connected to disk record
171 # you can conjure() it later to disk
175 my $self = $class->_new($name);
179 # public constructor #1
184 open F
, '<', jailed_file
("/etc/passwd") or die "user load failed: $!";
188 next unless (shift eq $name);
190 my $self = $class->_new($name);
193 (undef, $self->{uid
}, undef, $email_uuid_etc) = @_;
194 ($self->{keys}, $self->{auth
}, $self->{authtype
}) = $self->_sshkey_load;
195 ($self->{email
}, $self->{uuid
}, $self->{creationtime
}) = split ',', $email_uuid_etc;
198 $self->{uuid
} or $self->_passwd_update;
205 # public constructor #2
210 open F
, '<', jailed_file
("/etc/passwd") or die "user load failed: $!";
214 next unless ($_[2] eq $uid);
216 my $self = $class->_new($_[0]);
219 (undef, undef, $self->{uid
}, undef, $email_uuid_etc) = @_;
220 ($self->{keys}, $self->{auth
}, $self->{authtype
}) = $self->_sshkey_load;
221 ($self->{email
}, $self->{uuid
}, $self->{creationtime
}) = split ',', $email_uuid_etc;
224 $self->{uuid
} or $self->_passwd_update;
231 # $user may not be in sane state if this returns false!
235 my $cgi = $gcgi->cgi;
237 $self->{name
} = $gcgi->wparam('name');
238 Girocco
::User
::valid_name
($self->{name
})
239 or $gcgi->err("Name contains invalid characters.");
241 length($self->{name
}) <= 64
242 or $gcgi->err("Your user name is longer than 64 characters. Do you really need that much?");
244 $self->{email
} = $gcgi->wparam('email');
245 valid_email
($self->{email
})
246 or $gcgi->err("Your email sure looks weird...?");
247 length($self->{email
}) <= 96
248 or $gcgi->err("Your email is longer than 96 characters. Do you really need that much?");
250 $self->keys_fill($gcgi);
256 my $email = shift || '';
258 if (valid_email
($email)) {
259 $self->{email
} = $email;
260 $self->_passwd_update;
262 $gcgi->err("Your email sure looks weird...?");
265 not $gcgi->err_check;
270 my ($type, $bits, $fingerprint, $comment) = sshpub_validate
($key);
271 return $type ?
1 : 0;
277 my $cgi = $gcgi->cgi;
279 $self->{keys} = _trimkeys
($cgi->param('keys'));
280 length($self->{keys}) <= 4096
281 or $gcgi->err("The list of keys is more than 4kb. Do you really need that much?");
282 foreach my $key (split /\r?\n/, $self->{keys}) {
283 my ($type, $bits, $fingerprint, $comment);
284 ($type, $bits, $fingerprint, $comment) = sshpub_validate
($key)
285 if $key =~ /^ssh-(?:dss|rsa) [0-9A-Za-z+\/=]+ \S
+@\S
+$/;
287 my $keyval = CGI
::escapeHTML
($key);
289 $dsablurb = ' or ssh-dss' unless $Girocco::Config
::disable_dsa
;
291 Your ssh key ("$keyval") appears to have an invalid format
292 (does not start with ssh-rsa$dsablurb or does not end with <tt>\@</tt>-identifier) -
293 maybe your browser has split a single key onto multiple lines?
295 } elsif ($Girocco::Config
::disable_dsa
&& $type eq 'ssh-dss') {
296 my $keyval = CGI
::escapeHTML
($key);
298 Your ssh key ("$keyval") appears to be of type dsa but only rsa keys are
299 supported - please generate an rsa key (starts with ssh-rsa) and try again
301 } elsif ($Girocco::Config
::min_key_length
&& $bits < $Girocco::Config
::min_key_length
) {
302 my $keyval = CGI
::escapeHTML
($key);
304 Your ssh key ("$keyval") appears to have only $bits bit(s) but at least
305 $Girocco::Config::min_key_length are required - please generate a longer key
310 not $gcgi->err_check;
321 my @keys = split(/\r?\n/, $self->{keys});
324 my %types = ('ssh-dss' => 'DSA', 'ssh-rsa' => 'RSA');
328 my ($type, $bits, $fingerprint, $comment) = sshpub_validate
($_);
329 next unless $type && $types{$type};
330 my $euser = CGI
::escapeHTML
(CGI
::Util
::escape
($self->{name
}));
331 $html .= "<li>$bits <tt>$fingerprint</tt> ($types{$type}) $comment";
332 $html .= "<br /><a target=\"_blank\" ".
333 "href=\"@{[url_path($Girocco::Config::webadmurl)]}/usercert.cgi/$euser/$line/".
334 $Girocco::Config
::nickname
."_${euser}_user_$line.pem\">".
335 "download https push user authentication certificate</a> <sup>".
336 "<a target=\"_blank\" href=\"@{[url_path($Girocco::Config::htmlurl)]}/httpspush.html\">".
337 "(learn more)</a></sup>"
338 if $type eq 'ssh-rsa' && $Girocco::Config
::httpspushurl
&&
339 $Girocco::Config
::clientcert
&&
340 $Girocco::Config
::clientkey
;
350 $type = 'REPO' unless $type && $type =~ /^[A-Z]+$/;
352 $self->{authtype
} = $type;
353 $self->{auth
} = sha1_hex
(time . $$ . rand() . $self->{keys});
361 delete $self->{auth
};
362 delete $self->{authtype
};
368 return @
{$self->{projects
}} if defined($self->{projects
});
369 my @projects = filedb_atomic_grep
(jailed_file
('/etc/group'),
373 my ($group, $users) = (split /:/)[0,3];
374 $group if $users && $users =~ /(^|,)\Q$self->{name}\E(,|$)/;
377 $self->{projects
} = \
@projects;
378 @
{$self->{projects
}};
391 require Girocco
::Project
;
392 foreach ($self->get_projects) {
393 if (Girocco
::Project
::does_exist
($_)) {
394 my $project = Girocco
::Project
->load($_);
395 $project->update if $project->remove_user($self->{name
});
399 $self->_passwd_remove;
406 /^[a-zA-Z0-9][a-zA-Z0-9+._-]*$/;
411 Girocco
::User
::valid_name
($name) or die "tried to query for user with invalid name $name!";
412 (-e jailed_file
("/etc/sshkeys/$name"));
417 $Girocco::Config
::chrooted
and undef; # TODO for ACLs within chroot
418 scalar(getpwnam($name));