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

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

Introduce include/ and source/ directories into libposix

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