KDECore
AuthServicesBackend.cpp
Go to the documentation of this file.
00001 /* 00002 * Copyright (C) 2008 Nicola Gigante <nicola.gigante@gmail.com> 00003 * 00004 * This program is free software; you can redistribute it and/or modify 00005 * it under the terms of the GNU Lesser General Public License as published by 00006 * the Free Software Foundation; either version 2.1 of the License, or 00007 * (at your option) any later version. 00008 * 00009 * This program is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU Lesser General Public License 00015 * along with this program; if not, write to the 00016 * Free Software Foundation, Inc., 00017 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . 00018 */ 00019 00020 #include "AuthServicesBackend.h" 00021 #include <Security/Security.h> 00022 00023 #include <QtCore/qplugin.h> 00024 00025 namespace KAuth 00026 { 00027 00028 static AuthorizationRef s_authRef = NULL; 00029 00030 AuthorizationRef authRef(); 00031 00032 AuthorizationRef authRef() 00033 { 00034 if (!s_authRef) { 00035 AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, kAuthorizationFlagDefaults, &s_authRef); 00036 } 00037 00038 return s_authRef; 00039 } 00040 00041 AuthServicesBackend::AuthServicesBackend() 00042 : AuthBackend() 00043 { 00044 setCapabilities(AuthorizeFromHelperCapability | CheckActionExistenceCapability); 00045 } 00046 00047 void AuthServicesBackend::setupAction(const QString&) 00048 { 00049 // Nothing to do here... 00050 } 00051 00052 // On OS X, the suggestion is to make the helper grant the actual privilege. The app does instead a 00053 // "pre-authorization", that's equivalent to look at isCallerAuthorized() in policykit. 00054 Action::AuthStatus AuthServicesBackend::authorizeAction(const QString &action) 00055 { 00056 return actionStatus(action); 00057 } 00058 00059 Action::AuthStatus AuthServicesBackend::actionStatus(const QString &action) 00060 { 00061 AuthorizationItem item; 00062 item.name = action.toUtf8(); 00063 item.valueLength = 0; 00064 item.value = NULL; 00065 item.flags = 0; 00066 00067 AuthorizationRights rights; 00068 rights.count = 1; 00069 rights.items = &item; 00070 00071 OSStatus result = AuthorizationCopyRights(authRef(), 00072 &rights, 00073 kAuthorizationEmptyEnvironment, 00074 kAuthorizationFlagExtendRights | kAuthorizationFlagPreAuthorize, 00075 NULL); 00076 00077 switch (result) { 00078 case errAuthorizationSuccess: 00079 return Action::Authorized; 00080 case errAuthorizationInteractionNotAllowed: 00081 return Action::AuthRequired; 00082 default: 00083 return Action::Denied; 00084 } 00085 } 00086 00087 QByteArray AuthServicesBackend::callerID() const 00088 { 00089 AuthorizationExternalForm ext; 00090 AuthorizationMakeExternalForm(authRef(), &ext); 00091 00092 QByteArray id((const char *)&ext, sizeof(ext)); 00093 00094 return id; 00095 } 00096 00097 bool AuthServicesBackend::isCallerAuthorized(const QString &action, QByteArray callerID) 00098 { 00099 AuthorizationExternalForm ext; 00100 memcpy(&ext, callerID.data(), sizeof(ext)); 00101 00102 AuthorizationRef auth; 00103 00104 if (AuthorizationCreateFromExternalForm(&ext, &auth) != noErr) 00105 return false; 00106 00107 AuthorizationItem item; 00108 item.name = action.toUtf8(); 00109 item.valueLength = 0; 00110 item.value = NULL; 00111 item.flags = 0; 00112 00113 AuthorizationRights rights; 00114 rights.count = 1; 00115 rights.items = &item; 00116 00117 OSStatus result = AuthorizationCopyRights(auth, 00118 &rights, 00119 kAuthorizationEmptyEnvironment, 00120 kAuthorizationFlagExtendRights | kAuthorizationFlagInteractionAllowed, 00121 NULL); 00122 00123 AuthorizationFree(auth, kAuthorizationFlagDefaults); 00124 00125 return result == errAuthorizationSuccess; 00126 } 00127 00128 bool AuthServicesBackend::actionExists(const QString& action) 00129 { 00130 OSStatus exists = AuthorizationRightGet(action.toUtf8(), NULL); 00131 00132 return exists == errAuthorizationSuccess; 00133 } 00134 00135 }; // namespace KAuth 00136 00137 Q_EXPORT_PLUGIN2(kauth_backend, KAuth::AuthServicesBackend)
KDE 4.6 API Reference