Skip to content

Commit

Permalink
instchown: honor DESTDIR
Browse files Browse the repository at this point in the history
  • Loading branch information
DerDakon committed Jan 28, 2024
1 parent 0bbd31a commit 21b74e7
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Makefile
Expand Up @@ -712,9 +712,9 @@ compile instcheck.c strerr.h error.h readwrite.h hier.h

instchown: \
load instchown.o instuidgid.o fifo.o hier.o auto_qmail.o auto_split.o \
ids.a strerr.a substdio.a error.a str.a fs.a
ids.a strerr.a substdio.a error.a str.a env.a fs.a stralloc.a
./load instchown instuidgid.o fifo.o hier.o auto_qmail.o auto_split.o \
ids.a strerr.a substdio.a error.a str.a fs.a
ids.a strerr.a substdio.a error.a str.a env.a fs.a stralloc.a

instchown.o: \
compile instchown.c strerr.h error.h exit.h hier.h
Expand Down
38 changes: 38 additions & 0 deletions instchown.c
@@ -1,7 +1,10 @@
#include <sys/stat.h>
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "env.h"
#include "stralloc.h"
#include "strerr.h"
#include "error.h"
#include "hier.h"
Expand All @@ -10,36 +13,66 @@ extern void init_uidgid();

#define FATAL "instchown: fatal: "

static void die_nomem()
{
strerr_die2sys(111,FATAL,"out of memory");
}

static void ddhome(stralloc *dd, const char *home)
{
const char *denv = env_get("DESTDIR");
if (denv)
if (!stralloc_copys(dd,denv)) die_nomem();

if (!stralloc_catb(dd,home,strlen(home))) die_nomem();
if (!stralloc_0(dd)) die_nomem();
}

void h(char *home, uid_t uid, gid_t gid, int mode)
{
stralloc dh = { 0 };
ddhome(&dh, home);
home=dh.s;
if (chown(home,uid,gid) == -1)
strerr_die4sys(111,FATAL,"unable to chown ",home,": ");
if (chmod(home,mode) == -1)
strerr_die4sys(111,FATAL,"unable to chmod ",home,": ");
free(dh.s);
}

void d(char *home, char *subdir, uid_t uid, gid_t gid, int mode)
{
stralloc dh = { 0 };
ddhome(&dh, home);
home=dh.s;
if (chdir(home) == -1)
strerr_die4sys(111,FATAL,"unable to switch to ",home,": ");
if (chown(subdir,uid,gid) == -1)
strerr_die6sys(111,FATAL,"unable to chown ",home,"/",subdir,": ");
if (chmod(subdir,mode) == -1)
strerr_die6sys(111,FATAL,"unable to chmod ",home,"/",subdir,": ");
free(dh.s);
}

void p(char *home, char *fifo, uid_t uid, gid_t gid, int mode)
{
stralloc dh = { 0 };
ddhome(&dh, home);
home=dh.s;
if (chdir(home) == -1)
strerr_die4sys(111,FATAL,"unable to switch to ",home,": ");
if (chown(fifo,uid,gid) == -1)
strerr_die6sys(111,FATAL,"unable to chown ",home,"/",fifo,": ");
if (chmod(fifo,mode) == -1)
strerr_die6sys(111,FATAL,"unable to chmod ",home,"/",fifo,": ");
free(dh.s);
}

void c(char *home, char *subdir, char *file, uid_t uid, gid_t gid, int mode)
{
stralloc dh = { 0 };
ddhome(&dh, home);
home=dh.s;
if (chdir(home) == -1)
strerr_die4sys(111,FATAL,"unable to switch to ",home,": ");
if (chdir(subdir) == -1) {
Expand All @@ -56,16 +89,21 @@ void c(char *home, char *subdir, char *file, uid_t uid, gid_t gid, int mode)
}
if (chmod(file,mode) == -1)
strerr_die6sys(111,FATAL,"unable to chmod .../",subdir,"/",file,": ");
free(dh.s);
}

void z(char *home, char *file, int len, uid_t uid, gid_t gid, int mode)
{
stralloc dh = { 0 };
ddhome(&dh, home);
home=dh.s;
if (chdir(home) == -1)
strerr_die4sys(111,FATAL,"unable to switch to ",home,": ");
if (chown(file,uid,gid) == -1)
strerr_die6sys(111,FATAL,"unable to chown ",home,"/",file,": ");
if (chmod(file,mode) == -1)
strerr_die6sys(111,FATAL,"unable to chmod ",home,"/",file,": ");
free(dh.s);
}

int main(void)
Expand Down

0 comments on commit 21b74e7

Please sign in to comment.