File indexing completed on 2024-04-28 04:50:03
0001 /* 0002 SPDX-FileCopyrightText: 2009-2011 Michal Malek <michalm@jabster.pl> 0003 SPDX-FileCopyrightText: 2010 Dario Freddi <drf@kde.org> 0004 SPDX-FileCopyrightText: 1998-2009 Sebastian Trueg <trueg@k3b.org> 0005 0006 SPDX-License-Identifier: GPL-2.0-or-later 0007 */ 0008 0009 #include "k3bhelper.h" 0010 #include "k3bhelperprogramitem.h" 0011 0012 #include <KAuth/HelperSupport> 0013 0014 #include <QFile> 0015 #include <QProcess> 0016 #include <QString> 0017 #include <QStringList> 0018 0019 #include <grp.h> 0020 #include <sys/types.h> 0021 #include <sys/stat.h> 0022 #include <unistd.h> 0023 0024 namespace { 0025 0026 bool updateDevicePermissions( ::group* g, const QString& device ) 0027 { 0028 bool success = true; 0029 if( g != 0 ) { 0030 if( ::chmod( QFile::encodeName(device), S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP ) ) 0031 success = false; 0032 0033 if( ::chown( QFile::encodeName(device), (gid_t)-1, g->gr_gid ) ) 0034 success = false; 0035 } 0036 else { 0037 if( ::chmod( QFile::encodeName(device), S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH ) ) 0038 success = false; 0039 } 0040 return success; 0041 } 0042 0043 0044 bool updateProgramPermissions( ::group* g, const QString& path, bool suid ) 0045 { 0046 bool success = true; 0047 if( g != 0 ) { 0048 if( ::chown( QFile::encodeName(path), (gid_t)0, g->gr_gid ) ) 0049 success = false; 0050 0051 int perm = 0; 0052 if( suid ) 0053 perm = S_ISUID|S_IRWXU|S_IXGRP; 0054 else 0055 perm = S_IRWXU|S_IXGRP|S_IRGRP; 0056 0057 if( ::chmod( QFile::encodeName(path), perm ) ) 0058 success = false; 0059 } 0060 else { 0061 if( ::chown( QFile::encodeName(path), 0, 0 ) ) 0062 success = false; 0063 0064 int perm = 0; 0065 if( suid ) 0066 perm = S_ISUID|S_IRWXU|S_IXGRP|S_IXOTH; 0067 else 0068 perm = S_IRWXU|S_IXGRP|S_IRGRP|S_IXOTH|S_IROTH; 0069 0070 if( ::chmod( QFile::encodeName(path), perm ) ) 0071 success = false; 0072 } 0073 return success; 0074 } 0075 0076 } // namespace 0077 0078 0079 namespace K3b { 0080 0081 Helper::Helper() 0082 { 0083 qRegisterMetaType<HelperProgramItem>(); 0084 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) 0085 qRegisterMetaTypeStreamOperators<HelperProgramItem>( "K3b::HelperProgramItem" ); 0086 #endif 0087 } 0088 0089 KAuth::ActionReply Helper::updatepermissions( QVariantMap args ) 0090 { 0091 QString burningGroup = args["burningGroup"].toString(); 0092 QStringList devices = args["devices"].toStringList(); 0093 QVariantList programs = args["programs"].value<QVariantList>(); 0094 0095 ::group* g = 0; 0096 if( !burningGroup.isEmpty() ) { 0097 g = ::getgrnam( burningGroup.toLocal8Bit() ); 0098 } 0099 0100 QStringList updated; 0101 QStringList failedToUpdate; 0102 0103 Q_FOREACH( const QString& dev, devices ) 0104 { 0105 if( updateDevicePermissions( g, dev ) ) 0106 updated.push_back( dev ); 0107 else 0108 failedToUpdate.push_back( dev ); 0109 } 0110 0111 Q_FOREACH( const QVariant& v, programs ) 0112 { 0113 HelperProgramItem program = v.value<HelperProgramItem>(); 0114 0115 if( !program.m_path.isEmpty() && updateProgramPermissions( g, program.m_path, program.m_needSuid ) ) 0116 updated.push_back( program.m_path ); 0117 else 0118 failedToUpdate.push_back( program.m_path ); 0119 } 0120 0121 KAuth::ActionReply reply = KAuth::ActionReply::SuccessReply(); 0122 QVariantMap data; 0123 data["updated"] = updated; 0124 data["failedToUpdate"] = failedToUpdate; 0125 reply.setData(data); 0126 0127 return reply; 0128 } 0129 0130 KAuth::ActionReply Helper::addtogroup( QVariantMap args ) 0131 { 0132 const QString groupName = args["groupName"].toString(); 0133 const QString userName = args["userName"].toString(); 0134 0135 QProcess gpasswd; 0136 int errorCode = gpasswd.execute( "gpasswd", QStringList() << "--add" << userName << groupName ); 0137 0138 KAuth::ActionReply reply; 0139 if( errorCode == 0 ) { 0140 reply = KAuth::ActionReply::SuccessReply(); 0141 } else { 0142 reply = KAuth::ActionReply::HelperErrorReply(); 0143 reply.setErrorCode( (KAuth::ActionReply::Error) errorCode ); 0144 reply.setErrorDescription( QString( "gpasswd --add " + userName + ' ' + groupName + " : " + QString::fromLocal8Bit( gpasswd.readAllStandardError().data() ) ) ); 0145 } 0146 0147 return reply; 0148 } 0149 0150 } // namespace K3b 0151 0152 KAUTH_HELPER_MAIN("org.kde.k3b", K3b::Helper) 0153 0154 #include "moc_k3bhelper.cpp"