1 /*
2 * Copyright 2008, Ingo Weinhold, ingo_weinhold@gmx.de. All rights reserved.
3 * Distributed under the terms of the MIT License.
4 */
5 #ifndef _LIBROOT_USER_GROUP_COMMON_H
6 #define _LIBROOT_USER_GROUP_COMMON_H
7
8 #include <grp.h>
9 #include <pwd.h>
10 #include <shadow.h>
11
12 #include <OS.h>
13
14
15 #define MAX_PASSWD_NAME_LEN (32)
16 #define MAX_PASSWD_PASSWORD_LEN (32)
17 #define MAX_PASSWD_REAL_NAME_LEN (128)
18 #define MAX_PASSWD_HOME_DIR_LEN (B_PATH_NAME_LENGTH)
19 #define MAX_PASSWD_SHELL_LEN (B_PATH_NAME_LENGTH)
20
21 #define MAX_PASSWD_BUFFER_SIZE ( \
22 MAX_PASSWD_NAME_LEN \
23 + MAX_PASSWD_PASSWORD_LEN \
24 + MAX_PASSWD_REAL_NAME_LEN \
25 + MAX_PASSWD_HOME_DIR_LEN \
26 + MAX_PASSWD_SHELL_LEN)
27
28 #define MAX_GROUP_NAME_LEN (32)
29 #define MAX_GROUP_PASSWORD_LEN (32)
30 #define MAX_GROUP_MEMBER_COUNT (32)
31
32 #define MAX_GROUP_BUFFER_SIZE ( \
33 MAX_GROUP_NAME_LEN \
34 + MAX_GROUP_PASSWORD_LEN \
35 + ((MAX_GROUP_MEMBER_COUNT + 1) * sizeof(char*)))
36 // MAX_GROUP_NAME_LEN and MAX_GROUP_PASSWORD_LEN are char* aligned
37
38
39 #define MAX_SHADOW_PWD_NAME_LEN (32)
40 #define MAX_SHADOW_PWD_PASSWORD_LEN (137)
41
42 #define MAX_SHADOW_PWD_BUFFER_SIZE ( \
43 MAX_SHADOW_PWD_NAME_LEN \
44 + MAX_SHADOW_PWD_PASSWORD_LEN)
45
46
47 #ifdef __cplusplus
48
49 #include <AutoLocker.h>
50
51
52 namespace BPrivate {
53
54 class KMessage;
55 class Tokenizer;
56
57 extern const char* kPasswdFile;
58 extern const char* kGroupFile;
59 extern const char* kShadowPwdFile;
60
61
62 // locking
63
64 status_t user_group_lock();
65 status_t user_group_unlock();
66
67
68 class UserGroupLocking {
69 public:
Lock(int *)70 inline bool Lock(int*)
71 {
72 return user_group_lock() == B_OK;
73 }
74
Unlock(int *)75 inline void Unlock(int*)
76 {
77 user_group_unlock();
78 }
79 };
80
81
82 class UserGroupLocker : public AutoLocker<int, UserGroupLocking> {
83 public:
UserGroupLocker()84 UserGroupLocker()
85 : AutoLocker<int, UserGroupLocking>((int*)1)
86 {
87 }
88 };
89
90
91 port_id get_registrar_authentication_port();
92 void set_registrar_authentication_port(port_id port);
93 status_t send_authentication_request_to_registrar(KMessage& request,
94 KMessage& reply);
95
96
97 template<typename Type>
98 static inline Type*
relocate_pointer(addr_t baseAddress,Type * & address)99 relocate_pointer(addr_t baseAddress, Type*& address)
100 {
101 return address = (Type*)(baseAddress + (addr_t)address);
102 }
103
104
105 // passwd
106
107 status_t copy_passwd_to_buffer(const char* name, const char* password, uid_t uid,
108 gid_t gid, const char* home, const char* shell, const char* realName,
109 passwd* entry, char* buffer, size_t bufferSize);
110 status_t copy_passwd_to_buffer(const passwd* from, passwd* entry, char* buffer,
111 size_t bufferSize);
112
113 status_t parse_passwd_line(char* line, char*& name, char*& password, uid_t& uid,
114 gid_t& gid, char*& home, char*& shell, char*& realName);
115
116
117 // group
118
119 status_t copy_group_to_buffer(const char* name, const char* password, gid_t gid,
120 const char* const* members, int memberCount, group* entry, char* buffer,
121 size_t bufferSize);
122 status_t copy_group_to_buffer(const group* from, group* entry, char* buffer,
123 size_t bufferSize);
124
125 status_t parse_group_line(char* line, char*& name, char*& password, gid_t& gid,
126 char** members, int& memberCount);
127
128
129 // shadow password
130
131 status_t copy_shadow_pwd_to_buffer(const char* name, const char* password,
132 int lastChanged, int min, int max, int warn, int inactive, int expiration,
133 int flags, spwd* entry, char* buffer, size_t bufferSize);
134 status_t copy_shadow_pwd_to_buffer(const spwd* from, spwd* entry,
135 char* buffer, size_t bufferSize);
136
137 status_t parse_shadow_pwd_line(char* line, char*& name, char*& password,
138 int& lastChanged, int& min, int& max, int& warn, int& inactive,
139 int& expiration, int& flags);
140
141
142 } // namespace BPrivate
143
144
145 #endif // __cplusplus
146
147 #endif // _LIBROOT_USER_GROUP_COMMON_H
148