s/qmail 4.2.29a
Next generation secure email transport
Loading...
Searching...
No Matches
qbiff.c
Go to the documentation of this file.
1#include <sys/types.h>
2#include <sys/stat.h>
3#include <unistd.h>
4#include "hasutmp.h"
5#ifdef HASUTMP
6#include <utmp.h>
7#ifndef UTMP_FILE
8#ifdef _PATH_UTMP
9#define UTMP_FILE _PATH_UTMP
10#else
11#define UTMP_FILE "/etc/utmp"
12#endif
13#endif
14#else
15#include <utmpx.h>
16#endif
17#include "stralloc.h"
18#include "buffer.h"
19#include "open.h"
20#include "byte.h"
21#include "str.h"
22#include "headerbody.h"
23#include "hfield.h"
24#include "env.h"
25#include "exit.h"
26
27buffer b;
28#ifdef HASUTMP
29char bufutmp[sizeof(struct utmp) * 16];
30int fdutmp;
31#endif
32char buftty[1024];
34
35#ifdef HASUTMP
36struct utmp ut;
37char line[sizeof(ut.ut_line) + 1];
38#else
39struct utmpx *ut;
40char line[sizeof(ut->ut_line) + 1];
41#endif
42
43stralloc woof = {0};
44stralloc tofrom = {0};
45stralloc text = {0};
46
47void doit(char *s, int n)
48{
49 if (!stralloc_catb(&text,s,n)) _exit(0);
50 if (text.len > 78) text.len = 78;
51}
52
53void dobody(stralloc *h) { doit(h->s,h->len); }
54
55void doheader(stralloc *h)
56{
57 int i;
58
59 if (hfield_known(h->s,h->len) == H_SUBJECT) {
60 i = hfield_skipname(h->s,h->len);
61 doit(h->s + i,h->len - i);
62 }
63}
64
65void finishheader() { ; }
66
67int main()
68{
69 char *user;
70 char *sender;
71 char *userext;
72 struct stat st;
73 int i;
74
75 if (chdir("/dev") == -1) _exit(0);
76
77 if (!(user = env_get("USER"))) _exit(0);
78 if (!(sender = env_get("SENDER"))) _exit(0);
79 if (!(userext = env_get("LOCAL"))) _exit(0);
80#ifdef HASUTMP
81 if (str_len(user) > sizeof(ut.ut_name)) _exit(0);
82#else
83 if (str_len(user) > sizeof(ut->ut_user)) _exit(0);
84#endif
85
86 if (!stralloc_copys(&tofrom,"*** TO <")) _exit(0);
87 if (!stralloc_cats(&tofrom,userext)) _exit(0);
88 if (!stralloc_cats(&tofrom,"> FROM <")) _exit(0);
89 if (!stralloc_cats(&tofrom,sender)) _exit(0);
90 if (!stralloc_cats(&tofrom,">")) _exit(0);
91
92 for (i = 0; i < tofrom.len; ++i)
93 if ((tofrom.s[i] < 32) || (tofrom.s[i] > 126))
94 tofrom.s[i] = '_';
95
96 if (!stralloc_copys(&text," ")) _exit(0);
97 if (headerbody(buffer_0,doheader,finishheader,dobody) == -1) _exit(0);
98
99 for (i = 0; i < text.len; ++i)
100 if ((text.s[i] < 32) || (text.s[i] > 126))
101 text.s[i] = '/';
102
103 if (!stralloc_copys(&woof,"\015\n\007")) _exit(0);
104 if (!stralloc_cat(&woof,&tofrom)) _exit(0);
105 if (!stralloc_cats(&woof,"\015\n")) _exit(0);
106 if (!stralloc_cat(&woof,&text)) _exit(0);
107 if (!stralloc_cats(&woof,"\015\n")) _exit(0);
108
109#ifdef HASUTMP
110 fdutmp = open_read(UTMP_FILE);
111 if (fdutmp == -1) _exit(0);
112 buffer_init(&b,read,fdutmp,bufutmp,sizeof(bufutmp));
113
114 while (buffer_get(&b,&ut,sizeof(ut)) == sizeof(ut))
115 if (!str_diffn(ut.ut_name,user,sizeof(ut.ut_name))) {
116#else
117 while ((ut = getutxent()) != 0)
118 if (ut->ut_type == USER_PROCESS && !str_diffn(ut->ut_user,user,sizeof(ut->ut_user))) {
119#endif
120#ifdef HASUTMP
121 byte_copy(line,sizeof(ut.ut_line),ut.ut_line);
122 line[sizeof(ut.ut_line)] = 0;
123#else
124 byte_copy(line,sizeof(ut->ut_line),ut->ut_line);
125 line[sizeof(ut->ut_line)] = 0;
126#endif
127 if (line[0] == '/') continue;
128 if (!line[0]) continue;
129 if (line[str_chr(line,'.')]) continue;
130
131 fdtty = open_append(line);
132 if (fdtty == -1) continue;
133 if (fstat(fdtty,&st) == -1) { close(fdtty); continue; }
134 if (!(st.st_mode & 0100)) { close(fdtty); continue; }
135 if (st.st_uid != getuid()) { close(fdtty); continue; }
136 buffer_init(&b,write,fdtty,buftty,sizeof(buftty));
137 buffer_putflush(&b,woof.s,woof.len);
138 close(fdtty);
139 }
140 _exit(0);
141}
int stralloc_copys(stralloc *, char const *)
void _exit()
stralloc sender
Definition: fastforward.c:71
int headerbody()
int hfield_known()
#define H_SUBJECT
Definition: hfield.h:16
unsigned int hfield_skipname()
void h(char *, int, int, int)
Definition: install.c:15
void doit()
Definition: newaliases.c:256
struct utmpx * ut
Definition: qbiff.c:39
void dobody(stralloc *h)
Definition: qbiff.c:53
buffer b
Definition: qbiff.c:27
stralloc text
Definition: qbiff.c:45
stralloc tofrom
Definition: qbiff.c:44
stralloc woof
Definition: qbiff.c:43
char buftty[1024]
Definition: qbiff.c:32
int main()
Definition: qbiff.c:67
void doheader(stralloc *h)
Definition: qbiff.c:55
void finishheader()
Definition: qbiff.c:65
char line[sizeof(ut->ut_line)+1]
Definition: qbiff.c:40
int fdtty
Definition: qbiff.c:33
stralloc user
int userext()
Definition: qmail-getpw.c:23
void write()