| 1 | #!/usr/bin/perl | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use lib qw(lib); | ||
| 7 | |||
| 8 | use File::Path; | ||
| 9 | use File::Basename; | ||
| 10 | use File::chmod qw(symchmod); | ||
| 11 | use File::Spec; | ||
| 12 | use Arepa::Config; | ||
| 13 | use Arepa::PackageDb; | ||
| 14 | |||
| 15 | $File::chmod::UMASK = 0; | ||
| 16 | |||
| 17 | my $arepa_user = "arepa-master"; | ||
| 18 | my $arepa_group = "arepa"; | ||
| 19 | my $web_user = "www-data"; | ||
| 20 | my $web_group = "www-data"; | ||
| 21 | |||
| 22 | my $config = Arepa::Config->new("/etc/arepa/config.yml"); | ||
| 23 | |||
| 24 | my $uid = getgrnam($arepa_user); | ||
| 25 | if (!defined $uid) { | ||
| 26 | print STDERR "ERROR: User '$arepa_user' doesn't exist\n"; | ||
| 27 | exit 1; | ||
| 28 | } | ||
| 29 | my $gid = getgrnam($arepa_group); | ||
| 30 | if (!defined $gid) { | ||
| 31 | print STDERR "ERROR: Group '$arepa_group' doesn't exist\n"; | ||
| 32 | exit 1; | ||
| 33 | } | ||
| 34 | my $web_uid = getgrnam($web_user); | ||
| 35 | if (!defined $web_uid) { | ||
| 36 | print STDERR "ERROR: User '$web_user' doesn't exist\n"; | ||
| 37 | exit 1; | ||
| 38 | } | ||
| 39 | my $web_gid = getgrnam($web_group); | ||
| 40 | if (!defined $web_gid) { | ||
| 41 | print STDERR "ERROR: Group '$web_group' doesn't exist\n"; | ||
| 42 | exit 1; | ||
| 43 | } | ||
| 44 | |||
| 45 | my $package_db_path = $config->get_key("package_db"); | ||
| 46 | foreach my $path (dirname($package_db_path), | ||
| 47 | $config->get_key("repository:path"), | ||
| 48 | File::Spec->catfile($config->get_key("repository:path"), | ||
| 49 | "conf"), | ||
| 50 | $config->get_key("upload_queue:path"), | ||
| 51 | $config->get_key("dir:build_logs")) { | ||
| 52 | print "Creating directory $path\n"; | ||
| 53 | mkpath($path); | ||
| 54 | chown($uid, $gid, $path); | ||
| 55 | symchmod("g+w", $path); | ||
| 56 | } | ||
| 57 | |||
| 58 | my $builder_dir = "/etc/arepa/builders"; | ||
| 59 | print "Creating builder configuration directory $builder_dir\n"; | ||
| 60 | mkpath($builder_dir); | ||
| 61 | chown($uid, $gid, $builder_dir); | ||
| 62 | symchmod("g+w", $builder_dir); | ||
| 63 | |||
| 64 | print "Creating package DB in $package_db_path\n"; | ||
| 65 | my $package_db = Arepa::PackageDb->new($package_db_path); | ||
| 66 | chown($uid, $gid, $package_db_path); | ||
| 67 | symchmod("g+w", $package_db_path); | ||
| 68 | |||
| 69 | my $db_dir = dirname($package_db_path); | ||
| 70 | print "Fixing permissions for database directory $db_dir\n"; | ||
| 71 | chown($uid, $gid, $db_dir); | ||
| 72 | symchmod("g+w", $db_dir); | ||
| 73 | |||
| 74 | my $session_db_path = $config->get_key("web_ui:session_db"); | ||
| 75 | if (! -r $session_db_path) { | ||
| 76 | print "Creating web UI session DB in $session_db_path\n"; | ||
| 77 | open F, ">>$session_db_path"; close F; | ||
| 78 | my $sqlite_cmd = <<EOC; | ||
| 79 | echo "CREATE TABLE session (sid VARCHAR(40) PRIMARY KEY, data TEXT, expires INTEGER UNSIGNED NOT NULL, UNIQUE(sid));" | sqlite3 '$session_db_path' | ||
| 80 | EOC | ||
| 81 | print "Creating session DB schema with:\n$sqlite_cmd"; | ||
| 82 | system($sqlite_cmd); | ||
| 83 | chown($web_uid, $web_gid, $session_db_path); | ||
| 84 | symchmod("g+w", $session_db_path); | ||
| 85 | } | ||
| 86 | |||
| 87 | my $repo_dists_conf = File::Spec->catfile($config->get_key("repository:path"), | ||
| 88 | "conf", | ||
| 89 | "distributions"); | ||
| 90 | print "Creating repo configuration file in $repo_dists_conf\n"; | ||
| 91 | open F, ">>$repo_dists_conf"; | ||
| 92 | close F; | ||
| 93 | chown($uid, $gid, $repo_dists_conf); | ||
| 94 | symchmod("g+w", $repo_dists_conf); | ||
| 95 | |||
| 96 | my $gpg_dir = $config->get_key("web_ui:gpg_homedir"); | ||
| 97 | print "Creating GPG directory in $gpg_dir\n"; | ||
| 98 | mkpath($gpg_dir); | ||
| 99 | chown($web_uid, $web_gid, $gpg_dir); | ||
| 100 | chmod(0700, $gpg_dir); | ||
| 101 | |||
| 102 | my $gpg_options = File::Spec->catfile($config->get_key("web_ui:gpg_homedir"), | ||
| 103 | "options"); | ||
| 104 | if (! -f $gpg_options) { | ||
| 105 | print "Creating options file $gpg_options\n"; | ||
| 106 | my $keyrings_dir = | ||
| 107 | File::Spec->catfile(dirname($config->get_key("web_ui:gpg_homedir")), | ||
| 108 | "keyrings"); | ||
| 109 | mkpath($keyrings_dir); | ||
| 110 | chown($uid, $gid, $keyrings_dir); | ||
| 111 | symchmod("g+w", $keyrings_dir); | ||
| 112 | |||
| 113 | open F, ">$gpg_options"; | ||
| 114 | print F "keyring $keyrings_dir/uploaders.gpg\n"; | ||
| 115 | close F; | ||
| 116 | chown($uid, $gid, $gpg_options); | ||
| 117 | symchmod("g+w", $gpg_options); | ||
| 118 | } |