summaryrefslogtreecommitdiffstats
path: root/filesharing/advanced/kcm_sambaconf/linuxpermissionchecker.cpp
blob: 081def4526a9af16254887ee922053ff94342bde (plain)
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
/***************************************************************************
    begin                : Tue May 17 2003
    copyright            : (C) 2003 by Jan Sch�er
    email                : janschaefer@users.sourceforge.net
 ***************************************************************************/

/******************************************************************************
 *                                                                            *
 *  This file is part of KSambaPlugin.                                        *
 *                                                                            *
 *  KSambaPlugin is free software; you can redistribute it and/or modify      *
 *  it under the terms of the GNU General Public License as published by      *
 *  the Free Software Foundation; either version 2 of the License, or         *
 *  (at your option) any later version.                                       *
 *                                                                            *
 *  KSambaPlugin is distributed in the hope that it will be useful,           *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of            *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
 *  GNU General Public License for more details.                              *
 *                                                                            *
 *  You should have received a copy of the GNU General Public License         *
 *  along with KSambaPlugin; if not, write to the Free Software               *
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA  *
 *                                                                            *
 ******************************************************************************/
#include <tqregexp.h> 
#include <tqstringlist.h> 

#include <tdemessagebox.h>
#include <tdelocale.h>
#include <kdebug.h>

#include "passwd.h"
#include "sambashare.h"  
#include "linuxpermissionchecker.h"

LinuxPermissionChecker::LinuxPermissionChecker(SambaShare* share,TQWidget* parent = 0L)
{
  m_sambaShare = share;
  m_parent = parent;

  if (!share) {
    kdWarning() << "WARNING: LinuxPermissionChecker: share is null !" << endl;
    return;        
  }
  
  m_fi = TQFileInfo(share->getValue("path"));
  
  if ( ! m_fi.exists()) {
    kdDebug(5009) << "LinuxPermissionChecker: path does not exists !" << endl;
  }

}


LinuxPermissionChecker::~LinuxPermissionChecker()
{
}

bool LinuxPermissionChecker::checkAllPermissions() {
  if (! m_sambaShare )
     return true;
     
  if ( ! m_fi.exists())
     return true;   

  if (! checkPublicPermissions())
     return false;     
     
  if (! checkAllUserPermissions())
     return false;
     
  return true;          
}
    
bool LinuxPermissionChecker::checkAllUserPermissions() {
  if (! m_sambaShare )
     return true;

  if ( ! m_fi.exists())
     return true;   
       
  TQStringList readList = TQStringList::split(TQRegExp("[,\\s]+"),m_sambaShare->getValue("read list"));
  
  for ( TQStringList::Iterator it = readList.begin(); it != readList.end(); ++it )
  {
    if (!checkUserReadPermissions(*it))
       return false;
  }    
  
  TQStringList writeList = TQStringList::split(TQRegExp("[,\\s]+"),m_sambaShare->getValue("write list"));
  
  for ( TQStringList::Iterator it = writeList.begin(); it != writeList.end(); ++it )
  {
    if (!checkUserWritePermissions(*it))
       return false;
  }    
  
  return true;     
}

bool LinuxPermissionChecker::checkPublicPermissions() {
  if (! m_sambaShare )
     return true;

  bool isPublic = m_sambaShare->getBoolValue("public");
  if (!isPublic)
     return true;
  
  TQString guestAccount = m_sambaShare->getValue("guest account");
  
  if ( ! checkUserReadPermissions(guestAccount,false))
  {
     if (KMessageBox::Cancel == KMessageBox::warningContinueCancel(
         0L,i18n(
           "<qt>You have specified <b>public read access</b> for this directory, but "
           "the guest account <b>%1</b> does not have the necessary read permissions;<br>" 
           "do you want to continue anyway?</qt>").arg(guestAccount)
           ,i18n("Warning")
           ,KStdGuiItem::cont()
           ,"KSambaPlugin_guestAccountHasNoReadPermissionsWarning"))
         return false;
  }
  
  
  if ( ! checkUserWritePermissions(guestAccount,false))
  {
     if (KMessageBox::Cancel == KMessageBox::warningContinueCancel(
         0L,i18n(
           "<qt>You have specified <b>public write access</b> for this directory, but "
           "the guest account <b>%1</b> does not have the necessary write permissions;<br>" 
           "do you want to continue anyway?</qt>").arg(guestAccount)
           ,i18n("Warning")
           ,KStdGuiItem::cont()
           ,"KSambaPlugin_guestAccountHasNoWritePermissionsWarning"))
         return false;
  }
  
  return true;
}
    
bool LinuxPermissionChecker::checkUserPermissions(const TQString & user) {
  if ( ! checkUserReadPermissions(user))
     return false;  
  
  if ( ! checkUserWritePermissions(user))
     return false;    
       
  return true;     
}

bool LinuxPermissionChecker::checkUserWritePermissions(const TQString & user, bool showMessageBox) {
  // If no write permissions are given, we don't need to check them.
  if (m_sambaShare->getBoolValue("read only"))
     return true;
  
  if (! ((m_fi.permission(TQFileInfo::WriteOther)) ||
          (m_fi.permission(TQFileInfo::WriteUser) && user == m_fi.owner()) ||
          (m_fi.permission(TQFileInfo::WriteGroup) && isUserInGroup(user, m_fi.group())))
      )
  {
    if (!showMessageBox)
      return false;
    
    if (KMessageBox::Cancel == KMessageBox::warningContinueCancel(
        0L,i18n(
          "<qt>You have specified <b>write access</b> to the user <b>%1</b> for this directory, but "
          "the user does not have the necessary write permissions;<br>" 
          "do you want to continue anyway?</qt>").arg(user)
          ,i18n("Warning")
          ,KStdGuiItem::cont()
          ,"KSambaPlugin_userHasNoWritePermissionsWarning"))
        return false;
  }
  
  return true;
}
    
bool LinuxPermissionChecker::checkUserReadPermissions(const TQString & user, bool showMessageBox) {
  if (! ((m_fi.permission(TQFileInfo::ReadOther)) ||
         (m_fi.permission(TQFileInfo::ReadUser) && user == m_fi.owner()) ||
         (m_fi.permission(TQFileInfo::ReadGroup) && isUserInGroup(user, m_fi.group())))
     )
  {     
     if (!showMessageBox)
        return false;
        
     if (KMessageBox::Cancel == KMessageBox::warningContinueCancel(
         0L,i18n(
           "<qt>You have specified <b>read access</b> to the user <b>%1</b> for this directory, but "
           "the user does not have the necessary read permissions;<br>" 
           "do you want to continue anyway?</qt>").arg(user)
           ,i18n("Warning")
           ,KStdGuiItem::cont()
           ,"KSambaPlugin_userHasNoReadPermissionsWarning"))
         return false;
     
  }
     
  return true;     
}