librostlab 1.0.20
Loading...
Searching...
No Matches
cxxpwd.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2011 Laszlo Kajan, Technical University of Munich, Germany
3
4 This file is part of librostlab.
5
6 librostlab is free software: you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
18*/
19#ifndef ROSTLAB_CXXPWD
20#define ROSTLAB_CXXPWD 1
21
22#include <pwd.h>
23#include <string>
24#include <sys/types.h>
25
27
28namespace bo = boost;
29
30namespace rostlab {
31
33{
34 std::string pw_name; /* username */
35 std::string pw_passwd; /* user password */
36 uid_t pw_uid; /* user ID */
37 gid_t pw_gid; /* group ID */
38 std::string pw_gecos; /* real name */
39 std::string pw_dir; /* home directory */
40 std::string pw_shell; /* shell program */
41
42 inline cxx_passwd(){};
43 inline cxx_passwd( const std::string& __pw_name, const std::string& __pw_passwd, uid_t __pw_uid, gid_t __pw_gid, const std::string& __pw_gecos, const std::string& __pw_dir, const std::string& __pw_shell ) :
44 pw_name(__pw_name), pw_passwd(__pw_passwd), pw_uid(__pw_uid), pw_gid(__pw_gid), pw_gecos(__pw_gecos), pw_dir(__pw_dir), pw_shell(__pw_shell) {};
45};
46
47class uid_not_found_error : public runtime_error { public: uid_not_found_error( const std::string& what ) : runtime_error(what) {} };
48class uname_not_found_error : public runtime_error { public: uname_not_found_error( const std::string& what ) : runtime_error(what) {} };
49
50// namespace functions
51inline uid_t getpwnam_r( const std::string& __uname );
52inline uid_t getpwnam_r( const std::string& __uname, cxx_passwd& __passwd );
53inline std::string getpwuid_r( uid_t __uid );
54inline std::string getpwuid_r( uid_t __uid, cxx_passwd& __passwd );
55
56
57
58inline std::string getpwuid_r( uid_t __uid )
59{
60 cxx_passwd pwd;
61 return getpwuid_r( __uid, pwd );
62}
63
64
65inline std::string getpwuid_r( uid_t __uid, cxx_passwd& __passwd )
66{
67 long int buflen = sysconf( _SC_GETPW_R_SIZE_MAX );
68 char buf[buflen];
69 struct passwd pwbuf;
70 struct passwd *pwbufp;
71
72 int _errno = getpwuid_r( __uid, &pwbuf, buf, buflen, &pwbufp );
73
74 if( _errno ) throw runtime_error( strerror( _errno ) );
75
76 if( pwbufp == NULL ) throw uid_not_found_error( bo::str( bo::format("uid '%d' not found") % __uid ) );
77
78 __passwd = cxx_passwd( pwbuf.pw_name, pwbuf.pw_passwd, pwbuf.pw_uid, pwbuf.pw_gid, pwbuf.pw_gecos, pwbuf.pw_dir, pwbuf.pw_shell );
79
80 return __passwd.pw_name;
81}
82
83
84inline uid_t getpwnam_r( const std::string& __uname )
85{
86 cxx_passwd pwd;
87 return getpwnam_r( __uname, pwd );
88}
89
90
91inline uid_t getpwnam_r( const std::string& __uname, cxx_passwd& __passwd )
92{
93 long int buflen = sysconf( _SC_GETPW_R_SIZE_MAX );
94 char buf[buflen];
95 struct passwd pwbuf;
96 struct passwd *pwbufp;
97
98 int _errno = getpwnam_r( __uname.c_str(), &pwbuf, buf, buflen, &pwbufp );
99
100 if( _errno ) throw runtime_error( strerror( _errno ) );
101
102 if( pwbufp == NULL ) throw uname_not_found_error( bo::str( bo::format("uname '%s' not found") % __uname ) );
103
104 __passwd = cxx_passwd( pwbuf.pw_name, pwbuf.pw_passwd, pwbuf.pw_uid, pwbuf.pw_gid, pwbuf.pw_gecos, pwbuf.pw_dir, pwbuf.pw_shell );
105
106 return pwbuf.pw_uid;
107}
108
109
110
111
112}; // namespace rostlab
113
114#endif // ROSTLAB_CXXPWD
115// vim:et:ai:ts=2:
uid_not_found_error(const std::string &what)
Definition cxxpwd.h:47
uname_not_found_error(const std::string &what)
Definition cxxpwd.h:48
std::string getpwuid_r(uid_t __uid)
Definition cxxpwd.h:58
uid_t getpwnam_r(const std::string &__uname)
Definition cxxpwd.h:84
std::string pw_shell
Definition cxxpwd.h:40
std::string pw_gecos
Definition cxxpwd.h:38
cxx_passwd(const std::string &__pw_name, const std::string &__pw_passwd, uid_t __pw_uid, gid_t __pw_gid, const std::string &__pw_gecos, const std::string &__pw_dir, const std::string &__pw_shell)
Definition cxxpwd.h:43
std::string pw_passwd
Definition cxxpwd.h:35
std::string pw_name
Definition cxxpwd.h:34
std::string pw_dir
Definition cxxpwd.h:39