From e70b7c569ba13a68aba1c2b127811e61ac88a902 Mon Sep 17 00:00:00 2001 From: Alexis211 Date: Wed, 21 Oct 2009 19:11:53 +0200 Subject: Started working on user managment --- Source/Kernel/UserManager/Group.class.h | 22 ++++++++ Source/Kernel/UserManager/User.class.h | 67 +++++++++++++++++++++++ Source/Kernel/UserManager/Usr.ns.cpp | 95 +++++++++++++++++++++++++++++++++ Source/Kernel/UserManager/Usr.ns.h | 23 ++++++++ 4 files changed, 207 insertions(+) create mode 100644 Source/Kernel/UserManager/Group.class.h create mode 100644 Source/Kernel/UserManager/User.class.h create mode 100644 Source/Kernel/UserManager/Usr.ns.cpp create mode 100644 Source/Kernel/UserManager/Usr.ns.h (limited to 'Source/Kernel/UserManager') diff --git a/Source/Kernel/UserManager/Group.class.h b/Source/Kernel/UserManager/Group.class.h new file mode 100644 index 0000000..e515058 --- /dev/null +++ b/Source/Kernel/UserManager/Group.class.h @@ -0,0 +1,22 @@ +#ifndef DEF_GROUP_CLASS_H +#define DEF_GROUP_CLASS_H + +#include + +class Group { + friend void Usr::load(); + + private: + Group(String name, u32int gid) : m_name(name), m_gid(gid) {} + + String m_name; + u32int m_gid; + + public: + void setName(String wat) { m_name = wat; Usr::save(); } + + String getName() { return m_name; } + u32int getGid() { return m_gid; } +}; + +#endif diff --git a/Source/Kernel/UserManager/User.class.h b/Source/Kernel/UserManager/User.class.h new file mode 100644 index 0000000..1ad55f4 --- /dev/null +++ b/Source/Kernel/UserManager/User.class.h @@ -0,0 +1,67 @@ +#ifndef DEF_USER_CLASS_H +#define DEF_USER_CLASS_H + +#include +#include + +class User { + friend void Usr::load(); + + private: + String m_username, m_completeName; + u32int m_uid; + Group* m_group; + Vector m_extraGroups; + + User(String username, String completeName, Group* group, String extragroups, u32int uid) + : m_username(username), m_completeName(completeName), m_uid(uid), m_group(group) { + Vector eg = extragroups.split(","); + for (u32int i = 0; i < eg.size(); i++) { + Group* g = Usr::group(eg[i].toInt()); + if (g != 0) m_extraGroups.push(g); + } + } + + public: + String getUserName() { return m_username; } + String getCompleteName() { return m_completeName; } + u32int getUid() { return m_uid; } + Group* getGroup() { return m_group; } + bool isInGroup(u32int gid) { + for (u32int i = 0; i < m_extraGroups.size(); i++) + if (m_extraGroups[i]->getGid() == gid) return true; + return false; + } + bool isInGroup(String name) { + for (u32int i = 0; i < m_extraGroups.size(); i++) + if (m_extraGroups[i]->getName() == name) return true; + return false; + } + bool isInGroup(Group* g) { + for (u32int i = 0; i < m_extraGroups.size(); i++) + if (m_extraGroups[i] == g) return true; + return false; + } + + String getGroups() { + String ret; + for (u32int i = 0; i < m_extraGroups.size(); i++) { + if (!ret.empty()) ret += ","; + ret += String::number(m_extraGroups[i]->getGid()); + } + return ret; + } + + void setUserName(String wat) { m_username = wat; Usr::save(); } + void setCompleteName(String wat) { m_completeName = wat; Usr::save(); } + void setGroup(Group* group) { m_group = group; Usr::save(); } + void addGroup(u32int gid) { + Group* g = Usr::group(gid); + if (g != 0 and !isInGroup(g)) { + m_extraGroups.push(g); + } + Usr::save(); + } +}; + +#endif diff --git a/Source/Kernel/UserManager/Usr.ns.cpp b/Source/Kernel/UserManager/Usr.ns.cpp new file mode 100644 index 0000000..a551344 --- /dev/null +++ b/Source/Kernel/UserManager/Usr.ns.cpp @@ -0,0 +1,95 @@ +#include "Usr.ns.h" + +#include +#include +#include +#include + +/* + * Syntax for Users and Groups configuration files : one entry per line + * syntax for Users : :::,: + * syntax for Groups : : + */ + +namespace Usr { + +SimpleList *m_users = 0; +SimpleList *m_groups = 0; + +void load() { + if (m_users != 0) delete m_users; + if (m_groups != 0) delete m_groups; + m_users = 0, m_groups = 0; + + TextFile groups("/System/Configuration/Groups", FM_READ); + while (!groups.eof()) { + String s = groups.readLine(); + Vector data = s.split(":"); + if (data.size() == 2 and !(s[0] == WChar("#"))) { + m_groups = m_groups->cons(Group(data[1], data[0].toInt())); + } + } + + TextFile users("/System/Configuration/Users", FM_READ); + while (!users.eof()) { + String s = users.readLine(); + Vector data = s.split(":"); + if (data.size() == 5 and !(s[0] == WChar("#"))) { + m_users = m_users->cons(User(data[1], data[4], group(data[2].toInt()), data[3], data[0].toInt())); + } + } +} + +void save() { + TextFile groups("/System/Configuration/Groups", FM_TRUNCATE); + for (SimpleList *iter = m_groups; iter != 0; iter = iter->next()) { + groups.write(String::number(iter->v().getGid()) + ":" + iter->v().getName(), true); + } + TextFile users("/System/Configuration/Users", FM_TRUNCATE); + for (SimpleList *iter = m_users; iter != 0; iter = iter->next()) { + users.write(String::number(iter->v().getUid()) + ":" + iter->v().getUserName() + ":" + + String::number(iter->v().getGroup()->getGid()) + ":" + + iter->v().getGroups() + ":" + iter->v().getCompleteName(), true); + } +} + +u32int uid() { + return Task::currProcess()->getUid(); +} + +User* user(u32int uid) { + for (SimpleList *iter = m_users; iter != 0; iter = iter->next()) { + if (iter->v().getUid() == uid) return &iter->v(); + } + return 0; +} + +User* user(String username) { + for (SimpleList *iter = m_users; iter != 0; iter = iter->next()) { + if (iter->v().getUserName() == username) return &iter->v(); + } + return 0; +} + +User* user() { return user(uid()); } + +Group* group(u32int gid) { + for (SimpleList *iter = m_groups; iter != 0; iter = iter->next()) { + if (iter->v().getGid() == gid) return &iter->v(); + } + return 0; +} + +Group* group(String name) { + for (SimpleList *iter = m_groups; iter != 0; iter = iter->next()) { + if (iter->v().getName() == name) return &iter->v(); + } + return 0; +} + +u32int uid(String username) { + User* x = user(username); + return (x != 0 ? x->getUid() : (u32int) - 1); +} + +}; diff --git a/Source/Kernel/UserManager/Usr.ns.h b/Source/Kernel/UserManager/Usr.ns.h new file mode 100644 index 0000000..397b393 --- /dev/null +++ b/Source/Kernel/UserManager/Usr.ns.h @@ -0,0 +1,23 @@ +#ifndef DEF_USR_NS_H +#define DEF_USR_NS_H + +#include +class Group; +class User; + +namespace Usr { + void load(); //Loads users into memory, from /System/Configuration/{Users,Groups} + void save(); //Saves config from mem to filesystem + + u32int uid(); //Returns current processes UID + + User* user(u32int uid); //Returns user from UID + User* user(String username); + User* user(); + Group* group(u32int gid); + Group* group(String name); + + u32int uid(String username); //Returns UID of username +}; + +#endif -- cgit v1.2.3