| 1 | #!@PERL@ |
|---|
| 2 | use strict; |
|---|
| 3 | |
|---|
| 4 | # should be running as root |
|---|
| 5 | if($> != 0) |
|---|
| 6 | { |
|---|
| 7 | printf "\nWARNING: this should be run as root\n\n" |
|---|
| 8 | } |
|---|
| 9 | |
|---|
| 10 | # check and get command line parameters |
|---|
| 11 | if($#ARGV < 2) |
|---|
| 12 | { |
|---|
| 13 | print <<__E; |
|---|
| 14 | |
|---|
| 15 | Setup bbstored config utility. |
|---|
| 16 | |
|---|
| 17 | Bad command line parameters. |
|---|
| 18 | Usage: |
|---|
| 19 | bbstored-config config-dir server-hostname username [raidfile-config] |
|---|
| 20 | |
|---|
| 21 | Parameters: |
|---|
| 22 | config-dir is usually @sysconfdir_expanded@/boxbackup |
|---|
| 23 | server-hostname is the hostname that clients will use to connect to |
|---|
| 24 | this server |
|---|
| 25 | username is the user to run the server under |
|---|
| 26 | raidfile-config is optional. Use if you have a non-standard |
|---|
| 27 | raidfile.conf file. |
|---|
| 28 | |
|---|
| 29 | __E |
|---|
| 30 | exit(1); |
|---|
| 31 | } |
|---|
| 32 | |
|---|
| 33 | # check for OPENSSL_CONF environment var being set |
|---|
| 34 | if(exists $ENV{'OPENSSL_CONF'}) |
|---|
| 35 | { |
|---|
| 36 | print <<__E; |
|---|
| 37 | |
|---|
| 38 | --------------------------------------- |
|---|
| 39 | |
|---|
| 40 | WARNING: |
|---|
| 41 | You have the OPENSSL_CONF environment variable set. |
|---|
| 42 | Use of non-standard openssl configs may cause problems. |
|---|
| 43 | |
|---|
| 44 | --------------------------------------- |
|---|
| 45 | |
|---|
| 46 | __E |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | # default locations |
|---|
| 50 | my $default_config_location = '@sysconfdir_expanded@/boxbackup/bbstored.conf'; |
|---|
| 51 | |
|---|
| 52 | # command line parameters |
|---|
| 53 | my ($config_dir,$server,$username,$raidfile_config) = @ARGV; |
|---|
| 54 | |
|---|
| 55 | $raidfile_config = $config_dir . '/raidfile.conf' unless $raidfile_config ne ''; |
|---|
| 56 | |
|---|
| 57 | # check server exists, but don't bother checking that it's actually this machine. |
|---|
| 58 | { |
|---|
| 59 | my @r = gethostbyname($server); |
|---|
| 60 | if($#r < 0) |
|---|
| 61 | { |
|---|
| 62 | die "Server '$server' not found. (check server name, test DNS lookup failed.)" |
|---|
| 63 | } |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | # check this exists |
|---|
| 67 | if(!-f $raidfile_config) |
|---|
| 68 | { |
|---|
| 69 | print "The RaidFile configuration file $raidfile_config doesn't exist.\nYou may need to create it with raidfile-config.\nWon't configure bbstored without it.\n"; |
|---|
| 70 | exit(1); |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | # check that the user exists |
|---|
| 74 | die "You shouldn't run bbstored as root" if $username eq 'root'; |
|---|
| 75 | my $user_uid = 0; |
|---|
| 76 | (undef,undef,$user_uid) = getpwnam($username); |
|---|
| 77 | if($user_uid == 0) |
|---|
| 78 | { |
|---|
| 79 | die "User $username doesn't exist\n"; |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | # check that directories are writeable |
|---|
| 83 | open RAIDCONF,$raidfile_config or die "Can't open $raidfile_config"; |
|---|
| 84 | { |
|---|
| 85 | my %done = (); |
|---|
| 86 | while(<RAIDCONF>) |
|---|
| 87 | { |
|---|
| 88 | next unless m/Dir\d\s*=\s*(.+)/; |
|---|
| 89 | my $d = $1; |
|---|
| 90 | $d = $d.'/backup' if -e $d.'/backup'; |
|---|
| 91 | print "Checking permissions on $d\n"; |
|---|
| 92 | my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = stat($d); |
|---|
| 93 | my $req_perms = ($uid == $user_uid)?0700:0007; |
|---|
| 94 | if(($mode & $req_perms) != $req_perms) |
|---|
| 95 | { |
|---|
| 96 | print "$username doesn't appear to have the necessary permissions on $d\n"; |
|---|
| 97 | print "Either adjust permissions, or create a directory 'backup' inside the\n"; |
|---|
| 98 | print "directory specified in raidfile.conf which is writable.\n"; |
|---|
| 99 | exit(1); |
|---|
| 100 | } |
|---|
| 101 | } |
|---|
| 102 | } |
|---|
| 103 | close RAIDCONF; |
|---|
| 104 | |
|---|
| 105 | # ssl stuff |
|---|
| 106 | my $private_key = "$config_dir/bbstored/$server-key.pem"; |
|---|
| 107 | my $certificate_request = "$config_dir/bbstored/$server-csr.pem"; |
|---|
| 108 | my $certificate = "$config_dir/bbstored/$server-cert.pem"; |
|---|
| 109 | my $ca_root_cert = "$config_dir/bbstored/clientCA.pem"; |
|---|
| 110 | |
|---|
| 111 | # other files |
|---|
| 112 | my $config_file = "$config_dir/bbstored.conf"; |
|---|
| 113 | my $accounts_file = "$config_dir/bbstored/accounts.txt"; |
|---|
| 114 | |
|---|
| 115 | # summarise configuration |
|---|
| 116 | |
|---|
| 117 | print <<__E; |
|---|
| 118 | |
|---|
| 119 | Setup bbstored config utility. |
|---|
| 120 | |
|---|
| 121 | Configuration: |
|---|
| 122 | Writing configuration file: $config_file |
|---|
| 123 | Writing empty accounts file: $accounts_file |
|---|
| 124 | Server hostname: $server |
|---|
| 125 | RaidFile config: $raidfile_config |
|---|
| 126 | |
|---|
| 127 | __E |
|---|
| 128 | |
|---|
| 129 | # create directories |
|---|
| 130 | if(!-d $config_dir) |
|---|
| 131 | { |
|---|
| 132 | print "Creating $config_dir...\n"; |
|---|
| 133 | mkdir $config_dir,0755 or die "Can't create $config_dir"; |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | if(!-d "$config_dir/bbstored") |
|---|
| 137 | { |
|---|
| 138 | print "Creating $config_dir/bbstored\n"; |
|---|
| 139 | mkdir "$config_dir/bbstored",0755 or die "Can't create $config_dir/bbstored"; |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | # create blank accounts file |
|---|
| 143 | if(!-f $accounts_file) |
|---|
| 144 | { |
|---|
| 145 | print "Creating blank accounts file\n"; |
|---|
| 146 | open ACC,">$accounts_file"; |
|---|
| 147 | close ACC; |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | # generate the private key for the server |
|---|
| 151 | if(!-f $private_key) |
|---|
| 152 | { |
|---|
| 153 | print "Generating private key...\n"; |
|---|
| 154 | if(system("openssl genrsa -out $private_key 2048") != 0) |
|---|
| 155 | { |
|---|
| 156 | die "Couldn't generate private key." |
|---|
| 157 | } |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | # generate a certificate request |
|---|
| 161 | if(!-f $certificate_request) |
|---|
| 162 | { |
|---|
| 163 | die "Couldn't run openssl for CSR generation" unless |
|---|
| 164 | open(CSR,"|openssl req -new -key $private_key -sha1 -out $certificate_request"); |
|---|
| 165 | print CSR <<__E; |
|---|
| 166 | . |
|---|
| 167 | . |
|---|
| 168 | . |
|---|
| 169 | . |
|---|
| 170 | . |
|---|
| 171 | $server |
|---|
| 172 | . |
|---|
| 173 | . |
|---|
| 174 | . |
|---|
| 175 | |
|---|
| 176 | __E |
|---|
| 177 | close CSR; |
|---|
| 178 | print "\n\n"; |
|---|
| 179 | die "Certificate request wasn't created.\n" unless -f $certificate_request |
|---|
| 180 | } |
|---|
| 181 | |
|---|
| 182 | # write the configuration file |
|---|
| 183 | print "Writing configuration file $config_file\n"; |
|---|
| 184 | open CONFIG,">$config_file" or die "Can't open config file for writing"; |
|---|
| 185 | print CONFIG <<__E; |
|---|
| 186 | |
|---|
| 187 | RaidFileConf = $raidfile_config |
|---|
| 188 | AccountDatabase = $accounts_file |
|---|
| 189 | |
|---|
| 190 | # Uncomment this line to see exactly what commands are being received from clients. |
|---|
| 191 | # ExtendedLogging = yes |
|---|
| 192 | |
|---|
| 193 | # scan all accounts for files which need deleting every 15 minutes. |
|---|
| 194 | |
|---|
| 195 | TimeBetweenHousekeeping = 900 |
|---|
| 196 | |
|---|
| 197 | Server |
|---|
| 198 | { |
|---|
| 199 | PidFile = @localstatedir_expanded@/run/bbstored.pid |
|---|
| 200 | User = $username |
|---|
| 201 | ListenAddresses = inet:$server |
|---|
| 202 | CertificateFile = $certificate |
|---|
| 203 | PrivateKeyFile = $private_key |
|---|
| 204 | TrustedCAsFile = $ca_root_cert |
|---|
| 205 | } |
|---|
| 206 | |
|---|
| 207 | |
|---|
| 208 | __E |
|---|
| 209 | |
|---|
| 210 | close CONFIG; |
|---|
| 211 | |
|---|
| 212 | # explain to the user what they need to do next |
|---|
| 213 | my $daemon_args = ($config_file eq $default_config_location)?'':" $config_file"; |
|---|
| 214 | |
|---|
| 215 | print <<__E; |
|---|
| 216 | |
|---|
| 217 | =================================================================== |
|---|
| 218 | |
|---|
| 219 | bbstored basic configuration complete. |
|---|
| 220 | |
|---|
| 221 | What you need to do now... |
|---|
| 222 | |
|---|
| 223 | 1) Sign $certificate_request |
|---|
| 224 | using the bbstored-certs utility. |
|---|
| 225 | |
|---|
| 226 | 2) Install the server certificate and root CA certificate as |
|---|
| 227 | $certificate |
|---|
| 228 | $ca_root_cert |
|---|
| 229 | |
|---|
| 230 | 3) You may wish to read the configuration file |
|---|
| 231 | $config_file |
|---|
| 232 | and adjust as appropraite. |
|---|
| 233 | |
|---|
| 234 | 4) Create accounts with bbstoreaccounts |
|---|
| 235 | |
|---|
| 236 | 5) Start the backup store daemon with the command |
|---|
| 237 | @sbindir_expanded@/bbstored$daemon_args |
|---|
| 238 | in /etc/rc.local, or your local equivalent. |
|---|
| 239 | |
|---|
| 240 | =================================================================== |
|---|
| 241 | |
|---|
| 242 | __E |
|---|
| 243 | |
|---|
| 244 | |
|---|
| 245 | |
|---|