Merge branch 'master' into plugin-interface
[platform/upstream/iotivity.git] / resource / csdk / security / include / internal / policyengine.h
1 //******************************************************************
2 //
3 // Copyright 2015 Intel Mobile Communications GmbH All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 #ifndef IOTVT_SRM_PE_H
22 #define IOTVT_SRM_PE_H
23
24 #include "ocstack.h"
25 #include "logger.h"
26 #include "securevirtualresourcetypes.h"
27 #include "cainterface.h"
28 #include "amsmgr.h"
29 #include <stdlib.h>
30 #include <stdint.h>
31
32 typedef struct AmsMgrContext AmsMgrContext_t;
33
34
35 typedef enum PEState
36 {
37     STOPPED = 0,              //Policy engine state machine is not running
38     AWAITING_REQUEST,         //Can process new request
39     AWAITING_AMS_RESPONSE,    //Can't process new request; waiting for AMS response
40     BUSY                      //Can't process new request as processing other requests
41 } PEState_t;
42
43
44 typedef struct PEContext
45 {
46     PEState_t   state;
47     OicUuid_t   subject;
48     char        resource[MAX_URI_LENGTH];
49     uint16_t    permission;
50     bool        matchingAclFound;
51     bool        amsProcessing;
52     SRMAccessResponse_t retVal;
53     AmsMgrContext_t     *amsMgrContext;
54 } PEContext_t;
55
56 /**
57  * Check whether a request should be allowed.
58  *
59  * @param   context     Pointer to Policy Engine context to use.
60  * @param   subjectId   Pointer to Id of the requesting entity.
61  * @param   resource    Pointer to URI of Resource being requested.
62  * @param   permission  Requested permission.
63  *
64  * @return  ACCESS_GRANTED if request should go through,
65  *          otherwise some flavor of ACCESS_DENIED
66  */
67 SRMAccessResponse_t CheckPermission(
68     PEContext_t     *context,
69     const OicUuid_t *subjectId,
70     const char      *resource,
71     const uint16_t  requestedPermission);
72
73 /**
74  * Initialize the Policy Engine. Call this before calling CheckPermission().
75  * TODO Eventually this and DeInit() need to be called from a new
76  *      "SRMInit(SRMContext_t *)" function, TBD after BeachHead.
77  * @param   context     Pointer to Policy Engine context to initialize.
78  * @return  OC_STACK_OK for Success, otherwise some error value
79  */
80 OCStackResult InitPolicyEngine(PEContext_t *context);
81
82 /**
83  * De-Initialize the Policy Engine. Call this before exiting to allow Policy
84  * Engine to do cleanup on context.
85  * @param   context     Pointer to Policy Engine context to de-initialize.
86  * @return  none
87  */
88 void DeInitPolicyEngine(PEContext_t *context);
89
90 /**
91  * Return the uint16_t CRUDN permission corresponding to passed CAMethod_t.
92  */
93 uint16_t GetPermissionFromCAMethod_t(const CAMethod_t method);
94
95
96 /*
97  * This method reset Policy Engine context to default state and update
98  * it's state to @param state.
99  *
100  * @param context  Policy engine context.
101  * @param state    Set Policy engine state to this.
102  *
103  * @return         none
104  */
105 void SetPolicyEngineState(PEContext_t *context, const PEState_t state);
106
107 #endif //IOTVT_SRM_PE_H