]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- |
2 | // vim: ts=8 sw=2 smarttab | |
3 | /* | |
4 | * Ceph - scalable distributed file system | |
5 | * | |
6 | * Copyright (C) 2016 Red Hat | |
7 | * | |
8 | * This is free software; you can redistribute it and/or | |
9 | * modify it under the terms of the GNU Lesser General Public | |
10 | * License version 2.1, as published by the Free Software | |
11 | * Foundation. See file COPYING. | |
12 | * | |
13 | */ | |
14 | ||
15 | #ifndef CEPH_CLIENT_USERPERM_H | |
16 | #define CEPH_CLIENT_USERPERM_H | |
17 | ||
18 | struct UserPerm | |
19 | { | |
20 | private: | |
21 | uid_t m_uid; | |
22 | gid_t m_gid; | |
23 | int gid_count; | |
24 | gid_t *gids; | |
25 | bool alloced_gids; | |
26 | void deep_copy_from(const UserPerm& b) { | |
27 | if (alloced_gids) { | |
28 | delete[] gids; | |
29 | alloced_gids = false; | |
30 | } | |
31 | m_uid = b.m_uid; | |
32 | m_gid = b.m_gid; | |
33 | gid_count = b.gid_count; | |
28e407b8 | 34 | if (gid_count > 0) { |
7c673cae FG |
35 | gids = new gid_t[gid_count]; |
36 | alloced_gids = true; | |
37 | for (int i = 0; i < gid_count; ++i) { | |
38 | gids[i] = b.gids[i]; | |
39 | } | |
40 | } | |
41 | } | |
42 | public: | |
43 | UserPerm() : m_uid(-1), m_gid(-1), gid_count(0), | |
44 | gids(NULL), alloced_gids(false) {} | |
45 | UserPerm(uid_t uid, gid_t gid, int ngids=0, gid_t *gidlist=NULL) : | |
46 | m_uid(uid), m_gid(gid), gid_count(ngids), | |
47 | gids(gidlist), alloced_gids(false) {} | |
48 | UserPerm(const UserPerm& o) : UserPerm() { | |
49 | deep_copy_from(o); | |
50 | } | |
51 | UserPerm(UserPerm && o) { | |
52 | m_uid = o.m_uid; | |
53 | m_gid = o.m_gid; | |
54 | gid_count = o.gid_count; | |
55 | gids = o.gids; | |
56 | alloced_gids = o.alloced_gids; | |
57 | o.gids = NULL; | |
58 | o.gid_count = 0; | |
59 | } | |
60 | ~UserPerm() { | |
61 | if (alloced_gids) | |
62 | delete[] gids; | |
63 | } | |
64 | UserPerm& operator=(const UserPerm o) { | |
65 | deep_copy_from(o); | |
66 | return *this; | |
67 | } | |
68 | ||
69 | uid_t uid() const { return m_uid != (uid_t)-1 ? m_uid : ::geteuid(); } | |
70 | gid_t gid() const { return m_gid != (gid_t)-1 ? m_gid : ::getegid(); } | |
71 | bool gid_in_groups(gid_t id) const { | |
72 | if (id == gid()) return true; | |
73 | for (int i = 0; i < gid_count; ++i) { | |
74 | if (id == gids[i]) return true; | |
75 | } | |
76 | return false; | |
77 | } | |
78 | int get_gids(const gid_t **_gids) const { *_gids = gids; return gid_count; } | |
79 | void init_gids(gid_t* _gids, int count) { | |
80 | gids = _gids; | |
81 | gid_count = count; | |
28e407b8 | 82 | alloced_gids = true; |
7c673cae | 83 | } |
7c673cae FG |
84 | void shallow_copy(const UserPerm& o) { |
85 | m_uid = o.m_uid; | |
86 | m_gid = o.m_gid; | |
87 | gid_count = o.gid_count; | |
88 | gids = o.gids; | |
89 | alloced_gids = false; | |
90 | } | |
91 | }; | |
92 | ||
93 | #endif |