Re: [hackers] [sbase][PATCH] Add implementation of tac(1)

2024-03-07 Thread Roberto E. Vargas Caballero
Hi,

As this is a topic more about sbase/ubase organization more
than about patches I am going to move the discussion to the
dev mailing list. Please, answer there instead  of here  in 
hackers.


Regards,



Re: [hackers] [sbase][PATCH] Add implementation of tac(1)

2024-03-07 Thread Roberto E. Vargas Caballero
Hi,

I was thinking about what to do with these patches adding new
commands. They raised a concern about what should be the scope of
sbase. The idea of sbase was to provide a minimal portable POSIX
base, while having ubase for the POSIX commands that cannot be
implemented in a portable way.  Saying that, it is obvious that
there are programs in sbase that are not part of POSIX:

- md5sum
- sha256sum
- sha384sum
- sha512sum
- sponge
- sync
- tar
- install

and maybe some others. At this point is not clear to me what to do
with tools like tac or shuf. There was a small discussion about
this topic in the irc channel, and it was proposed to add a 3rd
repository to contain all these tools that are not part of POSIX
(a bit like the moretools package).  From my point of view, the
main drawback of it is that it requires a 3rd -box program (currently
we have sbase-box and ubase-box). The current situation it not good,
because the two -box are not sharing the library, and the disk space
is duplicated (main reason of -box is to minimize disk space for
restricted environments), and a 3rd -box would make the situation
even worse. But in the other hand, I don't want to add more non
POSIX tools in sbase (in fact, I personally would like to remove
the current non POSIX tools).

I would like to move the discussion here and see what alternatives
we have and how to proceed in this case.

Regards,




[hackers] [sbase][PATCH] tar: support hardlinks during compression

2024-03-07 Thread Andrea Calligaris
This patch depends on:
"tar: fix long names crashing tar archiving"
https://lists.suckless.org/hackers/2402/19071.html

I just did the simplest possible implementation, in order to
have at least something (it's a feature of 'tar' that I do
actually use in my projects, so I needed it).
Insights for more interesting approaches can be found here:
https://lists.suckless.org/dev/2301/35084.html

This should finally cover the specifications, although one
might then want to support even longer paths (more than 150
(prefix) + 100 (name) for regular files, and more than 100
for links); but that should be discussed I think on dev.
---
 tar.c | 101 +++---
 1 file changed, 90 insertions(+), 11 deletions(-)

diff --git a/tar.c b/tar.c
index a2dea4d..a54d66d 100644
--- a/tar.c
+++ b/tar.c
@@ -52,6 +52,16 @@ struct header {
char prefix[155];
 };
 
+/* List of encountered hardlinks. */
+struct hlink {
+   struct hlink *next;
+   dev_t dev;
+   ino_t ino;
+   char linkname[100];
+};
+struct hlink *hlinklist = NULL;
+struct hlink *hlinktail = NULL;
+
 static struct dirtime {
char *name;
time_t mtime;
@@ -182,14 +192,15 @@ archive(const char *path)
char b[BLKSIZ];
struct group *gr;
struct header *h;
+   struct hlink *hlp;
struct passwd *pw;
struct stat st;
size_t chksum, i;
ssize_t l, r;
int fd = -1;
-   size_t path_len;
char tmp_prefix[PATH_MAX];
char *bsname;
+   int found_hlink = 0;
 
if (lstat(path, ) < 0) {
weprintf("lstat %s:", path);
@@ -205,14 +216,13 @@ archive(const char *path)
h = (struct header *)b;
memset(b, 0, sizeof(b));
 
-   path_len = strlen(path);
-   if (path_len > 100 - 1) {
+   if (strlen(path) >= 100) {
// Cover case where path name is too long (in which case we need
// to split it to prefix and name).
bsname = basename((char *)path);
-   strncpy(tmp_prefix, path, PATH_MAX);
+   estrlcpy(tmp_prefix, path, PATH_MAX);
dirname(tmp_prefix);
-   // Could still be too long to fit in the struct.
+   // Could still be too long to fit in the fields.
if (strlen(bsname) >= sizeof(h->name) ||
strlen(tmp_prefix) >= sizeof(h->prefix)) {
eprintf("filename too long: %s\n", path);
@@ -234,11 +244,55 @@ archive(const char *path)
estrlcpy(h->gname,   gr ? gr->gr_name : "",   sizeof(h->gname));
 
if (S_ISREG(st.st_mode)) {
-   h->type = REG;
-   putoctal(h->size, (unsigned)st.st_size,  sizeof(h->size));
-   fd = open(path, O_RDONLY);
-   if (fd < 0)
-   eprintf("open %s:", path);
+   if (st.st_nlink > 1) {
+   /* It's an hardlink */
+   for (hlp = hlinklist; hlp; hlp = hlp->next) {
+   if (hlp->ino == st.st_ino &&
+   hlp->dev == st.st_dev) {
+   /* Found in our list. */
+   found_hlink = 1;
+   h->type = HARDLINK;
+   putoctal(h->size, 0, sizeof(h->size));
+   estrlcpy(
+   h->linkname, hlp->linkname,
+   sizeof(h->linkname));
+   break;
+   }
+   }
+   if (!found_hlink) {
+   /* Never encountered this hardlink before. Let's
+* store it in our list. */
+   if (strlen(h->prefix) > 0)
+   eprintf(
+   "filename too long to be able to "
+   "store it as a hardlink: %s\n",
+   path);
+   struct hlink *new_hlink =
+   ecalloc(1, sizeof(struct hlink));
+   new_hlink->next = NULL;
+   new_hlink->dev = st.st_dev;
+   new_hlink->ino = st.st_ino;
+   estrlcpy(
+   new_hlink->linkname, h->name,
+   sizeof(new_hlink->linkname));
+   if (hlinklist == NULL)
+   hlinklist = new_hlink;
+   else
+   hlinktail->next = new_hlink;
+   hlinktail = new_hlink;
+ 

Re: [hackers] [ubase][PATCH 1/4] su: simplify logic

2024-03-07 Thread Roberto E. Vargas Caballero
Hi,

On Thu, Mar 07, 2024 at 02:52:49PM -0500, neeshy wrote:
> On Thu Mar 7, 2024 at 1:19 PM EST, Roberto E. Vargas Caballero wrote:
> > I think it makes it simpler while keeping the correct behaviour that I 
> > broke.
> 
> Looks good to me!

Pushed!



[hackers] [ubase] su: Fix running it without arguments The commit 8f5a0c3 introduced a regression and the logic to control the number of arguments was broken after it, giving an error when su was exec

2024-03-07 Thread git
commit a570a80ed1606bed43118cb148fc83c3ac22b5c1
Author: Roberto E. Vargas Caballero 
AuthorDate: Thu Mar 7 22:35:31 2024 +0100
Commit: Roberto E. Vargas Caballero 
CommitDate: Thu Mar 7 22:35:31 2024 +0100

su: Fix running it without arguments
The commit 8f5a0c3 introduced a regression and the logic
to control the number of arguments was broken after it,
giving an error when su was executed without parameters.

diff --git a/su.c b/su.c
index eb8bea7..161d2ec 100644
--- a/su.c
+++ b/su.c
@@ -26,7 +26,7 @@ usage(void)
 int
 main(int argc, char *argv[])
 {
-   char *usr = "root", *pass;
+   char *usr, *pass;
char *shell, *envshell, *term;
struct passwd *pw;
char *newargv[3];
@@ -43,9 +43,9 @@ main(int argc, char *argv[])
usage();
} ARGEND;
 
-   if (argc != 1)
+   if (argc > 1)
usage();
-   usr = argv[0];
+   usr = argc > 0 ? argv[0] : "root";
 
errno = 0;
pw = getpwnam(usr);



[hackers] [sbase][PATCH] cron: fix '~' range parsing

2024-03-07 Thread Elie Le Vaillant
'~' after number was recognized as abnormal.
---
 cron.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/cron.c b/cron.c
index 9da0c8a..18ee8b1 100644
--- a/cron.c
+++ b/cron.c
@@ -252,7 +252,7 @@ parserange(char *str, long low, long high, struct range *r)
 
errno = 0;
r->low = strtol(strlow, , 10);
-   if (*e || errno != 0)
+   if ((*e && *e != '~') || errno != 0)
return -1;
if (strhigh) {
if (!*strhigh || range != NULL) /* i.e. N- or N-M-... */
-- 
2.44.0




Re: [hackers] [ubase][PATCH 1/4] su: simplify logic

2024-03-07 Thread neeshy
On Thu Mar 7, 2024 at 1:19 PM EST, Roberto E. Vargas Caballero wrote:
> I think it makes it simpler while keeping the correct behaviour that I broke.

Looks good to me!



Re: [hackers] [ubase][PATCH 1/4] su: simplify logic

2024-03-07 Thread Roberto E. Vargas Caballero
Hi,

On Thu, Mar 07, 2024 at 02:18:28AM -0500, neeshy wrote:
> It seems that the modifications you made break the use case where su is
> called without a username. It would normally default to the root
> user, but now it invokes usage() instead. My original patch worked as
> intended. Could you rebase using the original patch instead? Thank you.
> 

Sadly, I cannot rebase it as it is already in the central repository.
What do you think about adding a new change like this:


diff --git a/su.c b/su.c
index eb8bea7..8eea82b 100644
--- a/su.c
+++ b/su.c
@@ -26,7 +26,7 @@ usage(void)
 int
 main(int argc, char *argv[])
 {
-   char *usr = "root", *pass;
+   char *usr, *pass;
char *shell, *envshell, *term;
struct passwd *pw;
char *newargv[3];
@@ -43,9 +43,14 @@ main(int argc, char *argv[])
usage();
} ARGEND;
 
-   if (argc != 1)
+   if (argc > 1)
usage();
-   usr = argv[0];
+   usr = argc > 0 ? argv[0] : "root";
 
errno = 0;
pw = getpwnam(usr);

I think it makes it simpler while keeping the correct behaviour that I broke.

Kind Regards,
Roberto Vargas



[hackers] [sbase][PATCH] libutil/random: cast to long to avoid overflow

2024-03-07 Thread Elie Le Vaillant
Thanks NRK
---
 libutil/random.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libutil/random.c b/libutil/random.c
index d5214ae..aa98d61 100644
--- a/libutil/random.c
+++ b/libutil/random.c
@@ -67,7 +67,7 @@ rng32_seed_r(uint64_t *state)
 {
struct timespec ts;
clock_gettime(CLOCK_REALTIME, );
-   *state = (intptr_t) ^ ts.tv_sec ^ (ts.tv_nsec * 0xAC5533CD);
+   *state = (intptr_t) ^ ts.tv_sec ^ ((unsigned long)ts.tv_nsec * 
0xAC5533CD);
 }
 
 void
-- 
2.44.0