blob: aeadd1865ac7118dd4bcfd82fdaa50e80d04616d [file] [log] [blame]
Oscar Montemayor05231562009-11-30 08:40:57 -08001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20#include <sys/mount.h>
21#include <sys/stat.h>
22#include <sys/types.h>
23#include <unistd.h>
24
25#include "efs_migration.h"
26#include "cutils/misc.h"
27#include "cutils/properties.h"
28#include "common.h"
29#include "mtdutils/mtdutils.h"
30#include "mtdutils/mounts.h"
31#include "roots.h"
32
33const char* efs_enabled_property = "persist.security.efs.enabled";
34const char* efs_transition_property = "persist.security.efs.trans";
35const char* efs_property_dir = "/data/property/";
36
37void get_property_file_name(char *buffer, const char *property_name) {
38 sprintf(buffer, "%s%s", efs_property_dir, property_name);
39}
40
41int get_text_file_contents(char *buffer, int buf_size, char *file_name) {
42 FILE *in_file;
43 char *read_data;
44
45 in_file = fopen(file_name, "r");
46 if (in_file == NULL) {
47 LOGE("Encrypted FS: error accessing properties.");
48 return EFS_ERROR;
49 }
50
51 read_data = fgets(buffer, buf_size, in_file);
52 if (read_data == NULL) {
53 // Error or unexpected data
54 fclose(in_file);
55 LOGE("Encrypted FS: error accessing properties.");
56 return EFS_ERROR;
57 }
58
59 fclose(in_file);
60 return EFS_OK;
61}
62
63int set_text_file_contents(char *buffer, char *file_name) {
64 FILE *out_file;
65 int result;
66
67 out_file = fopen(file_name, "w");
68 if (out_file == NULL) {
69 LOGE("Encrypted FS: error setting up properties.");
70 return EFS_ERROR;
71 }
72
73 result = fputs(buffer, out_file);
74 if (result != 0) {
75 // Error or unexpected data
76 fclose(out_file);
77 LOGE("Encrypted FS: error setting up properties.");
78 return EFS_ERROR;
79 }
80
81 fflush(out_file);
82 fclose(out_file);
83 return EFS_OK;
84}
85
86
87int read_efs_boolean_property(const char *prop_name, int *value) {
88 char prop_file_name[PROPERTY_KEY_MAX + 32];
89 char prop_value[PROPERTY_VALUE_MAX];
90 int result;
91
92 get_property_file_name(prop_file_name, prop_name);
93 result = get_text_file_contents(prop_value, PROPERTY_VALUE_MAX, prop_file_name);
94
95 if (result < 0) {
96 return result;
97 }
98
99 if (strncmp(prop_value, "1", 1) == 0) {
100 *value = 1;
101 } else if (strncmp(prop_value, "0", 1) == 0) {
102 *value = 0;
103 } else {
104 LOGE("Encrypted FS: error accessing properties.");
105 return EFS_ERROR;
106 }
107
108 return EFS_OK;
109}
110
111int write_efs_boolean_property(const char *prop_name, int value) {
112 char prop_file_name[PROPERTY_KEY_MAX + 32];
113 char prop_value[PROPERTY_VALUE_MAX];
114 int result;
115
116 get_property_file_name(prop_file_name, prop_name);
117
118 // Create the directory if needed
119 mkdir(efs_property_dir, 0755);
120 if (value == 1) {
121 result = set_text_file_contents("1", prop_file_name);
122 } else if (value == 0) {
123 result = set_text_file_contents("0", prop_file_name);
124 } else {
125 return EFS_ERROR;
126 }
127 if (result < 0) {
128 return result;
129 }
130
131 return EFS_OK;
132}
133
134int read_encrypted_fs_info(encrypted_fs_info *efs_data) {
135 int result;
136 int value;
137 result = ensure_root_path_mounted("DATA:");
138 if (result != 0) {
139 LOGE("Encrypted FS: error mounting userdata partition.");
140 return EFS_ERROR;
141 }
142
143 // STOPSHIP: Read the EFS key from a file (TBD later)
144 // Future code goes here...
145
146 result = ensure_root_path_unmounted("DATA:");
147 if (result != 0) {
148 LOGE("Encrypted FS: error unmounting data partition.");
149 return EFS_ERROR;
150 }
151
152 return EFS_OK;
153}
154
155int restore_encrypted_fs_info(encrypted_fs_info *efs_data) {
156 int result;
157 result = ensure_root_path_mounted("DATA:");
158 if (result != 0) {
159 LOGE("Encrypted FS: error mounting userdata partition.");
160 return EFS_ERROR;
161 }
162
163 // Set the EFS properties to their respective values
164 result = write_efs_boolean_property(efs_enabled_property, efs_data->encrypted_fs_mode);
165 if (result != 0) {
166 return result;
167 }
168
169 // Signal "transition" of Encrypted File System settings
170 result = write_efs_boolean_property(efs_transition_property, 1);
171 if (result != 0) {
172 return result;
173 }
174
175 result = ensure_root_path_unmounted("DATA:");
176 if (result != 0) {
177 LOGE("Encrypted FS: error unmounting data partition.");
178 return EFS_ERROR;
179 }
180
181 return EFS_OK;
182}
183