dmlite  0.6
security.h
Go to the documentation of this file.
1 /// @file include/dmlite/cpp/utils/security.h
2 /// @brief Security functionality shared between modules.
3 /// @details This is not a plugin!
4 /// @author Alejandro Álvarez Ayllón <aalvarez@cern.ch>
5 #ifndef DMLITE_CPP_UTILS_SECURITY_H_
6 #define DMLITE_CPP_UTILS_SECURITY_H_
7 
8 #include <stdint.h>
9 #include <sys/stat.h>
10 #include <string>
11 #include <vector>
12 #include "../authn.h"
13 #include "../exceptions.h"
14 
15 namespace dmlite {
16 
17  /// Possible outputs for validateToken
18  enum TokenResult {
19  kTokenOK = 0,
25  };
26 
27  /// ACL Entry
28  struct AclEntry {
29  /// ACL Type possible values
30  static const uint8_t kUserObj = 1;
31  static const uint8_t kUser = 2;
32  static const uint8_t kGroupObj = 3;
33  static const uint8_t kGroup = 4;
34  static const uint8_t kMask = 5;
35  static const uint8_t kOther = 6;
36  static const uint8_t kDefault = 0x20;
37 
38  uint8_t type;
39  uint8_t perm;
40  uint32_t id;
41 
42  // Operators
43  bool operator == (const AclEntry&) const;
44  bool operator != (const AclEntry&) const;
45  bool operator < (const AclEntry&) const;
46  bool operator > (const AclEntry&) const;
47  };
48 
49  struct Acl: public std::vector<AclEntry> {
50  public:
51  Acl() throw ();
52 
53  /// Creates an ACL from a string
54  explicit Acl(const std::string&) throw ();
55 
56  /// Creates a new ACL inheriting from parent.
57  /// @param parent The parent's ACL vector.
58  /// @param uid The current user uid.
59  /// @param gid The current user gid.
60  /// @param cmode The creation mode.
61  /// @param fmode The current file mode. It will be modified to fit the inheritance.
62  Acl(const Acl& parent, uid_t uid, gid_t gid, mode_t cmode, mode_t* fmode) throw ();
63 
64  /// Returns the position if there is an ACL entry with the type 'type'
65  /// -1 otherwise.
66  int has(uint8_t type) const throw ();
67 
68  std::string serialize(void) const throw ();
69  void validate (void) const throw (DmException);
70  };
71 
72  /// Check if the group vector contains the given gid.
73  /// @param groups The GroupInfo vector.
74  /// @param gid The gid to look for.
75  /// @return true if the vector contains the given gid. false otherwise.
76  bool hasGroup(const std::vector<GroupInfo>& groups, gid_t gid);
77 
78  /// Check if a specific user has the demanded rights.
79  /// @note This works using uid and gid, so it will only work with plug-ins that
80  /// provide this metadata (as unsigned!!).
81  /// @param context The security context.
82  /// @param acl The Access Control list.
83  /// @param stat A struct stat which mode will be checked.
84  /// @param mode The mode to be checked.
85  /// @return 0 if the mode is allowed, 1 if not.
86  int checkPermissions(const SecurityContext* context,
87  const Acl& acl, const struct ::stat& stat,
88  mode_t mode);
89 
90  /// Get the VO from a full DN.
91  /// @param mapfile The file that contains the user => group mapping.
92  /// @param dn The DN to parse.
93  /// @return The mapped VO.
94  std::string voFromDn(const std::string& mapfile, const std::string& dn);
95 
96  /// Get the VO from a role.
97  /// @param role The role.
98  /// @return The VO.
99  std::string voFromRole(const std::string& role);
100 
101  /// Get the subject from the certificate.
102  std::string getCertificateSubject(const std::string& path);
103 
104  /// Generate a token.
105  /// @param id A unique ID of the user. May be the DN, the IP...
106  /// @param pfn The PFN we want a token for.
107  /// @param passwd The password to be used.
108  /// @param lifetime Token lifetime.
109  /// @param write If true, this will be a token for write access.
110  std::string generateToken(const std::string& id, const std::string& pfn,
111  const std::string& passwd, time_t lifetime,
112  bool write = false);
113 
114  /// Validate a token. It must have been previously generated by generateToken.
115  /// @param token The token to validate.
116  /// @param id The SAME unique ID used to generate the token.
117  /// @param pfn The that is being accessed.
118  /// @param passwd The password that must be used to generate the token.
119  /// @param write If true, write access will be validated.
120  TokenResult validateToken(const std::string& token, const std::string& id,
121  const std::string& pfn, const std::string& passwd,
122  bool write = false);
123 
124 };
125 
126 #endif // DMLITE_CPP_UTILS_SECURITY_H_
Definition: security.h:20
uint8_t type
Definition: security.h:38
std::string voFromDn(const std::string &mapfile, const std::string &dn)
bool operator>(const AclEntry &) const
uint32_t id
Definition: security.h:40
Definition: security.h:49
std::string getCertificateSubject(const std::string &path)
Get the subject from the certificate.
TokenResult validateToken(const std::string &token, const std::string &id, const std::string &pfn, const std::string &passwd, bool write=false)
static const uint8_t kUser
Definition: security.h:31
Security context. To be created by the Authn.
Definition: authn.h:64
Base exception class.
Definition: exceptions.h:17
static const uint8_t kGroupObj
Definition: security.h:32
Definition: security.h:22
void validate(void) const
bool operator<(const AclEntry &) const
Definition: security.h:21
static const uint8_t kDefault
Definition: security.h:36
static const uint8_t kOther
Definition: security.h:35
int has(uint8_t type) const
Definition: security.h:19
bool hasGroup(const std::vector< GroupInfo > &groups, gid_t gid)
std::string generateToken(const std::string &id, const std::string &pfn, const std::string &passwd, time_t lifetime, bool write=false)
Definition: security.h:24
TokenResult
Possible outputs for validateToken.
Definition: security.h:18
ACL Entry.
Definition: security.h:28
bool operator!=(const AclEntry &) const
uint8_t perm
Definition: security.h:39
std::string voFromRole(const std::string &role)
static const uint8_t kGroup
Definition: security.h:33
std::string serialize(void) const
static const uint8_t kMask
Definition: security.h:34
static const uint8_t kUserObj
ACL Type possible values.
Definition: security.h:30
int checkPermissions(const SecurityContext *context, const Acl &acl, const struct::stat &stat, mode_t mode)
Definition: security.h:23
bool operator==(const AclEntry &) const