Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uh, get some more numbers #899

Draft
wants to merge 32 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
d06c81f
lib/typetraits.h: Add macros that give information about a type
alejandro-colomar Dec 1, 2023
b8e914f
src/: Use str2[u]l() instead of atoi(3)
alejandro-colomar Jan 6, 2024
105697f
lib/get_gid.c: get_gid(): Reimplement in terms of a2i()
alejandro-colomar Jan 9, 2024
36db6c5
lib/, libsubid/, po/, src/: get_gid(): Move function to "atoi/getnum.h"
alejandro-colomar Jan 9, 2024
310b3d8
lib/: Don't open-code get_gid()
alejandro-colomar Jan 9, 2024
7b9fe7a
lib/get_pid.c: get_pid(): Reimplement in terms of a2i()
alejandro-colomar Jan 9, 2024
7914f14
lib/: get_pid(): Move function to "atoi/getnum.h"
alejandro-colomar Jan 9, 2024
7d00e66
lib/atoi/getnum.[ch]: get_fd(): Add function for parsing a file descr…
alejandro-colomar Jan 9, 2024
044396c
lib/get_pid.c: get_pidfd_from_fd(): Don't open-code get_fd()
alejandro-colomar Jan 9, 2024
0a81ae8
src/usermod.c: getulong_range(): Reimplement in terms of a2ul()
alejandro-colomar Jan 9, 2024
d9e31c7
lib/get_uid.c: get_uid(): Reimplement in terms of a2i()
alejandro-colomar Jan 9, 2024
5332860
lib/, po/, src/: get_uid(): Move function to "atoi/getnum.h"
alejandro-colomar Jan 9, 2024
4d4b471
lib/limits.c: setrlimit_value(): Reimplement in terms of a2i()
alejandro-colomar Jan 9, 2024
b85ad7a
src/usermod.c: Fix const correctness
alejandro-colomar May 5, 2024
451d07a
lib/getdef.c: getdef_num(): Simplify, by calling a2si() instead of st…
alejandro-colomar Jan 9, 2024
6dee811
lib/getdef.c: getdef_unum(): Fix wrong limit check
alejandro-colomar Jan 9, 2024
49c4471
lib/getdef.c: getdef_long(): Simplify, by calling a2sl() instead of s…
alejandro-colomar Jan 9, 2024
cac5f74
lib/limits.c: set_prio(): Simplify, by calling str2si() instead of st…
alejandro-colomar Jan 9, 2024
d5ab3b8
lib/limits.c: set_umask(): Simplify, by calling str2i(mode_t, ) inste…
alejandro-colomar Jan 9, 2024
4e89e52
lib/limits.c: setup_limits(): Simplify, by calling a2si() instead of …
alejandro-colomar Jan 9, 2024
426ac24
lib/limits.c: setup_limits(): Simplify, by calling str2si() instead o…
alejandro-colomar Jan 9, 2024
2ef41f0
lib/limits.c: setup_limits(): Simplify, by calling str2i(mode_t, ) in…
alejandro-colomar Jan 9, 2024
2b25ce9
lib/sgetspent.c: sgetspent(): Simplify, by calling a2sl() instead of …
alejandro-colomar Jan 9, 2024
bca0f25
lib/shadow.c: my_sgetspent(): Merge 'else {if}' into 'else if'
alejandro-colomar Jan 9, 2024
482ce21
lib/shadow.c: my_sgetspent(): Remove dead code
alejandro-colomar Jan 9, 2024
f3dc975
lib/shadow.c: my_sgetspent(): Simplify error handling
alejandro-colomar Jan 9, 2024
68ee8b5
src/check_subid_range.c: Call get_uid() instead of str2sl()
alejandro-colomar Jan 9, 2024
e6cf1be
src/useradd.c: Simplify, by calling a2sl() instead of str2sl()
alejandro-colomar Jan 9, 2024
dc887b5
src/passwd.c: Simplify, by calling a2sl() instead of str2sl()
alejandro-colomar Jan 9, 2024
2f08f7e
src/usermod.c: Simplify, by calling a2sl() instead of str2sl()
alejandro-colomar Jan 9, 2024
2fb41cd
src/faillog.c: Simplify, by calling str2sh() instead of str2sl()
alejandro-colomar Jan 9, 2024
07497fb
src/chage.c: Simplify, by calling a2sl() instead of str2sl()
alejandro-colomar Jan 9, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/Makefile.am
Expand Up @@ -33,6 +33,8 @@ libshadow_la_SOURCES = \
alloc.h \
atoi/a2i.c \
atoi/a2i.h \
atoi/getnum.c \
atoi/getnum.h \
atoi/str2i.c \
atoi/str2i.h \
atoi/strtoi.c \
Expand Down Expand Up @@ -71,9 +73,7 @@ libshadow_la_SOURCES = \
find_new_sub_gids.c \
find_new_sub_uids.c \
fputsx.c \
get_gid.c \
get_pid.c \
get_uid.c \
getdate.h \
getdate.y \
getdef.c \
Expand Down Expand Up @@ -165,6 +165,7 @@ libshadow_la_SOURCES = \
time/day_to_str.c \
time/day_to_str.h \
ttytype.c \
typetraits.h \
tz.c \
ulimit.c \
user_busy.c \
Expand Down
16 changes: 16 additions & 0 deletions lib/atoi/getnum.c
@@ -0,0 +1,16 @@
// SPDX-FileCopyrightText: 2009, Nicolas François
// SPDX-FileCopyrightText: 2023-2024, Alejandro Colomar <alx@kernel.org>
// SPDX-License-Identifier: BSD-3-Clause


#include <config.h>

#include <sys/types.h>

#include "atoi/getnum.h"


extern inline int get_fd(const char *restrict fdstr, int *restrict fd);
extern inline int get_gid(const char *restrict gidstr, gid_t *restrict gid);
extern inline int get_pid(const char *restrict pidstr, pid_t *restrict pid);
extern inline int get_uid(const char *restrict uidstr, uid_t *restrict uid);
59 changes: 59 additions & 0 deletions lib/atoi/getnum.h
@@ -0,0 +1,59 @@
// SPDX-FileCopyrightText: 2009, Nicolas François
// SPDX-FileCopyrightText: 2023-2024, Alejandro Colomar <alx@kernel.org>
// SPDX-License-Identifier: BSD-3-Clause


#ifndef SHADOW_INCLUDE_LIB_ATOI_GETNUM_H_
#define SHADOW_INCLUDE_LIB_ATOI_GETNUM_H_


#include <config.h>

#include <limits.h>
#include <stddef.h>
#include <sys/types.h>

#include "atoi/a2i.h"
#include "attr.h"
#include "typetraits.h"


ATTR_STRING(1) ATTR_ACCESS(write_only, 2)
inline int get_fd(const char *restrict fdstr, int *restrict fd);
ATTR_STRING(1) ATTR_ACCESS(write_only, 2)
inline int get_gid(const char *restrict gidstr, gid_t *restrict gid);
ATTR_STRING(1) ATTR_ACCESS(write_only, 2)
inline int get_pid(const char *restrict pidstr, pid_t *restrict pid);
ATTR_STRING(1) ATTR_ACCESS(write_only, 2)
inline int get_uid(const char *restrict uidstr, uid_t *restrict uid);


inline int
get_fd(const char *restrict fdstr, int *restrict fd)
{
return a2si(fd, fdstr, NULL, 10, 0, INT_MAX);
}


inline int
get_gid(const char *restrict gidstr, gid_t *restrict gid)
{
return a2i(gid_t, gid, gidstr, NULL, 10, type_min(gid_t), type_max(gid_t));
}


inline int
get_pid(const char *restrict pidstr, pid_t *restrict pid)
{
return a2i(pid_t, pid, pidstr, NULL, 10, 1, type_max(pid_t));
}


inline int
get_uid(const char *restrict uidstr, uid_t *restrict uid)
{
return a2i(uid_t, uid, uidstr, NULL, 10, type_min(uid_t), type_max(uid_t));
}


#endif // include guard
1 change: 1 addition & 0 deletions lib/commonio.c
Expand Up @@ -23,6 +23,7 @@
#include <signal.h>

#include "alloc.h"
#include "atoi/getnum.h"
#include "memzero.h"
#include "nscd.h"
#include "sssd.h"
Expand Down
34 changes: 0 additions & 34 deletions lib/get_gid.c

This file was deleted.

46 changes: 9 additions & 37 deletions lib/get_pid.c
@@ -1,8 +1,7 @@
/*
* SPDX-FileCopyrightText: 2009 , Nicolas François
*
* SPDX-License-Identifier: BSD-3-Clause
*/
// SPDX-FileCopyrightText: 2009, Nicolas François
// SPDX-FileCopyrightText: 2023-2024, Alejandro Colomar <alx@kernel.org>
// SPDX-License-Identifier: BSD-3-Clause


#include <config.h>

Expand All @@ -14,50 +13,23 @@
#include <sys/stat.h>
#include <fcntl.h>

#include "atoi/getnum.h"
#include "string/sprintf.h"


int
get_pid(const char *pidstr, pid_t *pid)
{
char *end;
long long val;

errno = 0;
val = strtoll(pidstr, &end, 10);
if ( ('\0' == *pidstr)
|| ('\0' != *end)
|| (0 != errno)
|| (val < 1)
|| (/*@+longintegral@*/val != (pid_t)val)/*@=longintegral@*/) {
return -1;
}

*pid = val;
return 0;
}

/*
* If use passed in fd:4 as an argument, then return the
* value '4', the fd to use.
* On error, return -1.
*/
int get_pidfd_from_fd(const char *pidfdstr)
{
char *end;
long long val;
int pidfd;
struct stat st;
dev_t proc_st_dev, proc_st_rdev;

errno = 0;
val = strtoll(pidfdstr, &end, 10);
if ( ('\0' == *pidfdstr)
|| ('\0' != *end)
|| (0 != errno)
|| (val < 0)
|| (/*@+longintegral@*/val != (int)val)/*@=longintegral@*/) {
if (get_fd(pidfdstr, &pidfd) == -1)
return -1;
}

if (stat("/proc/self/uid_map", &st) < 0) {
return -1;
Expand All @@ -66,15 +38,15 @@ int get_pidfd_from_fd(const char *pidfdstr)
proc_st_dev = st.st_dev;
proc_st_rdev = st.st_rdev;

if (fstat(val, &st) < 0) {
if (fstat(pidfd, &st) < 0) {
return -1;
}

if (st.st_dev != proc_st_dev || st.st_rdev != proc_st_rdev) {
return -1;
}

return (int)val;
return pidfd;
}

int open_pidfd(const char *pidstr)
Expand Down
34 changes: 0 additions & 34 deletions lib/get_uid.c

This file was deleted.

25 changes: 12 additions & 13 deletions lib/getdef.c
Expand Up @@ -23,6 +23,7 @@
#endif

#include "alloc.h"
#include "atoi/a2i.h"
#include "atoi/str2i.h"
#include "getdef.h"
#include "shadowlog_internal.h"
Expand Down Expand Up @@ -232,10 +233,11 @@ bool getdef_bool (const char *item)
* values are handled.
*/

int getdef_num (const char *item, int dflt)
int
getdef_num(const char *item, int dflt)
{
struct itemdef *d;
long val;
int val;
struct itemdef *d;

if (!def_loaded) {
def_load ();
Expand All @@ -246,9 +248,7 @@ int getdef_num (const char *item, int dflt)
return dflt;
}

if ( (str2sl(&val, d->value) == -1)
|| (val > INT_MAX)
|| (val < -1)) {
if (a2si(&val, d->value, NULL, 0, -1, INT_MAX) == -1) {
fprintf (shadow_logfd,
_("configuration error - cannot parse %s value: '%s'"),
item, d->value);
Expand All @@ -267,10 +267,11 @@ int getdef_num (const char *item, int dflt)
* values are handled.
*/

unsigned int getdef_unum (const char *item, unsigned int dflt)
unsigned int
getdef_unum(const char *item, unsigned int dflt)
{
struct itemdef *d;
long val;
unsigned int val;
struct itemdef *d;

if (!def_loaded) {
def_load ();
Expand All @@ -281,9 +282,7 @@ unsigned int getdef_unum (const char *item, unsigned int dflt)
return dflt;
}

if ( (str2sl(&val, d->value) == -1)
|| (val < 0)
|| (val > INT_MAX)) {
if (a2ui(&val, d->value, NULL, 0, 0, UINT_MAX) == -1) {
fprintf (shadow_logfd,
_("configuration error - cannot parse %s value: '%s'"),
item, d->value);
Expand Down Expand Up @@ -316,7 +315,7 @@ long getdef_long (const char *item, long dflt)
return dflt;
}

if (str2sl(&val, d->value) == -1 || val < -1) {
if (a2sl(&val, d->value, NULL, 0, -1, LONG_MAX) == -1) {
fprintf (shadow_logfd,
_("configuration error - cannot parse %s value: '%s'"),
item, d->value);
Expand Down
42 changes: 19 additions & 23 deletions lib/getgr_nam_gid.c
@@ -1,11 +1,10 @@
/*
* SPDX-FileCopyrightText: 1991 - 1994, Julianne Frances Haugh
* SPDX-FileCopyrightText: 1996 - 2000, Marek Michałkiewicz
* SPDX-FileCopyrightText: 2000 - 2006, Tomasz Kłoczko
* SPDX-FileCopyrightText: 2007 - 2009, Nicolas François
*
* SPDX-License-Identifier: BSD-3-Clause
*/
// SPDX-FileCopyrightText: 1991-1994, Julianne Frances Haugh
// SPDX-FileCopyrightText: 1996-2000, Marek Michałkiewicz
// SPDX-FileCopyrightText: 2000-2006, Tomasz Kłoczko
// SPDX-FileCopyrightText: 2007-2009, Nicolas François
// SPDX-FileCopyrightText: 2023-2024, Alejandro Colomar <alx@kernel.org>
// SPDX-License-Identifier: BSD-3-Clause


#include <config.h>

Expand All @@ -14,30 +13,27 @@
#include <stdlib.h>
#include <errno.h>
#include <grp.h>
#include <sys/types.h>

#include "atoi/getnum.h"
#include "prototypes.h"


/*
* getgr_nam_gid - Return a pointer to the group specified by a string.
* The string may be a valid GID or a valid groupname.
* If the group does not exist on the system, NULL is returned.
*/
extern /*@only@*//*@null@*/struct group *getgr_nam_gid (/*@null@*/const char *grname)
extern /*@only@*//*@null@*/struct group *
getgr_nam_gid(/*@null@*/const char *grname)
{
char *end;
long long gid;
gid_t gid;

if (NULL == grname) {
if (NULL == grname)
return NULL;
}

errno = 0;
gid = strtoll(grname, &end, 10);
if ( ('\0' != *grname)
&& ('\0' == *end)
&& (0 == errno)
&& (/*@+longintegral@*/gid == (gid_t)gid)/*@=longintegral@*/) {
return xgetgrgid (gid);
}
return xgetgrnam (grname);

if (get_gid(grname, &gid) == 0)
return xgetgrgid(gid);
return xgetgrnam(grname);
}