adbncfs  0.9.1
userInfo.cpp
Go to the documentation of this file.
1 /*
2  * $Id: userInfo.cpp 4 2015-12-04 13:55:56Z wejaeger $
3  *
4  * File: userInfo.cpp
5  * Author: Werner Jaeger
6  *
7  * Created on November 26, 2015, 8:54 PM
8  *
9  * Copyright 2015 Werner Jaeger.
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program. If not, see <http://www.gnu.org/licenses/>.
23  */
24 #include <errno.h>
25 #include <string.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <sys/stat.h>
29 #include "userInfo.h"
30 
31 using namespace std;
32 
44 UserInfo::UserInfo(const char* pcUserInfo)
45 {
46  parseUserInfoString(pcUserInfo);
47 }
48 
94 int UserInfo::access(const int iUid, const int iGid, const unsigned int uiRawMode, const int iMask) const
95 {
96  int iRet((m_iUid == 0 && m_iGid == 0) ? 0 : EACCES); // root has all permissions
97 
98  if (m_iUid == iUid)
99  iRet = testAccesss((uiRawMode & S_IRWXU) >> 6, iMask);
100  else if (belongs2Group(iGid))
101  iRet = testAccesss((uiRawMode & S_IRWXG) >> 3, iMask);
102  else
103  iRet = testAccesss(uiRawMode & S_IRWXO, iMask);
104 
105  return(iRet);
106 }
107 
115 bool UserInfo::belongs2Group(const int iGuid) const
116 {
117  return(m_iGid == iGuid || m_Groups.find(iGuid) != m_Groups.end());
118 }
119 
126 int UserInfo::testAccesss(const int iMode, const int iMask) const
127 {
128  int iTestMode(iMode & iMask);
129  return(iTestMode == iMask ? 0 : EACCES);
130 }
131 
144 void UserInfo::parseUserInfoString(const char* pcUserInfo)
145 {
146  if (pcUserInfo)
147  {
148  const char* pcEqualDelim = "= ";
149  const char* pcUid = "uid";
150  const char* pcGroups = "groups";
151 
152  const int iLen(::strlen(pcUserInfo) + 1);
153 
154  char acTokens[iLen];
155  ::strncpy(acTokens, pcUserInfo, iLen);
156 
157  char* pcSave;
158  char* pch = ::strtok_r(acTokens, pcEqualDelim, &pcSave);
159  if (pch && ::strcmp(pch, pcUid) == 0)
160  {
161  pch = ::strtok_r(NULL, pcEqualDelim, &pcSave);
162  if (pch)
163  {
164  m_iUid = ::atoi(pch);
165  pch = ::strtok_r(NULL, pcEqualDelim, &pcSave);
166  const char* pcGid = "gid";
167  if (pch && ::strcmp(pch, pcGid) == 0)
168  {
169  pch = ::strtok_r(NULL, pcEqualDelim, &pcSave);
170  if (pch)
171  {
172  m_iGid = ::atoi(pch);
173  pch = ::strtok_r(NULL, pcEqualDelim, &pcSave);
174  }
175  }
176  }
177  }
178 
179  if (pch && ::strcmp(pch, pcGroups) == 0)
180  {
181  const char* pcCommaDelim = ",";
182  pch = ::strtok_r(NULL, pcCommaDelim, &pcSave);
183  while (pch != NULL)
184  {
185  m_Groups.insert(::atoi(pch));
186  pch = ::strtok_r(NULL, pcCommaDelim, &pcSave);
187  }
188  }
189  }
190 }
bool belongs2Group(const int iGuid) const
Tests if this user belongs to the specified group,.
Definition: userInfo.cpp:115
int testAccesss(const int iMode, const int iMask) const
Test requested access permissions against the mode;.
Definition: userInfo.cpp:126
int access(const int iUid, const int iGid, const unsigned int uiRawMode, const int iMask) const
This is the similar to the access(2) system call.
Definition: userInfo.cpp:94
void parseUserInfoString(const char *pcUserInfo)
Parse the specified user info string and store the results in the corresponding member variables...
Definition: userInfo.cpp:144
UserInfo()
Definition: userInfo.h:48