ezmlmx 0.68
ezmlmx
Loading...
Searching...
No Matches
install.c
Go to the documentation of this file.
1#include <fcntl.h>
2#include <sys/stat.h>
3#include "buffer.h"
4#include "stralloc.h"
5#include "getln.h"
6#include "readwrite.h"
7#include "exit.h"
8#include "open.h"
9#include "error.h"
10#include "byte.h"
11#include "scan.h"
12#include "logmsg.h"
13
14#define WHO "install"
15
16stralloc target = {0};
17char *to;
18
19void nomem() { logmsg(WHO,111,FATAL,"out of memory"); }
20
21char inbuf[BUFFER_INSIZE];
22char outbuf[BUFFER_OUTSIZE];
23buffer bi;
24buffer bo;
25
26void doit(stralloc *line)
27{
28 char *x;
29 unsigned int xlen;
30 unsigned int i;
31 char *type;
32 char *uidstr;
33 char *gidstr;
34 char *modestr;
35 char *mid;
36 char *name;
37 unsigned long uid;
38 unsigned long gid;
39 unsigned long mode;
40 int fdin;
41 int fdout;
42
43 x = line->s; xlen = line->len;
44
45 type = x;
46 i = byte_chr(x,xlen,':'); if (i == xlen) return;
47 x[i++] = 0; x += i; xlen -= i;
48
49 uidstr = x;
50 i = byte_chr(x,xlen,':'); if (i == xlen) return;
51 x[i++] = 0; x += i; xlen -= i;
52
53 gidstr = x;
54 i = byte_chr(x,xlen,':'); if (i == xlen) return;
55 x[i++] = 0; x += i; xlen -= i;
56
57 modestr = x;
58 i = byte_chr(x,xlen,':'); if (i == xlen) return;
59 x[i++] = 0; x += i; xlen -= i;
60
61 mid = x;
62 i = byte_chr(x,xlen,':'); if (i == xlen) return;
63 x[i++] = 0; x += i; xlen -= i;
64
65 name = x;
66 i = byte_chr(x,xlen,':'); if (i == xlen) return;
67 x[i++] = 0; x += i; xlen -= i;
68
69 if (!stralloc_copys(&target,to)) nomem();
70 if (!stralloc_cats(&target,mid)) nomem();
71 if (!stralloc_cats(&target,name)) nomem();
72 if (!stralloc_0(&target)) nomem();
73
74 uid = -1; if (*uidstr) scan_ulong(uidstr,&uid);
75 gid = -1; if (*gidstr) scan_ulong(gidstr,&gid);
76 scan_8long(modestr,&mode);
77
78 switch (*type) {
79 case 'd': if (mkdir(target.s,0700) == -1)
80 if (errno != EEXIST)
81 logmsg(WHO,111,FATAL,B("unable to mkdir :",target.s));
82 break;
83 case 'c': fdin = open_read(name);
84 if (fdin == -1)
85 logmsg(WHO,111,FATAL,B("unable to read :",name));
86 buffer_init(&bi,buffer_unixread,fdin,inbuf,sizeof(inbuf));
87 fdout = open_trunc(target.s);
88 if (fdout == -1)
89 logmsg(WHO,111,FATAL,B("unable to write :",target.s));
90 buffer_init(&bo,buffer_unixwrite,fdout,outbuf,sizeof(outbuf));
91
92 switch (buffer_copy(&bo,&bi)) {
93 case -2: logmsg(WHO,111,FATAL,B("unable to read :",name));
94 case -3: logmsg(WHO,111,FATAL,B("unable to write :",target.s));
95 }
96
97 close(fdin);
98 if (buffer_flush(&bo) == -1)
99 logmsg(WHO,111,FATAL,B("unable to write :",target.s));
100 if (fsync(fdout) == -1)
101 logmsg(WHO,111,FATAL,B("unable to write :",target.s));
102 close(fdout);
103 break;
104 default: return;
105 }
106
107 if (chown(target.s,uid,gid) == -1)
108 logmsg(WHO,111,FATAL,B("unable to chown :",target.s));
109 if (chmod(target.s,mode) == -1)
110 logmsg(WHO,111,FATAL,B("unable to chmod :",target.s));
111}
112
113char buf[256];
114buffer in = BUFFER_INIT(read,0,buf,sizeof(buf));
115stralloc line = {0};
116
117int main(int argc,char **argv)
118{
119 int match;
120
121 umask(077);
122
123 to = argv[1];
124 if (!to) logmsg(WHO,100,USAGE,"install dir");
125
126 for (;;) {
127 if (getln(&in,&line,&match,'\n') == -1)
128 logmsg(WHO,111,FATAL,"unable to read input :");
129 doit(&line);
130 if (!match)
131 _exit(0);
132 }
133
134 return 0;
135}
#define WHO
Definition author.c:1
char inbuf[1024]
char outbuf[1024]
buffer bi
buffer bo
int main()
Definition ezmlm-weed.c:69
stralloc name
Definition ezmlm-split.c:48
int fdin
Definition ezmlm-cron.c:71
int fdout
Definition ezmlm-cron.c:71
void doit(stralloc *line)
Definition install.c:26
char buf[256]
Definition install.c:113
void nomem()
Definition install.c:19
buffer in
Definition install.c:114
unsigned long uid
Definition ezmlm-cgi.c:134
int match
Definition ezmlm-cgi.c:140
stralloc to
Definition ezmlm-clean.c:97
const char * logmsg(const char *dir, unsigned long num, unsigned long listno, unsigned long subs, int done)
Definition loginfo.c:32