source: mainline/uspace/lib/posix/pwd.c@ c53a705

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c53a705 was f215bb5f, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 14 years ago

Various little changes.

  • Property mode set to 100644
File size: 4.0 KB
Line 
1/*
2 * Copyright (c) 2011 Jiri Zarevucky
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup libposix
30 * @{
31 */
32/** @file Password handling.
33 */
34
35#define LIBPOSIX_INTERNAL
36
37#include "pwd.h"
38#include "string.h"
39#include "errno.h"
40
41static bool entry_read = false;
42
43/* dummy user account */
44static const struct posix_passwd dummy_pwd = {
45 .pw_name = (char *) "user",
46 .pw_uid = 0,
47 .pw_gid = 0,
48 .pw_dir = (char *) "/",
49 .pw_shell = (char *) "/app/bdsh"
50};
51
52/**
53 * Since HelenOS doesn't have user accounts, this always returns
54 * the same made-up entry.
55 *
56 * @return
57 */
58struct posix_passwd *posix_getpwent(void)
59{
60 if (entry_read) {
61 return NULL;
62 }
63
64 entry_read = true;
65 return (struct posix_passwd *) &dummy_pwd;
66}
67
68/**
69 * "Rewind the user list".
70 */
71void posix_setpwent(void)
72{
73 entry_read = false;
74}
75
76/**
77 * Ends enumerating and releases all resources. (Noop)
78 */
79void posix_endpwent(void)
80{
81 /* noop */
82}
83
84/**
85 * Find an entry by name.
86 *
87 * @param name Name of the entry.
88 * @return
89 */
90struct posix_passwd *posix_getpwnam(const char *name)
91{
92 assert(name != NULL);
93
94 if (posix_strcmp(name, "user") != 0) {
95 return NULL;
96 }
97
98 return (struct posix_passwd *) &dummy_pwd;
99}
100
101/**
102 * Find an entry by name, thread safely.
103 *
104 * @param name Name of the entry.
105 * @param pwd
106 * @param buffer
107 * @param bufsize
108 * @param result
109 * @return
110 */
111int posix_getpwnam_r(const char *name, struct posix_passwd *pwd,
112 char *buffer, size_t bufsize, struct posix_passwd **result)
113{
114 assert(name != NULL);
115 assert(pwd != NULL);
116 assert(buffer != NULL);
117 assert(result != NULL);
118
119 if (posix_strcmp(name, "user") != 0) {
120 *result = NULL;
121 return 0;
122 }
123
124 return posix_getpwuid_r(0, pwd, buffer, bufsize, result);
125}
126
127/**
128 * Find an entry by UID.
129 *
130 * @param uid UID of the entry.
131 * @return
132 */
133struct posix_passwd *posix_getpwuid(posix_uid_t uid)
134{
135 if (uid != 0) {
136 return NULL;
137 }
138
139 return (struct posix_passwd *) &dummy_pwd;
140}
141
142/**
143 * Find an entry by UID, thread safely.
144 *
145 * @param uid UID of the entry.
146 * @param pwd
147 * @param buffer
148 * @param bufsize
149 * @param result
150 * @return
151 */
152int posix_getpwuid_r(posix_uid_t uid, struct posix_passwd *pwd,
153 char *buffer, size_t bufsize, struct posix_passwd **result)
154{
155 assert(pwd != NULL);
156 assert(buffer != NULL);
157 assert(result != NULL);
158
159 static const char bf[] = { 'u', 's', 'e', 'r', '\0',
160 '/', '\0', 'b', 'd', 's', 'h', '\0' };
161
162 if (uid != 0) {
163 *result = NULL;
164 return 0;
165 }
166 if (bufsize < sizeof(bf)) {
167 *result = NULL;
168 return ERANGE;
169 }
170
171 memcpy(buffer, bf, sizeof(bf));
172
173 pwd->pw_name = (char *) bf;
174 pwd->pw_uid = 0;
175 pwd->pw_gid = 0;
176 pwd->pw_dir = (char *) bf + 5;
177 pwd->pw_shell = (char *) bf + 7;
178 *result = (struct posix_passwd *) pwd;
179
180 return 0;
181}
182
183/** @}
184 */
Note: See TracBrowser for help on using the repository browser.