KDECore
AuthServicesBackend.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "AuthServicesBackend.h"
00021 #include <Security/Security.h>
00022
00023 namespace KAuth
00024 {
00025
00026 static AuthorizationRef s_authRef = NULL;
00027
00028 AuthorizationRef authRef();
00029
00030 AuthorizationRef authRef()
00031 {
00032 if (!s_authRef) {
00033 AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, kAuthorizationFlagDefaults, &s_authRef);
00034 }
00035
00036 return s_authRef;
00037 }
00038
00039 AuthServicesBackend::AuthServicesBackend() : AuthBackend() {}
00040
00041 void AuthServicesBackend::setupAction(const QString&)
00042 {
00043
00044 }
00045
00046
00047
00048 Action::AuthStatus AuthServicesBackend::authorizeAction(const QString &action)
00049 {
00050 return actionStatus(action);
00051 }
00052
00053 Action::AuthStatus AuthServicesBackend::actionStatus(const QString &action)
00054 {
00055 AuthorizationItem item;
00056 item.name = action.toUtf8();
00057 item.valueLength = 0;
00058 item.value = NULL;
00059 item.flags = 0;
00060
00061 AuthorizationRights rights;
00062 rights.count = 1;
00063 rights.items = &item;
00064
00065 OSStatus result = AuthorizationCopyRights(authRef(),
00066 &rights,
00067 kAuthorizationEmptyEnvironment,
00068 kAuthorizationFlagExtendRights | kAuthorizationFlagPreAuthorize,
00069 NULL);
00070
00071 switch (result) {
00072 case errAuthorizationSuccess:
00073 return Action::Authorized;
00074 case errAuthorizationInteractionNotAllowed:
00075 return Action::AuthRequired;
00076 default:
00077 return Action::Denied;
00078 }
00079 }
00080
00081 QByteArray AuthServicesBackend::callerID() const
00082 {
00083 AuthorizationExternalForm ext;
00084 AuthorizationMakeExternalForm(authRef(), &ext);
00085
00086 QByteArray id((const char *)&ext, sizeof(ext));
00087
00088 return id;
00089 }
00090
00091 bool AuthServicesBackend::isCallerAuthorized(const QString &action, QByteArray callerID)
00092 {
00093 AuthorizationExternalForm ext;
00094 memcpy(&ext, callerID.data(), sizeof(ext));
00095
00096 AuthorizationRef auth;
00097
00098 if (AuthorizationCreateFromExternalForm(&ext, &auth) != noErr)
00099 return false;
00100
00101 AuthorizationItem item;
00102 item.name = action.toUtf8();
00103 item.valueLength = 0;
00104 item.value = NULL;
00105 item.flags = 0;
00106
00107 AuthorizationRights rights;
00108 rights.count = 1;
00109 rights.items = &item;
00110
00111 OSStatus result = AuthorizationCopyRights(auth,
00112 &rights,
00113 kAuthorizationEmptyEnvironment,
00114 kAuthorizationFlagExtendRights | kAuthorizationFlagInteractionAllowed,
00115 NULL);
00116
00117 AuthorizationFree(auth, kAuthorizationFlagDefaults);
00118
00119 return result == errAuthorizationSuccess;
00120 }
00121
00122 };
00123
00124 Q_EXPORT_PLUGIN2(auth_backend, KAuth::AuthServicesBackend);