1# Development on IPC Authentication<a name="EN-US_TOPIC_0000001058671861"></a> 2 3## When to Use<a name="section18502174174019"></a> 4 5System services registered with Samgr can be accessed by other processes through IPC APIs. When a process requests to access such an API, IPC authentication is triggered to check whether the process has the required permission. If the process does not have the required permission, the access request will be denied. 6 7When developing a system service, you can use the IPC authentication component to configure access policies for APIs of the service. When other services access these APIs through IPC, Samgr calls APIs of the IPC authentication component to check whether the services have the access permission. 8 9## Available APIs<a name="section1633115419401"></a> 10 11The following table lists the APIs provided by IPC authentication \(intended for Samgr only\). 12 13**Table 1** APIs provided by IPC authentication 14 15<a name="table10494122145517"></a> 16<table><thead align="left"><tr id="row1494152195511"><th class="cellrowborder" valign="top" width="50%" id="mcps1.2.3.1.1"><p id="p14941221135515"><a name="p14941221135515"></a><a name="p14941221135515"></a>Function</p> 17</th> 18<th class="cellrowborder" valign="top" width="50%" id="mcps1.2.3.1.2"><p id="p8494172116555"><a name="p8494172116555"></a><a name="p8494172116555"></a>Description</p> 19</th> 20</tr> 21</thead> 22<tbody><tr id="row1849482118555"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.1 "><p id="p1414381815720"><a name="p1414381815720"></a><a name="p1414381815720"></a>int GetCommunicationStrategy(RegParams params, PolicyTrans **policies, unsigned int *policyNum)</p> 23</td> 24<td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.2 "><p id="p749582195510"><a name="p749582195510"></a><a name="p749582195510"></a>Obtains the access policies of a service API.</p> 25</td> 26</tr> 27<tr id="row8495521115517"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.1 "><p id="p966319247576"><a name="p966319247576"></a><a name="p966319247576"></a>int IsCommunicationAllowed(AuthParams params)</p> 28</td> 29<td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.2 "><p id="p134951921115511"><a name="p134951921115511"></a><a name="p134951921115511"></a>Checks whether a process has the permission to access an API of another process.</p> 30</td> 31</tr> 32</tbody> 33</table> 34 35## How to Develop<a name="section022611498210"></a> 36 37This section uses BMS as an example to describe how to configure access policies for APIs provided by the IPC authentication component. In this example, the service registered by BMS with Samgr is **bundlems**, and the feature registered for open APIs is **BmsFeature**. 38 391. <a name="li15901515152517"></a>On the OpenHarmony side, configure access policies in the **base/security/permission/services/permission\_lite/ipc\_auth/include/policy\_preset.h** file. On the device side, configure access policies in the **vendor/hisilicon/_product name_/hals/security/permission\_lite/ipc\_auth/include/policy\_preset\_product.h** file. After that, set **POLICY\_PRODUCT** in the header files to **1**. Access policies are classified into the following three types: 40 41 1. **RANGE**: Processes with a specified range of UIDs can access BMS APIs. **uidMin** and **uidMax** must be specified. 42 43 2. **FIXED**: Processes with specified UIDs can access BMS APIs. **fixedUid** must be specified, and a maximum of eight UIDs are allowed. 44 45 3. **BUNDLENAME**: An application with a specified **bundleName** can access BMS APIs. 46 47 ``` 48 FeaturePolicy bmsFeature[] = { 49 { 50 "BmsFeature", 51 { 52 { 53 .type=FIXED, // Processes with specified UIDs can access BMS APIs. 54 .fixedUid={2, 3, 8} 55 }, 56 { 57 .type=RANGE, // Processes with a specified range of UIDs can access BMS APIs. 58 .uidMin=100, 59 .uidMax=__INT_MAX__, 60 }, 61 } 62 }, 63 { 64 "BmsInnerFeature", 65 { 66 { 67 .type=FIXED, // Processes with specified UIDs can access BMS APIs. 68 .fixedUid={2, 3, 8} 69 }, 70 { 71 .type=RANGE, 72 .uidMin=100, 73 .uidMax=999, 74 }, 75 } 76 }, 77 }; 78 ``` 79 802. Add the policies configured for the features in [Step 1](#li15901515152517) to the global policy settings. You need to set the number of features. 81 82 ``` 83 static PolicySetting g_presetPolicies[] = { 84 {"permissionms", pmsFeature, 1}, 85 {"abilityms", amsFeature, 2}, 86 {"bundlems", bmsFeature, 2}, // Add the policies configured for the two features in [Step 1](#li15901515152517) to the global policy settings. 87 {"dtbschedsrv", dmsFeature, 1}, 88 {"samgr", samgrFeature, 1}, 89 {"appspawn", appspawnFeature, 1}, 90 {"WMS", wmsFeature, 1}, 91 {"bundle_daemon", bdsFeature, 1}, 92 }; 93 ``` 94 953. Register the **BmsFeature** defined in [Step 1](#li15901515152517) with Samgr. 96 97 ``` 98 const char BMS_SERVICE[] = "bundlems"; 99 const char BMS_FEATURE[] = "BmsFeature"; 100 static void Init() 101 { 102 SamgrLite *sm = SAMGR_GetInstance(); 103 if (sm == nullptr) { 104 return; 105 } 106 // Register the BmsFeature with Samgr. 107 sm->RegisterFeature(BMS_SERVICE, reinterpret_cast<Feature *>(BundleMsFeature::GetInstance())); 108 sm->RegisterFeatureApi(BMS_SERVICE, BMS_FEATURE, 109 GetBmsFeatureApi(reinterpret_cast<Feature *>(BundleMsFeature::GetInstance()))); 110 HILOG_DEBUG(HILOG_MODULE_APP, "BundleMS feature start success"); 111 } 112 APP_FEATURE_INIT(Init); 113 ``` 114 115 116When you register a service with Samgr, Samgr calls the **GetCommunicationStrategy** function of the IPC authentication component to obtain access policies of the service. When other services or applications access this service through IPC, Samgr calls the **IsCommunicationAllowed** function of the IPC authentication component to check whether the services or applications have the access permission. 117 118## FAQ<a name="section15729104510271"></a> 119 120- Registering a service with Samgr failed 121 122 **Problem** 123 124 During the startup of a new service, a message is displayed indicating that the service fails to be registered with Samgr. 125 126 **Cause** 127 128 The service UID is not configured in the IPC authentication component. 129 130 **Solution** 131 132 Configure a valid UID for the service in the **base/security/permission/services/permission\_lite/ipc\_auth/src/ipc\_auth\_impl.c** file. 133 134 135