librostlab  1.0.20
rostlab/cxxpwd.h
Go to the documentation of this file.
00001 /*
00002     Copyright (C) 2011 Laszlo Kajan, Technical University of Munich, Germany
00003 
00004     This file is part of librostlab.
00005 
00006     librostlab is free software: you can redistribute it and/or modify
00007     it under the terms of the GNU Lesser General Public License as published by
00008     the Free Software Foundation, either version 3 of the License, or
00009     (at your option) any later version.
00010 
00011     This program is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014     GNU Lesser General Public License for more details.
00015 
00016     You should have received a copy of the GNU Lesser General Public License
00017     along with this program.  If not, see <http://www.gnu.org/licenses/>.
00018 */
00019 #ifndef ROSTLAB_CXXPWD
00020 #define ROSTLAB_CXXPWD 1
00021 
00022 #include <pwd.h>
00023 #include <string>
00024 #include <sys/types.h>
00025 
00026 #include <rostlab/rostlab_stdexcept.h>
00027 
00028 namespace bo = boost;
00029 
00030 namespace rostlab {
00031 
00032 struct cxx_passwd
00033 {
00034   std::string pw_name;       /* username */
00035   std::string pw_passwd;     /* user password */
00036   uid_t  pw_uid;        /* user ID */
00037   gid_t  pw_gid;        /* group ID */
00038   std::string pw_gecos;      /* real name */
00039   std::string pw_dir;        /* home directory */
00040   std::string pw_shell;      /* shell program */
00041 
00042   inline          cxx_passwd(){};
00043   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 ) :
00044     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) {};
00045 };
00046 
00047 class uid_not_found_error : public runtime_error { public: uid_not_found_error( const std::string& what ) : runtime_error(what) {} };
00048 class uname_not_found_error : public runtime_error { public: uname_not_found_error( const std::string& what ) : runtime_error(what) {} };
00049 
00050 // namespace functions
00051 inline uid_t      getpwnam_r( const std::string& __uname );
00052 inline uid_t      getpwnam_r( const std::string& __uname, cxx_passwd& __passwd );
00053 inline std::string     getpwuid_r( uid_t __uid );
00054 inline std::string     getpwuid_r( uid_t __uid, cxx_passwd& __passwd );
00055 
00056 
00057 
00058 inline std::string     getpwuid_r( uid_t __uid )
00059 {
00060   cxx_passwd      pwd;
00061   return getpwuid_r( __uid, pwd );
00062 }
00063 
00064 
00065 inline std::string     getpwuid_r( uid_t __uid, cxx_passwd& __passwd )
00066 {
00067   long int buflen = sysconf( _SC_GETPW_R_SIZE_MAX );
00068   char buf[buflen];
00069   struct passwd pwbuf;
00070   struct passwd *pwbufp;
00071 
00072   int _errno = getpwuid_r( __uid, &pwbuf, buf, buflen, &pwbufp );
00073 
00074   if( _errno ) throw runtime_error( strerror( _errno ) );
00075 
00076   if( pwbufp == NULL ) throw uid_not_found_error( bo::str( bo::format("uid '%d' not found") % __uid ) );
00077 
00078   __passwd = cxx_passwd( pwbuf.pw_name, pwbuf.pw_passwd, pwbuf.pw_uid, pwbuf.pw_gid, pwbuf.pw_gecos, pwbuf.pw_dir, pwbuf.pw_shell );
00079 
00080   return __passwd.pw_name;
00081 }
00082 
00083 
00084 inline uid_t      getpwnam_r( const std::string& __uname )
00085 {
00086   cxx_passwd      pwd;
00087   return getpwnam_r( __uname, pwd );
00088 }
00089 
00090 
00091 inline uid_t      getpwnam_r( const std::string& __uname, cxx_passwd& __passwd )
00092 {
00093   long int buflen = sysconf( _SC_GETPW_R_SIZE_MAX );
00094   char buf[buflen];
00095   struct passwd pwbuf;
00096   struct passwd *pwbufp;
00097 
00098   int _errno = getpwnam_r( __uname.c_str(), &pwbuf, buf, buflen, &pwbufp );
00099 
00100   if( _errno ) throw runtime_error( strerror( _errno ) );
00101 
00102   if( pwbufp == NULL ) throw uname_not_found_error( bo::str( bo::format("uname '%s' not found") % __uname ) );
00103 
00104   __passwd = cxx_passwd( pwbuf.pw_name, pwbuf.pw_passwd, pwbuf.pw_uid, pwbuf.pw_gid, pwbuf.pw_gecos, pwbuf.pw_dir, pwbuf.pw_shell );
00105 
00106   return pwbuf.pw_uid;
00107 }
00108 
00109 
00110 
00111 
00112 }; // namespace rostlab
00113 
00114 #endif // ROSTLAB_CXXPWD
00115 // vim:et:ai:ts=2:
 All Classes Namespaces Files Functions Variables Typedefs