apps/fsutils/passwd: Add a password fila management library
This commit is contained in:
parent
f738166b17
commit
c98e51a31d
@ -1510,3 +1510,6 @@
|
|||||||
execution of other commands (2015-12-31).
|
execution of other commands (2015-12-31).
|
||||||
* apps/netutils/netlib: Add netlib_get_dripv4addr() and
|
* apps/netutils/netlib: Add netlib_get_dripv4addr() and
|
||||||
netlib_get_ipv4netmask(). From Pelle Windestam (2016-01-14).
|
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).
|
||||||
|
|
||||||
|
@ -6,5 +6,6 @@
|
|||||||
menu "File System Utilities"
|
menu "File System Utilities"
|
||||||
|
|
||||||
source "$APPSDIR/fsutils/mksmartfs/Kconfig"
|
source "$APPSDIR/fsutils/mksmartfs/Kconfig"
|
||||||
|
source "$APPSDIR/fsutils/passwd/Kconfig"
|
||||||
|
|
||||||
endmenu # FS Utilities
|
endmenu # FS Utilities
|
||||||
|
11
fsutils/passwd/.gitignore
vendored
Normal file
11
fsutils/passwd/.gitignore
vendored
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
/Make.dep
|
||||||
|
/.depend
|
||||||
|
/.built
|
||||||
|
/*.asm
|
||||||
|
/*.rel
|
||||||
|
/*.lst
|
||||||
|
/*.sym
|
||||||
|
/*.adb
|
||||||
|
/*.lib
|
||||||
|
/*.src
|
||||||
|
/*.obj
|
39
fsutils/passwd/Kconfig
Normal file
39
fsutils/passwd/Kconfig
Normal file
@ -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
|
38
fsutils/passwd/Make.defs
Normal file
38
fsutils/passwd/Make.defs
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
############################################################################
|
||||||
|
# apps/fsutils/passwd/Make.defs
|
||||||
|
#
|
||||||
|
# Copyright (C) 2016 Gregory Nutt. All rights reserved.
|
||||||
|
# Author: Gregory Nutt <gnutt@nuttx.org>
|
||||||
|
#
|
||||||
|
# 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
|
108
fsutils/passwd/Makefile
Normal file
108
fsutils/passwd/Makefile
Normal file
@ -0,0 +1,108 @@
|
|||||||
|
############################################################################
|
||||||
|
# apps/fsutils/passwd/Makefile
|
||||||
|
#
|
||||||
|
# Copyright (C) 2016 Gregory Nutt. All rights reserved.
|
||||||
|
# Author: Gregory Nutt <gnutt@nuttx.org>
|
||||||
|
#
|
||||||
|
# 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
|
162
fsutils/passwd/passwd.h
Normal file
162
fsutils/passwd/passwd.h
Normal file
@ -0,0 +1,162 @@
|
|||||||
|
/****************************************************************************
|
||||||
|
* apps/fsutils/passwd/passwd.h
|
||||||
|
*
|
||||||
|
* Copyright (C) 2016 Gregory Nutt. All rights reserved.
|
||||||
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||||
|
*
|
||||||
|
* 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 <sys/types.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <semaphore.h>
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* 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 */
|
105
fsutils/passwd/passwd_adduser.c
Normal file
105
fsutils/passwd/passwd_adduser.c
Normal file
@ -0,0 +1,105 @@
|
|||||||
|
/****************************************************************************
|
||||||
|
* apps/fsutils/passwd/passwd_adduser.c
|
||||||
|
*
|
||||||
|
* Copyright (C) 2016 Gregory Nutt. All rights reserved.
|
||||||
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||||
|
*
|
||||||
|
* 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 <semaphore.h>
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
|
#include <apps/fsutils/passwd.h>
|
||||||
|
#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;
|
||||||
|
}
|
104
fsutils/passwd/passwd_append.c
Normal file
104
fsutils/passwd/passwd_append.c
Normal file
@ -0,0 +1,104 @@
|
|||||||
|
/****************************************************************************
|
||||||
|
* apps/fsutils/passwd/passwd_append.c
|
||||||
|
*
|
||||||
|
* Copyright (C) 2016 Gregory Nutt. All rights reserved.
|
||||||
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||||
|
*
|
||||||
|
* 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 <stdio.h>
|
||||||
|
#include <assert.h>
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
|
#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;
|
||||||
|
}
|
265
fsutils/passwd/passwd_delete.c
Normal file
265
fsutils/passwd/passwd_delete.c
Normal file
@ -0,0 +1,265 @@
|
|||||||
|
/****************************************************************************
|
||||||
|
* apps/fsutils/passwd/passwd_delete.c
|
||||||
|
*
|
||||||
|
* Copyright (C) 2016 Gregory Nutt. All rights reserved.
|
||||||
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||||
|
*
|
||||||
|
* 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 <nuttx/config.h>
|
||||||
|
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <assert.h>
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
|
#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;
|
||||||
|
}
|
96
fsutils/passwd/passwd_deluser.c
Normal file
96
fsutils/passwd/passwd_deluser.c
Normal file
@ -0,0 +1,96 @@
|
|||||||
|
/****************************************************************************
|
||||||
|
* apps/fsutils/passwd/passwd_deluser.c
|
||||||
|
*
|
||||||
|
* Copyright (C) 2016 Gregory Nutt. All rights reserved.
|
||||||
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||||
|
*
|
||||||
|
* 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 <semaphore.h>
|
||||||
|
|
||||||
|
#include <apps/fsutils/passwd.h>
|
||||||
|
|
||||||
|
#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;
|
||||||
|
}
|
150
fsutils/passwd/passwd_encrypt.c
Normal file
150
fsutils/passwd/passwd_encrypt.c
Normal file
@ -0,0 +1,150 @@
|
|||||||
|
/****************************************************************************
|
||||||
|
* apps/fsutils/passwd/passwd_encrypt.c
|
||||||
|
*
|
||||||
|
* Copyright (C) 2016 Gregory Nutt. All rights reserved.
|
||||||
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||||
|
*
|
||||||
|
* 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 <nuttx/config.h>
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
|
#include <nuttx/crypto/tea.h>
|
||||||
|
|
||||||
|
#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;
|
||||||
|
}
|
175
fsutils/passwd/passwd_find.c
Normal file
175
fsutils/passwd/passwd_find.c
Normal file
@ -0,0 +1,175 @@
|
|||||||
|
/****************************************************************************
|
||||||
|
* apps/fsutils/passwd/passwd_find.c
|
||||||
|
*
|
||||||
|
* Copyright (C) 2016 Gregory Nutt. All rights reserved.
|
||||||
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||||
|
*
|
||||||
|
* 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 <sys/types.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <ctype.h>
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
|
#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;
|
||||||
|
}
|
137
fsutils/passwd/passwd_lock.c
Normal file
137
fsutils/passwd/passwd_lock.c
Normal file
@ -0,0 +1,137 @@
|
|||||||
|
/****************************************************************************
|
||||||
|
* apps/fsutils/passwd/passwd_lock.c
|
||||||
|
*
|
||||||
|
* Copyright (C) 2016 Gregory Nutt. All rights reserved.
|
||||||
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||||
|
*
|
||||||
|
* 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 <semaphore.h>
|
||||||
|
#include <assert.h>
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
|
#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;
|
||||||
|
}
|
103
fsutils/passwd/passwd_update.c
Normal file
103
fsutils/passwd/passwd_update.c
Normal file
@ -0,0 +1,103 @@
|
|||||||
|
/****************************************************************************
|
||||||
|
* apps/fsutils/passwd/passwd_update.c
|
||||||
|
*
|
||||||
|
* Copyright (C) 2016 Gregory Nutt. All rights reserved.
|
||||||
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||||
|
*
|
||||||
|
* 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 <semaphore.h>
|
||||||
|
|
||||||
|
#include <apps/fsutils/passwd.h>
|
||||||
|
#include <passwd.h>
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* 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;
|
||||||
|
}
|
106
fsutils/passwd/passwd_verify.c
Normal file
106
fsutils/passwd/passwd_verify.c
Normal file
@ -0,0 +1,106 @@
|
|||||||
|
/****************************************************************************
|
||||||
|
* apps/fsutils/passwd/passwd_verify.c
|
||||||
|
*
|
||||||
|
* Copyright (C) 2016 Gregory Nutt. All rights reserved.
|
||||||
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||||
|
*
|
||||||
|
* 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 <string.h>
|
||||||
|
#include <semaphore.h>
|
||||||
|
|
||||||
|
#include <apps/fsutils/passwd.h>
|
||||||
|
#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;
|
||||||
|
}
|
118
include/fsutils/passwd.h
Normal file
118
include/fsutils/passwd.h
Normal file
@ -0,0 +1,118 @@
|
|||||||
|
/****************************************************************************
|
||||||
|
* apps/include/fsutils/passwd.h
|
||||||
|
*
|
||||||
|
* Copyright (C) 2016 Gregory Nutt. All rights reserved.
|
||||||
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||||
|
*
|
||||||
|
* 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 <nuttx/compiler.h>
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* 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 */
|
Loading…
x
Reference in New Issue
Block a user