libs/libc/pwd: add new member to passwd struct

Add pw_passwd member to pass tlpi example:
https://man7.org/tlpi/code/online/dist/users_groups/check_password.c.html

Signed-off-by: chenzhijia <chenzhijia@xiaomi.com>
This commit is contained in:
chenzhijia 2024-05-07 13:55:52 +08:00 committed by GUIDINGLI
parent ff4b654be0
commit c0039d6623
10 changed files with 32 additions and 23 deletions

View file

@ -49,6 +49,7 @@
struct passwd
{
FAR char *pw_name;
FAR char *pw_passwd;
uid_t pw_uid;
gid_t pw_gid;
FAR char *pw_gecos;

View file

@ -289,6 +289,7 @@ static int pwd_foreach(pwd_foreach_match_t match, uintptr_t arg,
*ptr++ = '\0';
entry->pw_shell = ROOT_SHELL;
entry->pw_passwd = ROOT_PASSWD;
/* Check for a match */

View file

@ -57,10 +57,10 @@
FAR struct passwd *getpwbuf(uid_t uid, gid_t gid, FAR const char *name,
FAR const char *gecos, FAR const char *dir,
FAR const char *shell)
FAR const char *shell, FAR const char *passwd)
{
FAR struct passwd *pwd = NULL;
int ret = getpwbuf_r(uid, gid, name, gecos, dir, shell, &g_passwd,
int ret = getpwbuf_r(uid, gid, name, gecos, dir, shell, passwd, &g_passwd,
g_passwd_buffer, sizeof(g_passwd_buffer), &pwd);
return ret == 0 ? pwd : NULL;
}

View file

@ -67,20 +67,23 @@
int getpwbuf_r(uid_t uid, gid_t gid, FAR const char *name,
FAR const char *gecos, FAR const char *dir,
FAR const char *shell, FAR struct passwd *pwd,
FAR char *buf, size_t buflen, FAR struct passwd **result)
FAR const char *shell, FAR const char *passwd,
FAR struct passwd *pwd, FAR char *buf, size_t buflen,
FAR struct passwd **result)
{
size_t reqdlen;
size_t nsize;
size_t gsize;
size_t dsize;
size_t ssize;
size_t psize;
nsize = strlen(name) + 1;
gsize = strlen(gecos) + 1;
dsize = strlen(dir) + 1;
ssize = strlen(shell) + 1;
reqdlen = nsize + gsize + dsize + ssize;
psize = strlen(passwd) + 1;
reqdlen = nsize + gsize + dsize + ssize + psize;
if (buflen < reqdlen)
{
@ -94,6 +97,7 @@ int getpwbuf_r(uid_t uid, gid_t gid, FAR const char *name,
pwd->pw_gecos = &buf[nsize];
pwd->pw_dir = &buf[nsize + gsize];
pwd->pw_shell = &buf[nsize + gsize + dsize];
pwd->pw_passwd = &buf[nsize + gsize + dsize + ssize];
pwd->pw_uid = uid;
pwd->pw_gid = gid;
@ -101,6 +105,7 @@ int getpwbuf_r(uid_t uid, gid_t gid, FAR const char *name,
strlcpy(pwd->pw_gecos, gecos, gsize);
strlcpy(pwd->pw_dir, dir, dsize);
strlcpy(pwd->pw_shell, shell, ssize);
strlcpy(pwd->pw_passwd, passwd, psize);
*result = pwd;
return 0;

View file

@ -156,7 +156,7 @@ int getpwent_r(FAR struct passwd *pwd,
}
ret = getpwbuf_r(ROOT_UID, ROOT_GID, ROOT_NAME, ROOT_NAME, ROOT_DIR,
ROOT_SHELL, pwd, buf, buflen, result);
ROOT_SHELL, ROOT_PASSWD, pwd, buf, buflen, result);
if (ret == 0)
{
g_passwd_index++;

View file

@ -74,6 +74,6 @@ FAR struct passwd *getpwnam(FAR const char *name)
}
return getpwbuf(ROOT_UID, ROOT_GID, ROOT_NAME, ROOT_GEOCS, ROOT_DIR,
ROOT_SHELL);
ROOT_SHELL, ROOT_PASSWD);
#endif
}

View file

@ -85,6 +85,6 @@ int getpwnam_r(FAR const char *name, FAR struct passwd *pwd, FAR char *buf,
}
return getpwbuf_r(ROOT_UID, ROOT_GID, ROOT_NAME, ROOT_GEOCS, ROOT_DIR,
ROOT_SHELL, pwd, buf, buflen, result);
ROOT_SHELL, ROOT_PASSWD, pwd, buf, buflen, result);
#endif
}

View file

@ -73,6 +73,6 @@ FAR struct passwd *getpwuid(uid_t uid)
}
return getpwbuf(ROOT_UID, ROOT_GID, ROOT_NAME, ROOT_GEOCS, ROOT_DIR,
ROOT_SHELL);
ROOT_SHELL, ROOT_PASSWD);
#endif
}

View file

@ -84,6 +84,6 @@ int getpwuid_r(uid_t uid, FAR struct passwd *pwd, FAR char *buf,
}
return getpwbuf_r(ROOT_UID, ROOT_GID, ROOT_NAME, ROOT_GEOCS, ROOT_DIR,
ROOT_SHELL, pwd, buf, buflen, result);
ROOT_SHELL, ROOT_PASSWD, pwd, buf, buflen, result);
#endif
}

View file

@ -45,6 +45,7 @@
#define ROOT_GEOCS "root"
#define ROOT_DIR "/root"
#define ROOT_SHELL "/bin/nsh"
#define ROOT_PASSWD "root"
/****************************************************************************
* Public Data
@ -70,11 +71,12 @@ EXTERN char g_passwd_buffer[CONFIG_LIBC_PASSWD_LINESIZE];
FAR struct passwd *getpwbuf(uid_t uid, gid_t gid, FAR const char *name,
FAR const char *gecos, FAR const char *dir,
FAR const char *shell);
FAR const char *shell, FAR const char *passwd);
int getpwbuf_r(uid_t uid, gid_t gid, FAR const char *name,
FAR const char *gecos, FAR const char *dir,
FAR const char *shell, FAR struct passwd *pwd,
FAR char *buf, size_t buflen, FAR struct passwd **result);
FAR const char *shell, FAR const char *passwd,
FAR struct passwd *pwd, FAR char *buf, size_t buflen,
FAR struct passwd **result);
#ifdef CONFIG_LIBC_PASSWD_FILE
int pwd_findby_name(FAR const char *uname, FAR struct passwd *entry,