07fee11d0c8e27dc893333fcfc1f3edb9eaaa828
[platform/core/security/krate.git] / module / krate-builder.cpp
1 /*
2  *  Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
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 #include <fcntl.h>
17 #include <unistd.h>
18 #include <sys/stat.h>
19 #include <sys/types.h>
20 #include <sys/mount.h>
21
22 #include "krate-builder.h"
23
24 #include <klay/exception.h>
25 #include <klay/filesystem.h>
26
27 KrateBuilder::KrateBuilder(const runtime::User& user, const std::string& manifestPath) :
28         name(user.getName()), uid(user.getUid()), gid(user.getGid())
29 {
30         runtime::File data(manifestPath);
31         if (data.exists()) {
32                 manifest.reset(xml::Parser::parseFile(manifestPath));
33         }
34 }
35
36 KrateBuilder::~KrateBuilder()
37 {
38 }
39
40 void KrateBuilder::bindFilesystemNode(const std::string& source, const std::string& target,
41                                                                          const std::string& type, const std::string& options,
42                                                                          bool create)
43 {
44         if (create) {
45                 runtime::File dir(target);
46                 if (!dir.exists()) {
47                         dir.makeDirectory(true, uid, gid);
48                 }
49         }
50
51         runtime::Mount::mountEntry(source, target, type, options);
52 }
53
54 void KrateBuilder::mountOwnFilesystem()
55 {
56         if (manifest.get()) {
57                 xml::Node::NodeList entries = manifest->evaluate("/manifest/filesystem/entry");
58                 for (const xml::Node& entry : entries) {
59                         bindFilesystemNode(entry.getProp("source"), entry.getProp("target"),
60                                                            entry.getProp("type"), entry.getProp("options"));
61                 }
62         }
63
64         bindFilesystemNode("/home/" + name,
65                                            "/home/" + name + "/.krate/" + name,
66                                            "none", "rw,bind");
67
68         bindFilesystemNode("/home/" + name + "/.krate", "/home",
69                                            "none", "rw,rbind");
70 }