Alternc  latest
Alternc logiel libre pour l'hébergement
fixperms_mail.sh
Go to the documentation of this file.
1 #!/bin/bash -e
2 #
3 # ----------------------------------------------------------------------
4 # AlternC - Web Hosting System
5 # Copyright (C) 2000-2016 by the AlternC Development Team.
6 # https://alternc.org/
7 # ----------------------------------------------------------------------
8 # LICENSE
9 #
10 # This program is free software; you can redistribute it and/or
11 # modify it under the terms of the GNU General Public License (GPL)
12 # as published by the Free Software Foundation; either version 2
13 # of the License, or (at your option) any later version.
14 #
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
19 #
20 # To read the license please visit http://www.gnu.org/copyleft/gpl.html
21 # ----------------------------------------------------------------------
22 # Original Author of file: Remi - 2016-04-27
23 # Purpose of file: Fixes permissions and ownerships of AlternC mailboxes
24 # ----------------------------------------------------------------------
25 #
26 
27 
28 show_help() {
29 cat << EOT
30 Usage: `basename $0` [-c] [-n] [-l <login>] [-u <uid>] [-p <directory>] [-d <domain>]
31 
32 Fixes rights of AlternC mailboxes
33 
34  -c
35  Compatibility mode: adapts rights for both pre 1.X and newer versions (using acl)
36 
37  -l
38  login of an AlternC account
39 
40  -u
41  uid of an AlternC account
42 
43  -p
44  path to a directory, if the path does not contain an underscore (_),
45  this is considered as a prefix.
46 
47  -d
48  fix mails belonging to a FQDN
49 
50  -n
51  dry run. Causes the program to show the modifications, without actually executing them.
52 
53  -h
54  shows this help message
55 
56 EOT
57 }
58 
59 DRY_RUN=0
60 ACL=0
61 
62 execute_cmd() {
63  if [ $DRY_RUN -eq 1 ]; then
64  echo $@
65  else
66  eval $@
67  fi
68 }
69 
70 query="select m.path, mem.uid from mailbox m join address a on m.address_id=a.id join domaines d on a.domain_id=d.id join membres mem on d.compte=mem.uid where delivery='dovecot'"
71 
72 while getopts "hl:u:p:d:cn" optname
73 do
74  case "$optname" in
75  "c")
76  ACL=1
77  ;;
78  "n")
79  DRY_RUN=1
80  ;;
81  ## login
82  "l")
83  if [[ "$OPTARG" =~ ^[a-zA-Z0-9_]+$ ]]; then
84  query="$query and mem.login='$OPTARG'"
85  else
86  echo "error: \"$OPTARG\" is not a valid login" 1>&2
87  show_help
88  exit 1
89  fi
90  ;;
91  ## uid
92  "u")
93  if [[ "$OPTARG" =~ ^[0-9]+$ ]]; then
94  query="$query and mem.uid='$OPTARG'"
95  else
96  echo "error: \"$OPTARG\" is not a valid uid" 1>&2
97  show_help
98  exit 1
99  fi
100  ;;
101  ## domain
102  "d")
103  if [[ "$OPTARG" != *"'"* ]]; then
104  query="$query and d.domaine='$OPTARG'"
105  fi
106  ;;
107  ## path
108  "p")
109  ## if path contains an underscore it's a full path, otherwise it's a prefix
110  if [ -d "$OPTARG" ]; then
111  if [[ $OPTARG == *"_"* ]]; then
112  query="$query and m.path='${OPTARG%/}'"
113  else
114  query="$query and m.path LIKE '$OPTARG%'"
115  fi
116  else
117  echo "error: \"$OPTARG\" is not a valid directory" 1>&2
118  show_help
119  exit 1
120  fi
121  ;;
122  ## show help
123  "h")
124  show_help
125  exit 0
126  ;;
127  "?")
128  echo "Unkown option: $OPTARG" 1>&2
129  show_help
130  exit 1
131  ;;
132  *)
133  show_help
134  exit 1
135  ;;
136  esac
137 done
138 
139 
140 echo $query | mysql --defaults-file=/etc/alternc/my.cnf -N -B | while read path uid; do
141  echo "** Fixing $path ($uid)"
142 
143  if [ $ACL -eq 1 ]; then
144  execute_cmd chown -R www-data.$uid $path
145  execute_cmd find $path -type d -exec chmod 2755 {} \\\;
146  execute_cmd setfacl -bknR -m d:u:$uid:rwx -m u:$uid:rwx -m d:o::--- -m o::---\
147  -m d:u:www-data:rwx -m u:www-data:rwx -m d:g:$uid:rwx -m g:$uid:rwx\
148  -m d:mask:rwx -m mask:rwx "$path"
149  else
150  execute_cmd chown -R $uid.vmail $path
151  execute_cmd find $path -type d -exec chmod 0700 {} \\\;
152  fi
153 
154 done