sympl 28.6 KB
Newer Older
1
2
3
4
#!/bin/bash
#
# Sympl command line interface.
#
Paul Cammish's avatar
Paul Cammish committed
5
# Copyright 2019, Paul Cammish <sympl@kelduum.net>, part of the Sympl Project.
6
7
#
# Licenced under GPL3+
Paul Cammish's avatar
Paul Cammish committed
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
#
set -e

I_AM="$(whoami)"
VERBOSE=TRUE
FUNCTION=base
ACTION=none
TARGET=all

##############################################################################
#                              Output Functions                              #
##############################################################################

_debug() {
  if [ $DEBUG ]; then
    echo -e "\033[2mDEBUG: $@\033[0m"
  fi
}

_warn() {
  echo -e "\033[1m\033[33m WARN: \033[0m $@"
}

_error() {
  echo -e "\033[1m\033[31mERROR: \033[0m $@"
  exit 1
}

_verbose() {
  if [ $DEBUG ]; then
    echo " INFO: $@"
  elif [ $VERBOSE ]; then
    echo "$@"
  fi
}

##############################################################################
#                                    Help                                    #
##############################################################################

_help() {
  echo 'This is a big set of parser stuff.'
  exit 0
}

##############################################################################
#                              Support Functions                            #
##############################################################################

_func() {
  if [ $FUNCTION == base ]; then
    FUNCTION=$1
  else
    _error "Function '$FUNCTION' already selected, can't set to $1"
  fi
}

_action() {
  if [ $ACTION == none ]; then
    ACTION=$1
    if [ "$ACTION" == "set" ] && [ "x$3" != "x" ]; then
      OPTION=$2
      SETTING=$3
    fi
  else
    _error "Action '$ACTION' already selected, can't set to '$1'"
  fi
}

_is_domain() {

  if [ "$1" == "all" ]; then _error "Please specify a domain."; fi

  _debug "Checking if '$1' is a valid domain..."
  if [[ $1 =~ ^[0-9a-z\.-]+$ ]] && [[ $1 == *"."* ]] && [[ $1 != "."* ]] && [[ $1 != *"." ]]; then
    _debug "  $1 is valid"
  else
    _error "'$1' is not a valid domain"
  fi
}

_is_email() {
90
91
  
  if [ "$1" == "all" ]; then _error "Please specify an email address."; fi
Paul Cammish's avatar
Paul Cammish committed
92

93
94
95
  _debug "Checking if '$1' is a valid email address..."
  readarray -d @ -t part < <(printf '%s' "$1")
  _is_domain ${part[1]}
Paul Cammish's avatar
Paul Cammish committed
96

97
98
99
100
101
102
103
  _debug "Checking if '${part[0]}' is valid"
  if [[ ${part[0]} =~ ^[0-9a-zA-Z._%+-]+$ ]]; then 
    _debug "'${part[0]}' is valid"
  else
    _error "'${part[0]}' is not valid a valid mailbox name."
  fi
  
Paul Cammish's avatar
Paul Cammish committed
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
}

_is_database() {

  if [ "$1" == "all" ]; then _error "Please specify a database."; fi

  _debug "Checking if '$1' is a valid database... "

  if [[ $1 =~ ^[0-9A-Za-z_]+$ ]] ; then #&& [[ $(expr length $1) -le 32 ]]; then
    _debug "  $1 is valid"
  else
    _error "'$1' is not a valid database name"
  fi
}

##############################################################################
#                                      Base                                  #
##############################################################################

Paul Cammish's avatar
Paul Cammish committed
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
_base_none() {
  echo "Sympl command-line - a basic parser for common system administraton tasks.

Usage: sympl [function] [action] [target]

Where function is one of:
  web, mail, mysql, dns, ftp, cron, backup or blank for the system itself.
And action is one of:
  create, enable, disable, audit, update, destroy, set <item|option> <value>
And target is the domain/email address/database/etc.

Useful things:
 - Other than using set, the function, action and target can be in any order.
 - 'set' requires two arguments immediately after it, the option and value.
 - If more permissions are needed, you'll be prompted for your password.
 - 'destory' will only destroy something which has already been disabled.

Examples:

  sympl web create example.com

  sympl mysql disable example_db

  sympl enable mail username@example.com

  sympl audit backup

  sympl destroy cron example.com

  sympl example.com set only-ssl on

  sympl update

Note: Not all functions are implimented yet.
      Please report bugs via http://bugs.sympl.host.
"

}

Paul Cammish's avatar
Paul Cammish committed
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
_base_create() {
  _noop
}

_base_enable() {
  _noop
}

_base_disable() {
  _noop
}

_base_destroy() {
  _noop
}

_base_set() {
  _noop
}

_base_audit() {
  _noop
}

_base_update() {
Paul Cammish's avatar
Paul Cammish committed
187
188
189
190
  _debug "Updating Apt"
  sudo apt-get -qq update
  _debug "Upgrading installed packages"
  sudo apt-get -q -y install --only-upgrade 'sympl-*'
Paul Cammish's avatar
Paul Cammish committed
191
192
193
194
195
196
}

##############################################################################
#                                       Web                                  #
##############################################################################

Paul Cammish's avatar
Paul Cammish committed
197
198
199
200
_web_none() {
  _noop
}

Paul Cammish's avatar
Paul Cammish committed
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
_web_create() {

  _is_domain $1

  _debug "Check for disabled sites"
  if [ -d /srv/__disabled__$1/public/htdocs ]; then
    _error "The domain $1 is disabled. Use 'sympl enable $1' to enable it."
  elif [ -d /srv/$1/public/__disabled__htdocs ] ; then
    _error "The website $1 is disabled. Use 'sympl web enable $1' to enable it."
  fi

  _debug "Check to see if $1 already exists"
  if [ -d /srv/$1/public/htdocs ]; then
    _error "$1 Already exists."
  fi

  _verbose "Creating Website for $1 at /srv/$1/public/htdocs..."
  mkdir -p "/srv/$1/public/htdocs"
Paul Cammish's avatar
Paul Cammish committed
219
220
  chmod -R 2775 "/srv/$1/public" > /dev/null 2>&1
  chown -R www-data:www-data "/srv/$1/public" > /dev/null 2>&1
Paul Cammish's avatar
Paul Cammish committed
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

}

_web_enable() {

  _is_domain $1

  _debug "Checking $1 is disabled and exists"
  if [ -d /srv/$1/public/htdocs ] || [ ! -d /srv/$1/public/__disabled__htdocs ]; then
    _error "The website $1 is already enabled."
  elif [ ! -d /srv/$1 ]; then
    _error "The domain $1 does not exist."
  fi

  _verbose "Enabling Website $1"
  mv /srv/$1/public/__disabled__htdocs /srv/$1/public/htdocs

}

_web_disable() {

  _is_domain $1

  _debug "Checking $1 is enabled and exists"
  if [ -d /srv/$1/public/__disabled__htdocs ]; then
    _error "The website $1 is already disabled."
  elif [ ! -d /srv/$1/public/htdocs ]; then
    _error "The website $1 does not exist."i
  fi

  _verbose "Disabling Website $1"
  mv /srv/$1/public/htdocs /srv/$1/public/__disabled__htdocs

}

_web_destroy() {

  _is_domain $1

  _debug "Confirm '$1' is disabled and exists"
  if [ -d /srv/$1/public/htdocs ]; then
    _error "The website '$1' is enabled. It must be disabled before it can be destroyed."
  elif [ ! -d /srv/$1/public/__disabled__htdocs ]; then
    _error "The website '$1' does not exist."
  elif [ ! -d /srv/$1 ]; then
    _error "The domain '$1' does not exist."
  fi

  _verbose "Destroying /srv/$1/public/__disabled__htdocs..."
  rm -r /srv/$1/public/__disabled__htdocs/
}

_web_audit() {

  # TODO: List details of specific site, otherwise list all sites and any aliasses.

  if [ "$1" != "all" ]; then _is_domain $1 ; fi

279
  ( umask 077; touch /dev/shm/sympl_web_audit )
Paul Cammish's avatar
Paul Cammish committed
280
281
282
283
284
285
286
287
  find /srv/*/public/ -type d -name '*htdocs' > /dev/shm/sympl_web_audit

  _verbose "Enabled sites:"
  grep -v __disabled__ /dev/shm/sympl_web_audit | cut -d '/' -f 3 | sort

  _verbose "Disabled sites:"
  grep __disabled__ /dev/shm/sympl_web_audit | cut -d '/' -f 3 | sort

288
  rm /dev/shm/sympl_web_audit
Paul Cammish's avatar
Paul Cammish committed
289
290
291
292
}

_web_set() {
  _noop
Paul Cammish's avatar
Paul Cammish committed
293
}
Paul Cammish's avatar
Paul Cammish committed
294

Paul Cammish's avatar
Paul Cammish committed
295
296
_web_update() {
  _noop
Paul Cammish's avatar
Paul Cammish committed
297
298
299
300
301
302
}

##############################################################################
#                                Mysql/MariaDB                               #
##############################################################################

Paul Cammish's avatar
Paul Cammish committed
303
304
305
_mysql_none() {
  _noop
}
Paul Cammish's avatar
Paul Cammish committed
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

_mysql_create() {

  _is_database $1

  _debug "Check if $1 already exists"
  if [ $( mysql -NBe 'show databases;' | grep -c "^$1$" ) -eq 0 ]; then
    if [ $( mysql -NBe "use mysql; select User from user;" | grep -c "^$1$" ) -eq 0 ]; then
      _verbose "Creating database '$1'..."
      password="$( openssl rand -base64 32 | cut -c 1-32 )"
      _debug "Password '$password', creating DB -- CREATE DATABASE $1;"
      mysql -e "CREATE DATABASE $1;"
      _debug 'Creating user -- GRANT ALL PRIVILEGES ON $1.* TO '$1'@'localhost' IDENTIFIED BY '$password';'
      mysql -e "GRANT ALL PRIVILEGES ON $1.* TO '$1'@'localhost' IDENTIFIED BY '$password';"
      _debug 'Outputting password'
      echo "$password" >> /home/sympl/mysql_$1_password
      _debug 'Securing file'
      chmod 600 /home/sympl/mysql_$1_password
      chown sympl:sympl /home/sympl/mysql_$1_password
      _verbose "Database '$1' created with user '$1' and password '$password'"
      _verbose "  saved to /home/sympl/mysql_$1_password"
    else
      _error "User '$1' already exists."
    fi
  else
    _error "Database '$1' already exists."
  fi
}

_mysql_enable() {

  _is_database $1

  _debug "Check if $1 exists and is disabled"
  if [ $( mysql -NBe 'show databases;' | grep -c "^$1$" ) -eq 1 ]; then
    if [ $( mysql -NBe "use mysql; select User from user;" | grep -c "^$1$" ) -eq 1 ]; then
      if [ $( mysql -NBe "SHOW GRANTS for '$1'@'localhost'" 2> /dev/null | grep -c "^GRANT ALL PRIVILEGES ON \`$1\`.* TO '$1'@'localhost'$" ) -eq 0 ]; then
        _verbose "Enabling user '$1' for database '$1'"
        mysql -e "GRANT ALL PRIVILEGES ON $1.* TO '$1'@'localhost'"
      else
        _error "User '$1' is already enabled for database '$1'."
      fi
    else
      _error "User '$1'@'localhost' does not exist, although the database does."
    fi
  else
    _error "Database '$1' does not exist."
  fi

}

_mysql_disable() {

  _is_database $1

  _debug "Check if $1 exists and is enabled"
  if [ $( mysql -NBe 'show databases;' | grep -c "^$1$" ) -eq 1 ]; then
    if [ $( mysql -NBe "use mysql; select User from user;" | grep -c "^$1$" ) -eq 1 ]; then
      if [ $( mysql -NBe "SHOW GRANTS for '$1'@'localhost'" 2> /dev/null | grep -c "^GRANT ALL PRIVILEGES ON \`$1\`.* TO '$1'@'localhost'$" ) -eq 1 ]; then
        _verbose "Disabling user '$1' for database '$1'"
        mysql -e "REVOKE ALL PRIVILEGES, GRANT OPTION FROM '$1'@'localhost';"
      else
        _error "Database '$1' is already disabled for database '$1'."
      fi
    else
      _error "User '$1'@'localhost' does not exist, although the database does."
    fi
  else
    _error "Database '$1' does not exist."
  fi

}

_mysql_destroy() {

  _is_database $1

  _debug "Check if $1 exists and is disabled"
  if [ $( mysql -NBe 'show databases;' | grep -c "^$1$" ) -eq 1 ]; then
    if [ $( mysql -NBe "use mysql; select User from user;" | grep -c "^$1$" ) -eq 1 ]; then
      if [ $( mysql -NBe "SHOW GRANTS for '$1'@'localhost'" 2> /dev/null | grep -c "^GRANT ALL PRIVILEGES ON \`$1\`.* TO '$1'@'localhost'$" ) -eq 0 ]; then
        _verbose "Destroying database and user '$1'"
        mysql -e "REVOKE ALL PRIVILEGES, GRANT OPTION FROM '$1'@'localhost';"
        mysql -e "DROP USER '$1'@'localhost';"
        mysql -e "DROP DATABASE $1;"
      else
        _error "Database '$1' is not disabled. Please disable it with 'sympl mysql disable $1'"
      fi
    else
      _error "User '$1' does not exist, but the database does."
    fi
  else
    _error "Database '$1' does not exist."
  fi
}

_mysql_audit() {
  mysql -e 'show databases;'
  mysql -e 'use mysql; select User from user;'
}

Paul Cammish's avatar
Paul Cammish committed
407
408
409
410
_mysql_update() {
  _noop
}

Paul Cammish's avatar
Paul Cammish committed
411
##############################################################################
Paul Cammish's avatar
Paul Cammish committed
412
#                                     DNS                                    #
Paul Cammish's avatar
Paul Cammish committed
413
414
##############################################################################

Paul Cammish's avatar
Paul Cammish committed
415
_dns_none() {
Paul Cammish's avatar
Paul Cammish committed
416
417
418
  _noop
}

Paul Cammish's avatar
Paul Cammish committed
419
_dns_create() {
Paul Cammish's avatar
Paul Cammish committed
420
421
422
  _noop
}

Paul Cammish's avatar
Paul Cammish committed
423
_dns_enable() {
Paul Cammish's avatar
Paul Cammish committed
424
425
426
  _noop
}

Paul Cammish's avatar
Paul Cammish committed
427
_dns_disable() {
Paul Cammish's avatar
Paul Cammish committed
428
429
430
  _noop
}

Paul Cammish's avatar
Paul Cammish committed
431
_dns_destroy() {
Paul Cammish's avatar
Paul Cammish committed
432
433
434
  _noop
}

Paul Cammish's avatar
Paul Cammish committed
435
436
437
438
439
440
441
442
443
_dns_set() {
  _noop
}

_dns_audit() {
  _noop
}

_dns_update() {
Paul Cammish's avatar
Paul Cammish committed
444
445
446
447
448
449
450
  _noop
}

##############################################################################
#                                     Mail                                   #
##############################################################################

Paul Cammish's avatar
Paul Cammish committed
451
452
453
454
_mail_none() {
  _noop
}

Paul Cammish's avatar
Paul Cammish committed
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
_mail_create() {
  _noop
}

_mail_enable() {
  _noop
}

_mail_disable() {
  _noop
}

_mail_destroy() {
  _noop
}

_mail_set() {
  _noop
}

_mail_audit() {
  _noop
}

##############################################################################
#                                     FTP                                    #
##############################################################################

Paul Cammish's avatar
Paul Cammish committed
483
_ftp_none() {
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
  echo "Sympl FTP command line interface

Usage: sympl ftp [action] [target|user@target]
 
Where action is one of: 
  create, enable, disable, reset, destroy, set, audit
And target is the domain or user to work on.

Where create is the action:
  the domain folder will be created if it is not already present
Where enable is the action:
  the password will be enabled if disabled and set if not present
Where disable is the action: 
  FTP Access will be disabled for per-domain or user access
Where reset is the action:
  The password will be reset either per-domain or per-user, even if the 
  domain or user are disabled
Where destroy is the action:
  FTP Access will be removed for both per-domain and per-user access assuming 
  accounts have been disabled
Where set is the action: 
  you can restrict per-user access for the user to a specific folder (by 
  default access is granted to the root of the public folder for that domain)
  or by quota. the '-' character can be used to clear the set value for both 
  the folder and the quota so the folder reverts back to the domains public
  folder and the quota reverts back to unlimited.
   
Examples:

  sympl ftp create example.com

  sympl ftp enable example.com

  sympl ftp disable example.com

  sympl ftp destroy user2@example.com

  sympl ftp audit all|example.com

  sympl ftp enable user1@example.com

  sympl ftp reset user2@example.com

  sympl ftp set folder <folder name> user2@example.com

  sympl ftp set quota <10K|M|G> user2@example.com
"
Paul Cammish's avatar
Paul Cammish committed
531
532
}

Paul Cammish's avatar
Paul Cammish committed
533
_ftp_create() {
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

  if [[ "$1" =~ .*"@".* ]]; then
    _is_email $1
    readarray -d @ -t part < <(printf '%s' "$1")
    mailbox=${part[0]}
    domain=${part[1]}
    else
    _is_domain $1
    domain=$1
  fi

  _debug "Check for disabled sites"
  if [ -d /srv/__disabled__$domain/public ]; then
    _error "The domain '$domain' is disabled. Use 'sympl enable $domain' to enable it."
  fi

  _debug "Check to see if '$domain' already exists"
  if [ -d /srv/$domain/public ]; then
    _error "$domain Already exists. No need to continue"
  fi

  _verbose "Creating FTP folder for '$domain' at /srv/$domain/public/"
  mkdir -p "/srv/$domain/public"
  chmod -R 2775 "/srv/$domain/public" > /dev/null 2>&1
  chown -R sympl: "/srv/$domain/public" > /dev/null
Paul Cammish's avatar
Paul Cammish committed
559
560
561
}

_ftp_enable() {
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
  
  if [[ "$1" =~ .*"@".* ]]; then
    _is_email $1
    readarray -d @ -t part < <(printf '%s' "$1")
    mailbox=${part[0]}
    domain=${part[1]}
    else
    _is_domain $1
    domain=$1
  fi
  

  _debug "Check if domain is set up"
  if [ ! -d /srv/$domain ]; then
    _error "The domain $domain does not exist."
  fi

  _debug "Check if the domains config folder exists"
  if [ ! -d /srv/$domain/config ]; then
    _verbose "Creating the config folder for domain '$domain'."
    mkdir -p /srv/$domain/config
  fi

  if [ "x$mailbox" != "x"  ]; then

    _debug "Check for the ftp-users file and create a blank one if it is absent."
    if [ ! -f /srv/$domain/config/ftp-users ]; then
      _verbose "Setting up ftp-users file"
      echo "#username:password:folder:quota" > /srv/$domain/config/ftp-users
      chown sympl: /srv/$domain/config/ftp-users
    fi

    _debug "Checking for user $mailbox in domain $domain"
    if [ "$( grep -R ^$mailbox: /srv/$domain/config/ftp-users )" ]; then
      _error "FTP user $mailbox already enabled on domain $domain."
    elif [ "$( grep -R ^#$mailbox: /srv/$domain/config/ftp-users )" ]; then
      _verbose "Enabling disabled user '$mailbox' on domain '$domain'."
      sed -ri "/^#$mailbox/s/#//" /srv/$domain/config/ftp-users
    else 
      _verbose "Adding user '$mailbox' to domain '$domain'."
      password="$( openssl rand -base64 32 | sed 's/\///g' | cut -c 1-32 )"
      echo "$mailbox:$password" >> /srv/$domain/config/ftp-users
    fi

  else

    _debug "Check if per-domain access is already enabled"
    if [ -f /srv/$domain/config/ftp-password ]; then
      _error "The domain $domain is already enabled for FTP access, to reset the pasword use <sympl ftp reset $domain>."
    fi
    _debug "Check for disabled per-domain access for '$domain'"
    if [ -f /srv/$domain/config/__disabled__ftp-password ]; then
      _verbose "Re-enabling per-domain ftp access for $domain."
      mv /srv/$domain/config/__disabled__ftp-password /srv/$domain/config/ftp-password
    else
      _verbose "Setting per-domain FTP password for '$domain'"
      password="$( openssl rand -base64 32 | cut -c 1-32 )"
      _debug "Password '$password' for user $domain on domain $domain;"
      echo "$password" > /srv/$domain/config/ftp-password
      _verbose "Access created for '$domain' with password '$password'"
      chown sympl: "/srv/$domain/config/ftp-password"
    fi
  fi
  
Paul Cammish's avatar
Paul Cammish committed
626
627
628
}

_ftp_disable() {
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
  
  if [[ "$1" =~ .*"@".* ]]; then
    _is_email $1
    readarray -d @ -t part < <(printf '%s' "$1")
    mailbox=${part[0]}
    domain=${part[1]}
    else
    _is_domain $1
    domain=$1
  fi

  if [ ! -d /srv/$domain ]; then
    _error "The domain '$domain' does not exist."
  fi

  if [ "x$mailbox" != "x"  ]; then

    _debug "Checking for user $mailbox in domain $domain"
    if [ "$( grep -R ^#$mailbox: /srv/$domain/config/ftp-users )" ]; then
      _error "FTP user $mailbox already disabled on domain $domain."
    elif [ "$( grep -R ^$mailbox: /srv/$domain/config/ftp-users )" ]; then
      _verbose "Disabling user '$mailbox' on domain '$domain'."
      sed -ri "s/(^$mailbox:)/#\1/" /srv/$domain/config/ftp-users
      return 0;
    else
      _error "User '$mailbox' not found for domain '$domain'."
    fi
  
  else 

    _debug "Checking per-domain ftp access for $domain is enabled and exists"
    if [ -f /srv/$domain/config/__disabled__ftp-password ]; then
      _error "The per-domain ftp access for $domain is already disabled."
    elif [ ! -f /srv/$domain/config/ftp-password ]; then
      _error "The per-domain ftp access for $domain does not exist."
    fi

    _verbose "Disabling per-domain FTP access for $domain"
    mv /srv/$domain/config/ftp-password /srv/$domain/config/__disabled__ftp-password
  
  fi
}

_ftp_reset() {

  if [[ "$1" =~ .*"@".* ]]; then
    _is_email $1
    readarray -d @ -t part < <(printf '%s' "$1")
    mailbox=${part[0]}
    domain=${part[1]}
  else
    _is_domain $1
    domain=$1
  fi

  if [ -d /srv/$domain ]; then
    _error "The domain '$domain' does not exist."
  fi

  if [ "x$mailbox" != "x" ]; then

    _debug "Checking for per user access on domain '$domain'"
    password="$( openssl rand -base64 32 | sed 's/\///g' | cut -c 1-32 )"
    if [ ! -f /srv/$domain/config/ftp-users ]; then
      _error "per user access not set up for domain '$domain'. please use <sympl ftp enable $mailbox@$domain>."
    elif [ "$( grep -R ^#*$mailbox: /srv/$domain/config/ftp-users )" ]; then
      if [ "$( grep -R ^#$mailbox: /srv/$domain/config/ftp-users )" ]; then
        _verbose "Resetting password for disabled user '$mailbox' on '$domain' to '$password'."
      else
        _verbose "Resetting password for '$mailbox' on '$domain' to '$password'."
      fi
      sed -ri "s/(^#*$mailbox:)[^:]*(.*)/\1$password\2/" /srv/$domain/config/ftp-users
      return 0;
    else
      _error "User '$mailbox' not foung for per-user access on domain '$domain'."
    fi

  else

    _debug "Resetting the per-domain access password for $domain"
    password="$( openssl rand -base64 26 | cut -c 1-32 )"  
    _debug "Checking if per-domain ftp access for $domain exists"
    if [ -f /srv/$domain/config/__disabled__ftp-password ]; then
      _debug "The per-domain ftp access for $domain is disabled. Resetting password"
      echo "$password" > /srv/$domain/config/__disabled__ftp-password
      _verbose "Setting the disabled per-domain FTP access password for '$domain' to $password"
      return 0;
    elif [ ! -f /srv/$domain/config/ftp-password ]; then
      _error "The per-domain ftp access for $domain does not exist."
    fi

    _verbose "Resetting the password for per-domain FTP access for $domain"
    printf '%s' "$password" > /srv/$domain/config/ftp-password

  fi

Paul Cammish's avatar
Paul Cammish committed
725
726
727
}

_ftp_destroy() {
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769

  if [[ "$1" =~ .*"@".* ]]; then
    _is_email $1
    readarray -d @ -t part < <(printf '%s' "$1")
    mailbox=${part[0]}
    domain=${part[1]}
    else
    _is_domain $1
    domain=$1
  fi

  if [ ! -d /srv/$domain ]; then
    _error "The domain '$domain' does not exist."
  fi

  if [ "x$mailbox" != "x" ]; then
    _debug "Confirm '$mailbox' is disabled and exists."
    if [ "$( grep -R ^$mailbox: /srv/$domain/config/ftp-users )" ]; then
      _error "The user '$mailbox' is enabled for domain '$domain'. It must be disabled before it can be destroyed."
    elif [ "$( grep -R ^#mailbox: /srv/$domain/config/ftp-users )" ]; then
      _verbose "destroying user '$mailbox' for domain '$domain'."
      sed -ri "/^#$mailbox:/d" /srv/$domain/config/ftp-users
      return 0;
    elif [ ! -f /srv/$domain/config/ftp-users ]; then
      _error "Per-user access is not set up for the domain '$domain'."
    else
      _error "User '$mailbox' not found for domain '$domain'"
    fi

  else

    _debug "Confirm '$domain' is disabled and exists"
    if [ -f /srv/$domain/config/ftp-password ]; then
      _error "The ftp access for '$domain' is enabled. It must be disabled before it can be destroyed."
    elif [ ! -f /srv/$domain/config/__disabled__ftp-password ]; then
      _error "The ftp access for '$domain' does not exist."
    fi

    _verbose "Destroying /srv/$domain/config/__disabled__ftp-password..."
    rm -r /srv/$domain/config/__disabled__ftp-password

  fi
Paul Cammish's avatar
Paul Cammish committed
770
771
772
}

_ftp_set() {
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828

  _is_email $1
  readarray -d @ -t part < <(printf '%s' "$1")
  mailbox=${part[0]}
  domain=${part[1]}

  if [ -z "$3" ]; then
    _error "Set requires quota or folder to have a size or location argument"
  fi 
 
  _debug "Check if domain is set up for FTP"
  if [ ! -d /srv/$domain ]; then
    _error "The domain $domain does not exist."
  elif [ ! -d /srv/$domain/public ]; then
    _error "The domain $domain is not set up for FTP access. Please use <sympl ftp create $domain>"
  fi
  
  _debug "Check if the domains config folder exists"
  if [ ! -d /srv/$domain/config ]; then
    _verbose "Creating the config folder for domain $domain."
    mkdir -p /srv/$domain/config
  fi
 

  if [[ "$2" == "folder" ]]; then
    _debug "Checking for user $mailbox"
    if [ "$3" == "-" ]; then set -- "${@:1:2}"; fi
    if [ "$( grep -R ^#*$mailbox: /srv/$domain/config/ftp-users )" ]; then
      _debug "Setting folder for $mailbox on $domain to $3" 
      while read line; do
        readarray -d : -t item < <(printf '%s' "$line")
        sed -ri "s/(^#*$mailbox:).*/\1${item[1]}:$3:${item[3]}/" /srv/$domain/config/ftp-users
      done <<< $( grep -R "^#*$mailbox:" /srv/$domain/config/ftp-users )
      _verbose "Folder reset for '$mailbox' on domain '$domain' to '$3'"
      return 0;
    else
      _error "User $mailbox not found in configuration, please use <sympl ftp enable username@domain>."
    fi
  fi

  if [[ "$2" == "quota" ]]; then
    _debug "Checking for user $mailbox"
    if [ "$3" == "-" ]; then set -- "${@:1:2}"; fi
    if [ "$( grep -R ^#*$mailbox: /srv/$domain/config/ftp-users )" ]; then
      _debug "Setting quota for $mailbox on $domain to $3" 	
      while read line; do
        readarray -d : -t item < <(printf '%s' "$line")
        sed -ri "s/(^#*$mailbox:).*/\1${item[1]}:${item[2]}:$3/" /srv/$domain/config/ftp-users
      done <<< $( grep -R "^#*$mailbox:" /srv/$domain/config/ftp-users )
      _verbose "Quota reset for '$mailbox' on domain '$domain' to '$3'"
      return 0;
    else
      _error "User $mailbox not found in configuration, please use <sympl ftp set username@domain>."
    fi
  fi

Paul Cammish's avatar
Paul Cammish committed
829
830
831
}

_ftp_audit() {
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857

  if [ "$1" != "all" ]; then _is_domain $1 ; else set "*"; fi
  
  ( umask 077; touch /dev/shm/sympl_ftp_audit )
  find /srv/$1/config/ -type f -name '*ftp-password' > /dev/shm/sympl_ftp_audit
  find /srv/$1/config/ -type f -name 'ftp-users' >> /dev/shm/sympl_ftp_audit

  _verbose "Enabled per-domain access:"
  dom=$( grep '/ftp-password' /dev/shm/sympl_ftp_audit | cut -d '/' -f 3 | sort )
  [ -z "$dom" ] && echo "None found." || echo "$dom"; unset dom
 
  _verbose "Disabled per-domain access:"
  dom=$( grep __disabled__ftp-password /dev/shm/sympl_ftp_audit | cut -d '/' -f 3 | sort )
  [ -z "$dom" ] && echo "None found." || echo "$dom"; unset dom

  for domain in $( grep ftp-users /dev/shm/sympl_ftp_audit | cut -d '/' -f 3 | sort ); do
    _verbose "Per-user access exists for domain $domain:"
    printf '\t%-25s%-10s%-10s\n' "<username>" "<folder>" "<quota>"
    while read line; do
      readarray -d : -t item <<< "$line"
      printf '\t%-25s%-10s%-10s\n' "${item[0]}@$domain" "${item[2]}" "${item[3]::-1}"
    done <<< $( sed '/^#.*username/d' /srv/$domain/config/ftp-users )
  done 
  _debug "Cleaning up audit file"
  rm /dev/shm/sympl_ftp_audit

Paul Cammish's avatar
Paul Cammish committed
858
859
}

Paul Cammish's avatar
Paul Cammish committed
860
861
862
863
_ftp_update() {
  _noop
}

Paul Cammish's avatar
Paul Cammish committed
864
865
866
867
##############################################################################
#                                    Backup                                  #
##############################################################################

868

Paul Cammish's avatar
Paul Cammish committed
869
870
871
872
_backup_none() {
  _noop
}

Paul Cammish's avatar
Paul Cammish committed
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
_backup_create() {
  _verbose "Taking backup..."
  sudo backup2l -b
}

_backup_enable() {
  if [ ! -x /etc/cron.daily/zz-backup2l ]; then
    _verbose "Enabling backups."
    sudo chmod +x /etc/cron.daily/zz-backup2l
  else
    _error "Backups are already enabled."
  fi
}

_backup_disable() {
  _verbose "Disabling backups."
  if [ -x /etc/cron.daily/zz-backup2l ]; then
    _verbose "Disabling backups."
    sudo chmod -x /etc/cron.daily/zz-backup2l
  else
    _error "Backups are already disabled."
  fi
}

_backup_destroy() {
  if [ $( backup2l -s | grep -c '^all.2 ' ) -ge 1 ]; then
    _verbose "Removing oldest backup set."
    sudo backup2l -p 1 > /dev/null
  else
    _error "Not removing only backup set."
  fi
}

_backup_set() {
  _error "Backup has no settings."
}

_backup_audit() {
  _verbose "Backup status:"
  backup2l -s
}

Paul Cammish's avatar
Paul Cammish committed
915
916
917
918
_backup_audit() {
  _noop
}

Paul Cammish's avatar
Paul Cammish committed
919
920
921
922
##############################################################################
#                                     Cron                                   #
##############################################################################

Paul Cammish's avatar
Paul Cammish committed
923
924
925
926
_cron_none() {
  _noop
}

Paul Cammish's avatar
Paul Cammish committed
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
_cron_create() {
  _noop
}

_cron_enable() {
  _noop
}

_cron_disable() {
  _noop
}

_cron_destroy() {
  _noop
}

_cron_set() {
  _noop
}

_cron_audit() {
  _noop
}

Paul Cammish's avatar
Paul Cammish committed
951
952
953
954
_cron_update() {
  _noop
}

Paul Cammish's avatar
Paul Cammish committed
955
956
957
958
959
960
##############################################################################
#                                  NoOp Stub                                 #
##############################################################################


_noop() {
Paul Cammish's avatar
Paul Cammish committed
961
  _warn "Sorry this doesn't do anything yet, but it may be in a new version.\n        Run 'sympl update' for an updated version."
Paul Cammish's avatar
Paul Cammish committed
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
  exit 0
}

##############################################################################
#                              Handle Command Line                           #
##############################################################################

_main() {

  UNHANDLED=()
  while [ $# -gt 0 ]; do
    case $1 in
      --debug)                  DEBUG=TRUE               ; shift ;;
      --quiet|-q)               VERBOSE=FALSE            ; shift ;;
      web|http|website)         _func web                ; shift ;;
      mail|email)               _func mail               ; shift ;;
      mysql|database|mariadb)   _func mysql              ; shift ;;
      dns|domain)               _func dns                ; shift ;;
      backup)                   _func backup             ; shift ;;
      cron)                     _func cron               ; shift ;;
      ftp)                      _func ftp                ; shift ;;
      create)                   _action 'create'         ; shift ;;
      destroy)                  _action 'destroy'        ; shift ;;
      enable)                   _action 'enable'         ; shift ;;
      disable)                  _action 'disable'        ; shift ;;
      audit|list)               _action 'audit'          ; shift ;;
988
      reset)		        	_action 'reset'	      	 ; shift ;;
Paul Cammish's avatar
Paul Cammish committed
989
      update|upgrade)           _action 'update'         ; shift ;;
Paul Cammish's avatar
Paul Cammish committed
990
991
992
993
994
995
996
997
998
999
1000
1001
      set)                      _action 'set' "$2" "$3"  ; shift ; shift ; shift ;;
      *) # unhandled parameter
        if [ $TARGET = all ]; then
          TARGET="$1"
        else
          UNHANDLED+=("$1")
        fi
        shift # past argument
      ;;
    esac
  done

Paul Cammish's avatar
Paul Cammish committed
1002
1003
1004
1005
  _debug "Command line parameters: $@"

  _debug "Running as '$I_AM', EUID '$EUID'"

Paul Cammish's avatar
Paul Cammish committed
1006
1007
1008
1009
1010
1011
1012
1013
1014
  _debug "Runtime variables:
  Unhanded         | $UNHANDLED
  FUNCTION         | $FUNCTION
  ACTION           | $ACTION
  TARGET           | $TARGET
  OPTION           | $OPTION
  SETTING          | $SETTING"

  if [ "x$UNHANDLED" != "x" ]; then
Paul Cammish's avatar
Paul Cammish committed
1015
    _error "Unexpected input: '$UNHANDLED', run 'sympl' for help."
Paul Cammish's avatar
Paul Cammish committed
1016
1017
1018
1019
1020
1021
1022
1023
  fi

  _${FUNCTION}_${ACTION} $TARGET $OPTION $SETTING

}


_main $@
1024

Paul Cammish's avatar
Paul Cammish committed
1025
exit 0