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 (128) 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: 70 inline bool Lock(int*) 71 { 72 return user_group_lock() == B_OK; 73 } 74 75 inline void Unlock(int*) 76 { 77 user_group_unlock(); 78 } 79 }; 80 81 82 class UserGroupLocker : public AutoLocker<int, UserGroupLocking> { 83 public: 84 UserGroupLocker() 85 : AutoLocker<int, UserGroupLocking>((int*)1) 86 { 87 } 88 }; 89 90 91 port_id get_registrar_authentication_port(); 92 status_t send_authentication_request_to_registrar(KMessage& request, 93 KMessage& reply); 94 95 96 template<typename Type> 97 static inline Type* 98 relocate_pointer(addr_t baseAddress, Type*& address) 99 { 100 return address = (Type*)(baseAddress + (addr_t)address); 101 } 102 103 104 // passwd 105 106 status_t copy_passwd_to_buffer(const char* name, const char* password, uid_t uid, 107 gid_t gid, const char* home, const char* shell, const char* realName, 108 passwd* entry, char* buffer, size_t bufferSize); 109 status_t copy_passwd_to_buffer(const passwd* from, passwd* entry, char* buffer, 110 size_t bufferSize); 111 112 status_t parse_passwd_line(char* line, char*& name, char*& password, uid_t& uid, 113 gid_t& gid, char*& home, char*& shell, char*& realName); 114 115 116 // group 117 118 status_t copy_group_to_buffer(const char* name, const char* password, gid_t gid, 119 const char* const* members, int memberCount, group* entry, char* buffer, 120 size_t bufferSize); 121 status_t copy_group_to_buffer(const group* from, group* entry, char* buffer, 122 size_t bufferSize); 123 124 status_t parse_group_line(char* line, char*& name, char*& password, gid_t& gid, 125 char** members, int& memberCount); 126 127 128 // shadow password 129 130 status_t copy_shadow_pwd_to_buffer(const char* name, const char* password, 131 int lastChanged, int min, int max, int warn, int inactive, int expiration, 132 int flags, spwd* entry, char* buffer, size_t bufferSize); 133 status_t copy_shadow_pwd_to_buffer(const spwd* from, spwd* entry, 134 char* buffer, size_t bufferSize); 135 136 status_t parse_shadow_pwd_line(char* line, char*& name, char*& password, 137 int& lastChanged, int& min, int& max, int& warn, int& inactive, 138 int& expiration, int& flags); 139 140 141 } // namespace BPrivate 142 143 144 #endif // __cplusplus 145 146 #endif // _LIBROOT_USER_GROUP_COMMON_H 147