Usermode - POSIX and C conformance changes
[tpg/acess2.git] / Usermode / Libraries / libposix.so_src / pwd.c
1 /*
2  * Acess2 POSIX Emulation
3  * - By John Hodge (thePowersGang)
4  *
5  * pwd.c
6  * - Password Structure
7  */
8 #include <pwd.h>
9 #include <errno.h>
10 #include <string.h>
11
12 // TODO: Parse something like '/Acess/Conf/Users'
13
14 // === GLOBALS ===
15 static const struct passwd      gPwd_RootInfo = {
16         .pw_name = "root",
17         .pw_uid = 0,
18         .pw_gid = 0,
19         .pw_dir = "/Acess/Root",
20         .pw_shell = "/Acess/Bin/CLIShell",
21         .pw_passwd = "",
22 };
23
24 // === CODE ===
25 struct passwd *getpwnam(const char *name)
26 {
27         static struct passwd    ret_struct;
28         static char     ret_buf[64];
29         struct passwd   *ret_ptr;
30         errno = getpwnam_r(name, &ret_struct, ret_buf, sizeof(ret_buf), &ret_ptr);
31         return ret_ptr;
32 }
33
34 struct passwd *getpwuid(uid_t uid)
35 {
36         static struct passwd    ret_struct;
37         static char     ret_buf[64];
38         struct passwd   *ret_ptr;
39         errno = getpwuid_r(uid, &ret_struct, ret_buf, sizeof(ret_buf), &ret_ptr);
40         return ret_ptr;
41 }
42
43 static int fill_pwd(const struct passwd *tpl, struct passwd *pwd, char *buf, size_t buflen)
44 {
45         size_t  ofs = 0;
46         #define _setstr(field)  do { \
47                 if( ofs + strlen(tpl->field)+1 > buflen ) \
48                         return ERANGE; \
49                 pwd->field = buf + ofs; \
50                 strcpy(pwd->field, tpl->field); \
51         } while(0)
52         _setstr(pw_name);
53         pwd->pw_uid = tpl->pw_uid;
54         pwd->pw_gid = tpl->pw_gid;
55         _setstr(pw_dir);
56         _setstr(pw_shell);
57         _setstr(pw_passwd);
58         return 0;
59 }
60
61 int getpwnam_r(const char *name, struct passwd *pwd, char *buf, size_t buflen, struct passwd **result)
62 {
63         *result = NULL;
64         if( strcmp(name, "root") == 0 ) {
65                 int ret = fill_pwd(&gPwd_RootInfo, pwd, buf, buflen);
66                 if(ret) return ret;
67                 *result = pwd;
68         }
69         return 0;
70 }
71
72 int getpwuid_r(uid_t uid, struct passwd *pwd, char *buf, size_t buflen, struct passwd **result)
73 {
74         *result = NULL;
75         if( uid == 0 ) {
76                 int ret = fill_pwd(&gPwd_RootInfo, pwd, buf, buflen);
77                 if(ret) return ret;
78                 *result = pwd;
79         }
80         return 0;
81 }
82
83 void endpwent(void)
84 {
85 }
86
87 struct passwd *getpwent(void)
88 {
89         return NULL;
90 }
91
92 void setpwent(void)
93 {
94
95 }
96

UCC git Repository :: git.ucc.asn.au