diff --git a/include/pwd.h b/include/pwd.h new file mode 100644 index 00000000000..bd919bc4db6 --- /dev/null +++ b/include/pwd.h @@ -0,0 +1,90 @@ +/**************************************************************************** + * include/pwd.h + * + * Copyright (C) 2019 Gregory Nutt. All rights reserved. + * Author: Michael Jung + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +#ifndef __INCLUDE_PWD_H +#define __INCLUDE_PWD_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#include + +#include + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/**************************************************************************** + * Public Type Definitions + ****************************************************************************/ + +struct passwd +{ + FAR char *pw_name; + uid_t pw_uid; + gid_t pw_gid; + FAR char *pw_dir; + FAR char *pw_shell; +}; + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +#undef EXTERN +#if defined(__cplusplus) +#define EXTERN extern "C" +extern "C" +{ +#else +#define EXTERN extern +#endif + +FAR struct passwd *getpwnam(FAR const char *name); +FAR struct passwd *getpwuid(uid_t uid); +int getpwnam_r(FAR const char *name, FAR struct passwd *pwd, FAR char *buf, + size_t buflen, FAR struct passwd **result); +int getpwuid_r(uid_t uid, FAR struct passwd *pwd, FAR char *buf, + size_t buflen, FAR struct passwd **result); + +#undef EXTERN +#if defined(__cplusplus) +} +#endif + +#endif /* __INCLUDE_PWD_H */ diff --git a/libs/libc/Makefile b/libs/libc/Makefile index 2cc61c68674..db1bcaf5859 100644 --- a/libs/libc/Makefile +++ b/libs/libc/Makefile @@ -72,6 +72,7 @@ include modlib/Make.defs include net/Make.defs include netdb/Make.defs include pthread/Make.defs +include pwd/Make.defs include queue/Make.defs include sched/Make.defs include semaphore/Make.defs diff --git a/libs/libc/README.txt b/libs/libc/README.txt index f7db05e64ac..e09a876deb1 100644 --- a/libs/libc/README.txt +++ b/libs/libc/README.txt @@ -43,6 +43,7 @@ we have: modlib - Part of module and shared library logic: nuttx/lib/modlib.h net - Various network-related header files: netinet/ether.h, arpa/inet.h pthread - pthread.h + pwd - pwd.h queue - queue.h sched - sched.h semaphore - semaphore.h diff --git a/libs/libc/pwd/Make.defs b/libs/libc/pwd/Make.defs new file mode 100644 index 00000000000..0cc22c688bc --- /dev/null +++ b/libs/libc/pwd/Make.defs @@ -0,0 +1,44 @@ +############################################################################ +# libs/libc/pwd/Make.defs +# +# Copyright (C) 2011-2012, 2019 Gregory Nutt. All rights reserved. +# Author: Gregory Nutt +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in +# the documentation and/or other materials provided with the +# distribution. +# 3. Neither the name NuttX nor the names of its contributors may be +# used to endorse or promote products derived from this software +# without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. +# +############################################################################ + +# Add the pwd C files to the build + +CSRCS += lib_getpwnam.c lib_getpwnamr.c lib_getpwuid.c lib_getpwuidr.c +CSRCS += lib_pwd.c + +# Add the pwd directory to the build + +DEPPATH += --dep-path pwd +VPATH += :pwd diff --git a/libs/libc/pwd/lib_getpwnam.c b/libs/libc/pwd/lib_getpwnam.c new file mode 100644 index 00000000000..21de0510a11 --- /dev/null +++ b/libs/libc/pwd/lib_getpwnam.c @@ -0,0 +1,78 @@ +/**************************************************************************** + * libs/libc/pwd/lib_getpwnam.c + * + * Copyright (C) 2019 Gregory Nutt. All rights reserved. + * Author: Michael Jung + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include + +#include "pwd/lib_pwd.h" + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: getpwnam + * + * Description: + * The getpwnam() function searches the user database for an entry with a + * matching name. + * + * Input Parameters: + * name - The user name to return a passwd structure for. + * + * Returned Value: + * A pointer to a statically allocated passwd structure, or NULL if no + * matching entry is found or an error occurs. Applications wishing to + * check for error situations should set errno to 0 before calling + * getpwnam(). If getpwnam() returns a null pointer and errno is set to + * non-zero, an error occurred. + * + ****************************************************************************/ + +FAR struct passwd *getpwnam(FAR const char *name) +{ + if (strcmp(name, ROOT_NAME)) + { + return NULL; + } + + return getpwbuf(ROOT_UID, ROOT_GID, ROOT_NAME, ROOT_DIR, ROOT_SHELL); +} diff --git a/libs/libc/pwd/lib_getpwnamr.c b/libs/libc/pwd/lib_getpwnamr.c new file mode 100644 index 00000000000..74b43245079 --- /dev/null +++ b/libs/libc/pwd/lib_getpwnamr.c @@ -0,0 +1,88 @@ +/**************************************************************************** + * libs/libc/pwd/lib_getpwnamr.c + * + * Copyright (C) 2019 Gregory Nutt. All rights reserved. + * Author: Michael Jung + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include + +#include "pwd/lib_pwd.h" + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: getpwnam_r + * + * Description: + * The getpwnam_r() function searches the user database for an entry with a + * matching name and stores the retrieved passwd structure in the space + * pointed to by pwd. + * + * Input Parameters: + * name - The name to return a passwd structure for. + * pwd - Pointer to the space to store the retrieved passwd structure in. + * buf - The string fields pointed to by the passwd struct are stored here. + * buflen - The length of buf in bytes. + * result - Pointer to the resulting passwd struct, or NULL in case of fail. + * + * Returned Value: + * On success getpwnam_r returns 0 and sets *result to pwd. If no match + * is found, 0 is returned and *result is set to NULL. In case of failure + * an error number is returned. + * + ****************************************************************************/ + +int getpwnam_r(FAR const char *name, FAR struct passwd *pwd, FAR char *buf, + size_t buflen, FAR struct passwd **result) +{ + if (strcmp(name, ROOT_NAME)) + { + /* The only known user is 'root', which has a uid of 0. Thus, report + * back that no match was found. + */ + + *result = NULL; + return 0; + } + + return getpwbuf_r(ROOT_UID, ROOT_GID, ROOT_NAME, ROOT_DIR, ROOT_SHELL, pwd, + buf, buflen, result); +} diff --git a/libs/libc/pwd/lib_getpwuid.c b/libs/libc/pwd/lib_getpwuid.c new file mode 100644 index 00000000000..79aa396675b --- /dev/null +++ b/libs/libc/pwd/lib_getpwuid.c @@ -0,0 +1,77 @@ +/**************************************************************************** + * libs/libc/pwd/lib_getpwuid.c + * + * Copyright (C) 2019 Gregory Nutt. All rights reserved. + * Author: Michael Jung + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include + +#include "pwd/lib_pwd.h" + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: getpwuid + * + * Description: + * The getpwuid() function searches the user database for an entry with a + * matching uid. + * + * Input Parameters: + * uid - The uid to return a passwd structure for. + * + * Returned Value: + * A pointer to a statically allocated passwd structure, or NULL if no + * matching entry is found or an error occurs. Applications wishing to + * check for error situations should set errno to 0 before calling + * getpwuid(). If getpwuid() returns a null pointer and errno is set to + * non-zero, an error occurred. + * + ****************************************************************************/ + +FAR struct passwd *getpwuid(uid_t uid) +{ + if (uid != ROOT_UID) + { + return NULL; + } + + return getpwbuf(ROOT_UID, ROOT_GID, ROOT_NAME, ROOT_DIR, ROOT_SHELL); +} diff --git a/libs/libc/pwd/lib_getpwuidr.c b/libs/libc/pwd/lib_getpwuidr.c new file mode 100644 index 00000000000..81abcaced08 --- /dev/null +++ b/libs/libc/pwd/lib_getpwuidr.c @@ -0,0 +1,87 @@ +/**************************************************************************** + * libs/libc/pwd/lib_getpwuidr.c + * + * Copyright (C) 2019 Gregory Nutt. All rights reserved. + * Author: Michael Jung + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include + +#include "pwd/lib_pwd.h" + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: getpwuid_r + * + * Description: + * The getpwuid_r() function searches the user database for an entry with a + * matching uid and stores the retrieved passwd structure in the space + * pointed to by pwd. + * + * Input Parameters: + * uid - The uid to return a passwd structure for. + * pwd - Pointer to the space to store the retrieved passwd structure in. + * buf - The string fields pointed to by the passwd struct are stored here. + * buflen - The length of buf in bytes. + * result - Pointer to the resulting passwd struct, or NULL in case of fail. + * + * Returned Value: + * On success getpwuid_r returns 0 and sets *result to pwd. If no match + * is found, 0 is returned and *result is set to NULL. In case of failure + * an error number is returned. + * + ****************************************************************************/ + +int getpwuid_r(uid_t uid, FAR struct passwd *pwd, FAR char *buf, + size_t buflen, FAR struct passwd **result) +{ + if (uid != ROOT_UID) + { + /* The only known user is 'root', which has a uid of 0. Thus, report + * back that no match was found. + */ + + *result = NULL; + return 0; + } + + return getpwbuf_r(ROOT_UID, ROOT_GID, ROOT_NAME, ROOT_DIR, ROOT_SHELL, pwd, + buf, buflen, result); +} diff --git a/libs/libc/pwd/lib_pwd.c b/libs/libc/pwd/lib_pwd.c new file mode 100644 index 00000000000..05da8ab93c8 --- /dev/null +++ b/libs/libc/pwd/lib_pwd.c @@ -0,0 +1,184 @@ +/**************************************************************************** + * libs/libc/pwd/lib_pwd.c + * + * Copyright (C) 2019 Gregory Nutt. All rights reserved. + * Author: Michael Jung + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include +#include +#include + +#include "pwd/lib_pwd.h" + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static FAR char *g_buf; +static FAR struct passwd *g_pwd; + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: getpwbuf + * + * Description: + * libc/grp internal helper function for getpwgid and getpwnam to allocate + * and setup a passwd structure once a matching entry has been found. + * + * Input Parameters: + * uid - Value to set the passwd structure's pw_uid field to. + * gid - Value to set the passwd structure's pw_gid field to. + * name - Value to set the passwd structure's pw_name field to. + * dir - Value to set the passwd structure's pw_dir field to. + * shell - Value to set the passwd structure's pw_shell field to. + * + * Returned Value: + * A pointer to a statically allocated passwd structure, or NULL if an + * error occurs, in which case errno is set appropriately. + * + ****************************************************************************/ + +FAR struct passwd *getpwbuf(uid_t uid, gid_t gid, FAR const char *name, + FAR const char *dir, FAR const char *shell) +{ + FAR struct passwd *result; + FAR char *newbuf; + size_t buflen; + int err; + + buflen = strlen(name) + 1 + strlen(dir) + 1 + strlen(shell) + 1; + + newbuf = (FAR char *)realloc(g_buf, buflen); + + if (!newbuf) + { + err = ENOMEM; + goto error; + } + + g_buf = newbuf; + + if (!g_pwd) + { + g_pwd = (FAR struct passwd *)malloc(sizeof(struct passwd)); + } + + if (!g_pwd) + { + err = ENOMEM; + goto error; + } + + err = getpwbuf_r(uid, gid, name, dir, shell, g_pwd, g_buf, buflen, &result); + + if (err) + { + goto error; + } + + return result; + +error: + free(g_pwd); + free(g_buf); + g_pwd = NULL; + g_buf = NULL; + set_errno(err); + + return NULL; +} + +/**************************************************************************** + * Name: getpwbuf_r + * + * Description: + * libc/grp internal helper function for getpwuid_r and getpwnam_r to setup + * the caller supplied 'pwd' and 'buf' buffers once a matching entry has + * been found. + * + * Input Parameters: + * uid - Value to set the passwd structure's pw_uid field to. + * gid - Value to set the passwd structure's pw_gid field to. + * name - Value to set the passwd structure's pw_name field to. + * dir - Value to set the passwd structure's pw_dir field to. + * shell - Value to set the passwd structure's pw_shell field to. + * pwd - Pointer to the space to store the retrieved passwd structure in. + * buf - String fields pointed to by the passwd struct are stored here. + * buflen - The length of buf in bytes. + * result - Pointer to the resulting passwd struct, or NULL in case of fail. + * + * Returned Value: + * On success getpwgid_r returns 0 and sets *result to pwd. In case of + * failure an error number is returned. + * + ****************************************************************************/ + +int getpwbuf_r(uid_t uid, gid_t gid, FAR const char *name, + FAR const char *dir, FAR const char *shell, + FAR struct passwd *pwd, FAR char *buf, size_t buflen, + FAR struct passwd **result) +{ + size_t reqdlen; + + reqdlen = strlen(name) + 1 + strlen(dir) + 1 + strlen(shell) + 1; + + if (buflen < reqdlen) + { + /* Insufficient buffer space supplied. */ + + *result = NULL; + return ERANGE; + } + + pwd->pw_name = buf; + pwd->pw_dir = &buf[strlen(name) + 1]; + pwd->pw_shell = &buf[strlen(name) + 1 + strlen(dir) + 1]; + + pwd->pw_uid = uid; + pwd->pw_gid = gid; + strcpy(pwd->pw_name, name); + strcpy(pwd->pw_dir, dir); + strcpy(pwd->pw_shell, shell); + + *result = pwd; + return 0; +} diff --git a/libs/libc/pwd/lib_pwd.h b/libs/libc/pwd/lib_pwd.h new file mode 100644 index 00000000000..5c1915f2514 --- /dev/null +++ b/libs/libc/pwd/lib_pwd.h @@ -0,0 +1,85 @@ +/**************************************************************************** + * libs/libc/pwd/lib_pwd.h + * + * Copyright (C) 2019 Gregory Nutt. All rights reserved. + * Author: Michael Jung + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +#ifndef __LIBS_LIBC_PWD_LIB_PWD_H +#define __LIBS_LIBC_PWD_LIB_PWD_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define ROOT_NAME "root" +#define ROOT_UID 0 +#define ROOT_GID 0 +#define ROOT_DIR "/root" +#define ROOT_SHELL "/bin/nsh" + +/**************************************************************************** + * Public Data + ****************************************************************************/ + +#ifdef __cplusplus +#define EXTERN extern "C" +extern "C" +{ +#else +#define EXTERN extern +#endif + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +FAR struct passwd *getpwbuf(uid_t uid, gid_t gid, FAR const char *name, + FAR const char *dir, FAR const char *shell); +int getpwbuf_r(uid_t uid, gid_t gid, FAR const char *name, + FAR const char *dir, FAR const char *shell, + FAR struct passwd *pwd, FAR char *buf, size_t buflen, + FAR struct passwd **result); + +#undef EXTERN +#ifdef __cplusplus +} +#endif + +#endif /* __LIBS_LIBC_PWD_LIB_PWD_H */