summaryrefslogtreecommitdiff
path: root/idedit.c
blob: e6747b503195430de0ada8b76da9af64cc96b783 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <sys/types.h>
#include <pwd.h>
#include <grp.h>
#include "readwrite.h"
#include "exit.h"
#include "scan.h"
#include "fmt.h"
#include "strerr.h"
#include "open.h"
#include "seek.h"
#include "fork.h"

#define FATAL "idedit: fatal: "
#define WARNING "idedit: warning: "

int fd;

void byte(pos,value)
char *pos;
unsigned int value;
{
  unsigned long u;
  unsigned char ch;

  if (pos[scan_ulong(pos,&u)]) return;

  if (seek_set(fd,(seek_pos) u) == -1)
    strerr_die2sys(111,FATAL,"unable to seek: ");

  ch = value;
  if (write(fd,&ch,1) != 1)
    strerr_die2sys(111,FATAL,"unable to write: ");
}

char *args[10];

void run()
{
  int pid;
  int wstat;

  pid = fork();
  if (pid == -1)
    strerr_die2sys(111,FATAL,"unable to fork: ");

  if (pid == 0) {
    execv(*args,args);
    strerr_die4sys(111,WARNING,"unable to run ",*args,": ");
  }

  if (wait_pid(&wstat,pid) != pid)
    strerr_die2sys(111,FATAL,"waitpid surprise");
}

void u(account,group,home,pos0,pos1,pos2,pos3)
char *account;
char *group;
char *home;
char *pos0;
char *pos1;
char *pos2;
char *pos3;
{
  struct passwd *pw;
  unsigned int value;

  pw = getpwnam(account);

  if (!pw && group) {
    args[0] = "add-account";
    args[1] = account;
    args[2] = group;
    args[3] = home;
    args[4] = 0;
    run();
    pw = getpwnam(account);
  }

  if (!pw)
    strerr_die3x(111,FATAL,"unable to find uid for ",account);

  value = pw->pw_uid;
  byte(pos0,value); value >>= 8;
  byte(pos1,value); value >>= 8;
  byte(pos2,value); value >>= 8;
  byte(pos3,value); value >>= 8;
  if (value)
    strerr_die3x(111,FATAL,"excessively large uid for ",account);
}

void g(group,pos0,pos1,pos2,pos3)
char *group;
char *pos0;
char *pos1;
char *pos2;
char *pos3;
{
  struct group *gr;
  unsigned int value;

  gr = getgrnam(group);

  if (!gr) {
    args[0] = "add-group";
    args[1] = group;
    args[2] = 0;
    run();
    gr = getgrnam(group);
  }

  if (!gr)
    strerr_die3x(111,FATAL,"unable to find gid for ",group);

  value = gr->gr_gid;
  byte(pos0,value); value >>= 8;
  byte(pos1,value); value >>= 8;
  byte(pos2,value); value >>= 8;
  byte(pos3,value); value >>= 8;
  if (value)
    strerr_die3x(111,FATAL,"excessively large gid for ",group);
}

void main(argc,argv)
int argc;
char **argv;
{
  if (argc < 42) _exit(100);

  fd = open_write(argv[1]);
  if (fd == -1) strerr_die4sys(111,FATAL,"unable to open ",argv[1],": ");

  g("qmail",argv[34],argv[35],argv[36],argv[37]);
  g("nofiles",argv[38],argv[39],argv[40],argv[41]);

  u("root",(char *) 0,"/",argv[14],argv[15],argv[16],argv[17]);

  u("qmaild","nofiles","/var/qmail",argv[6],argv[7],argv[8],argv[9]);
  u("qmaill","nofiles","/var/qmail",argv[10],argv[11],argv[12],argv[13]);
  u("qmailp","nofiles","/var/qmail",argv[18],argv[19],argv[20],argv[21]);
  u("alias","nofiles","/var/qmail/alias",argv[2],argv[3],argv[4],argv[5]);

  u("qmailq","qmail","/var/qmail",argv[22],argv[23],argv[24],argv[25]);
  u("qmailr","qmail","/var/qmail",argv[26],argv[27],argv[28],argv[29]);
  u("qmails","qmail","/var/qmail",argv[30],argv[31],argv[32],argv[33]);

  _exit(0);
}