source: mainline/uspace/lib/posix/source/pwd.c@ 32b3a12

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 32b3a12 was 32b3a12, checked in by Vojtech Horky <vojtechhorky@…>, 13 years ago

libposix: even less includes from libc

  • Property mode set to 100644
File size: 4.9 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 "posix/stdbool.h"
38#include "posix/pwd.h"
39#include "posix/string.h"
40#include "posix/errno.h"
41#include "posix/assert.h"
42
43static bool entry_read = false;
44
45/* dummy user account */
46static const struct posix_passwd dummy_pwd = {
47 .pw_name = (char *) "user",
48 .pw_uid = 0,
49 .pw_gid = 0,
50 .pw_dir = (char *) "/",
51 .pw_shell = (char *) "/app/bdsh"
52};
53
54/**
55 * Retrieve next broken-down entry from the user database.
56 *
57 * Since HelenOS doesn't have user accounts, this always returns
58 * the same made-up entry.
59 *
60 * @return Next user database entry or NULL if not possible. Since HelenOS
61 * doesn't have user accounts, this always returns the same made-up entry.
62 */
63struct posix_passwd *posix_getpwent(void)
64{
65 if (entry_read) {
66 return NULL;
67 }
68
69 entry_read = true;
70 return (struct posix_passwd *) &dummy_pwd;
71}
72
73/**
74 * Rewind the user list.
75 */
76void posix_setpwent(void)
77{
78 entry_read = false;
79}
80
81/**
82 * Ends enumerating and releases all resources. (Noop)
83 */
84void posix_endpwent(void)
85{
86 /* noop */
87}
88
89/**
90 * Find an entry by name.
91 *
92 * @param name Name of the entry.
93 * @return Either found entry or NULL if no such entry exists.
94 */
95struct posix_passwd *posix_getpwnam(const char *name)
96{
97 assert(name != NULL);
98
99 if (posix_strcmp(name, "user") != 0) {
100 return NULL;
101 }
102
103 return (struct posix_passwd *) &dummy_pwd;
104}
105
106/**
107 * Find an entry by name, thread safely.
108 *
109 * @param name Name of the entry.
110 * @param pwd Original structure.
111 * @param buffer Buffer for the strings referenced from the result structure.
112 * @param bufsize Length of the buffer.
113 * @param result Where to store updated structure.
114 * @return Zero on success (either found or not found, but without an error),
115 * non-zero error number if error occured.
116 */
117int posix_getpwnam_r(const char *name, struct posix_passwd *pwd,
118 char *buffer, size_t bufsize, struct posix_passwd **result)
119{
120 assert(name != NULL);
121 assert(pwd != NULL);
122 assert(buffer != NULL);
123 assert(result != NULL);
124
125 if (posix_strcmp(name, "user") != 0) {
126 *result = NULL;
127 return 0;
128 }
129
130 return posix_getpwuid_r(0, pwd, buffer, bufsize, result);
131}
132
133/**
134 * Find an entry by UID.
135 *
136 * @param uid UID of the entry.
137 * @return Either found entry or NULL if no such entry exists.
138 */
139struct posix_passwd *posix_getpwuid(posix_uid_t uid)
140{
141 if (uid != 0) {
142 return NULL;
143 }
144
145 return (struct posix_passwd *) &dummy_pwd;
146}
147
148/**
149 * Find an entry by UID, thread safely.
150 *
151 * @param uid UID of the entry.
152 * @param pwd Original structure.
153 * @param buffer Buffer for the strings referenced from the result structure.
154 * @param bufsize Length of the buffer.
155 * @param result Where to store updated structure.
156 * @return Zero on success (either found or not found, but without an error),
157 * non-zero error number if error occured.
158 */
159int posix_getpwuid_r(posix_uid_t uid, struct posix_passwd *pwd,
160 char *buffer, size_t bufsize, struct posix_passwd **result)
161{
162 assert(pwd != NULL);
163 assert(buffer != NULL);
164 assert(result != NULL);
165
166 static const char bf[] = { 'u', 's', 'e', 'r', '\0',
167 '/', '\0', 'b', 'd', 's', 'h', '\0' };
168
169 if (uid != 0) {
170 *result = NULL;
171 return 0;
172 }
173 if (bufsize < sizeof(bf)) {
174 *result = NULL;
175 return ERANGE;
176 }
177
178 memcpy(buffer, bf, sizeof(bf));
179
180 pwd->pw_name = (char *) bf;
181 pwd->pw_uid = 0;
182 pwd->pw_gid = 0;
183 pwd->pw_dir = (char *) bf + 5;
184 pwd->pw_shell = (char *) bf + 7;
185 *result = (struct posix_passwd *) pwd;
186
187 return 0;
188}
189
190/** @}
191 */
Note: See TracBrowser for help on using the repository browser.