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