1/*
2 * Copyright (c) 2000-2004,2007 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 *
23 *  AuthorizationEngine.h
24 *  Authorization
25 *
26 */
27#ifndef _H_AUTHORIZATIONENGINE
28#define _H_AUTHORIZATIONENGINE  1
29
30#include <Security/Authorization.h>
31#include <Security/AuthorizationPlugin.h>
32#include <security_cdsa_utilities/AuthorizationData.h>
33
34#include <security_utilities/threading.h>
35#include <security_utilities/osxcode.h>
36
37#include <CoreFoundation/CFDate.h>
38#include <CoreFoundation/CFDictionary.h>
39#include <sys/stat.h>
40#include <sys/types.h>
41
42#include "authority.h"
43
44#include "AuthorizationRule.h"
45#include "AuthorizationDBPlist.h"
46
47namespace Authorization
48{
49
50class Error : public CommonError {
51protected:
52    Error(int err);
53public:
54    const int error;
55    virtual int unixError() const throw();
56    virtual OSStatus osStatus() const throw();
57    virtual const char *what () const throw();
58    static void throwMe(int err) __attribute((noreturn));
59};
60
61
62/* The engine which performs the actual authentication and authorization computations.
63
64	The implementation of a typical call to AuthorizationCreate would look like:
65
66	Get the current shared CredentialSet for this session.
67	Call authorizedRights() with inRights and the shared CredentialSet.
68	Compute the difference set between the rights requested and the rights returned from authorizedRights().
69	Call credentialIds() with the rights computed above (for which we have no credentials yet).
70	Call aquireCredentials() for the credentialIds returned from credentialIds()
71	For each credential returned place it in the session (replacing when needed) if shared() returns true.
72	The authorization returned to the user should now refer to the credentials in the session and the non shared ones returned by aquireCredentials().
73
74	When a call to AuthorizationCopyRights() is made, just call authorizedRights() using the union of the session credentials and the credentials tied to the authorization specified.
75
76	When a call to AuthorizationCopyInfo() is made, ask the Credential specified by tag for it info and return it.
77
78	When a call to AuthorizationFree() is made, delete all the non-shared credentials ascociated with the authorization specified.  If the kAuthorizationFreeFlagDestroy is set.  Also delete the shared credentials ascociated with the authorization specified.
79 */
80class Engine
81{
82public:
83	Engine(const char *configFile);
84	~Engine();
85
86	OSStatus authorize(const AuthItemSet &inRights, const AuthItemSet &environment,
87		AuthorizationFlags flags, const CredentialSet *inCredentials, CredentialSet *outCredentials,
88		AuthItemSet &outRights, AuthorizationToken &auth);
89	OSStatus getRule(string &inRightName, CFDictionaryRef *outRuleDefinition);
90	OSStatus setRule(const char *inRightName, CFDictionaryRef inRuleDefinition, const CredentialSet *inCredentials, CredentialSet *outCredentials, AuthorizationToken &auth);
91	OSStatus removeRule(const char *inRightName, const CredentialSet *inCredentials, CredentialSet *outCredentials, AuthorizationToken &auth);
92
93private:
94	OSStatus verifyModification(string inRightName, bool remove,
95	const CredentialSet *inCredentials, CredentialSet *outCredentials, AuthorizationToken &auth);
96
97	AuthorizationDBPlist mAuthdb;
98    mutable Mutex mLock;
99};
100
101}; // namespace Authorization
102
103#endif /* ! _H_AUTHORIZATIONENGINE */
104