debian/rules: Use `git' potty wrapper.
[qmail] / qmail-popup.c
CommitLineData
212b6f5d 1#include "commands.h"
2117e02e
MW
2#include "fd.h"
3#include "sig.h"
2117e02e
MW
4#include "stralloc.h"
5#include "substdio.h"
2117e02e 6#include "alloc.h"
2117e02e
MW
7#include "wait.h"
8#include "str.h"
212b6f5d 9#include "byte.h"
2117e02e
MW
10#include "now.h"
11#include "fmt.h"
12#include "exit.h"
13#include "readwrite.h"
212b6f5d
MW
14#include "timeoutread.h"
15#include "timeoutwrite.h"
2117e02e 16
212b6f5d 17void die() { _exit(1); }
2117e02e 18
212b6f5d 19int saferead(fd,buf,len) int fd; char *buf; int len;
2117e02e 20{
212b6f5d
MW
21 int r;
22 r = timeoutread(1200,fd,buf,len);
23 if (r <= 0) die();
24 return r;
2117e02e
MW
25}
26
212b6f5d
MW
27int safewrite(fd,buf,len) int fd; char *buf; int len;
28{
29 int r;
30 r = timeoutwrite(1200,fd,buf,len);
31 if (r <= 0) die();
32 return r;
33}
2117e02e 34
212b6f5d
MW
35char ssoutbuf[128];
36substdio ssout = SUBSTDIO_FDBUF(safewrite,1,ssoutbuf,sizeof ssoutbuf);
2117e02e 37
212b6f5d
MW
38char ssinbuf[128];
39substdio ssin = SUBSTDIO_FDBUF(saferead,0,ssinbuf,sizeof ssinbuf);
40
41void puts(s) char *s;
2117e02e 42{
212b6f5d 43 substdio_puts(&ssout,s);
2117e02e 44}
212b6f5d 45void flush()
2117e02e 46{
212b6f5d 47 substdio_flush(&ssout);
2117e02e
MW
48}
49void err(s) char *s;
50{
212b6f5d
MW
51 puts("-ERR ");
52 puts(s);
53 puts("\r\n");
54 flush();
2117e02e 55}
212b6f5d 56
2117e02e
MW
57void die_usage() { err("usage: popup hostname subprogram"); die(); }
58void die_nomem() { err("out of memory"); die(); }
59void die_pipe() { err("unable to open pipe"); die(); }
60void die_write() { err("unable to write pipe"); die(); }
61void die_fork() { err("unable to fork"); die(); }
62void die_childcrashed() { err("aack, child crashed"); }
63void die_badauth() { err("authorization failed"); }
64
65void err_syntax() { err("syntax error"); }
66void err_wantuser() { err("USER first"); }
67void err_authoriz() { err("authorization first"); }
68
212b6f5d
MW
69void okay() { puts("+OK \r\n"); flush(); }
70void pop3_quit() { okay(); die(); }
2117e02e
MW
71
72
73char unique[FMT_ULONG + FMT_ULONG + 3];
74char *hostname;
75stralloc username = {0};
76int seenuser = 0;
77char **childargs;
78substdio ssup;
79char upbuf[128];
80
81
82void doanddie(user,userlen,pass)
83char *user;
84unsigned int userlen; /* including 0 byte */
85char *pass;
86{
212b6f5d
MW
87 int child;
88 int wstat;
89 int pi[2];
90
91 if (fd_copy(2,1) == -1) die_pipe();
92 close(3);
93 if (pipe(pi) == -1) die_pipe();
94 if (pi[0] != 3) die_pipe();
95 switch(child = fork()) {
96 case -1:
97 die_fork();
98 case 0:
99 close(pi[1]);
100 sig_pipedefault();
101 execvp(*childargs,childargs);
102 _exit(1);
2117e02e 103 }
212b6f5d
MW
104 close(pi[0]);
105 substdio_fdbuf(&ssup,write,pi[1],upbuf,sizeof upbuf);
106 if (substdio_put(&ssup,user,userlen) == -1) die_write();
107 if (substdio_put(&ssup,pass,str_len(pass) + 1) == -1) die_write();
108 if (substdio_puts(&ssup,"<") == -1) die_write();
109 if (substdio_puts(&ssup,unique) == -1) die_write();
110 if (substdio_puts(&ssup,hostname) == -1) die_write();
111 if (substdio_put(&ssup,">",2) == -1) die_write();
112 if (substdio_flush(&ssup) == -1) die_write();
113 close(pi[1]);
114 byte_zero(pass,str_len(pass));
115 byte_zero(upbuf,sizeof upbuf);
116 if (wait_pid(&wstat,child) == -1) die();
117 if (wait_crashed(wstat)) die_childcrashed();
118 if (wait_exitcode(wstat)) die_badauth();
119 die();
2117e02e
MW
120}
121void pop3_greet()
122{
212b6f5d
MW
123 char *s;
124 s = unique;
125 s += fmt_uint(s,getpid());
126 *s++ = '.';
127 s += fmt_ulong(s,(unsigned long) now());
128 *s++ = '@';
129 *s++ = 0;
130 puts("+OK <");
131 puts(unique);
132 puts(hostname);
133 puts(">\r\n");
134 flush();
2117e02e
MW
135}
136void pop3_user(arg) char *arg;
137{
212b6f5d
MW
138 if (!*arg) { err_syntax(); return; }
139 okay();
140 seenuser = 1;
141 if (!stralloc_copys(&username,arg)) die_nomem();
142 if (!stralloc_0(&username)) die_nomem();
2117e02e
MW
143}
144void pop3_pass(arg) char *arg;
145{
212b6f5d
MW
146 if (!seenuser) { err_wantuser(); return; }
147 if (!*arg) { err_syntax(); return; }
148 doanddie(username.s,username.len,arg);
2117e02e
MW
149}
150void pop3_apop(arg) char *arg;
151{
212b6f5d
MW
152 char *space;
153 space = arg + str_chr(arg,' ');
154 if (!*space) { err_syntax(); return; }
155 *space++ = 0;
156 doanddie(arg,space - arg,space);
2117e02e
MW
157}
158
212b6f5d
MW
159struct commands pop3commands[] = {
160 { "user", pop3_user, 0 }
161, { "pass", pop3_pass, 0 }
162, { "apop", pop3_apop, 0 }
163, { "quit", pop3_quit, 0 }
164, { "noop", okay, 0 }
165, { 0, err_authoriz, 0 }
166} ;
2117e02e
MW
167
168void main(argc,argv)
169int argc;
170char **argv;
171{
212b6f5d
MW
172 sig_alarmcatch(die);
173 sig_pipeignore();
174
175 hostname = argv[1];
176 if (!hostname) die_usage();
177 childargs = argv + 2;
178 if (!*childargs) die_usage();
179
180 pop3_greet();
181 commands(&ssin,pop3commands);
182 die();
2117e02e 183}