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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
|
#include "substdio.h"
#include "strerr.h"
#include "stralloc.h"
#include "getln.h"
#include "open.h"
#include "readwrite.h"
#include "token822.h"
#include "control.h"
#include "auto_qmail.h"
#include "case.h"
#include "cdbmss.h"
#define FATAL "newaliases: fatal: "
void nomem()
{
strerr_die2x(111,FATAL,"out of memory");
}
void nulbyte()
{
strerr_die2x(100,FATAL,"NUL bytes are not permitted");
}
void longaddress()
{
strerr_die2x(100,FATAL,"addresses over 800 bytes are not permitted");
}
void writeerr()
{
strerr_die2sys(111,FATAL,"unable to write to /etc/aliases.tmp: ");
}
void readerr()
{
strerr_die2sys(111,FATAL,"unable to read /etc/aliases: ");
}
void die_control()
{
strerr_die2sys(111,FATAL,"unable to read controls: ");
}
stralloc me = {0};
stralloc defaulthost = {0};
stralloc defaultdomain = {0};
stralloc plusdomain = {0};
void readcontrols()
{
int r;
int fddir;
fddir = open_read(".");
if (fddir == -1)
strerr_die2sys(111,FATAL,"unable to open current directory: ");
if (chdir(auto_qmail) == -1)
strerr_die4sys(111,FATAL,"unable to chdir to ",auto_qmail,": ");
r = control_readline(&me,"control/me");
if (r == -1) die_control();
if (!r) if (!stralloc_copys(&me,"me")) nomem();
r = control_readline(&defaultdomain,"control/defaultdomain");
if (r == -1) die_control();
if (!r) if (!stralloc_copy(&defaultdomain,&me)) nomem();
r = control_readline(&defaulthost,"control/defaulthost");
if (r == -1) die_control();
if (!r) if (!stralloc_copy(&defaulthost,&me)) nomem();
r = control_readline(&plusdomain,"control/plusdomain");
if (r == -1) die_control();
if (!r) if (!stralloc_copy(&plusdomain,&me)) nomem();
if (fchdir(fddir) == -1)
strerr_die2sys(111,FATAL,"unable to set current directory: ");
}
stralloc target = {0};
stralloc fulltarget = {0};
stralloc instr = {0};
stralloc cbuf = {0};
token822_alloc toks = {0};
token822_alloc tokaddr = {0};
stralloc address = {0};
void gotincl()
{
token822_reverse(&tokaddr);
if (token822_unquote(&address,&tokaddr) != 1) nomem();
tokaddr.len = 0;
if (!address.len)
strerr_die2x(111,FATAL,"empty :include: filenames not permitted");
if (byte_chr(address.s,address.len,'\0') < address.len)
strerr_die2x(111,FATAL,"NUL not permitted in :include: filenames");
if ((address.s[0] != '.') && (address.s[0] != '/'))
if (!stralloc_cats(&instr,"./")) nomem();
if (!stralloc_cat(&instr,&address)) nomem();
if (!stralloc_cats(&instr,".bin")) nomem();
if (!stralloc_0(&instr)) nomem();
}
void gotaddr()
{
int i;
int j;
int flaghasat;
token822_reverse(&tokaddr);
if (token822_unquote(&address,&tokaddr) != 1) nomem();
if (!address.len)
strerr_die2x(111,FATAL,"empty recipient addresses not permitted");
flaghasat = 0;
for (i = 0;i < tokaddr.len;++i)
if (tokaddr.t[i].type == TOKEN822_AT)
flaghasat = 1;
tokaddr.len = 0;
if (!address.len) return;
if (!flaghasat)
if (address.s[0] == '/') {
if (!stralloc_0(&address)) nomem();
strerr_die4x(111,FATAL,"file delivery ",address.s," not supported");
}
if (!flaghasat)
if (address.s[0] == '|') {
if (byte_chr(address.s,address.len,'\0') < address.len)
strerr_die2x(111,FATAL,"NUL not permitted in program names");
if (!stralloc_cats(&instr,"!")) nomem();
if (!stralloc_catb(&instr,address.s + 1,address.len - 1)) nomem();
if (!stralloc_0(&instr)) nomem();
return;
}
if (target.len) {
if (!stralloc_cats(&instr,"&")) nomem();
if (!stralloc_cat(&instr,&fulltarget)) nomem();
if (!stralloc_0(&instr)) nomem();
}
if (!flaghasat)
if (!stralloc_cats(&address,"@")) nomem();
if (!stralloc_copy(&target,&address)) nomem();
if (!stralloc_copy(&fulltarget,&address)) nomem();
if (fulltarget.s[fulltarget.len - 1] == '@')
if (!stralloc_cat(&fulltarget,&defaulthost)) nomem();
if (fulltarget.s[fulltarget.len - 1] == '+') {
fulltarget.s[fulltarget.len - 1] = '.';
if (!stralloc_cat(&fulltarget,&plusdomain)) nomem();
}
j = 0;
for (i = 0;i < fulltarget.len;++i) if (fulltarget.s[i] == '@') j = i;
for (i = j;i < fulltarget.len;++i) if (fulltarget.s[i] == '.') break;
if (i == fulltarget.len) {
if (!stralloc_cats(&fulltarget,".")) nomem();
if (!stralloc_cat(&fulltarget,&defaultdomain)) nomem();
}
if (fulltarget.len > 800) longaddress();
if (byte_chr(fulltarget.s,fulltarget.len,'\0') < fulltarget.len)
strerr_die2x(111,FATAL,"NUL not permitted in recipient addresses");
}
stralloc line = {0};
stralloc newline = {0};
int match;
void parseerr()
{
if (!stralloc_0(&line)) nomem();
strerr_die3x(111,FATAL,"unable to parse this line: ",line.s);
}
void parseline()
{
int wordok;
struct token822 *t;
struct token822 *beginning;
switch(token822_parse(&toks,&line,&cbuf)) {
case -1: nomem();
case 0: parseerr();
}
beginning = toks.t;
t = toks.t + toks.len;
wordok = 1;
if (!token822_readyplus(&tokaddr,1)) nomem();
tokaddr.len = 0;
while (t > beginning)
switch((--t)->type) {
case TOKEN822_SEMI:
break; /*XXX*/
case TOKEN822_COLON:
if (t >= beginning + 2)
if (t[-2].type == TOKEN822_COLON)
if (t[-1].type == TOKEN822_ATOM)
if (t[-1].slen == 7)
if (!byte_diff(t[-1].s,7,"include")) {
gotincl();
t -= 2;
}
break; /*XXX*/
case TOKEN822_RIGHT:
if (tokaddr.len) gotaddr();
while ((t > beginning) && (t[-1].type != TOKEN822_LEFT))
if (!token822_append(&tokaddr,--t)) nomem();
gotaddr();
if (t <= beginning) parseerr();
--t;
while ((t > beginning) && ((t[-1].type == TOKEN822_COMMENT) || (t[-1].type == TOKEN822_ATOM) || (t[-1].type == TOKEN822_QUOTE) || (t[-1].type == TOKEN822_AT) || (t[-1].type == TOKEN822_DOT)))
--t;
wordok = 0;
continue;
case TOKEN822_ATOM: case TOKEN822_QUOTE: case TOKEN822_LITERAL:
if (!wordok) if (tokaddr.len) gotaddr();
wordok = 0;
if (!token822_append(&tokaddr,t)) nomem();
continue;
case TOKEN822_COMMENT:
/* comment is lexically a space; shouldn't affect wordok */
break;
case TOKEN822_COMMA:
if (tokaddr.len) gotaddr();
wordok = 1;
break;
default:
wordok = 1;
if (!token822_append(&tokaddr,t)) nomem();
continue;
}
if (tokaddr.len) gotaddr();
}
char inbuf[1024];
substdio ssin;
struct cdbmss cdbmss;
stralloc key = {0};
void doit()
{
if (!instr.len) {
if (target.len) parseerr();
return;
}
if (!target.len) parseerr();
if (stralloc_starts(&target,"owner-")) {
if (!stralloc_copys(&key,"?")) nomem();
if (!stralloc_catb(&key,target.s + 6,target.len - 6)) nomem();
case_lowerb(key.s,key.len);
if (cdbmss_add(&cdbmss,key.s,key.len,fulltarget.s,fulltarget.len) == -1) writeerr();
}
if (!stralloc_copys(&key,":")) nomem();
if (!stralloc_cat(&key,&target)) nomem();
case_lowerb(key.s,key.len);
if (cdbmss_add(&cdbmss,key.s,key.len,instr.s,instr.len) == -1) writeerr();
}
void main()
{
int fd;
umask(033);
readcontrols();
fd = open_read("/etc/aliases");
if (fd == -1) readerr();
substdio_fdbuf(&ssin,read,fd,inbuf,sizeof inbuf);
fd = open_trunc("/etc/aliases.tmp");
if (fd == -1) strerr_die2sys(111,FATAL,"unable to create /etc/aliases.tmp: ");
if (cdbmss_start(&cdbmss,fd) == -1) writeerr();
if (!stralloc_copys(&line,"")) nomem();
for (;;) {
if (getln(&ssin,&newline,&match,'\n') != 0) readerr();
if (match && (newline.s[0] == '\n')) continue;
if (match && ((newline.s[0] == ' ') || (newline.s[0] == '\t'))) {
if (!stralloc_cat(&line,&newline)) nomem();
continue;
}
if (line.len)
if (line.s[0] != '#') {
if (!stralloc_copys(&target,"")) nomem();
if (!stralloc_copys(&fulltarget,"")) nomem();
if (!stralloc_copys(&instr,"")) nomem();
parseline();
doit();
}
if (!match) break;
if (!stralloc_copy(&line,&newline)) nomem();
}
if (cdbmss_finish(&cdbmss) == -1) writeerr();
if (fsync(fd) == -1) writeerr();
if (close(fd) == -1) writeerr(); /* NFS stupidity */
if (rename("/etc/aliases.tmp","/etc/aliases.cdb") == -1)
strerr_die2sys(111,FATAL,"unable to move /etc/aliases.tmp to /etc/aliases.cdb: ");
_exit(0);
}
|