From 1fc73b2d45351f9c38e58fb104af882f71bbff84 Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Sun, 4 Aug 2019 08:54:08 -0600 Subject: [PATCH] apps/examples/uid: Demo used to verify PASSWD and GROUP interfaces. --- examples/uid/.gitignore | 12 ++ examples/uid/Kconfig | 30 +++++ examples/uid/Make.defs | 39 ++++++ examples/uid/Makefile | 59 +++++++++ examples/uid/uid_main.c | 264 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 404 insertions(+) create mode 100644 examples/uid/.gitignore create mode 100644 examples/uid/Kconfig create mode 100644 examples/uid/Make.defs create mode 100644 examples/uid/Makefile create mode 100644 examples/uid/uid_main.c diff --git a/examples/uid/.gitignore b/examples/uid/.gitignore new file mode 100644 index 000000000..caa9bdef2 --- /dev/null +++ b/examples/uid/.gitignore @@ -0,0 +1,12 @@ +/hello +/Make.dep +/.depend +/.built +/*.asm +/*.obj +/*.rel +/*.lst +/*.sym +/*.adb +/*.lib +/*.src diff --git a/examples/uid/Kconfig b/examples/uid/Kconfig new file mode 100644 index 000000000..44ecacc5c --- /dev/null +++ b/examples/uid/Kconfig @@ -0,0 +1,30 @@ +# +# For a description of the syntax of this configuration file, +# see the file kconfig-language.txt in the NuttX tools repository. +# + +config EXAMPLES_UID + tristate "UID/GID example" + default n + ---help--- + Enable the UID/GID example + +if EXAMPLES_UID + +config EXAMPLES_UID_PROGNAME + string "Program name" + default "uid" + depends on BUILD_LOADABLE + ---help--- + This is the name of the program that will be use when the NSH ELF + program is installed. + +config EXAMPLES_UID_PRIORITY + int "UID/GID task priority" + default 100 + +config EXAMPLES_UID_STACKSIZE + int "UID/GID stack size" + default 2048 + +endif diff --git a/examples/uid/Make.defs b/examples/uid/Make.defs new file mode 100644 index 000000000..fccf7d4b0 --- /dev/null +++ b/examples/uid/Make.defs @@ -0,0 +1,39 @@ +############################################################################ +# apps/examples/uid/Make.defs +# Adds selected applications to apps/ build +# +# Copyright (C) 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. +# +############################################################################ + +ifneq ($(CONFIG_EXAMPLES_UID),) +CONFIGURED_APPS += examples/uid +endif diff --git a/examples/uid/Makefile b/examples/uid/Makefile new file mode 100644 index 000000000..5b6c4993c --- /dev/null +++ b/examples/uid/Makefile @@ -0,0 +1,59 @@ +############################################################################ +# apps/examples/uid/Makefile +# +# Copyright (C) 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. +# +############################################################################ + +-include $(TOPDIR)/Make.defs + +# UID/GID built-in application info + +CONFIG_EXAMPLES_UID_PRIORITY ?= SCHED_PRIORITY_DEFAULT +CONFIG_EXAMPLES_UID_STACKSIZE ?= 2048 + +APPNAME = uid + +PRIORITY = $(CONFIG_EXAMPLES_UID_PRIORITY) +STACKSIZE = $(CONFIG_EXAMPLES_UID_STACKSIZE) + +# UID/GID Example + +ASRCS = +CSRCS = +MAINSRC = uid_main.c + +CONFIG_EXAMPLES_UID_PROGNAME ?= uid$(EXEEXT) +PROGNAME = $(CONFIG_EXAMPLES_UID_PROGNAME) + +MODULE = CONFIG_EXAMPLES_UID + +include $(APPDIR)/Application.mk diff --git a/examples/uid/uid_main.c b/examples/uid/uid_main.c new file mode 100644 index 000000000..cdb44424b --- /dev/null +++ b/examples/uid/uid_main.c @@ -0,0 +1,264 @@ +/**************************************************************************** + * examples/uid/uid_main.c + * + * Copyright (C) 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. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include +#include +#include +#include + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * show_usage + ****************************************************************************/ + +static void show_usage(FAR const char *progname, FAR FILE *stream, + int exit_code) +{ + fprintf(stream, "USAGE:\n"); + fprintf(stream, "\t%s -uid - Show user info by ID\n", progname); + fprintf(stream, "\t%s -uname - Show user info by name\n", progname); + fprintf(stream, "\t%s -gid - Show group info by ID\n", progname); + fprintf(stream, "\t%s -gname - Show group info by name\n", progname); + fprintf(stream, "\t%s -h - Show this help info\n", progname); + exit(exit_code); +} + +/**************************************************************************** + * show_pwd + ****************************************************************************/ + +static void show_pwd(FAR struct passwd *pwd) +{ + printf("Name: %s\n", pwd->pw_name); + printf("UID: %d\n", pwd->pw_uid); + printf("GID: %d\n", pwd->pw_gid); + printf("Home: %s\n", pwd->pw_dir); + printf("Shell: %s\n", pwd->pw_shell); +} + +/**************************************************************************** + * show_grp + ****************************************************************************/ + +static void show_grp(FAR struct group *grp) +{ + printf("Name: %s\n", grp->gr_name); + printf("Passwd: %d\n", grp->gr_passwd); + printf("GID: %d\n", grp->gr_gid); + printf("Mem: %s\n", grp->gr_mem); +} + +/**************************************************************************** + * show_user_by_id + ****************************************************************************/ + +static void show_user_by_id(uid_t uid) +{ + FAR struct passwd *result; + struct passwd pwd; + char buffer[80]; + int ret; + + ret = getpwuid_r(uid, &pwd, buffer, 80, &result); + if (ret != 0) + { + fprintf(stderr, "ERPOR: getpwuid_r failed: %d\n", ret); + } + else if (result == NULL) + { + fprintf(stderr, "No such user ID: %d\n", uid); + } + else + { + show_pwd(&pwd); + } +} + +/**************************************************************************** + * show_user_by_name + ****************************************************************************/ + +static void show_user_by_name(FAR const char *uname) +{ + FAR struct passwd *result; + struct passwd pwd; + char buffer[80]; + int ret; + + ret = getpwnam_r(uname, &pwd, buffer, 80, &result); + if (ret != 0) + { + fprintf(stderr, "ERPOR: getpwnam_r failed: %d\n", ret); + } + else if (result == NULL) + { + fprintf(stderr, "No such user name: %s\n", uname); + } + else + { + show_pwd(&pwd); + } +} + +/**************************************************************************** + * show_group_by_id + ****************************************************************************/ + +static void show_group_by_id(gid_t gid) +{ + FAR struct group *result; + struct group grp; + char buffer[80]; + int ret; + + ret = getgrgid_r(gid, &grp, buffer, 80, &result); + if (ret != 0) + { + fprintf(stderr, "ERPOR: getgrgid_r failed: %d\n", ret); + } + else if (result == NULL) + { + fprintf(stderr, "No such group ID: %d\n", gid); + } + else + { + show_grp(&grp); + } +} + +/**************************************************************************** + * show_group_by_name + ****************************************************************************/ + +static void show_group_by_name(FAR const char *gname) +{ + FAR struct group *result; + struct group grp; + char buffer[80]; + int ret; + + ret = getgrnam_r(gname, &grp, buffer, 80, &result); + if (ret != 0) + { + fprintf(stderr, "ERPOR: getgrnam_r failed: %d\n", ret); + } + else if (result == NULL) + { + fprintf(stderr, "No such group name: %s\n", gname); + } + else + { + show_grp(&grp); + } +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * uid_main + ****************************************************************************/ + +#if defined(BUILD_MODULE) +int main(int argc, FAR char *argv[]) +#else +int uid_main(int argc, char *argv[]) +#endif +{ + if (argc < 2) + { + fprintf(stderr, "ERROR: Missing options\n"); + show_usage(argv[0], stderr, EXIT_FAILURE); + } + + /* Check for help */ + + if (strcmp(argv[1], "-h") == 0) + { + if (argc != 2) + { + fprintf(stderr, + "ERROR: Invalid number of arguments for help: %d\n", + argc - 1); + show_usage(argv[0], stderr, EXIT_FAILURE); + } + + show_usage(argv[0], stdout, EXIT_SUCCESS); + } + + if (argc != 3) + { + fprintf(stderr, "ERROR: Invalid number of arguments: %d\n", + argc - 1); + show_usage(argv[0], stderr, EXIT_FAILURE); + } + + if (strcmp(argv[1], "-uid") == 0) + { + int uid = atoi(argv[2]); + show_user_by_id((uid_t)uid); + } + else if (strcmp(argv[1], "-uname") == 0) + { + show_user_by_name(argv[2]); + } + else if (strcmp(argv[1], "-gid") == 0) + { + int gid = atoi(argv[2]); + show_group_by_id((gid_t)gid); + } + else if (strcmp(argv[1], "-gname") == 0) + { + show_group_by_name(argv[2]); + } + else + { + fprintf(stderr, "ERROR: Unrecognized option: %s\n", argv[1]); + show_usage(argv[0], stderr, EXIT_FAILURE); + } + + return EXIT_SUCCESS; +}