diff options
author | Tobias Stoeckmann <tobias@stoeckmann.org> | 2023-12-13 20:25:51 +0000 |
---|---|---|
committer | Alejandro Colomar <alx@kernel.org> | 2024-02-14 04:07:32 +0100 |
commit | 55f9635ecf1f637abb86f77ec456f7e69e9e9ebc (patch) | |
tree | 6f96d830ca4d3af7e71b2d85798968dec442193f | |
parent | 25fd8eb4042fa59d6fcce19fc5288df03d8018ea (diff) |
lib/, src/: Remove SCALE definition
SCALE is always DAY (and has to be always DAY), so replace it with DAY
in source code and remove unneeded calculations.
Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Link: <https://github.com/shadow-maint/shadow/pull/876>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
Cherry-picked-from: ecc3508877d2 ("lib/, src/: Remove SCALE definition")
Cc: "Serge E. Hallyn" <serge@hallyn.com>
Link: <https://github.com/shadow-maint/shadow/pull/888>
Link: <https://github.com/shadow-maint/shadow/pull/876>
[alx: This is a pre-requisite for 674409e2265e ("lib/: Saturate addition to avoid overflow")]
Signed-off-by: Alejandro Colomar <alx@kernel.org>
-rw-r--r-- | lib/age.c | 3 | ||||
-rw-r--r-- | lib/defines.h | 3 | ||||
-rw-r--r-- | lib/isexpired.c | 4 | ||||
-rw-r--r-- | lib/pwd2spwd.c | 4 | ||||
-rw-r--r-- | src/chage.c | 28 | ||||
-rw-r--r-- | src/chpasswd.c | 2 | ||||
-rw-r--r-- | src/newusers.c | 4 | ||||
-rw-r--r-- | src/passwd.c | 26 | ||||
-rw-r--r-- | src/pwck.c | 4 | ||||
-rw-r--r-- | src/pwconv.c | 2 | ||||
-rw-r--r-- | src/useradd.c | 22 | ||||
-rw-r--r-- | src/usermod.c | 5 |
12 files changed, 46 insertions, 61 deletions
@@ -139,7 +139,7 @@ int expire (const struct passwd *pw, /*@null@*/const struct spwd *sp) void agecheck (/*@null@*/const struct spwd *sp) { - long now = time(NULL) / SCALE; + long now = time(NULL) / DAY; long remain; if (NULL == sp) { @@ -164,7 +164,6 @@ void agecheck (/*@null@*/const struct spwd *sp) remain = sp->sp_lstchg + sp->sp_max - now; if (remain <= sp->sp_warn) { - remain /= DAY / SCALE; if (remain > 1) { (void) printf (_("Your password will expire in %ld days.\n"), remain); diff --git a/lib/defines.h b/lib/defines.h index 00f3da72..bd32f00b 100644 --- a/lib/defines.h +++ b/lib/defines.h @@ -153,7 +153,6 @@ static inline void memzero(void *ptr, size_t size) * * DAY - seconds / day * WEEK - seconds / week - * SCALE - seconds / aging unit */ /* Solaris defines this in shadow.h */ @@ -163,8 +162,6 @@ static inline void memzero(void *ptr, size_t size) #define WEEK (7*DAY) -#define SCALE DAY - #define WIDTHOF(x) (sizeof(x) * CHAR_BIT) #define NITEMS(arr) (sizeof((arr)) / sizeof((arr)[0])) #define STRLEN(s) (NITEMS(s) - 1) diff --git a/lib/isexpired.c b/lib/isexpired.c index 738b8ef5..45c7601e 100644 --- a/lib/isexpired.c +++ b/lib/isexpired.c @@ -40,7 +40,7 @@ int isexpired (const struct passwd *pw, /*@null@*/const struct spwd *sp) { long now; - now = time(NULL) / SCALE; + now = time(NULL) / DAY; if (NULL == sp) { return 0; @@ -84,7 +84,7 @@ int isexpired (const struct passwd *pw, /*@null@*/const struct spwd *sp) if ( (-1 == sp->sp_lstchg) || (-1 == sp->sp_max) - || (sp->sp_max >= ((10000L * DAY) / SCALE))) { + || (sp->sp_max >= 10000)) { return 0; } diff --git a/lib/pwd2spwd.c b/lib/pwd2spwd.c index 5f87cc53..eea95198 100644 --- a/lib/pwd2spwd.c +++ b/lib/pwd2spwd.c @@ -39,8 +39,8 @@ struct spwd *pwd_to_spwd (const struct passwd *pw) * Defaults used if there is no pw_age information. */ sp.sp_min = 0; - sp.sp_max = (10000L * DAY) / SCALE; - sp.sp_lstchg = gettime () / SCALE; + sp.sp_max = 10000; + sp.sp_lstchg = gettime () / DAY; if (0 == sp.sp_lstchg) { /* Better disable aging than requiring a password * change */ diff --git a/src/chage.c b/src/chage.c index b11ed3d8..8a6d3584 100644 --- a/src/chage.c +++ b/src/chage.c @@ -175,10 +175,10 @@ static int new_fields (void) return 0; } - if (-1 == lstchgdate || lstchgdate > LONG_MAX / SCALE) { + if (-1 == lstchgdate || lstchgdate > LONG_MAX / DAY) { strcpy (buf, "-1"); } else { - date_to_str (sizeof(buf), buf, lstchgdate * SCALE); + date_to_str (sizeof(buf), buf, lstchgdate * DAY); } change_field (buf, sizeof buf, _("Last Password Change (YYYY-MM-DD)")); @@ -206,10 +206,10 @@ static int new_fields (void) return 0; } - if (-1 == expdate || LONG_MAX / SCALE < expdate) { + if (-1 == expdate || LONG_MAX / DAY < expdate) { strcpy (buf, "-1"); } else { - date_to_str (sizeof(buf), buf, expdate * SCALE); + date_to_str (sizeof(buf), buf, expdate * DAY); } change_field (buf, sizeof buf, @@ -258,12 +258,12 @@ static void list_fields (void) * was last modified. The date is the number of days since 1/1/1970. */ (void) fputs (_("Last password change\t\t\t\t\t: "), stdout); - if (lstchgdate < 0 || lstchgdate > LONG_MAX / SCALE) { + if (lstchgdate < 0 || lstchgdate > LONG_MAX / DAY) { (void) puts (_("never")); } else if (lstchgdate == 0) { (void) puts (_("password must be changed")); } else { - changed = lstchgdate * SCALE; + changed = lstchgdate * DAY; print_date (changed); } @@ -275,12 +275,12 @@ static void list_fields (void) if (lstchgdate == 0) { (void) puts (_("password must be changed")); } else if ( (lstchgdate < 0) - || (maxdays >= (10000 * (DAY / SCALE))) + || (maxdays >= 10000) || (maxdays < 0) - || ((LONG_MAX - changed) / SCALE < maxdays)) { + || ((LONG_MAX - changed) / DAY < maxdays)) { (void) puts (_("never")); } else { - expires = changed + maxdays * SCALE; + expires = changed + maxdays * DAY; print_date (expires); } @@ -295,13 +295,13 @@ static void list_fields (void) (void) puts (_("password must be changed")); } else if ( (lstchgdate < 0) || (inactdays < 0) - || (maxdays >= (10000 * (DAY / SCALE))) + || (maxdays >= 10000) || (maxdays < 0) || (maxdays > LONG_MAX - inactdays) - || ((LONG_MAX - changed) / SCALE < maxdays + inactdays)) { + || ((LONG_MAX - changed) / DAY < maxdays + inactdays)) { (void) puts (_("never")); } else { - expires = changed + (maxdays + inactdays) * SCALE; + expires = changed + (maxdays + inactdays) * DAY; print_date (expires); } @@ -310,10 +310,10 @@ static void list_fields (void) * password expiring or not. */ (void) fputs (_("Account expires\t\t\t\t\t\t: "), stdout); - if (expdate < 0 || LONG_MAX / SCALE < expdate) { + if (expdate < 0 || LONG_MAX / DAY < expdate) { (void) puts (_("never")); } else { - expires = expdate * SCALE; + expires = expdate * DAY; print_date (expires); } diff --git a/src/chpasswd.c b/src/chpasswd.c index 1a1a5d59..21d3018f 100644 --- a/src/chpasswd.c +++ b/src/chpasswd.c @@ -621,7 +621,7 @@ int main (int argc, char **argv) if (NULL != sp) { newsp = *sp; newsp.sp_pwdp = cp; - newsp.sp_lstchg = gettime () / SCALE; + newsp.sp_lstchg = gettime () / DAY; if (0 == newsp.sp_lstchg) { /* Better disable aging than requiring a * password change */ diff --git a/src/newusers.c b/src/newusers.c index 08f79798..2a3dd79e 100644 --- a/src/newusers.c +++ b/src/newusers.c @@ -527,7 +527,7 @@ static int add_passwd (struct passwd *pwd, const char *password) } spent.sp_pwdp = cp; } - spent.sp_lstchg = gettime () / SCALE; + spent.sp_lstchg = gettime () / DAY; if (0 == spent.sp_lstchg) { /* Better disable aging than requiring a password * change */ @@ -584,7 +584,7 @@ static int add_passwd (struct passwd *pwd, const char *password) */ spent.sp_pwdp = "!"; #endif - spent.sp_lstchg = gettime () / SCALE; + spent.sp_lstchg = gettime () / DAY; if (0 == spent.sp_lstchg) { /* Better disable aging than requiring a password change */ spent.sp_lstchg = -1; diff --git a/src/passwd.c b/src/passwd.c index 5d59e8c4..3e0d8fde 100644 --- a/src/passwd.c +++ b/src/passwd.c @@ -414,9 +414,9 @@ static void check_password (const struct passwd *pw, const struct spwd *sp) */ if (sp->sp_lstchg > 0) { time_t ok; - ok = (time_t) sp->sp_lstchg * SCALE; + ok = (time_t) sp->sp_lstchg * DAY; if (sp->sp_min > 0) { - ok += (time_t) sp->sp_min * SCALE; + ok += (time_t) sp->sp_min * DAY; } if (now < ok) { @@ -451,15 +451,15 @@ static void print_status (const struct passwd *pw) sp = prefix_getspnam (pw->pw_name); /* local, no need for xprefix_getspnam */ if (NULL != sp) { - date_to_str (sizeof(date), date, sp->sp_lstchg * SCALE), - (void) printf ("%s %s %s %lld %lld %lld %lld\n", + date_to_str (sizeof(date), date, sp->sp_lstchg * DAY), + (void) printf ("%s %s %s %ld %ld %ld %ld\n", pw->pw_name, pw_status (sp->sp_pwdp), date, - ((long long)sp->sp_min * SCALE) / DAY, - ((long long)sp->sp_max * SCALE) / DAY, - ((long long)sp->sp_warn * SCALE) / DAY, - ((long long)sp->sp_inact * SCALE) / DAY); + sp->sp_min, + sp->sp_max, + sp->sp_warn, + sp->sp_inact); } else if (NULL != pw->pw_passwd) { (void) printf ("%s %s\n", pw->pw_name, pw_status (pw->pw_passwd)); @@ -637,21 +637,21 @@ static void update_shadow (void) } nsp->sp_pwdp = update_crypt_pw (nsp->sp_pwdp); if (xflg) { - nsp->sp_max = (age_max * DAY) / SCALE; + nsp->sp_max = age_max; } if (nflg) { - nsp->sp_min = (age_min * DAY) / SCALE; + nsp->sp_min = age_min; } if (wflg) { - nsp->sp_warn = (warn * DAY) / SCALE; + nsp->sp_warn = warn; } if (iflg) { - nsp->sp_inact = (inact * DAY) / SCALE; + nsp->sp_inact = inact; } if (!use_pam) { if (do_update_age) { - nsp->sp_lstchg = gettime () / SCALE; + nsp->sp_lstchg = gettime () / DAY; if (0 == nsp->sp_lstchg) { /* Better disable aging than requiring a password * change */ @@ -609,7 +609,7 @@ static void check_pw_file (int *errors, bool *changed) sp.sp_inact = -1; sp.sp_expire = -1; sp.sp_flag = SHADOW_SP_FLAG_UNSET; - sp.sp_lstchg = gettime () / SCALE; + sp.sp_lstchg = gettime () / DAY; if (0 == sp.sp_lstchg) { /* Better disable aging than * requiring a password change @@ -816,7 +816,7 @@ static void check_spw_file (int *errors, bool *changed) if (!quiet) { time_t t = time (NULL); if ( (t != 0) - && (spw->sp_lstchg > (long) t / SCALE)) { + && (spw->sp_lstchg > (long) t / DAY)) { printf (_("user %s: last password change in the future\n"), spw->sp_namp); *errors += 1; diff --git a/src/pwconv.c b/src/pwconv.c index 356802c4..0788d076 100644 --- a/src/pwconv.c +++ b/src/pwconv.c @@ -247,7 +247,7 @@ int main (int argc, char **argv) spent.sp_flag = SHADOW_SP_FLAG_UNSET; } spent.sp_pwdp = pw->pw_passwd; - spent.sp_lstchg = gettime () / SCALE; + spent.sp_lstchg = gettime () / DAY; if (0 == spent.sp_lstchg) { /* Better disable aging than requiring a password * change */ diff --git a/src/useradd.c b/src/useradd.c index 677ea5a6..5c62dbaf 100644 --- a/src/useradd.c +++ b/src/useradd.c @@ -214,7 +214,6 @@ static struct group * get_local_group (char * grp_name); static void usage (int status); static void new_pwent (struct passwd *); -static long scale_age (long); static void new_spent (struct spwd *); static void grp_update (void); @@ -1008,15 +1007,6 @@ static void new_pwent (struct passwd *pwent) pwent->pw_shell = (char *) user_shell; } -static long scale_age (long x) -{ - if (x <= 0) { - return x; - } - - return x * (DAY / SCALE); -} - /* * new_spent - initialize the values in a shadow password file entry * @@ -1028,17 +1018,17 @@ static void new_spent (struct spwd *spent) memzero (spent, sizeof *spent); spent->sp_namp = (char *) user_name; spent->sp_pwdp = (char *) user_pass; - spent->sp_lstchg = gettime () / SCALE; + spent->sp_lstchg = gettime () / DAY; if (0 == spent->sp_lstchg) { /* Better disable aging than requiring a password change */ spent->sp_lstchg = -1; } if (!rflg) { - spent->sp_min = scale_age (getdef_num ("PASS_MIN_DAYS", -1)); - spent->sp_max = scale_age (getdef_num ("PASS_MAX_DAYS", -1)); - spent->sp_warn = scale_age (getdef_num ("PASS_WARN_AGE", -1)); - spent->sp_inact = scale_age (def_inactive); - spent->sp_expire = scale_age (user_expire); + spent->sp_min = getdef_num ("PASS_MIN_DAYS", -1); + spent->sp_max = getdef_num ("PASS_MAX_DAYS", -1); + spent->sp_warn = getdef_num ("PASS_WARN_AGE", -1); + spent->sp_inact = def_inactive; + spent->sp_expire = user_expire; } else { spent->sp_min = -1; spent->sp_max = -1; diff --git a/src/usermod.c b/src/usermod.c index 9e896802..502d31be 100644 --- a/src/usermod.c +++ b/src/usermod.c @@ -613,7 +613,7 @@ static void new_spent (struct spwd *spent) spent->sp_pwdp = new_pw_passwd (spent->sp_pwdp); if (pflg) { - spent->sp_lstchg = gettime () / SCALE; + spent->sp_lstchg = gettime () / DAY; if (0 == spent->sp_lstchg) { /* Better disable aging than requiring a password * change. */ @@ -1059,7 +1059,6 @@ static void process_flags (int argc, char **argv) Prog, optarg); exit (E_BAD_ARG); } - user_newexpire *= DAY / SCALE; eflg = true; break; case 'f': @@ -1745,7 +1744,7 @@ static void usr_update (void) spent.sp_pwdp = xstrdup (pwent.pw_passwd); pwent.pw_passwd = xstrdup (SHADOW_PASSWD_STRING); - spent.sp_lstchg = gettime () / SCALE; + spent.sp_lstchg = gettime () / DAY; if (0 == spent.sp_lstchg) { /* Better disable aging than * requiring a password change */ |