chiark / gitweb /
Merge branches 'idx/verh' and 'idx/qmqpc'
[qmail] / install.c
1 #include "substdio.h"
2 #include "strerr.h"
3 #include "error.h"
4 #include "open.h"
5 #include "readwrite.h"
6 #include "exit.h"
7
8 extern void hier();
9
10 #define FATAL "install: fatal: "
11
12 int fdsourcedir = -1;
13
14 void h(home,uid,gid,mode)
15 char *home;
16 int uid;
17 int gid;
18 int mode;
19 {
20   if (mkdir(home,0700) == -1)
21     if (errno != error_exist)
22       strerr_die4sys(111,FATAL,"unable to mkdir ",home,": ");
23   if (chown(home,uid,gid) == -1)
24     strerr_die4sys(111,FATAL,"unable to chown ",home,": ");
25   if (chmod(home,mode) == -1)
26     strerr_die4sys(111,FATAL,"unable to chmod ",home,": ");
27 }
28
29 void d(home,subdir,uid,gid,mode)
30 char *home;
31 char *subdir;
32 int uid;
33 int gid;
34 int mode;
35 {
36   if (chdir(home) == -1)
37     strerr_die4sys(111,FATAL,"unable to switch to ",home,": ");
38   if (mkdir(subdir,0700) == -1)
39     if (errno != error_exist)
40       strerr_die6sys(111,FATAL,"unable to mkdir ",home,"/",subdir,": ");
41   if (chown(subdir,uid,gid) == -1)
42     strerr_die6sys(111,FATAL,"unable to chown ",home,"/",subdir,": ");
43   if (chmod(subdir,mode) == -1)
44     strerr_die6sys(111,FATAL,"unable to chmod ",home,"/",subdir,": ");
45 }
46
47 void p(home,fifo,uid,gid,mode)
48 char *home;
49 char *fifo;
50 int uid;
51 int gid;
52 int mode;
53 {
54   if (chdir(home) == -1)
55     strerr_die4sys(111,FATAL,"unable to switch to ",home,": ");
56   if (fifo_make(fifo,0700) == -1)
57     if (errno != error_exist)
58       strerr_die6sys(111,FATAL,"unable to mkfifo ",home,"/",fifo,": ");
59   if (chown(fifo,uid,gid) == -1)
60     strerr_die6sys(111,FATAL,"unable to chown ",home,"/",fifo,": ");
61   if (chmod(fifo,mode) == -1)
62     strerr_die6sys(111,FATAL,"unable to chmod ",home,"/",fifo,": ");
63 }
64
65 char inbuf[SUBSTDIO_INSIZE];
66 char outbuf[SUBSTDIO_OUTSIZE];
67 substdio ssin;
68 substdio ssout;
69
70 void c(home,subdir,file,uid,gid,mode)
71 char *home;
72 char *subdir;
73 char *file;
74 int uid;
75 int gid;
76 int mode;
77 {
78   int fdin;
79   int fdout;
80
81   if (fchdir(fdsourcedir) == -1)
82     strerr_die2sys(111,FATAL,"unable to switch back to source directory: ");
83
84   fdin = open_read(file);
85   if (fdin == -1)
86     strerr_die4sys(111,FATAL,"unable to read ",file,": ");
87   substdio_fdbuf(&ssin,read,fdin,inbuf,sizeof inbuf);
88
89   if (chdir(home) == -1)
90     strerr_die4sys(111,FATAL,"unable to switch to ",home,": ");
91   if (chdir(subdir) == -1)
92     strerr_die6sys(111,FATAL,"unable to switch to ",home,"/",subdir,": ");
93
94   fdout = open_trunc(file);
95   if (fdout == -1)
96     strerr_die6sys(111,FATAL,"unable to write .../",subdir,"/",file,": ");
97   substdio_fdbuf(&ssout,write,fdout,outbuf,sizeof outbuf);
98
99   switch(substdio_copy(&ssout,&ssin)) {
100     case -2:
101       strerr_die4sys(111,FATAL,"unable to read ",file,": ");
102     case -3:
103       strerr_die6sys(111,FATAL,"unable to write .../",subdir,"/",file,": ");
104   }
105
106   close(fdin);
107   if (substdio_flush(&ssout) == -1)
108     strerr_die6sys(111,FATAL,"unable to write .../",subdir,"/",file,": ");
109   if (fsync(fdout) == -1)
110     strerr_die6sys(111,FATAL,"unable to write .../",subdir,"/",file,": ");
111   if (close(fdout) == -1) /* NFS silliness */
112     strerr_die6sys(111,FATAL,"unable to write .../",subdir,"/",file,": ");
113
114   if (chown(file,uid,gid) == -1)
115     strerr_die6sys(111,FATAL,"unable to chown .../",subdir,"/",file,": ");
116   if (chmod(file,mode) == -1)
117     strerr_die6sys(111,FATAL,"unable to chmod .../",subdir,"/",file,": ");
118 }
119
120 void z(home,file,len,uid,gid,mode)
121 char *home;
122 char *file;
123 int len;
124 int uid;
125 int gid;
126 int mode;
127 {
128   int fdout;
129
130   if (chdir(home) == -1)
131     strerr_die4sys(111,FATAL,"unable to switch to ",home,": ");
132
133   fdout = open_trunc(file);
134   if (fdout == -1)
135     strerr_die6sys(111,FATAL,"unable to write ",home,"/",file,": ");
136   substdio_fdbuf(&ssout,write,fdout,outbuf,sizeof outbuf);
137
138   while (len-- > 0)
139     if (substdio_put(&ssout,"",1) == -1)
140       strerr_die6sys(111,FATAL,"unable to write ",home,"/",file,": ");
141
142   if (substdio_flush(&ssout) == -1)
143     strerr_die6sys(111,FATAL,"unable to write ",home,"/",file,": ");
144   if (fsync(fdout) == -1)
145     strerr_die6sys(111,FATAL,"unable to write ",home,"/",file,": ");
146   if (close(fdout) == -1) /* NFS silliness */
147     strerr_die6sys(111,FATAL,"unable to write ",home,"/",file,": ");
148
149   if (chown(file,uid,gid) == -1)
150     strerr_die6sys(111,FATAL,"unable to chown ",home,"/",file,": ");
151   if (chmod(file,mode) == -1)
152     strerr_die6sys(111,FATAL,"unable to chmod ",home,"/",file,": ");
153 }
154
155 void main(argc,argv)
156 int argc;
157 char *argv[];
158 {
159   char *home = 0;
160   if (argc > 1)
161     home = argv[1];
162   fdsourcedir = open_read(".");
163   if (fdsourcedir == -1)
164     strerr_die2sys(111,FATAL,"unable to open current directory: ");
165
166   umask(077);
167   hier(home);
168   _exit(0);
169 }