Direct-BT v3.3.0-1-gc2d430c
Direct-BT - Direct Bluetooth Programming.
user_info.hpp
Go to the documentation of this file.
1/**
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2024 Gothel Software e.K.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24#pragma once
25
26#include <cstdint>
27#include <cassert>
28#include <cinttypes>
29#include <cstring>
30#include <string>
31
32namespace jau::os {
33
34 /** \addtogroup OSSup
35 *
36 * @{
37 */
38
39 /**
40 * User account information of the underlying OS.
41 */
42 class UserInfo {
43 public:
44 typedef uint64_t id_t;
45 private:
46 bool m_valid;
47 id_t m_uid;
48 id_t m_gid;
49 std::string m_username;
50 std::string m_homedir;
51 std::string m_shell;
52 std::vector<id_t> m_gid_list;
53
54 static bool set_groups(const std::vector<id_t>& list) noexcept;
55 static bool set_effective_gid(id_t group_id) noexcept;
56 static bool set_effective_uid(id_t user_id) noexcept;
57
58 static bool get_groups(std::vector<id_t>& list) noexcept;
59 static bool get_env_uid(id_t& res_uid, const bool try_sudo) noexcept;
60 static bool get_env_username(std::string& username, const bool try_sudo) noexcept;
61
62 /** get creds by passed res_uid (updated) */
63 static bool get_creds(id_t& res_uid, id_t& res_gid, std::string& username, std::string& homedir, std::string& shell) noexcept;
64 /** get creds by passed username */
65 static bool get_creds(const std::string& username_lookup, id_t& res_uid, id_t& res_gid, std::string& username, std::string& homedir, std::string& shell) noexcept;
66
67 public:
68 /** Create instance of the user executing this application. */
69 UserInfo() noexcept;
70
71 /** Create instance of the given user id. */
72 UserInfo(id_t uid) noexcept;
73
74 /** Create instance of the given user name. */
75 UserInfo(const std::string& username) noexcept;
76
77 bool isValid() const noexcept { return m_valid; }
78 id_t uid() const noexcept { return m_uid; }
79 id_t gid() const noexcept { return m_gid; }
80 const std::string& username() const noexcept { return m_username; }
81 const std::string& homedir() const noexcept { return m_homedir; }
82 const std::string& shell() const noexcept { return m_shell; }
83 const std::vector<id_t>& groups() const noexcept { return m_gid_list; }
84
85 std::string toString() const noexcept {
86 std::string s = "UserInfo['";
87 if( m_valid ) {
88 s.append(username()).append("', uid ").append(std::to_string(uid())).append(", gid ").append(std::to_string(gid()))
89 .append(", home '").append(homedir()).append("', shell '").append(shell())
90 .append("', groups [").append(jau::to_string(groups())).append("]]");
91 } else {
92 s.append("undef]");
93 }
94 return s;
95 }
96 };
97
98 /**@}*/
99
100}
User account information of the underlying OS.
Definition: user_info.hpp:42
const std::string & homedir() const noexcept
Definition: user_info.hpp:81
id_t gid() const noexcept
Definition: user_info.hpp:79
id_t uid() const noexcept
Definition: user_info.hpp:78
const std::string & shell() const noexcept
Definition: user_info.hpp:82
const std::string & username() const noexcept
Definition: user_info.hpp:80
bool isValid() const noexcept
Definition: user_info.hpp:77
UserInfo() noexcept
Create instance of the user executing this application.
Definition: user_info.cpp:191
const std::vector< id_t > & groups() const noexcept
Definition: user_info.hpp:83
std::string toString() const noexcept
Definition: user_info.hpp:85
std::string to_string(const endian_t v) noexcept
Return std::string representation of the given endian.
std::string to_string(const alphabet &v) noexcept
Definition: base_codec.hpp:97
Author: Sven Gothel sgothel@jausoft.com Copyright (c) 2024 Gothel Software e.K.
Definition: dyn_linker.hpp:37
STL namespace.