From c98e51a31df74ef7722007b4682a5a54a68f8afc Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Tue, 19 Jan 2016 19:19:04 -0600 Subject: [PATCH 1/2] apps/fsutils/passwd: Add a password fila management library --- ChangeLog.txt | 3 + fsutils/Kconfig | 1 + fsutils/passwd/.gitignore | 11 ++ fsutils/passwd/Kconfig | 39 +++++ fsutils/passwd/Make.defs | 38 +++++ fsutils/passwd/Makefile | 108 +++++++++++++ fsutils/passwd/passwd.h | 162 +++++++++++++++++++ fsutils/passwd/passwd_adduser.c | 105 +++++++++++++ fsutils/passwd/passwd_append.c | 104 +++++++++++++ fsutils/passwd/passwd_delete.c | 265 ++++++++++++++++++++++++++++++++ fsutils/passwd/passwd_deluser.c | 96 ++++++++++++ fsutils/passwd/passwd_encrypt.c | 150 ++++++++++++++++++ fsutils/passwd/passwd_find.c | 175 +++++++++++++++++++++ fsutils/passwd/passwd_lock.c | 137 +++++++++++++++++ fsutils/passwd/passwd_update.c | 103 +++++++++++++ fsutils/passwd/passwd_verify.c | 106 +++++++++++++ include/fsutils/passwd.h | 118 ++++++++++++++ 17 files changed, 1721 insertions(+) create mode 100644 fsutils/passwd/.gitignore create mode 100644 fsutils/passwd/Kconfig create mode 100644 fsutils/passwd/Make.defs create mode 100644 fsutils/passwd/Makefile create mode 100644 fsutils/passwd/passwd.h create mode 100644 fsutils/passwd/passwd_adduser.c create mode 100644 fsutils/passwd/passwd_append.c create mode 100644 fsutils/passwd/passwd_delete.c create mode 100644 fsutils/passwd/passwd_deluser.c create mode 100644 fsutils/passwd/passwd_encrypt.c create mode 100644 fsutils/passwd/passwd_find.c create mode 100644 fsutils/passwd/passwd_lock.c create mode 100644 fsutils/passwd/passwd_update.c create mode 100644 fsutils/passwd/passwd_verify.c create mode 100644 include/fsutils/passwd.h diff --git a/ChangeLog.txt b/ChangeLog.txt index e7738f6a4..cd753379c 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -1510,3 +1510,6 @@ execution of other commands (2015-12-31). * apps/netutils/netlib: Add netlib_get_dripv4addr() and netlib_get_ipv4netmask(). From Pelle Windestam (2016-01-14). + * apps/fsutils/passwd: Utility library for accessing a password + file like /etc/passwd (2016-01-19). + diff --git a/fsutils/Kconfig b/fsutils/Kconfig index e0fcb8d56..35609b8ac 100644 --- a/fsutils/Kconfig +++ b/fsutils/Kconfig @@ -6,5 +6,6 @@ menu "File System Utilities" source "$APPSDIR/fsutils/mksmartfs/Kconfig" +source "$APPSDIR/fsutils/passwd/Kconfig" endmenu # FS Utilities diff --git a/fsutils/passwd/.gitignore b/fsutils/passwd/.gitignore new file mode 100644 index 000000000..83bd7b811 --- /dev/null +++ b/fsutils/passwd/.gitignore @@ -0,0 +1,11 @@ +/Make.dep +/.depend +/.built +/*.asm +/*.rel +/*.lst +/*.sym +/*.adb +/*.lib +/*.src +/*.obj diff --git a/fsutils/passwd/Kconfig b/fsutils/passwd/Kconfig new file mode 100644 index 000000000..8146a9943 --- /dev/null +++ b/fsutils/passwd/Kconfig @@ -0,0 +1,39 @@ +# +# For a description of the syntax of this configuration file, +# see the file kconfig-language.txt in the NuttX tools repository. +# + +config FSUTILS_PASSWD + bool "Password file support" + default n + depends on FS_READABLE + ---help--- + Enables support for /etc/passwd file access routines + +if FSUTILS_PASSWD + +config FSUTILS_PASSWD_PATH + string "Path to the passwd file" + default "/etc/passwd" + +config FSUTILS_PASSWD_IOBUFFER_SIZE + int "Allocated I/O buffer size" + default 512 + +config FSUTILS_PASSWD_KEY1 + hex "Encryption key value 1" + default 0x12345678 + +config FSUTILS_PASSWD_KEY2 + hex "Encryption key value 2" + default 0x9abcdef0 + +config FSUTILS_PASSWD_KEY3 + hex "Encryption key value 3" + default 0x12345678 + +config FSUTILS_PASSWD_KEY4 + hex "Encryption key value 4" + default 0x9abcdef0 + +endif # FSUTILS_PASSWD diff --git a/fsutils/passwd/Make.defs b/fsutils/passwd/Make.defs new file mode 100644 index 000000000..a1299b7ec --- /dev/null +++ b/fsutils/passwd/Make.defs @@ -0,0 +1,38 @@ +############################################################################ +# apps/fsutils/passwd/Make.defs +# +# Copyright (C) 2016 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. +# +############################################################################ + +ifeq ($(CONFIG_FSUTILS_PASSWD),y) +CONFIGURED_APPS += fsutils/passwd +endif diff --git a/fsutils/passwd/Makefile b/fsutils/passwd/Makefile new file mode 100644 index 000000000..14ff23932 --- /dev/null +++ b/fsutils/passwd/Makefile @@ -0,0 +1,108 @@ +############################################################################ +# apps/fsutils/passwd/Makefile +# +# Copyright (C) 2016 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)/.config +-include $(TOPDIR)/Make.defs +include $(APPDIR)/Make.defs + +# Password file access library + +ASRCS = +CSRCS = + +ifeq ($(CONFIG_FSUTILS_PASSWD),y) +ifeq ($(CONFIG_FS_READABLE),y) +CSRCS += passwd_verify.c passwd_find.c passwd_encrypt.c +ifeq ($(CONFIG_FS_WRITABLE),y) +CSRCS += passwd_adduser.c passwd_deluser.c passwd_update.c passwd_append.c +CSRCS += passwd_delete.c passwd_lock.c +endif +endif +endif + +AOBJS = $(ASRCS:.S=$(OBJEXT)) +COBJS = $(CSRCS:.c=$(OBJEXT)) + +SRCS = $(ASRCS) $(CSRCS) +OBJS = $(AOBJS) $(COBJS) + +ifeq ($(CONFIG_WINDOWS_NATIVE),y) + BIN = ..\..\libapps$(LIBEXT) +else +ifeq ($(WINTOOL),y) + BIN = ..\\..\\libapps$(LIBEXT) +else + BIN = ../../libapps$(LIBEXT) +endif +endif + +ROOTDEPPATH = --dep-path . + +# Common build + +VPATH = + +all: .built +.PHONY: context depend clean distclean + +$(AOBJS): %$(OBJEXT): %.S + $(call ASSEMBLE, $<, $@) + +$(COBJS): %$(OBJEXT): %.c + $(call COMPILE, $<, $@) + +.built: $(OBJS) + $(call ARCHIVE, $(BIN), $(OBJS)) + $(Q) touch .built + +install: + +context: + +.depend: Makefile $(SRCS) + $(Q) $(MKDEP) $(ROOTDEPPATH) "$(CC)" -- $(CFLAGS) -- $(SRCS) >Make.dep + $(Q) touch $@ + +depend: .depend + +clean: + $(call DELFILE, .built) + $(call CLEAN) + +distclean: clean + $(call DELFILE, Make.dep) + $(call DELFILE, .depend) + +-include Make.dep diff --git a/fsutils/passwd/passwd.h b/fsutils/passwd/passwd.h new file mode 100644 index 000000000..1a04475d5 --- /dev/null +++ b/fsutils/passwd/passwd.h @@ -0,0 +1,162 @@ +/**************************************************************************** + * apps/fsutils/passwd/passwd.h + * + * Copyright (C) 2016 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. + * + ****************************************************************************/ + +#ifndef __APPS_FSUTILS_PASSWD_PASSWD_H +#define __APPS_FSUTILS_PASSWD_PASSWD_H 1 + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#include +#include +#include + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define MAX_ENCRYPTED 48 /* Maximum size of a password (encrypted, ASCII) */ +#define MAX_USERNAME 48 /* Maximum size of a username */ +#define MAX_RECORD (MAX_USERNAME + MAX_ENCRYPTED + 1) +#define MAX_PASSWORD (MAX_ENCRYPTED / 2) + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +struct passwd_s +{ + off_t offset; /* File offset (start of record) */ + char encrypted[MAX_ENCRYPTED + 1]; /* Encrtyped password in file */ +}; + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +/**************************************************************************** + * Name: passwd_lock and passwd_unlock + * + * Description: + * Lock the /etc/passwd file. This is not a real lock at the level of the + * file system. Rather, it only prevents concurrent modification of the + * /etc/passwd file by passwd_adduser(), passwd_deluser(), and + * passwd_update(). Other accesses to /etc/passwd could still cause + * concurrency problem and file corruption. + * + * Input Parameters: + * + * Returned Value: + * Zero (OK) is returned on success; a negated errno value is returned on + * failure. + * + ****************************************************************************/ + +#if CONFIG_FS_WRITABLE +int passwd_lock(FAR sem_t **semp); +int passwd_unlock(FAR sem_t *sem); +#else +# define passwd_lock(semp) (0) +# define passwd_unlock(sem) (0) +#endif + +/**************************************************************************** + * Name: passwd_encrypt + * + * Description: + * Encrypt a password. Currently uses the Tiny Encryption Algorithm. + * + * Input Parameters: + * password -- The password string to be encrypted + * + * Returned Value: + * Zero (OK) is returned on success; a negated errno value is returned on + * failure. + * + ****************************************************************************/ + +int passwd_encrypt(FAR const char *password, char encrypted[MAX_ENCRYPTED + 1]); + +/**************************************************************************** + * Name: passwd_append + * + * Description: + * Append a new record to the end of the /etc/passwd file + * + * Input Parameters: + * + * Returned Value: + * Zero (OK) is returned on success; a negated errno value is returned on + * failure. + * + ****************************************************************************/ + +int passwd_append(FAR const char *username, FAR const char *password); + +/**************************************************************************** + * Name: passwd_delete + * + * Description: + * Delete on record from the password file at offset. + * + * Input Parameters: + * + * Returned Value: + * Zero (OK) is returned on success; a negated errno value is returned on + * failure. + * + ****************************************************************************/ + +int passwd_delete(off_t offset); + +/**************************************************************************** + * Name: passwd_find + * + * Description: + * Find a password in the + * + * Input Parameters: + * + * Returned Value: + * Zero (OK) is returned on success; a negated errno value is returned on + * failure. + * + ****************************************************************************/ + +int passwd_find(FAR const char *username, FAR struct passwd_s *passwd); + +#endif /* __APPS_FSUTILS_PASSWD_PASSWD_H */ diff --git a/fsutils/passwd/passwd_adduser.c b/fsutils/passwd/passwd_adduser.c new file mode 100644 index 000000000..e8a0946ba --- /dev/null +++ b/fsutils/passwd/passwd_adduser.c @@ -0,0 +1,105 @@ +/**************************************************************************** + * apps/fsutils/passwd/passwd_adduser.c + * + * Copyright (C) 2016 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 "passwd.h" + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: passwd_adduser + * + * Description: + * Add a new user to the /etc/passwd file. If the user already exists, + * then this function will fail with -EEXIST. + * + * Input Parameters: + * + * Returned Value: + * Zero (OK) is returned on success; a negated errno value is returned on + * failure. + * + ****************************************************************************/ + +int passwd_adduser(FAR const char *username, FAR const char *password) +{ + struct passwd_s passwd; + FAR sem_t *sem; + int ret; + + /* Get exclusive access to the /etc/passwd file */ + + ret = passwd_lock(&sem); + if (ret < 0) + { + return ret; + } + + /* Check if the username already exists */ + + ret = passwd_find(username, &passwd); + if (ret >= 0) + { + /* The username already exists in the /etc/passwd file */ + + ret = -EEXIST; + goto errout_with_lock; + } + + /* Append the new user to the end of the file */ + + ret = passwd_append(username, password); + if (ret < 0) + { + goto errout_with_lock; + } + + /* Return success */ + + ret = OK; + +errout_with_lock: + (void)passwd_unlock(sem); + return ret; +} diff --git a/fsutils/passwd/passwd_append.c b/fsutils/passwd/passwd_append.c new file mode 100644 index 000000000..6581c15c1 --- /dev/null +++ b/fsutils/passwd/passwd_append.c @@ -0,0 +1,104 @@ +/**************************************************************************** + * apps/fsutils/passwd/passwd_append.c + * + * Copyright (C) 2016 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 "passwd.h" + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: passwd_append + * + * Description: + * Append a new record to the end of the /etc/passwd file + * + * Input Parameters: + * + * Returned Value: + * Zero (OK) is returned on success; a negated errno value is returned on + * failure. + * + ****************************************************************************/ + +int passwd_append(FAR const char *username, FAR const char *password) +{ + char encrypted[MAX_ENCRYPTED + 1]; + FILE *stream; + int ret; + + /* Encrypt the raw password */ + + ret = passwd_encrypt(password, encrypted); + if (ret < 0) + { + return ret; + } + + /* Append the new user record to the end of the password file */ + + stream = fopen(CONFIG_FSUTILS_PASSWD_PATH, "at"); + if (stream == NULL) + { + int errcode = errno; + DEBUGASSERT(errcode > 0); + return errcode; + } + + ret = fprintf(stream, "%s %s\n", username, encrypted); + if (ret < 0) + { + int errcode = errno; + DEBUGASSERT(errcode > 0); + ret = -errcode; + goto errout_with_stream; + } + + /* Return success */ + + ret = OK; + +errout_with_stream: + (void)fclose(stream); + return ret; +} diff --git a/fsutils/passwd/passwd_delete.c b/fsutils/passwd/passwd_delete.c new file mode 100644 index 000000000..0a795d123 --- /dev/null +++ b/fsutils/passwd/passwd_delete.c @@ -0,0 +1,265 @@ +/**************************************************************************** + * apps/fsutils/passwd/passwd_delete.c + * + * Copyright (C) 2016 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 + +#include "passwd.h" + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: passwd_copyfile + * + * Description: + * Copy copysize from instream to outstream (or until an error or EOF is + * encountered) + * + * Input Parameters: + * + * Returned Value: + * Zero (OK) is returned on success; a negated errno value is returned on + * failure. + * + ****************************************************************************/ + +static int passwd_copyfile(FAR char *iobuffer, FILE *instream, + FILE *outstream, size_t copysize) +{ + FAR char *buffer; + ssize_t nxfrd; + size_t nwritten; + size_t nread; + size_t nbytes; + size_t gulpsize; + size_t ncopied; + + /* Copy 'offset' bytes from the instream to the outstream */ + + for (ncopied = 0; ncopied < copysize; ncopied += nwritten) + { + /* How big of a gulp can we take on this pass through the loop */ + + gulpsize = copysize; + if (gulpsize > CONFIG_FSUTILS_PASSWD_IOBUFFER_SIZE) + { + gulpsize = CONFIG_FSUTILS_PASSWD_IOBUFFER_SIZE; + } + + /* Read a buffer of data from the instream */ + + buffer = iobuffer; + nbytes = gulpsize; + nread = 0; + + do + { + nxfrd = fread(buffer, 1, nbytes, instream); + if (nxfrd < 0) + { + int errcode = errno; + DEBUGASSERT(errcode > 0); + + if (errcode != EINTR) + { + return -errcode; + } + } + else + { + nread += nxfrd; + buffer += nxfrd; + nbytes -= nxfrd; + } + } + while (nread < gulpsize); + + /* Write the buffer of data to outstream */ + + buffer = iobuffer; + nbytes = nread; + nwritten = 0; + + do + { + nxfrd = fwrite(buffer, 1, nbytes, instream); + if (nxfrd < 0) + { + int errcode = errno; + DEBUGASSERT(errcode > 0); + + if (errcode != EINTR) + { + return -errcode; + } + } + else + { + nwritten += nxfrd; + buffer += nxfrd; + nbytes -= nxfrd; + } + } + while (nwritten < nread); + copysize -= nwritten; + } + + return OK; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: passwd_delete + * + * Description: + * Delete on record from the password file at offset. + * + * Input Parameters: + * + * Returned Value: + * Zero (OK) is returned on success; a negated errno value is returned on + * failure. + * + ****************************************************************************/ + +int passwd_delete(off_t offset) +{ + FAR char *iobuffer; + FILE *instream; + FILE *outstream; + int ret; + + /* Allocate an I/O buffer for the transfer */ + + iobuffer = (FAR char *)malloc(CONFIG_FSUTILS_PASSWD_IOBUFFER_SIZE); + if (iobuffer == NULL) + { + return -ENOMEM; + } + + /* Rename the /set/password file */ + + ret = rename(CONFIG_FSUTILS_PASSWD_PATH, CONFIG_FSUTILS_PASSWD_PATH ".tmp"); + if (ret < 0) + { + ret = -errno; + DEBUGASSERT(ret < 0); + goto errout_with_iobuffer; + } + + /* Open the renamed file for reading; re-create the /etc/passwd file for + * writing. + */ + + instream = fopen(CONFIG_FSUTILS_PASSWD_PATH ".tmp", "rt"); + if (instream == NULL) + { + ret = -errno; + DEBUGASSERT(ret < 0); + goto errout_with_iobuffer; + } + + outstream = fopen(CONFIG_FSUTILS_PASSWD_PATH, "wt"); + if (outstream == NULL) + { + ret = -errno; + DEBUGASSERT(ret < 0); + goto errout_with_instream; + } + + /* Copy 'offset' bytes from the renamed file to the original file */ + + ret = passwd_copyfile(iobuffer, instream, outstream, offset); + if (ret < 0) + { + goto errout_with_outstream; + } + + /* Now read from the instream and discard the current line */ + + for (; ; ) + { + int ch = fgetc(instream); + if (ch == EOF) + { + if (feof(instream)) + { + /* Could this really happen without encountering the + * newline terminator? + */ + + break; + } + else + { + ret = -errno; + DEBUGASSERT(ret < 0); + goto errout_with_instream; + } + } + else if (ch == '\n') + { + break; + } + } + + /* Copy the rest of the file */ + + ret = passwd_copyfile(iobuffer, instream, outstream, SIZE_MAX); + +errout_with_outstream: + (void)fclose(outstream); + +errout_with_instream: + (void)fclose(instream); + +errout_with_iobuffer: + free(iobuffer); + return ret; +} diff --git a/fsutils/passwd/passwd_deluser.c b/fsutils/passwd/passwd_deluser.c new file mode 100644 index 000000000..870b2b5b0 --- /dev/null +++ b/fsutils/passwd/passwd_deluser.c @@ -0,0 +1,96 @@ +/**************************************************************************** + * apps/fsutils/passwd/passwd_deluser.c + * + * Copyright (C) 2016 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 "passwd.h" + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: passwd_deluser + * + * Description: + * Remove an existing user from the /etc/passwd file. If the user does + * not exist, then this function will fail. + * + * Input Parameters: + * + * Returned Value: + * Zero (OK) is returned on success; a negated errno value is returned on + * failure. + * + ****************************************************************************/ + +int passwd_deluser(FAR const char *username, FAR const char *password) +{ + struct passwd_s passwd; + FAR sem_t *sem; + int ret; + + /* Get exclusive access to the /etc/passwd file */ + + ret = passwd_lock(&sem); + if (ret < 0) + { + return ret; + } + + /* Verify that the username exists in the /etc/passwd file */ + + ret = passwd_find(username, &passwd); + if (ret < 0) + { + /* The username does not exist in the /etc/passwd file */ + + goto errout_with_lock; + } + + /* Remove the line containing this user from the /etc/passwd file */ + + ret = passwd_delete(passwd.offset); + +errout_with_lock: + (void)passwd_unlock(sem); + return ret; +} diff --git a/fsutils/passwd/passwd_encrypt.c b/fsutils/passwd/passwd_encrypt.c new file mode 100644 index 000000000..89c8703ea --- /dev/null +++ b/fsutils/passwd/passwd_encrypt.c @@ -0,0 +1,150 @@ +/**************************************************************************** + * apps/fsutils/passwd/passwd_encrypt.c + * + * Copyright (C) 2016 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 + +#include "passwd.h" + +/**************************************************************************** + * Private Data + ****************************************************************************/ +/* This should be better protected */ + +static uint32_t g_tea_key[4] = +{ + CONFIG_FSUTILS_PASSWD_KEY1, + CONFIG_FSUTILS_PASSWD_KEY2, + CONFIG_FSUTILS_PASSWD_KEY3, + CONFIG_FSUTILS_PASSWD_KEY4 +}; + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: passwd_encrypt + * + * Description: + * Encrypt a password. Currently uses the Tiny Encryption Algorithm. + * + * Input Parameters: + * password -- The password string to be encrypted + * + * Returned Value: + * Zero (OK) is returned on success; a negated errno value is returned on + * failure. + * + ****************************************************************************/ + +int passwd_encrypt(FAR const char *password, char encrypted[MAX_ENCRYPTED + 1]) +{ + union + { + char b[8]; + uint32_t l[2]; + } value; + + FAR const char *src; + FAR char *dest; + int remaining; + int converted; + int enclen; + int gulpsize; + int i; + + /* How long is the password? */ + + remaining = strlen(password); + if (remaining > MAX_PASSWORD) + { + return -E2BIG; + } + + /* Convert the password in 8-byte TEA cycles */ + + src = password; + encrypted[0] = '\0'; + enclen = 0; + + for (converted = 0; converted < remaining; converted += 8) + { + /* Copy bytes */ + + gulpsize = 8; + if (gulpsize > remaining) + { + gulpsize = remaining; + } + + dest = value.b; + for (i = 0; i < gulpsize; i++) + { + *dest++ = *src++; + } + + /* Pad with spaces if necessary */ + + for (; i < 8; i++) + { + *dest++ = ' '; + } + + /* Perform the conversion for this cycle */ + + tea_encrypt(value.l, g_tea_key); + + /* Generate the output from this cycle */ + + enclen += snprintf(&encrypted[enclen], + MAX_ENCRYPTED - enclen, + "%08lx%08lx", + (unsigned long)value.l[0], + (unsigned long)value.l[1]); + } + + return OK; +} diff --git a/fsutils/passwd/passwd_find.c b/fsutils/passwd/passwd_find.c new file mode 100644 index 000000000..b71d75efe --- /dev/null +++ b/fsutils/passwd/passwd_find.c @@ -0,0 +1,175 @@ +/**************************************************************************** + * apps/fsutils/passwd/passwd_find.c + * + * Copyright (C) 2016 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 + +#include "passwd.h" + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: passwd_find + * + * Description: + * Find a password in the + * + * Input Parameters: + * + * Returned Value: + * Zero (OK) is returned on success; a negated errno value is returned on + * failure. + * + ****************************************************************************/ + +int passwd_find(FAR const char *username, FAR struct passwd_s *passwd) +{ + FAR char *iobuffer; + FAR char *name; + FAR char *src; + FAR char *dest; + FILE *stream; + off_t offset; + int enclen; + int ret; + + /* Allocate an I/O buffer for the transfer */ + + iobuffer = (FAR char *)malloc(CONFIG_FSUTILS_PASSWD_IOBUFFER_SIZE); + if (iobuffer == NULL) + { + return -ENOMEM; + } + + /* Open the password file for reading */ + + stream = fopen(CONFIG_FSUTILS_PASSWD_PATH, "at"); + if (stream == NULL) + { + int errcode = errno; + DEBUGASSERT(errcode > 0); + return errcode; + } + + /* Read the password file line by line until the record with the matching + * username is found, or until the end of the file is reached. + */ + + offset = 0; + ret = -ENOENT; + + while (fgets(iobuffer, CONFIG_FSUTILS_PASSWD_IOBUFFER_SIZE, stream) != NULL) + { + /* Skip over any leading whitespace */ + + for (src = iobuffer; *src && isspace((int)*src); src++); + if (*src == '\0') + { + /* Bad file format? */ + + continue; + } + + name = src; + + /* Skip to the end of the name and properly terminate it */ + + for (; *src && !isspace((int)*src); src++); + if (*src == '\0') + { + /* Bad file format? */ + + continue; + } + + *src++ = '\0'; + + /* Check for a username match */ + + if (strcmp(username, name) == 0) + { + /* We have a match, skip over any whitespace after the user name */ + + for (src = iobuffer; *src && isspace((int)*src); src++); + if (*src == '\0') + { + /* Bad file format? */ + + ret = -EINVAL; + break; + } + + /* Copy the offset and password into the returned structure */ + + passwd->offset = offset; + dest = passwd->encrypted; + enclen = 0; + + while (*src && !isspace((int)*src) && enclen < MAX_ENCRYPTED) + { + *dest++ = *src++; + enclen++; + } + + if (enclen >= MAX_ENCRYPTED) + { + ret = -E2BIG; + break; + } + + *dest = '\0'; + ret = OK; + break; + } + + /* Get the next file offset */ + + offset = ftell(stream); + } + + fclose(stream); + free(iobuffer); + return ret; +} diff --git a/fsutils/passwd/passwd_lock.c b/fsutils/passwd/passwd_lock.c new file mode 100644 index 000000000..dcd586222 --- /dev/null +++ b/fsutils/passwd/passwd_lock.c @@ -0,0 +1,137 @@ +/**************************************************************************** + * apps/fsutils/passwd/passwd_lock.c + * + * Copyright (C) 2016 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 "passwd.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#ifdef CONFIG_BUILD_KERNEL +/* In the kernel build mode, we need to use a named semaphore so that all + * processes will share the same, named semaphore instance. + */ + +# define PASSWD_SEMNAME "pwsem" /* Global named semaphore */ +#endif + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +#ifndef CONFIG_BUILD_KERNEL +/* In the FLAT and PROTECTED build modes, we do not need to bother with a + * named semaphore. We use a single global semaphore in theses cases. + */ + +static sem_t g_passwd_sem = SEM_INITIALIZER(1); +#endif + +/**************************************************************************** + * Public Functions + ****************************************************************************/ +/**************************************************************************** + * Name: passwd_lock and passwd_unlock + * + * Description: + * Lock the /etc/passwd file. This is not a real lock at the level of the + * file system. Rather, it only prevents concurrent modification of the + * /etc/passwd file by passwd_adduser(), passwd_deluser(), and + * passwd_update(). Other accesses to /etc/passwd could still cause + * concurrency problem and file corruption. + * + * Input Parameters: + * + * Returned Value: + * Zero (OK) is returned on success; a negated errno value is returned on + * failure. + * + ****************************************************************************/ + +int passwd_lock(FAR sem_t **semp) +{ + FAR sem_t *sem; + +#ifdef CONFIG_BUILD_KERNEL + /* Open the shared, named semaphore */ + + sem = sem_open(PASSWD_SEMNAME, O_CREAT, 0644, 1); + if (sem == NULL) + { + int errcode = errno; + DEBUGASSERT(errcode > 0); + return -errcode; + } +#else + /* Use the global semaphore */ + + sem = &g_passwd_sem; +#endif + + /* Take the semaphore. Only EINTR errors are expected. */ + + while (sem_wait(sem) < 0) + { + int errcode = errno; + DEBUGASSERT(errcode == EINTR); + UNUSED(errcode); + } + + *semp = sem; + return OK; +} + +int passwd_unlock(FAR sem_t *sem) +{ + /* Release our count on the semaphore */ + + sem_post(sem); + +#ifdef CONFIG_BUILD_KERNEL + /* Close the named semaphore */ + + (void)sem_close(sem); +#endif + + return OK; +} diff --git a/fsutils/passwd/passwd_update.c b/fsutils/passwd/passwd_update.c new file mode 100644 index 000000000..de7754e81 --- /dev/null +++ b/fsutils/passwd/passwd_update.c @@ -0,0 +1,103 @@ +/**************************************************************************** + * apps/fsutils/passwd/passwd_update.c + * + * Copyright (C) 2016 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 + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: passwd_update + * + * Description: + * Change a new user to the /etc/passwd file. If the user does not exist, + * then this function will fail. + * + * Input Parameters: + * + * Returned Value: + * Zero (OK) is returned on success; a negated errno value is returned on + * failure. + * + ****************************************************************************/ + +int passwd_update(FAR const char *username, FAR const char *password) +{ + struct passwd_s passwd; + FAR sem_t *sem; + int ret; + + /* Get exclusive access to the /etc/passwd file */ + + ret = passwd_lock(&sem); + if (ret < 0) + { + return ret; + } + + /* Verify that the username exists in the /etc/passwd file */ + + ret = passwd_find(username, &passwd); + if (ret < 0) + { + /* The username does not exist in the /etc/passwd file */ + + goto errout_with_lock; + } + + /* Remove the line containing this user from the /etc/passwd file */ + + ret = passwd_delete(passwd.offset); + if (ret < 0) + { + goto errout_with_lock; + } + + /* Then append the new password record to the end of the file */ + + ret = passwd_append(username, password); + +errout_with_lock: + (void)passwd_unlock(sem); + return ret; +} diff --git a/fsutils/passwd/passwd_verify.c b/fsutils/passwd/passwd_verify.c new file mode 100644 index 000000000..07899f448 --- /dev/null +++ b/fsutils/passwd/passwd_verify.c @@ -0,0 +1,106 @@ +/**************************************************************************** + * apps/fsutils/passwd/passwd_verify.c + * + * Copyright (C) 2016 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 "passwd.h" + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: passwd_verify + * + * Description: + * Return true if the username exists in the /etc/passwd file and if the + * password matches the user password in that faile. + * + * Input Parameters: + * + * Returned Value: + * One (1) is returned on success match, Zero (OK) is returned on an + * unsuccessful match; a negated errno value is returned on any other + * failure. + * + ****************************************************************************/ + +int passwd_verify(FAR const char *username, FAR const char *password) +{ + struct passwd_s passwd; + char encrypted[MAX_ENCRYPTED + 1]; + FAR sem_t *sem; + int ret; + + /* Get exclusive access to the /etc/passwd file */ + + ret = passwd_lock(&sem); + if (ret < 0) + { + return ret; + } + + /* Verify that the username exists in the /etc/passwd file */ + + ret = passwd_find(username, &passwd); + if (ret < 0) + { + /* The username does not exist in the /etc/passwd file */ + + goto errout_with_lock; + } + + /* Encrypt the provided password */ + + ret = passwd_encrypt(password, encrypted); + if (ret < 0) + { + goto errout_with_lock; + } + + /* Compare the encrypted passwords */ + + ret = (strcmp(passwd.encrypted, encrypted) == 0) ? 1 : 0; + +errout_with_lock: + (void)passwd_unlock(sem); + return ret; +} diff --git a/include/fsutils/passwd.h b/include/fsutils/passwd.h new file mode 100644 index 000000000..0850621e2 --- /dev/null +++ b/include/fsutils/passwd.h @@ -0,0 +1,118 @@ +/**************************************************************************** + * apps/include/fsutils/passwd.h + * + * Copyright (C) 2016 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. + * + ****************************************************************************/ + +#ifndef __APPS_INCLUDE_FSUTILS_PASSWD_H +#define __APPS_INCLUDE_FSUTILS_PASSWD_H 1 + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +/**************************************************************************** + * Name: passwd_adduser + * + * Description: + * Add a new user to the /etc/passwd file. If the user already exists, + * then this function will fail with -EEXIST. + * + * Input Parameters: + * + * Returned Value: + * Zero (OK) is returned on success; a negated errno value is returned on + * failure. + * + ****************************************************************************/ + +int passwd_adduser(FAR const char *username, FAR const char *password); + +/**************************************************************************** + * Name: passwd_deluser + * + * Description: + * Remove an existing user from the /etc/passwd file. If the user does + * not exist, then this function will fail. + * + * Input Parameters: + * + * Returned Value: + * Zero (OK) is returned on success; a negated errno value is returned on + * failure. + * + ****************************************************************************/ + +int passwd_deluser(FAR const char *username, FAR const char *password); + +/**************************************************************************** + * Name: passwd_update + * + * Description: + * Change a new user to the /etc/passwd file. If the user does not exist, + * then this function will fail. + * + * Input Parameters: + * + * Returned Value: + * Zero (OK) is returned on success; a negated errno value is returned on + * failure. + * + ****************************************************************************/ + +int passwd_update(FAR const char *username, FAR const char *password); + +/**************************************************************************** + * Name: passwd_verify + * + * Description: + * Return true if the username exists in the /etc/passwd file and if the + * password matches the user password in that faile. + * + * Input Parameters: + * + * Returned Value: + * One (1) is returned on success match, Zero (OK) is returned on an + * unsuccessful match; a negated errno value is returned on any other + * failure. + * + ****************************************************************************/ + +int passwd_verify(FAR const char *username, FAR const char *password); + +#endif /* __APPS_INCLUDE_FSUTILS_PASSWD_H */ From 8630130299c67f19ddc8a0eec6282726228f89c8 Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Tue, 19 Jan 2016 19:22:45 -0600 Subject: [PATCH 2/2] Remove dangle whitespace at the end of the line and some files with carriage returns in them --- fsutils/passwd/Makefile | 2 +- fsutils/passwd/passwd.h | 12 +- fsutils/passwd/passwd_adduser.c | 4 +- fsutils/passwd/passwd_append.c | 4 +- fsutils/passwd/passwd_delete.c | 6 +- fsutils/passwd/passwd_deluser.c | 2 +- fsutils/passwd/passwd_encrypt.c | 2 +- fsutils/passwd/passwd_find.c | 4 +- fsutils/passwd/passwd_lock.c | 98 ++++++------- fsutils/passwd/passwd_update.c | 2 +- fsutils/passwd/passwd_verify.c | 2 +- include/fsutils/passwd.h | 236 ++++++++++++++++---------------- 12 files changed, 187 insertions(+), 187 deletions(-) diff --git a/fsutils/passwd/Makefile b/fsutils/passwd/Makefile index 14ff23932..3b99759ba 100644 --- a/fsutils/passwd/Makefile +++ b/fsutils/passwd/Makefile @@ -3,7 +3,7 @@ # # Copyright (C) 2016 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: diff --git a/fsutils/passwd/passwd.h b/fsutils/passwd/passwd.h index 1a04475d5..57c3427aa 100644 --- a/fsutils/passwd/passwd.h +++ b/fsutils/passwd/passwd.h @@ -71,7 +71,7 @@ struct passwd_s /**************************************************************************** * Name: passwd_lock and passwd_unlock * - * Description: + * Description: * Lock the /etc/passwd file. This is not a real lock at the level of the * file system. Rather, it only prevents concurrent modification of the * /etc/passwd file by passwd_adduser(), passwd_deluser(), and @@ -97,7 +97,7 @@ int passwd_unlock(FAR sem_t *sem); /**************************************************************************** * Name: passwd_encrypt * - * Description: + * Description: * Encrypt a password. Currently uses the Tiny Encryption Algorithm. * * Input Parameters: @@ -114,7 +114,7 @@ int passwd_encrypt(FAR const char *password, char encrypted[MAX_ENCRYPTED + 1]); /**************************************************************************** * Name: passwd_append * - * Description: + * Description: * Append a new record to the end of the /etc/passwd file * * Input Parameters: @@ -130,7 +130,7 @@ int passwd_append(FAR const char *username, FAR const char *password); /**************************************************************************** * Name: passwd_delete * - * Description: + * Description: * Delete on record from the password file at offset. * * Input Parameters: @@ -146,8 +146,8 @@ int passwd_delete(off_t offset); /**************************************************************************** * Name: passwd_find * - * Description: - * Find a password in the + * Description: + * Find a password in the * * Input Parameters: * diff --git a/fsutils/passwd/passwd_adduser.c b/fsutils/passwd/passwd_adduser.c index e8a0946ba..e8f8c4656 100644 --- a/fsutils/passwd/passwd_adduser.c +++ b/fsutils/passwd/passwd_adduser.c @@ -50,7 +50,7 @@ /**************************************************************************** * Name: passwd_adduser * - * Description: + * Description: * Add a new user to the /etc/passwd file. If the user already exists, * then this function will fail with -EEXIST. * @@ -98,7 +98,7 @@ int passwd_adduser(FAR const char *username, FAR const char *password) /* Return success */ ret = OK; - + errout_with_lock: (void)passwd_unlock(sem); return ret; diff --git a/fsutils/passwd/passwd_append.c b/fsutils/passwd/passwd_append.c index 6581c15c1..12e27093f 100644 --- a/fsutils/passwd/passwd_append.c +++ b/fsutils/passwd/passwd_append.c @@ -50,7 +50,7 @@ /**************************************************************************** * Name: passwd_append * - * Description: + * Description: * Append a new record to the end of the /etc/passwd file * * Input Parameters: @@ -97,7 +97,7 @@ int passwd_append(FAR const char *username, FAR const char *password) /* Return success */ ret = OK; - + errout_with_stream: (void)fclose(stream); return ret; diff --git a/fsutils/passwd/passwd_delete.c b/fsutils/passwd/passwd_delete.c index 0a795d123..27b1caa56 100644 --- a/fsutils/passwd/passwd_delete.c +++ b/fsutils/passwd/passwd_delete.c @@ -54,7 +54,7 @@ /**************************************************************************** * Name: passwd_copyfile * - * Description: + * Description: * Copy copysize from instream to outstream (or until an error or EOF is * encountered) * @@ -157,7 +157,7 @@ static int passwd_copyfile(FAR char *iobuffer, FILE *instream, /**************************************************************************** * Name: passwd_delete * - * Description: + * Description: * Delete on record from the password file at offset. * * Input Parameters: @@ -248,7 +248,7 @@ int passwd_delete(off_t offset) break; } } - + /* Copy the rest of the file */ ret = passwd_copyfile(iobuffer, instream, outstream, SIZE_MAX); diff --git a/fsutils/passwd/passwd_deluser.c b/fsutils/passwd/passwd_deluser.c index 870b2b5b0..0604acc50 100644 --- a/fsutils/passwd/passwd_deluser.c +++ b/fsutils/passwd/passwd_deluser.c @@ -50,7 +50,7 @@ /**************************************************************************** * Name: passwd_deluser * - * Description: + * Description: * Remove an existing user from the /etc/passwd file. If the user does * not exist, then this function will fail. * diff --git a/fsutils/passwd/passwd_encrypt.c b/fsutils/passwd/passwd_encrypt.c index 89c8703ea..4b6f39e79 100644 --- a/fsutils/passwd/passwd_encrypt.c +++ b/fsutils/passwd/passwd_encrypt.c @@ -68,7 +68,7 @@ static uint32_t g_tea_key[4] = /**************************************************************************** * Name: passwd_encrypt * - * Description: + * Description: * Encrypt a password. Currently uses the Tiny Encryption Algorithm. * * Input Parameters: diff --git a/fsutils/passwd/passwd_find.c b/fsutils/passwd/passwd_find.c index b71d75efe..3bae9f8df 100644 --- a/fsutils/passwd/passwd_find.c +++ b/fsutils/passwd/passwd_find.c @@ -53,8 +53,8 @@ /**************************************************************************** * Name: passwd_find * - * Description: - * Find a password in the + * Description: + * Find a password in the * * Input Parameters: * diff --git a/fsutils/passwd/passwd_lock.c b/fsutils/passwd/passwd_lock.c index dcd586222..1990a1b72 100644 --- a/fsutils/passwd/passwd_lock.c +++ b/fsutils/passwd/passwd_lock.c @@ -1,48 +1,48 @@ -/**************************************************************************** - * apps/fsutils/passwd/passwd_lock.c - * - * Copyright (C) 2016 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 "passwd.h" - +/**************************************************************************** + * apps/fsutils/passwd/passwd_lock.c + * + * Copyright (C) 2016 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 "passwd.h" + /**************************************************************************** * Pre-processor Definitions ****************************************************************************/ @@ -73,7 +73,7 @@ static sem_t g_passwd_sem = SEM_INITIALIZER(1); /**************************************************************************** * Name: passwd_lock and passwd_unlock * - * Description: + * Description: * Lock the /etc/passwd file. This is not a real lock at the level of the * file system. Rather, it only prevents concurrent modification of the * /etc/passwd file by passwd_adduser(), passwd_deluser(), and @@ -116,7 +116,7 @@ int passwd_lock(FAR sem_t **semp) DEBUGASSERT(errcode == EINTR); UNUSED(errcode); } - + *semp = sem; return OK; } @@ -131,7 +131,7 @@ int passwd_unlock(FAR sem_t *sem) /* Close the named semaphore */ (void)sem_close(sem); -#endif - +#endif + return OK; } diff --git a/fsutils/passwd/passwd_update.c b/fsutils/passwd/passwd_update.c index de7754e81..8f54d9acc 100644 --- a/fsutils/passwd/passwd_update.c +++ b/fsutils/passwd/passwd_update.c @@ -49,7 +49,7 @@ /**************************************************************************** * Name: passwd_update * - * Description: + * Description: * Change a new user to the /etc/passwd file. If the user does not exist, * then this function will fail. * diff --git a/fsutils/passwd/passwd_verify.c b/fsutils/passwd/passwd_verify.c index 07899f448..139109760 100644 --- a/fsutils/passwd/passwd_verify.c +++ b/fsutils/passwd/passwd_verify.c @@ -50,7 +50,7 @@ /**************************************************************************** * Name: passwd_verify * - * Description: + * Description: * Return true if the username exists in the /etc/passwd file and if the * password matches the user password in that faile. * diff --git a/include/fsutils/passwd.h b/include/fsutils/passwd.h index 0850621e2..3374b2571 100644 --- a/include/fsutils/passwd.h +++ b/include/fsutils/passwd.h @@ -1,118 +1,118 @@ -/**************************************************************************** - * apps/include/fsutils/passwd.h - * - * Copyright (C) 2016 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. - * - ****************************************************************************/ - -#ifndef __APPS_INCLUDE_FSUTILS_PASSWD_H -#define __APPS_INCLUDE_FSUTILS_PASSWD_H 1 - -/**************************************************************************** - * Included Files - ****************************************************************************/ - -#include - -/**************************************************************************** - * Public Function Prototypes - ****************************************************************************/ - -/**************************************************************************** - * Name: passwd_adduser - * - * Description: - * Add a new user to the /etc/passwd file. If the user already exists, - * then this function will fail with -EEXIST. - * - * Input Parameters: - * - * Returned Value: - * Zero (OK) is returned on success; a negated errno value is returned on - * failure. - * - ****************************************************************************/ - -int passwd_adduser(FAR const char *username, FAR const char *password); - -/**************************************************************************** - * Name: passwd_deluser - * - * Description: - * Remove an existing user from the /etc/passwd file. If the user does - * not exist, then this function will fail. - * - * Input Parameters: - * - * Returned Value: - * Zero (OK) is returned on success; a negated errno value is returned on - * failure. - * - ****************************************************************************/ - -int passwd_deluser(FAR const char *username, FAR const char *password); - -/**************************************************************************** - * Name: passwd_update - * - * Description: - * Change a new user to the /etc/passwd file. If the user does not exist, - * then this function will fail. - * - * Input Parameters: - * - * Returned Value: - * Zero (OK) is returned on success; a negated errno value is returned on - * failure. - * - ****************************************************************************/ - -int passwd_update(FAR const char *username, FAR const char *password); - -/**************************************************************************** - * Name: passwd_verify - * - * Description: - * Return true if the username exists in the /etc/passwd file and if the - * password matches the user password in that faile. - * - * Input Parameters: - * - * Returned Value: - * One (1) is returned on success match, Zero (OK) is returned on an - * unsuccessful match; a negated errno value is returned on any other - * failure. - * - ****************************************************************************/ - -int passwd_verify(FAR const char *username, FAR const char *password); - -#endif /* __APPS_INCLUDE_FSUTILS_PASSWD_H */ +/**************************************************************************** + * apps/include/fsutils/passwd.h + * + * Copyright (C) 2016 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. + * + ****************************************************************************/ + +#ifndef __APPS_INCLUDE_FSUTILS_PASSWD_H +#define __APPS_INCLUDE_FSUTILS_PASSWD_H 1 + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +/**************************************************************************** + * Name: passwd_adduser + * + * Description: + * Add a new user to the /etc/passwd file. If the user already exists, + * then this function will fail with -EEXIST. + * + * Input Parameters: + * + * Returned Value: + * Zero (OK) is returned on success; a negated errno value is returned on + * failure. + * + ****************************************************************************/ + +int passwd_adduser(FAR const char *username, FAR const char *password); + +/**************************************************************************** + * Name: passwd_deluser + * + * Description: + * Remove an existing user from the /etc/passwd file. If the user does + * not exist, then this function will fail. + * + * Input Parameters: + * + * Returned Value: + * Zero (OK) is returned on success; a negated errno value is returned on + * failure. + * + ****************************************************************************/ + +int passwd_deluser(FAR const char *username, FAR const char *password); + +/**************************************************************************** + * Name: passwd_update + * + * Description: + * Change a new user to the /etc/passwd file. If the user does not exist, + * then this function will fail. + * + * Input Parameters: + * + * Returned Value: + * Zero (OK) is returned on success; a negated errno value is returned on + * failure. + * + ****************************************************************************/ + +int passwd_update(FAR const char *username, FAR const char *password); + +/**************************************************************************** + * Name: passwd_verify + * + * Description: + * Return true if the username exists in the /etc/passwd file and if the + * password matches the user password in that faile. + * + * Input Parameters: + * + * Returned Value: + * One (1) is returned on success match, Zero (OK) is returned on an + * unsuccessful match; a negated errno value is returned on any other + * failure. + * + ****************************************************************************/ + +int passwd_verify(FAR const char *username, FAR const char *password); + +#endif /* __APPS_INCLUDE_FSUTILS_PASSWD_H */