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