1/*
2 * Copyright (c) 2011, 2012, 2013, ETH Zurich.
3 * All rights reserved.
4 *
5 * This file is distributed under the terms in the attached LICENSE file.
6 * If you do not find this file, copies can be found by writing to:
7 * ETH Zurich D-INFK, Universitaetstrasse 6, CH-8092 Zurich. Attn: Systems Group.
8 */
9
10#include <unistd.h>
11#include <pwd.h>
12#include <sys/types.h>
13#include <assert.h>
14#include <stdio.h>
15#include "posixcompat.h"
16#include "userdb.h"
17
18static struct passwd *dummyuser = &userdb[0];
19
20uid_t geteuid(void)
21{
22    POSIXCOMPAT_DEBUG("geteuid(): returning %d\n", dummyuser->pw_uid);
23    return dummyuser->pw_uid;
24}
25
26uid_t getuid(void)
27{
28    POSIXCOMPAT_DEBUG("getuid(): returning %d\n", dummyuser->pw_uid);
29    return dummyuser->pw_uid;
30}
31
32struct passwd *getpwuid(uid_t uid)
33{
34    struct passwd* user = NULL;
35
36    setpwent();
37    while ((user = getpwent()) != NULL) {
38        if (user->pw_uid == uid) {
39            POSIXCOMPAT_DEBUG("getpwuid(%d): returning user \"%s\"\n",
40                              uid, user->pw_name);
41            return user;
42        }
43    }
44    endpwent();
45
46    // No matching user found
47    POSIXCOMPAT_DEBUG("getpwuid(%d): no user found\n", uid);
48    return NULL;
49}
50
51/**
52 * \brief Get the effective group ID.
53 */
54gid_t getegid(void)
55{
56    POSIXCOMPAT_DEBUG("getegid(): returning %d\n", dummyuser->pw_gid);
57    return dummyuser->pw_gid;
58}
59
60/**
61 * \brief Get the real group ID.
62 */
63gid_t getgid(void)
64{
65    POSIXCOMPAT_DEBUG("getgid(): returning %d\n", dummyuser->pw_gid);
66    return dummyuser->pw_gid;
67}
68
69/**
70 * \brief Set the effective group ID.
71 */
72int setegid(gid_t gid)
73{
74    assert(!"NYI");
75    return -1;
76}
77
78/**
79 * \brief Set-group-ID.
80 */
81int setgid(gid_t gid)
82{
83    assert(!"NYI");
84    return -1;
85}
86
87struct passwd *getpwnam(const char *name)
88{
89    struct passwd* user = NULL;
90
91    setpwent();
92    while ((user = getpwent()) != NULL) {
93        if (strcmp(user->pw_name, name) == 0) {
94            POSIXCOMPAT_DEBUG("getpwnam(%s): returning user \"%d\"\n",
95                              name, user->pw_uid);
96            return user;
97        }
98    }
99    endpwent();
100
101    // No matching user found
102    POSIXCOMPAT_DEBUG("getpwnam(%s): no user found\n", name);
103    return NULL;
104}
105
106/**
107 * \brief Set effective user ID.
108 */
109int seteuid(uid_t uid)
110{
111    POSIXCOMPAT_DEBUG("seteuid(%d): nothing changed", uid);
112    return 0;
113}
114
115/**
116 * \brief Set user ID.
117 */
118int setuid(uid_t uid)
119{
120    POSIXCOMPAT_DEBUG("setuid(%d): nothing changed", uid);
121    return 0;
122}
123