This file is indexed.

/usr/sbin/oar-database is in oar-server 2.5.2-4.1.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
#!/usr/bin/perl

use strict;
use warnings;
use DBI();
use OAR::IO;
use OAR::Conf qw(init_conf dump_conf get_conf is_conf set_value);
use File::Basename;
use Getopt::Long;
use Sort::Versions;
use Term::UI;
use Term::ReadLine;

Getopt::Long::Configure ("gnu_getopt");

#
# Default values used in this script.
#
my $DEFAULT_CONFFILE = "/etc/oar/oar.conf";
my $DEFAULT_MYSQL_PORT = 3306;
my $DEFAULT_PGSQL_PORT = 5432;
my $DEFAULT_DB_NAME = "oar";
my $DEFAULT_DB_HOST = "localhost";
my $DEFAULT_DB_USER = "oar";
my $DEFAULT_DB_RO_USER = "oar_ro";

my $DEFAULT_MYSQL_STRUCTURE="/usr/lib/oar/database/mysql_structure.sql";
my $DEFAULT_MYSQL_ADMISSION_RULES="/usr/lib/oar/database/mysql_default_admission_rules.sql";
my $DEFAULT_MYSQL_DEFAULT_DATA="/usr/lib/oar/database/default_data.sql";
my $DEFAULT_MYSQL_RESET="/usr/lib/oar/database/mysql_reset_structure.sql";

my $DEFAULT_PGSQL_STRUCTURE="/usr/lib/oar/database/pg_structure.sql";
my $DEFAULT_PGSQL_ADMISSION_RULES="/usr/lib/oar/database/pg_default_admission_rules.sql";
my $DEFAULT_PGSQL_DEFAULT_DATA="/usr/lib/oar/database/default_data.sql";
my $DEFAULT_PGSQL_RESET="/usr/lib/oar/database/pg_reset_structure.sql";

my %DEFAULT_PGSQL_UPGRADES = ();
my $DEFAULT_PGSQL_LAST_VERSION;

my %DEFAULT_MYSQL_UPGRADES = ();
my $DEFAULT_MYSQL_LAST_VERSION;

#
# meta functions/parameters defined during the execution (depending on the db type)
#
my $exec_sql;
my $exec_sql_file;
my %DEFAULT_UPGRADES;
my $DEFAULT_LAST_VERSION;
my $DEFAULT_STRUCTURE;
my $DEFAULT_ADMISSION_RULES;
my $DEFAULT_DATA;
my $DEFAULT_RESET;

#
# Options from the command line
#
my $usage;
my $debug;
my $force_sql;
my $force_yes;
my $db_type;
my $db_host;
my $db_port;
my $db_name;
my $db_user;
my $db_pass;
my $db_ro_user;
my $db_ro_pass;
my $db_admin_user;
my $db_admin_pass;
my $db_is_local;
my $action;
my $conffile;

#
# database handler
#
my $dbh;

#
# Gather the sql file information for upgrading the database
#
open LIST, "find /usr/lib/oar/database -name '*_structure_upgrade_*-*.sql' |" or die "Unable to list the upstructure upgrade sql file";
while (<LIST>) {
    my ($line) = $_;
    chomp($line);
    if ($line =~ /.*\/(mysql|pg)_structure_upgrade_([\d\.]+)-([\d\.]+)\.sql$/) {
        my ($type, $version_from, $version_to) = ($1,$2,$3);
        if ($type eq "mysql") {
            $DEFAULT_MYSQL_UPGRADES{$version_from} = [$version_to,$line];
            # process the last existing version
            if ($DEFAULT_MYSQL_LAST_VERSION) {
                if (versioncmp($version_to,$DEFAULT_MYSQL_LAST_VERSION) > 0) {
                    $DEFAULT_MYSQL_LAST_VERSION = $version_to;
                }

            } else {
                $DEFAULT_MYSQL_LAST_VERSION = $version_to;
            }
        } elsif ($type eq "pg") {
            $DEFAULT_PGSQL_UPGRADES{$version_from} = [$version_to,$line];
            # process the last existing version
            if ($DEFAULT_PGSQL_LAST_VERSION) {
                if (versioncmp($version_to,$DEFAULT_PGSQL_LAST_VERSION) > 0) {
                    $DEFAULT_PGSQL_LAST_VERSION = $version_to;
                }

            } else {
                $DEFAULT_PGSQL_LAST_VERSION = $version_to;
            }
        }
    }
}


#
# Display usage
#
sub print_usage() {
    print <<EOS;
Usage: $0 [-h|--help]
       $0 --init [OPTIONS]
       $0 --reset [OPTIONS]
       $0 --create [OPTIONS]
       $0 --drop [OPTIONS]

Setup the database used by OAR

Actions:
     --create                       create a new db user / database
     --check                        check if the database is ready to be used
     --drop                         drop the current db user / database
     --setup                        Setup the oar database with an exisiting db user / database
     --reset                        Reset the oar database with an exisiting db user / database

Options:
 -h, --help                         Show this help screen
 -d, --debug                        Print debug information
 -f, --force-sql                    Force the execution even if an sql instruction fails
 -y, --force-yes                    This option will cause oar-database to continue without prompting if it is doins something potentially harmful
     --conf=OAR_CONF_FILE           Use the configuration file given in parameters. Default '/etc/oar/oar.conf'
     --db-type=DB_TYPE              Specify the database type ('mysql' or 'Pg')
     --db-host=DB_HOST              Specify the database hostname
     --db-port=DB_PORT              Specify the database port
     --db-user=DB_USER              Specify the database user
     --db-pass=DB_PASS              Specify the database password
     --db-name=DB_NAME              Specify the database name
     --db-ro-user=DB_RO_USER        Specify the read-only database user 
     --db-ro-pass=DB_RO_PASS        Specify the read-only database pass 

     --db-is-local                  Tell that the database is local. oar-database can use local admin account to execute command (usefull for postgres). Only used when using --create
     --db-admin-user=DB_ADMIN_USER  Specify the database admin user needed to create user/database. Only used when using --create
     --db-admin-pass=DB_ADMIN_PASS  Specify the database admin user needed to create user/database. Only used when using --create

For more information, see the manual page.
EOS
}

sub get_current_schema_version() {
    my $query;

    if ($db_type eq "Pg") {
        $query="select version from schema limit 1;";
    } else {
        $query="select version from `schema` limit 1;";
    }
     
    my $sth = $dbh->prepare($query);
    $sth->execute();
    my $ver = $sth->fetchrow_hashref;

    unless ($ver) { return ""; }
    return $ver->{'version'};
}


sub init_database() {
    check_user_db_access($db_user,$db_pass);
    print "initializing the database '$db_name'...\n";
    &$exec_sql_file($DEFAULT_STRUCTURE) == 0       or die "Fail to execute $DEFAULT_STRUCTURE.\n";
    &$exec_sql_file($DEFAULT_DATA) == 0            or die "Fail to execute $DEFAULT_DATA.\n";
    &$exec_sql_file($DEFAULT_ADMISSION_RULES) == 0 or die "Fail to execute $DEFAULT_ADMISSION_RULES.\n";
}

sub reset_database() {
    print "resetting the database '$db_name'...\n";
    &$exec_sql_file($DEFAULT_RESET) == 0  or die "Fail to execute $DEFAULT_RESET\n.";
    init_database();
}

sub upgrade_database() {
    my ($to_version, $to_file);

    my $current_version = get_current_schema_version();
    while($current_version ne $DEFAULT_LAST_VERSION) {
	if ($DEFAULT_UPGRADES{$current_version}) {
		($to_version, $to_file) = @{$DEFAULT_UPGRADES{$current_version}};
                print "uprgading from '$current_version' to '$to_version'...\n";
		&$exec_sql_file($to_file) == 0 or die "Fail to upgrade the database from $current_version to $to_file with $to_file\n.";
	} else {
		die "Unable to upgrade from the version '$current_version'\n";
	}
        $current_version = get_current_schema_version();
    }
}

sub setup_database() {
    check_user_db_access($db_user,$db_pass);
    # Retrieve the current version (check if shema table exist before)
    my $db_version = get_current_schema_version();

    if (!$db_version) {
        print "No schema version found. Initialising the database.\n";
        init_database();
    } elsif ($db_version eq $DEFAULT_LAST_VERSION) {
        print "The database is up to date (version $db_version).\n";
    } else {
        print "The current schema version is $db_version. Upgrading to $DEFAULT_LAST_VERSION.\n"; 
        upgrade_database();
    }
}

sub mysql_ro_user_privileges() {
    # All except the challenge table (localhost)
    mysql_admin_exec_sql("GRANT SELECT ON $db_name.schema TO '$db_ro_user'".'@'."'localhost' IDENTIFIED BY '$db_ro_pass'") ;
    mysql_admin_exec_sql("GRANT SELECT ON $db_name.accounting TO '$db_ro_user'".'@'."'localhost' IDENTIFIED BY '$db_ro_pass'") ;
    mysql_admin_exec_sql("GRANT SELECT ON $db_name.admission_rules TO '$db_ro_user'".'@'."'localhost' IDENTIFIED BY '$db_ro_pass'") ;
    mysql_admin_exec_sql("GRANT SELECT ON $db_name.assigned_resources TO '$db_ro_user'".'@'."'localhost' IDENTIFIED BY '$db_ro_pass'") ;
    mysql_admin_exec_sql("GRANT SELECT ON $db_name.event_log_hostnames TO '$db_ro_user'".'@'."'localhost' IDENTIFIED BY '$db_ro_pass'") ;
    mysql_admin_exec_sql("GRANT SELECT ON $db_name.event_logs TO '$db_ro_user'".'@'."'localhost' IDENTIFIED BY '$db_ro_pass'") ;
    mysql_admin_exec_sql("GRANT SELECT ON $db_name.files TO '$db_ro_user'".'@'."'localhost' IDENTIFIED BY '$db_ro_pass'") ;
    mysql_admin_exec_sql("GRANT SELECT ON $db_name.frag_jobs TO '$db_ro_user'".'@'."'localhost' IDENTIFIED BY '$db_ro_pass'") ;
    mysql_admin_exec_sql("GRANT SELECT ON $db_name.gantt_jobs_predictions TO '$db_ro_user'".'@'."'localhost' IDENTIFIED BY '$db_ro_pass'") ;
    mysql_admin_exec_sql("GRANT SELECT ON $db_name.gantt_jobs_predictions_visu TO '$db_ro_user'".'@'."'localhost' IDENTIFIED BY '$db_ro_pass'") ;
    mysql_admin_exec_sql("GRANT SELECT ON $db_name.gantt_jobs_resources TO '$db_ro_user'".'@'."'localhost' IDENTIFIED BY '$db_ro_pass'") ;
    mysql_admin_exec_sql("GRANT SELECT ON $db_name.gantt_jobs_resources_visu TO '$db_ro_user'".'@'."'localhost' IDENTIFIED BY '$db_ro_pass'") ;
    mysql_admin_exec_sql("GRANT SELECT ON $db_name.job_dependencies TO '$db_ro_user'".'@'."'localhost' IDENTIFIED BY '$db_ro_pass'") ;
    mysql_admin_exec_sql("GRANT SELECT ON $db_name.job_resource_descriptions TO '$db_ro_user'".'@'."'localhost' IDENTIFIED BY '$db_ro_pass'") ;
    mysql_admin_exec_sql("GRANT SELECT ON $db_name.job_resource_groups TO '$db_ro_user'".'@'."'localhost' IDENTIFIED BY '$db_ro_pass'") ;
    mysql_admin_exec_sql("GRANT SELECT ON $db_name.job_state_logs TO '$db_ro_user'".'@'."'localhost' IDENTIFIED BY '$db_ro_pass'") ;
    mysql_admin_exec_sql("GRANT SELECT ON $db_name.job_types TO '$db_ro_user'".'@'."'localhost' IDENTIFIED BY '$db_ro_pass'") ;
    mysql_admin_exec_sql("GRANT SELECT ON $db_name.jobs TO '$db_ro_user'".'@'."'localhost' IDENTIFIED BY '$db_ro_pass'") ;
    mysql_admin_exec_sql("GRANT SELECT ON $db_name.moldable_job_descriptions TO '$db_ro_user'".'@'."'localhost' IDENTIFIED BY '$db_ro_pass'") ;
    mysql_admin_exec_sql("GRANT SELECT ON $db_name.queues TO '$db_ro_user'".'@'."'localhost' IDENTIFIED BY '$db_ro_pass'") ;
    mysql_admin_exec_sql("GRANT SELECT ON $db_name.resource_logs TO '$db_ro_user'".'@'."'localhost' IDENTIFIED BY '$db_ro_pass'") ;
    mysql_admin_exec_sql("GRANT SELECT ON $db_name.resources TO '$db_ro_user'".'@'."'localhost' IDENTIFIED BY '$db_ro_pass'") ;
    # All except the challenge table (other hosts)
    mysql_admin_exec_sql("GRANT SELECT ON $db_name.schema TO '$db_ro_user'".'@'."'%' IDENTIFIED BY '$db_ro_pass'") ;
    mysql_admin_exec_sql("GRANT SELECT ON $db_name.accounting TO '$db_ro_user'".'@'."'%' IDENTIFIED BY '$db_ro_pass'") ;
    mysql_admin_exec_sql("GRANT SELECT ON $db_name.admission_rules TO '$db_ro_user'".'@'."'%' IDENTIFIED BY '$db_ro_pass'") ;
    mysql_admin_exec_sql("GRANT SELECT ON $db_name.assigned_resources TO '$db_ro_user'".'@'."'%' IDENTIFIED BY '$db_ro_pass'") ;
    mysql_admin_exec_sql("GRANT SELECT ON $db_name.event_log_hostnames TO '$db_ro_user'".'@'."'%' IDENTIFIED BY '$db_ro_pass'") ;
    mysql_admin_exec_sql("GRANT SELECT ON $db_name.event_logs TO '$db_ro_user'".'@'."'%' IDENTIFIED BY '$db_ro_pass'") ;
    mysql_admin_exec_sql("GRANT SELECT ON $db_name.files TO '$db_ro_user'".'@'."'%' IDENTIFIED BY '$db_ro_pass'") ;
    mysql_admin_exec_sql("GRANT SELECT ON $db_name.frag_jobs TO '$db_ro_user'".'@'."'%' IDENTIFIED BY '$db_ro_pass'") ;
    mysql_admin_exec_sql("GRANT SELECT ON $db_name.gantt_jobs_predictions TO '$db_ro_user'".'@'."'%' IDENTIFIED BY '$db_ro_pass'") ;
    mysql_admin_exec_sql("GRANT SELECT ON $db_name.gantt_jobs_predictions_visu TO '$db_ro_user'".'@'."'%' IDENTIFIED BY '$db_ro_pass'") ;
    mysql_admin_exec_sql("GRANT SELECT ON $db_name.gantt_jobs_resources TO '$db_ro_user'".'@'."'%' IDENTIFIED BY '$db_ro_pass'") ;
    mysql_admin_exec_sql("GRANT SELECT ON $db_name.gantt_jobs_resources_visu TO '$db_ro_user'".'@'."'%' IDENTIFIED BY '$db_ro_pass'") ;
    mysql_admin_exec_sql("GRANT SELECT ON $db_name.job_dependencies TO '$db_ro_user'".'@'."'%' IDENTIFIED BY '$db_ro_pass'") ;
    mysql_admin_exec_sql("GRANT SELECT ON $db_name.job_resource_descriptions TO '$db_ro_user'".'@'."'%' IDENTIFIED BY '$db_ro_pass'") ;
    mysql_admin_exec_sql("GRANT SELECT ON $db_name.job_resource_groups TO '$db_ro_user'".'@'."'%' IDENTIFIED BY '$db_ro_pass'") ;
    mysql_admin_exec_sql("GRANT SELECT ON $db_name.job_state_logs TO '$db_ro_user'".'@'."'%' IDENTIFIED BY '$db_ro_pass'") ;
    mysql_admin_exec_sql("GRANT SELECT ON $db_name.job_types TO '$db_ro_user'".'@'."'%' IDENTIFIED BY '$db_ro_pass'") ;
    mysql_admin_exec_sql("GRANT SELECT ON $db_name.jobs TO '$db_ro_user'".'@'."'%' IDENTIFIED BY '$db_ro_pass'") ;
    mysql_admin_exec_sql("GRANT SELECT ON $db_name.moldable_job_descriptions TO '$db_ro_user'".'@'."'%' IDENTIFIED BY '$db_ro_pass'") ;
    mysql_admin_exec_sql("GRANT SELECT ON $db_name.queues TO '$db_ro_user'".'@'."'%' IDENTIFIED BY '$db_ro_pass'") ;
    mysql_admin_exec_sql("GRANT SELECT ON $db_name.resource_logs TO '$db_ro_user'".'@'."'%' IDENTIFIED BY '$db_ro_pass'") ;
    mysql_admin_exec_sql("GRANT SELECT ON $db_name.resources TO '$db_ro_user'".'@'."'%' IDENTIFIED BY '$db_ro_pass'") ;
}

sub fix_ro_user_privileges() {
    # This revokes the wildcard select privilege that was made by previous
    # versions of this script and set up the correct privileges for the ro user
    # (the bug affected only mysql databases)
    if ($db_type eq "mysql") {
      print "Fixing the ro user privileges...\n";
      mysql_admin_exec_sql("REVOKE SELECT ON $db_name.* FROM '$db_ro_user'".'@'."'localhost'") ;
      mysql_admin_exec_sql("REVOKE SELECT ON $db_name.* FROM '$db_ro_user'".'@'."'%'") ;
      mysql_ro_user_privileges();
    }
}

sub create_database() {

    if ($db_type eq "mysql") {
        print "Creating the database...\n";
        mysql_admin_exec_sql("CREATE DATABASE $db_name CHARACTER SET latin1") ;

        print "Creating the database user...\n";
        mysql_admin_exec_sql("GRANT ALL PRIVILEGES ON $db_name.* TO '$db_user'" . '@' . "'localhost' IDENTIFIED BY '$db_pass'") ;
        mysql_admin_exec_sql("GRANT ALL PRIVILEGES ON $db_name.* TO '$db_user'" . '@' ."'%' IDENTIFIED BY '$db_pass'");
    } elsif ($db_type eq "Pg") {
        print "Creating the database user...\n";
        pgsql_admin_exec_sql("CREATE ROLE $db_user LOGIN PASSWORD '$db_pass';");
        if ($db_ro_user and $db_ro_pass) {
            print "Creating the database read-only user...\n";
            pgsql_admin_exec_sql("CREATE ROLE $db_ro_user LOGIN PASSWORD '$db_ro_pass';");
        }
        print "Creating the database...\n";
        pgsql_admin_exec_sql("CREATE DATABASE $db_name OWNER $db_user");
        pgsql_admin_exec_sql("GRANT ALL PRIVILEGES ON DATABASE $db_name TO $db_user");
    }


    init_database();

    if ($db_ro_user and $db_ro_pass) {
        if ($db_type eq "Pg")  {
            pgsql_admin_exec_sql("GRANT SELECT ON schema,accounting,admission_rules,assigned_resources,event_log_hostnames,event_logs,files,frag_jobs,gantt_jobs_predictions,gantt_jobs_predictions_visu,gantt_jobs_resources,gantt_jobs_resources_visu,job_dependencies,job_resource_descriptions,job_resource_groups,job_state_logs,job_types,jobs,moldable_job_descriptions,queues,resource_logs,resources,admission_rules_id_seq,event_logs_event_id_seq,files_file_id_seq,job_resource_groups_res_group_id_seq,job_state_logs_job_state_log_id_seq,job_types_job_type_id_seq,moldable_job_descriptions_moldable_id_seq,resource_logs_resource_log_id_seq,resources_resource_id_seq,jobs_job_id_seq TO $db_ro_user", $db_name);
        } elsif ($db_type eq "mysql") {
            print "Creating the database read-only user...\n";
            mysql_ro_user_privileges();
        }
    }
}

sub drop_database() {
    print "Removing the database...\n";
    if ($db_type eq "mysql") {
        mysql_admin_exec_sql("DROP DATABASE $db_name");
        mysql_admin_exec_sql("DROP USER $db_user". '@' . "'%'");
        mysql_admin_exec_sql("DROP USER $db_user" . '@localhost');
        if ($db_ro_user) {
            mysql_admin_exec_sql("DROP USER $db_ro_user" .'@'. "'%'");
            mysql_admin_exec_sql("DROP USER $db_ro_user" . '@localhost');
        }
    } elsif ($db_type eq "Pg") {
        pgsql_admin_exec_sql("DROP DATABASE $db_name");
            pgsql_admin_exec_sql("DROP USER $db_user");
        if ($db_ro_user) {
            pgsql_admin_exec_sql("DROP USER $db_ro_user");
        }
    }
}

sub mysql_exec_sql_file {
    my ($file) = @_;
    if ($debug) { 
        print "executing '$file'\n";
    }
    if ($force_sql) {
        return system("mysql --force --user='$db_user' --password='$db_pass' --host='$db_host' --port='$db_port' $db_name < $file");
    } else {
        return system("mysql --user='$db_user' --password='$db_pass' --host='$db_host' --port='$db_port' $db_name < $file");
    }
}

sub pgsql_exec_sql_file {
    my ($file) = @_;
    if ($debug) { 
        print "executing '$file'\n";
    }
    return system("PGUSER='$db_user' PGPASSWORD='$db_pass' PGHOST='$db_host' PGPORT='$db_port' psql -q $db_name < $file");
    
}

sub mysql_exec_sql {
    my ($query) = @_;
    if ($debug) {
        print "executing '$query'\n";
    }
    if ($force_sql) {
        return system("mysql --force --user='$db_user' --password='$db_pass' --host='$db_host' --port='$db_port' $db_name -e \"$query\"");
    } else {
        return system("mysql --user='$db_user' --password='$db_pass' --host='$db_host' --port='$db_port' $db_name -e \"$query\"");
    }
}

sub mysql_admin_exec_sql {
    my ($query) = @_;
    if ($debug) {
        print "executing '$query'\n";
    }
    return system("mysql --user='$db_admin_user' --password='$db_admin_pass' --host='$db_host' --port='$db_port' -e \"$query\"");
}
sub pgsql_exec_sql {
    my ($query) = @_;
    if ($debug) {
        print "executing '$query'\n";
    }
    return system("PGUSER='$db_user' PGPASSWORD='$db_pass' PGHOST='$db_host' PGPORT='$db_port' psql -q $db_name -c \"$query\"");

}

sub pgsql_admin_exec_sql {
    my ($query,$db) = @_;
    unless ($db) {
        $db="";
    }
    if ($debug) {
        print "executing '$query'\n";
    }
    if ($db_is_local) {
        return system("echo \"$query\" | su - postgres -c \"psql $db\"");
    } else { 
        return system("PGUSER='$db_admin_user' PGPASSWORD='$db_admin_pass' PGHOST='$db_host' PGPORT='$db_port' psql $db -c \"$query\"");
    }
}


GetOptions ("help|h"                => \$usage,
            "debug|d"               => \$debug,
            "force-sql|f"           => \$force_sql,
            "force-yes|y"           => \$force_yes,
            "setup|s"               => sub { $action = 'setup' },
            "reset|r"               => sub { $action = 'reset' },
            "create|c"              => sub { $action = 'create' },
            "drop"                  => sub { $action = 'drop' },
            "check"                 => sub { $action = 'check' },
            "fix-ro-user-priv"      => sub { $action = 'fix-ro-user-priv' },
            "conf=s"                => \$conffile,
#            "update-conf"           => sub { $action = 'update-conf' },
            "db-type=s"             => \$db_type,
            "db-host=s"             => \$db_host,
            "db-port=i"             => \$db_port,
            "db-user=s"             => \$db_user,
            "db-pass=s"             => \$db_pass,
            "db-ro-user=s"          => \$db_ro_user,
            "db-ro-pass=s"          => \$db_ro_pass,
            "db-name=s"             => \$db_name,
            "db-admin-user=s"       => \$db_admin_user,
            "db-admin-pass=s"       => \$db_admin_pass,
            "db-is-local"           => \$db_is_local
);


#
# Usage
#
if ($usage){ print_usage(); exit(0); }


#
# Check the action
#
if ( !$action ) {
    print "You must specify the action do be done.\n";
    print_usage();
    exit(1);
}


#
# Check if the given conffile.
#
if ( ! $conffile ) { 
    $conffile=$DEFAULT_CONFFILE; 

}
unless ( -r $conffile ) {
    print "The configuration file isn't readable ! Failing.\n";
    print_usage();
    exit(1);
}


#
# Set the parameters to use
#
OAR::Conf::init_conf($conffile);

if (! $db_type) {
    if (OAR::Conf::is_conf("DB_TYPE")) {
        $db_type = OAR::Conf::get_conf("DB_TYPE");

        if (OAR::Conf::is_conf("DB_PORT")) {
            $db_port = OAR::Conf::get_conf("DB_PORT");
        }
    } else {
        print "Your must set the database type ! Failing \n";
        print_usage();
        exit(1);
    }
} elsif ( $db_type ne "Pg" && $db_type ne "mysql") {
    print "The database type must be 'Pg' or 'mysql'. Failing.\n";
    print_usage();
    exit(1);
}

if (! $db_port ) {
    if ($db_type eq "mysql") { $db_port = $DEFAULT_MYSQL_PORT; }
    if ($db_type eq "Pg") { $db_port = $DEFAULT_PGSQL_PORT; }
}

unless ($db_host) {
    $db_host = (OAR::Conf::is_conf("DB_HOSTNAME") ? OAR::Conf::get_conf("DB_HOSTNAME") : $DEFAULT_DB_HOST);
}

unless ($db_user) {
    $db_user = (OAR::Conf::is_conf("DB_BASE_LOGIN") ? OAR::Conf::get_conf("DB_BASE_LOGIN") : $DEFAULT_DB_USER);
}
unless ($db_pass) {
    $db_pass = (OAR::Conf::is_conf("DB_BASE_PASSWD") ? OAR::Conf::get_conf("DB_BASE_PASSWD") : "");

    if (($db_pass eq "") or ($db_pass eq '""')) {
        print "The user password is empty. Please define a password. Failing.\n";
        exit(1);
    }
}

unless ($db_ro_user) {
    if (OAR::Conf::is_conf("DB_BASE_LOGIN_RO")) {
        $db_ro_user = OAR::Conf::get_conf("DB_BASE_LOGIN_RO");
    }
}

unless ($db_ro_pass) {
    if (OAR::Conf::is_conf("DB_BASE_PASSWD_RO")) {
        $db_ro_pass = OAR::Conf::get_conf("DB_BASE_PASSWD_RO");
    }
    if (($db_ro_pass eq "") or ($db_ro_pass eq '""')) {
        print "The user read-only password is empty. Please define a password. Failing.\n";
        exit(1);
    }
}

unless ($db_name) {
    $db_name = (OAR::Conf::is_conf("DB_BASE_NAME") ? OAR::Conf::get_conf("DB_BASE_NAME") : $DEFAULT_DB_NAME);
}

if ($action eq "create" or $action eq "drop" or $action eq "fix-ro-user-priv") {
    unless ($db_is_local and $db_type eq "Pg") {
        unless ($db_admin_user) {
            print "You need to specify the admin user (--db-admin-user) and probably the password (--db-admin-pass) to use. See the manual for more information. Failing.\n";
            exit(1);
        }
        unless ($db_admin_pass) {
            print "The admin password is empty. Proceeding with an empty password.\n";
            $db_admin_pass="";
        }
    }
}



#
# Set the meta function depending on the db type
#
if ($db_type eq "mysql") {
    $exec_sql                = \&mysql_exec_sql;
    $exec_sql_file           = \&mysql_exec_sql_file;
    $DEFAULT_LAST_VERSION    = $DEFAULT_MYSQL_LAST_VERSION;
    %DEFAULT_UPGRADES        = %DEFAULT_MYSQL_UPGRADES;
    $DEFAULT_RESET           = $DEFAULT_MYSQL_RESET;
    $DEFAULT_STRUCTURE       = $DEFAULT_MYSQL_STRUCTURE;
    $DEFAULT_ADMISSION_RULES = $DEFAULT_MYSQL_ADMISSION_RULES;
    $DEFAULT_DATA            = $DEFAULT_MYSQL_DEFAULT_DATA;
} elsif ($db_type eq "Pg") {
    $exec_sql                = \&pgsql_exec_sql;
    $exec_sql_file           = \&pgsql_exec_sql_file;
    $DEFAULT_LAST_VERSION    = $DEFAULT_PGSQL_LAST_VERSION;
    %DEFAULT_UPGRADES        = %DEFAULT_PGSQL_UPGRADES;
    $DEFAULT_RESET           = $DEFAULT_PGSQL_RESET;
    $DEFAULT_STRUCTURE       = $DEFAULT_PGSQL_STRUCTURE;
    $DEFAULT_ADMISSION_RULES = $DEFAULT_PGSQL_ADMISSION_RULES;
    $DEFAULT_DATA            = $DEFAULT_PGSQL_DEFAULT_DATA;
} else {
    die "You shouldn't execute this part of the code. Contact the developper\n";
}


#
# try a connection to the database
#
sub check_user_db_access {
    my ($user,$pass) = @_;

    my $dbc = "DBI:$db_type:database=$db_name;host=$db_host;port=$db_port";
    $dbh = DBI->connect($dbc, $user,$pass, {'InactiveDestroy' => 1, 'PrintError' => 1});
    if (!defined($dbh)) {
        print "The connection to the database has failed (user=$user,host=$db_host,port=$db_port,database=$db_name). Please check if the database server is up and ready.\n";
        print "Please see the file /usr/share/doc/oar-server/README.database for the instructions.\n";
        exit(1);
    }
}

#
# update oar.conf if asked
#
if ($action eq "update-conf") {
    print "Updating $conffile with the given parameters.\n";
    OAR::Conf::set_value("DB_HOSTNAME",$db_host);
    OAR::Conf::set_value("DB_PORT",$db_port);
    OAR::Conf::set_value("DB_BASE_NAME",$db_name);
    OAR::Conf::set_value("DB_BASE_LOGIN",$db_user);
    OAR::Conf::set_value("DB_BASE_PASSWD",$db_pass);
    OAR::Conf::set_value("DB_BASE_LOGIN_RO",$db_ro_user);
    OAR::Conf::set_value("DB_BASE_PASSWD_RO",$db_ro_pass);
    OAR::Conf::set_value("DB_TYPE",$db_type);
}


#
# setup the database
#
my $term = Term::ReadLine->new("brand");
$Term::UI::VERBOSE = 0;

if ($action eq "check") {
    $ENV{'OARCONFFILE'} = "/etc/oar/oar.conf";
    if (defined(OAR::IO::connect_ro_one_log("log"))) {
        print "The OAR database is accessible and ready to be used.\n";
        exit 0;
    }
    elsei {
        print "The OAR database isn't ready.\n";
        exit 1;
    }
} elsif ($action eq "setup") {
    check_user_db_access($db_user,$db_pass);
    if($force_yes || $term->ask_yn(
            prompt => "Are you sure you want to upgrade your database ? (Please backup the database before)", 
            default => 'n')) {
        setup_database();
    } else {
        print "The database hasn't been changed\n."
    }
} elsif ($action eq "reset") {
    check_user_db_access($db_user,$db_pass);
    if ($force_yes || $term->ask_yn(
            prompt => "Are you sure you want to reset your database ? (The database content will be lost)",
            default => 'n')) {
        reset_database();
    } else {
        print "The database hasn't been changed\n."
    }
} elsif ($action eq "create") {
    create_database();
} elsif ($action eq "drop") {
    drop_database();
} elsif ($action eq "fix-ro-user-priv") {
    fix_ro_user_privileges();
}