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
};
37 my @md5 = unpack('C*', md5
(time . $$ . rand() . join(':',%$self)));
38 $md5[6] = 0x40 | ($md5[6] & 0x0F); # Version 4 -- random
39 $md5[8] = 0x80 | ($md5[8] & 0x3F); # RFC 4122 specification
41 '%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x',
47 my (undef, undef, $gid) = getgrnam($Girocco::Config
::owning_group
||'');
48 my $owngroupid = $gid ?
$gid : 65534;
49 Girocco
::User
->load($self->{name
}) and die "User $self->{name} already exists";
50 $self->{uuid
} = $self->_gen_uuid;
51 my $email_uuid = join ',', $self->{email
}, $self->{uuid
};
52 filedb_atomic_append
(jailed_file
('/etc/passwd'),
53 join(':', $self->{name
}, 'x', '\i', $owngroupid, $email_uuid, '/', '/bin/git-shell-verify'));
58 filedb_atomic_edit
(jailed_file
('/etc/passwd'),
62 if ($self->{name
} eq (split /:/)[0]) {
63 # preserve all but login name and comment field
64 my @fields=split(/:/, $_, -1);
65 $fields[0] = $self->{name
};
66 $self->{uuid
} = (split(',', $fields[4]))[1] || '';
67 $self->{uuid
} or $self->{uuid
} = $self->_gen_uuid;
68 $fields[4] = join(',', $self->{email
}, $self->{uuid
});
69 return join(':', @fields)."\n";
79 '/etc/sshkeys/'.$self->{name
};
84 open F
, "<".jailed_file
($self->_sshkey_path) or die "sshkey load failed: $!";
89 if (/^ssh-(?:dss|rsa) /) {
91 } elsif (/^# REPOAUTH ([0-9a-f]+) (\d+)/) {
93 $auth = $1 unless (time >= $expire);
97 my $keys = join("\n", @keys); chomp $keys;
103 open F
, ">".jailed_file
($self->_sshkey_path) or die "sshkey failed: $!";
104 if (defined($self->{auth
}) && $self->{auth
}) {
105 my $expire = time + 24 * 3600;
106 print F
"# REPOAUTH $self->{auth} $expire\n";
108 print F
$self->{keys}."\n";
110 chmod 0664, jailed_file
($self->_sshkey_path);
113 # private constructor, do not use
117 Girocco
::User
::valid_name
($name) or die "refusing to create user with invalid name ($name)!";
118 my $proj = { name
=> $name };
123 # public constructor #0
124 # creates a virtual user not connected to disk record
125 # you can conjure() it later to disk
129 my $self = $class->_new($name);
133 # public constructor #1
138 open F
, jailed_file
("/etc/passwd") or die "user load failed: $!";
142 next unless (shift eq $name);
144 my $self = $class->_new($name);
147 (undef, $self->{uid
}, undef, $email_uuid) = @_;
148 ($self->{keys}, $self->{auth
}) = $self->_sshkey_load;
149 ($self->{email
}, $self->{uuid
}) = split ',', $email_uuid;
152 $self->{uuid
} or $self->_passwd_update;
159 # public constructor #2
164 open F
, jailed_file
("/etc/passwd") or die "user load failed: $!";
168 next unless ($_[2] eq $uid);
170 my $self = $class->_new($_[0]);
173 (undef, undef, $self->{uid
}, undef, $email_uuid) = @_;
174 ($self->{keys}, $self->{auth
}) = $self->_sshkey_load;
175 ($self->{email
}, $self->{uuid
}) = split ',', $email_uuid;
178 $self->{uuid
} or $self->_passwd_update;
185 # $user may not be in sane state if this returns false!
189 my $cgi = $gcgi->cgi;
191 $self->{name
} = $gcgi->wparam('name');
192 Girocco
::User
::valid_name
($self->{name
})
193 or $gcgi->err("Name contains invalid characters.");
195 $self->{email
} = $gcgi->wparam('email');
196 valid_email
($self->{email
})
197 or $gcgi->err("Your email sure looks weird...?");
199 $self->keys_fill($gcgi);
205 foreach (split /\r\n|\r|\n/, $keys) {
206 next if /^[ \t]*$/ || /^[ \t]*#/;
209 return join("\n", @lines);
215 my $email = shift || '';
217 if (valid_email
($email)) {
218 $self->{email
} = $email;
219 $self->_passwd_update;
221 $gcgi->err("Your email sure looks weird...?");
224 not $gcgi->err_check;
229 my ($type, $bits, $fingerprint, $comment) = sshpub_validate
($key);
230 return $type ?
1 : 0;
236 my $cgi = $gcgi->cgi;
238 $self->{keys} = _trimkeys
($cgi->param('keys'));
239 length($self->{keys}) <= 4096
240 or $gcgi->err("The list of keys is more than 4kb. Do you really need that much?");
241 foreach (split /\r?\n/, $self->{keys}) {
243 /^ssh-(?:dss|rsa) [0-9A-Za-z+\/=]+ \S
+@\S
+$/ && _checkkey
($_)
244 or $keyval=CGI
::escapeHTML
($_),$gcgi->err(<<EOT);
245 Your ssh key ("$keyval") appears to have an invalid format
246 (does not start with ssh-dss or ssh-rsa or does not end with <tt>\@</tt>-identifier) -
247 maybe your browser has split a single key onto multiple lines?
251 not $gcgi->err_check;
262 my @keys = split(/\r?\n/, $self->{keys});
265 my %types = ('ssh-dss' => 'DSA', 'ssh-rsa' => 'RSA');
269 my ($type, $bits, $fingerprint, $comment) = sshpub_validate
($_);
270 next unless $type && $types{$type};
271 my $euser = CGI
::escapeHTML
(CGI
::Util
::escape
($self->{name
}));
272 $html .= "<li>$bits <tt>$fingerprint</tt> ($types{$type}) $comment";
273 $html .= "<br /><a target=\"_blank\" href=\"/usercert.cgi/$euser/$line/".
274 $Girocco::Config
::nickname
."_${euser}_user_$line.pem\">".
275 "download https push user authentication certificate</a> <sup>".
276 "<a target=\"_blank\" href=\"$Girocco::Config::htmlurl/httpspush.html\">".
277 "(learn more)</a></sup>"
278 if $type eq 'ssh-rsa' && $Girocco::Config
::httpspushurl
&&
279 $Girocco::Config
::clientcert
&&
280 $Girocco::Config
::clientkey
;
290 $self->{auth
} = sha1_hex
(time . $$ . rand() . $self->{keys});
298 delete $self->{auth
};
312 /^[a-zA-Z0-9+._-]+$/;
317 Girocco
::User
::valid_name
($name) or die "tried to query for user with invalid name $name!";
318 (-e jailed_file
("/etc/sshkeys/$name"));
323 $Girocco::Config
::chrooted
and undef; # TODO for ACLs within chroot
324 scalar(getpwnam($name));