summaryrefslogtreecommitdiff
path: root/notification-ex/shared_file.cc
blob: 9ed7effe8f8c99ddc61706740a73d95ad83df7b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
/*
 * Copyright (c) 2019 Samsung Electronics Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <unistd.h>
#include <dlog.h>
#include <linux/xattr.h>
#include <sys/smack.h>
#include <gio/gio.h>
#include <glib/gstdio.h>
#include <utime.h>
#include <tzplatform_config.h>
#include <security-manager.h>

#include <string>
#include <list>
#include <map>
#include <algorithm>
#include <vector>

#include "notification-ex/common.h"
#include "notification-ex/shared_file.h"
#include "notification-ex/group_item.h"

#ifdef LOG_TAG
#undef LOG_TAG
#endif

#define LOG_TAG "NOTIFICATION_EX"
#define CHECK_LABEL "::RO"
#define NOTI_PRIV_DATA_DIR "data/.notification_ex"

using namespace std;

namespace notification {
namespace item {
SharedFile::SharedFile() = default;
SharedFile::~SharedFile() = default;
SharedFile::SharingData::SharingData() = default;
SharedFile::SharingData::~SharingData() = default;
SharedFile::SharingTarget::SharingTarget() = default;
SharedFile::SharingTarget::~SharingTarget() = default;

const char* SharedFile::GetLastIndex(const char* path, const char* search) const {
  int i;
  int search_len;
  const char* index;

  if (path == nullptr || search == nullptr)
    return nullptr;

  search_len = strlen(search);
  index = path + strlen(path) - search_len;

  while (index >= path) {
    for (i = 0; i < search_len; i++) {
      if (index[i] != search[i])
        break;
    }

    if (i == search_len)
      return index;

    index--;
  }

  return nullptr;
}

bool SharedFile::MakeDir(const char* path) {
  if (access(path, R_OK) == 0)
    return true;

  auto noti_dir = unique_ptr<GFile, decltype(&g_object_unref)>(
                        g_file_new_for_path(path), g_object_unref);
  if (noti_dir == nullptr)
    return false;

  GError* g_err = nullptr;
  if (!g_file_make_directory(noti_dir.get(), nullptr, &g_err)) {
    if (g_err) {
      LOGE("Failed to make sharing dir[%s]", g_err->message);
      g_error_free(g_err);
    }
    return false;
  }

  return true;
}

string SharedFile::GetDir(string path) {
  int dir_len;
  const char* index;
  string dir;

  if (path.empty())
    return "";

  index = GetLastIndex(path.c_str(), "/");
  if (index == nullptr) {
    LOGE("Failed to find directory separator");
    return "";
  }

  dir_len = index - path.c_str() + 1;
  if (dir_len <= 0 || dir_len > PATH_MAX)
    return "";

  dir.reserve(PATH_MAX);
  snprintf(&dir[0], dir_len, "%s", path.c_str());

  return dir;
}

int SharedFile::CopyFile(const char* source, const char* dest) {
  GError* g_err = nullptr;
  struct utimbuf ut = {0, };

  if (source == nullptr || dest == nullptr) {
    LOGE("Invalid parameter");
    return ERROR_INVALID_PARAMETER;
  }

  auto dst = unique_ptr<GFile, decltype(&g_object_unref)>(
                g_file_new_for_path(dest), g_object_unref);
  if (dst == nullptr) {
    LOGE("dest path is wrong [%s]", dest);
    return ERROR_IO_ERROR;
  }

  if (g_file_query_exists(dst.get(), nullptr)) {
    LOGD("dst path already existed [%s]", dest);
    return  ERROR_ALREADY_EXIST_ID;
  }

  auto src = unique_ptr<GFile, decltype(&g_object_unref)>(
                g_file_new_for_path(source), g_object_unref);
  if (src == nullptr) {
    LOGE("src path is wrong [%s]", source);
    return ERROR_IO_ERROR;
  }

  if (!g_file_copy(src.get(), dst.get(), G_FILE_COPY_NOFOLLOW_SYMLINKS, nullptr,
                nullptr, nullptr, &g_err)) {
    if (g_err) {
      LOGE("Copying file from [%s] to [%s] is failed [%s]", source, dest,
          g_err->message);
      g_error_free(g_err);
    }
    return ERROR_IO_ERROR;
  }

  ut.modtime = time(nullptr);
  if (g_utime(dest, &ut) != 0)
    LOGD("Failed to set g_utime %d ", errno);

  return ERROR_NONE;
}

vector<char*> SharedFile::ConvertListToArray(const list<string>& data) {
  vector<char*> v;

  if (data.empty())
    return {};

  for (const auto& i : data)
    v.push_back(const_cast<char*>(i.c_str()));

  return v;
}

bool SharedFile::IsPrivatePath(string path) const {
  char* smack_label = nullptr;
  bool ret = false;

  if (path.empty()) {
    LOGE("Invalid parameter");
    return false;
  }

  if (smack_new_label_from_path(path.c_str(), XATTR_NAME_SMACK, 1, &smack_label)
        <= 0) {
    LOGE("smack_new_label_from_path failed");
    return false;
  }

  if (GetLastIndex(smack_label, CHECK_LABEL) != nullptr)
    ret = true;

  free(smack_label);
  return ret;
}

string SharedFile::GetDataPath(string app_id, string path) const {
  string user_app;
  string dir;

  if (app_id.empty() || path.empty()) {
    LOGE("Invalid parameter");
    return nullptr;
  }

  tzplatform_set_user(getuid());
  user_app = tzplatform_getenv(TZ_USER_APP);
  tzplatform_reset_user();

  dir = user_app + "/" + app_id + "/" + NOTI_PRIV_DATA_DIR + "/";
  dir += string(GetLastIndex(path.c_str(), "/") + 1);

  return dir;
}

int SharedFile::SetSharingData(SharingData sharing_data,
    list<string> new_shared_file_list, list<string> new_receiver_group_list,
    multimap<string, string> receiver_group_map) {
  private_sharing_req* handle = nullptr;
  vector<char*> path_array;
  vector<char*> target_array;
  const char* appid = nullptr;
  int ret;

  if (new_shared_file_list.empty() || new_receiver_group_list.empty()) {
    LOGE("Invalid parameter");
    return ERROR_INVALID_PARAMETER;
  }

  ret = security_manager_private_sharing_req_new(&handle);
  if (ret != SECURITY_MANAGER_SUCCESS) {
    LOGE("Failed to create private sharing request handle[%d]", ret);
    return ERROR_IO_ERROR;
  }

  auto req = unique_ptr<private_sharing_req,
                decltype(&security_manager_private_sharing_req_free)>(
                handle, security_manager_private_sharing_req_free);
  if (req == nullptr)
    return ERROR_IO_ERROR;

  appid = strdup(sharing_data.app_id.c_str());
  if (appid == nullptr) {
    LOGE("Failed to get Sender appid");
    return ERROR_OUT_OF_MEMORY;
  }

  ret = security_manager_private_sharing_req_set_owner_appid(req.get(), appid);
  if (ret != SECURITY_MANAGER_SUCCESS) {
    LOGE("Failed to set owner appid[%s][%d]", appid, ret);
    return ERROR_IO_ERROR;
  }

  path_array = ConvertListToArray(new_shared_file_list);
  if (path_array.empty()) {
    LOGE("path_array is null");
    return ERROR_IO_ERROR;
  }

  ret = security_manager_private_sharing_req_add_paths(req.get(),
            const_cast<const char**>(path_array.data()), path_array.size());
  if (ret != SECURITY_MANAGER_SUCCESS) {
    LOGE("Failed to add paths [%d]", ret);
    return ERROR_IO_ERROR;
  }

  target_array = ConvertListToArray(new_receiver_group_list);
  if (target_array.empty()) {
    LOGE("target_array is null");
    return ERROR_IO_ERROR;
  }

  for (int i = 0; i < static_cast<int>(target_array.size()); i++) {
    multimap<string, string>::iterator it;
    for (it = receiver_group_map.begin(); it != receiver_group_map.end(); it++) {
      if (it->first.compare(target_array[i]) == 0) {
        ret = security_manager_private_sharing_req_set_target_appid(req.get(),
                it->second.c_str());
        if (ret != SECURITY_MANAGER_SUCCESS) {
          LOGE("Failed to set target appid [%s]", appid);
          return ERROR_IO_ERROR;
        }

        ret = security_manager_private_sharing_apply(req.get());
        if (ret != SECURITY_MANAGER_SUCCESS) {
          LOGE("Failed to apply private sharing [%d]", ret);
          return ERROR_IO_ERROR;
        }
      }
    }
  }

  return ERROR_NONE;
}

int SharedFile::UnsetSharingData(SharingData sharing_data,
    multimap<string, string> receiver_group_map) {
  private_sharing_req* handle = nullptr;
  vector<char*> path_array;
  vector<char*> target_array;
  const char* appid = nullptr;
  int ret;

  if (receiver_group_map.empty()) {
    LOGE("Invalid parameter");
    return ERROR_INVALID_PARAMETER;
  }

  ret = security_manager_private_sharing_req_new(&handle);
  if (ret != SECURITY_MANAGER_SUCCESS) {
    LOGE("Failed to create private sharing request handle[%d]", ret);
    return ERROR_IO_ERROR;
  }

  auto req = unique_ptr<private_sharing_req,
                decltype(&security_manager_private_sharing_req_free)>(
                handle, security_manager_private_sharing_req_free);
  if (req == nullptr)
    return ERROR_IO_ERROR;

  appid = strdup(sharing_data.app_id.c_str());
  if (appid == nullptr) {
    LOGE("Failed to get Sender appid");
    return ERROR_OUT_OF_MEMORY;
  }

  ret = security_manager_private_sharing_req_set_owner_appid(req.get(), appid);
  if (ret != SECURITY_MANAGER_SUCCESS) {
    LOGE("Failed to set owner appid[%s][%d]", appid, ret);
    return ERROR_IO_ERROR;
  }

  path_array = ConvertListToArray(sharing_data.shared_file_list);
  if (path_array.empty()) {
    LOGE("path_array is null");
    return ERROR_IO_ERROR;
  }

  ret = security_manager_private_sharing_req_add_paths(req.get(),
            const_cast<const char**>(path_array.data()), path_array.size());
  if (ret != SECURITY_MANAGER_SUCCESS) {
    LOGE("Failed to add paths [%d]", ret);
    return ERROR_IO_ERROR;
  }

  target_array = ConvertListToArray(sharing_data.receiver_group_list);
  if (target_array.empty()) {
    LOGE("target_array is null");
    return ERROR_IO_ERROR;
  }

  for (int i = 0; i < static_cast<int>(target_array.size()); i++) {
    multimap<string, string>::iterator it;
    for (it = receiver_group_map.begin(); it != receiver_group_map.end(); it++) {
      if (it->first.compare(target_array[i]) == 0) {
        ret = security_manager_private_sharing_req_set_target_appid(req.get(),
                it->second.c_str());
        if (ret != SECURITY_MANAGER_SUCCESS) {
          LOGE("Failed to set target appid [%s]", appid);
          return ERROR_IO_ERROR;
        }

        ret = security_manager_private_sharing_drop(req.get());
        if (ret != SECURITY_MANAGER_SUCCESS) {
          LOGE("Failed to drop private sharing [%d]", ret);
          return ERROR_IO_ERROR;
        }
      }
    }
  }

  return ERROR_NONE;
}

SharedFile::SharingData SharedFile::FindSharingData(string appid) {
  if (sharing_data_list_.size() == 0 || appid.empty())
    return {};

  for (auto sharing_data : sharing_data_list_) {
    if (sharing_data.app_id.compare(appid) == 0)
      return sharing_data;
  }

  return {};
}

int SharedFile::SetPrivateSharing(list<shared_ptr<AbstractItem>> item,
    multimap<string, string> receiver_group_map) {
  int ret;

  if (item.empty() || receiver_group_map.empty()) {
    LOGE("Invalid parameter");
    return ERROR_INVALID_PARAMETER;
  }

  for (auto& i : item) {
    SharingData sharing_data;
    list<string> new_shared_file_list;
    list<string> new_receiver_group_list;

    list<string> shared_path_list = i->GetSharedPath();
    if (shared_path_list.empty())
      continue;

    string appid = i->GetSenderAppId();
    if (appid.empty())
      continue;

    sharing_data = FindSharingData(appid);
    if (!sharing_data.app_id.empty()) {
      string noti_id = i->GetId();
      list<string>::iterator it;
      it = find_if(sharing_data.noti_id_list.begin(),
                    sharing_data.noti_id_list.end(),
                    [&noti_id](std::string id){
                      return id.compare(noti_id) == 0;
                    });
      if (it == sharing_data.noti_id_list.end())
        sharing_data.noti_id_list.push_back(noti_id);

      for (auto& shared_path : shared_path_list) {
        list<string>::iterator it;
        it = find_if(sharing_data.shared_file_list.begin(),
                    sharing_data.shared_file_list.end(),
                    [&shared_path](std::string shared_file){
                      return shared_file.compare(shared_path) == 0;
                    });
        if (it == sharing_data.shared_file_list.end()) {
          sharing_data.shared_file_list.push_back(shared_path);
          new_shared_file_list.push_back(shared_path);
        }
      }

      list<string> receiver_list = i->GetReceiverList();
      for (auto& receiver : receiver_list) {
        list<string>::iterator it;
        it = find_if(sharing_data.receiver_group_list.begin(),
                    sharing_data.receiver_group_list.end(),
                    [&receiver](std::string receiver_group){
                      return receiver_group.compare(receiver) == 0;
                    });
        if (it == sharing_data.receiver_group_list.end()) {
          sharing_data.receiver_group_list.push_back(receiver);
          new_receiver_group_list.push_back(receiver);
        }
      }
    } else {
      sharing_data.app_id = appid;
      sharing_data.dir = GetDir(*(shared_path_list.begin()));
      sharing_data.noti_id_list.emplace_back(i->GetId());
      sharing_data.shared_file_list = shared_path_list;
      sharing_data.receiver_group_list = i->GetReceiverList();
      sharing_data_list_.emplace_back(sharing_data);

      new_shared_file_list = shared_path_list;
      new_receiver_group_list = i->GetReceiverList();
    }

    ret = SetSharingData(sharing_data, new_shared_file_list,
                new_receiver_group_list, receiver_group_map);
    if (ret == ERROR_NONE)
      i->SetSharedPath();
  }

  LOGD("SetPrivateSharing");
  return ERROR_NONE;
}

int SharedFile::UpdatePrivateSharing(shared_ptr<AbstractItem> item,
    multimap<string, string> receiver_group_map) {
  list<string> new_shared_file_list;
  list<string> new_receiver_group_list;
  int ret;

  if (item == nullptr || receiver_group_map.empty()) {
    LOGE("Invalid parameter");
    return ERROR_INVALID_PARAMETER;
  }

  list<string> shared_path_list = item->GetSharedPath();
  if (shared_path_list.empty())
    return ERROR_NONE;

  string appid = item->GetSenderAppId();
  SharingData sharing_data = FindSharingData(appid);
  if (appid.empty() || sharing_data.app_id.empty())
    return ERROR_IO_ERROR;

  for (auto& shared_path : shared_path_list) {
    std::list<std::string>::iterator it;
    it = std::find_if(sharing_data.shared_file_list.begin(),
                    sharing_data.shared_file_list.end(),
                    [&shared_path](std::string shared_file){
                      return shared_file.compare(shared_path) == 0;
                    });
    if (it == sharing_data.shared_file_list.end()) {
      sharing_data.shared_file_list.push_back(shared_path);
      new_shared_file_list.push_back(shared_path);
    }
  }

  new_receiver_group_list = item->GetReceiverList();
  ret = SetSharingData(sharing_data, new_shared_file_list,
            new_receiver_group_list, receiver_group_map);
  if (ret == ERROR_NONE)
    item->SetSharedPath();

  LOGD("UpdatePrivateSharing");
  return ERROR_NONE;
}

int SharedFile::RemovePrivateSharing(shared_ptr<AbstractItem> item,
    multimap<string, string> receiver_group_map) {
  if (item == nullptr || receiver_group_map.empty()) {
    LOGE("Invalid parameter");
    return ERROR_INVALID_PARAMETER;
  }

  string appid = item->GetSenderAppId();
  if (appid.empty())
    return ERROR_IO_ERROR;

  SharingData sharing_data = FindSharingData(appid);
  if (sharing_data.app_id.empty())
    return ERROR_NONE;

  sharing_data.noti_id_list.remove(item->GetId());
  if (sharing_data.noti_id_list.size() == 0) {
    UnsetSharingData(sharing_data, receiver_group_map);

    vector<char*> path_array = ConvertListToArray(sharing_data.shared_file_list);
    if (!path_array.empty()) {
      for (int i = 0; i < static_cast<int>(path_array.size()); i++) {
        if (g_remove(path_array[i]) == -1)
          LOGE("Failed to remove shared_file(%s)", path_array[i]);
      }
    }
    g_rmdir(sharing_data.dir.c_str());
  }

  LOGD("RemovePrivateSharing");
  return ERROR_NONE;
}

int SharedFile::CopyPrivateFile(shared_ptr<item::AbstractItem> item) {
  if (item == nullptr) {
    LOGE("Invalid paramenter");
    return ERROR_INVALID_PARAMETER;
  }

  list<map<string, string>> path_map_list = item->GetPathMapList();
  if (path_map_list.empty())
    return ERROR_NONE;

  if (!MakeDir(GetDir((*(path_map_list.begin())).begin()->second).c_str()))
    return ERROR_IO_ERROR;

  list<map<string, string>>::iterator list_it;
  for (list_it = path_map_list.begin(); list_it != path_map_list.end(); ++list_it) {
    map<string, string>::iterator map_it;
    for (map_it = (*list_it).begin(); map_it != (*list_it).end(); map_it++) {
      CopyFile((map_it->first).c_str(), (map_it->second).c_str());
    }
  }

  return ERROR_NONE;
}

}  // namespace item
}  // namespace notification