[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 | */
|
---|
| 32 | /** @file
|
---|
| 33 | */
|
---|
| 34 |
|
---|
| 35 | #define LIBPOSIX_INTERNAL
|
---|
| 36 | #include "pwd.h"
|
---|
| 37 | #include "string.h"
|
---|
| 38 | #include "errno.h"
|
---|
| 39 |
|
---|
| 40 |
|
---|
| 41 | static bool entry_read = false;
|
---|
| 42 |
|
---|
| 43 | /* dummy user account */
|
---|
| 44 | static const struct posix_passwd dummy_pwd = {
|
---|
| 45 | .pw_name = (char *) "user",
|
---|
| 46 | .pw_uid = 1,
|
---|
| 47 | .pw_gid = 1,
|
---|
| 48 | .pw_dir = (char *) "/",
|
---|
[f232189] | 49 | .pw_shell = (char *) "/app/bdsh"
|
---|
[90c5eb6] | 50 | };
|
---|
| 51 |
|
---|
| 52 | /**
|
---|
| 53 | * Since HelenOS doesn't have user accounts, this always returns
|
---|
| 54 | * the same made-up entry.
|
---|
| 55 | */
|
---|
| 56 | struct posix_passwd *posix_getpwent(void)
|
---|
| 57 | {
|
---|
| 58 | if (entry_read) {
|
---|
| 59 | return NULL;
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | entry_read = true;
|
---|
| 63 | return (struct posix_passwd *) &dummy_pwd;
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | /**
|
---|
| 67 | * "Rewind the user list".
|
---|
| 68 | */
|
---|
| 69 | void posix_setpwent(void)
|
---|
| 70 | {
|
---|
| 71 | entry_read = false;
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | /**
|
---|
| 75 | * Ends enumerating and releases all resources. (Noop)
|
---|
| 76 | */
|
---|
| 77 | void posix_endpwent(void)
|
---|
| 78 | {
|
---|
| 79 | /* noop */
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | /**
|
---|
| 83 | * Find an entry by name.
|
---|
| 84 | *
|
---|
| 85 | * @param name Name of the entry.
|
---|
| 86 | * @return
|
---|
| 87 | */
|
---|
| 88 | struct posix_passwd *posix_getpwnam(const char *name)
|
---|
| 89 | {
|
---|
| 90 | assert(name != NULL);
|
---|
| 91 |
|
---|
| 92 | if (posix_strcmp(name, "user") != 0) {
|
---|
| 93 | return NULL;
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | return (struct posix_passwd *) &dummy_pwd;
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | /**
|
---|
| 100 | * Find an entry by name, thread safely.
|
---|
| 101 | *
|
---|
| 102 | * @param name Name of the entry.
|
---|
| 103 | * @param pwd
|
---|
| 104 | * @param buffer
|
---|
| 105 | * @param bufsize
|
---|
| 106 | * @param result
|
---|
| 107 | * @return
|
---|
| 108 | */
|
---|
| 109 | int posix_getpwnam_r(const char *name, struct posix_passwd *pwd,
|
---|
| 110 | char *buffer, size_t bufsize, struct posix_passwd **result)
|
---|
| 111 | {
|
---|
| 112 | assert(name != NULL);
|
---|
| 113 |
|
---|
| 114 | if (posix_strcmp(name, "user") != 0) {
|
---|
| 115 | *result = NULL;
|
---|
| 116 | return 0;
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | return posix_getpwuid_r(1, pwd, buffer, bufsize, result);
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | /**
|
---|
| 123 | * Find an entry by UID.
|
---|
| 124 | *
|
---|
| 125 | * @param uid UID of the entry.
|
---|
| 126 | * @return
|
---|
| 127 | */
|
---|
| 128 | struct posix_passwd *posix_getpwuid(posix_uid_t uid)
|
---|
| 129 | {
|
---|
| 130 | if (uid != 1) {
|
---|
| 131 | return NULL;
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | return (struct posix_passwd *) &dummy_pwd;
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | /**
|
---|
| 138 | * Find an entry by UID, thread safely.
|
---|
| 139 | *
|
---|
| 140 | * @param uid UID of the entry.
|
---|
| 141 | * @param pwd
|
---|
| 142 | * @param buffer
|
---|
| 143 | * @param bufsize
|
---|
| 144 | * @param result
|
---|
| 145 | * @return
|
---|
| 146 | */
|
---|
| 147 | int posix_getpwuid_r(posix_uid_t uid, struct posix_passwd *pwd,
|
---|
| 148 | char *buffer, size_t bufsize, struct posix_passwd **result)
|
---|
| 149 | {
|
---|
| 150 | assert(pwd != NULL);
|
---|
| 151 | assert(buffer != NULL);
|
---|
| 152 | assert(result != NULL);
|
---|
| 153 |
|
---|
| 154 | static const char bf[] = { 'u', 's', 'e', 'r', '\0',
|
---|
| 155 | '/', '\0', 'b', 'd', 's', 'h', '\0' };
|
---|
| 156 |
|
---|
| 157 | if (uid != 1) {
|
---|
| 158 | *result = NULL;
|
---|
| 159 | return 0;
|
---|
| 160 | }
|
---|
| 161 | if (bufsize < sizeof(bf)) {
|
---|
| 162 | *result = NULL;
|
---|
| 163 | return ERANGE;
|
---|
| 164 | }
|
---|
| 165 |
|
---|
| 166 | memcpy(buffer, bf, sizeof(bf));
|
---|
| 167 |
|
---|
| 168 | pwd->pw_name = (char *) bf;
|
---|
| 169 | pwd->pw_uid = 1;
|
---|
| 170 | pwd->pw_gid = 1;
|
---|
| 171 | pwd->pw_dir = (char *) bf + 5;
|
---|
| 172 | pwd->pw_shell = (char *) bf + 7;
|
---|
| 173 | *result = (struct posix_passwd *) pwd;
|
---|
| 174 |
|
---|
| 175 | return 0;
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | /** @}
|
---|
| 179 | */
|
---|
| 180 |
|
---|