s/qmail 4.3.20
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#include "qmail.h"
27
28buffer b;
29#ifdef HASUTMP
30char bufutmp[sizeof(struct utmp) * 16];
31int fdutmp;
32#endif
35
36#ifdef HASUTMP
37struct utmp ut;
38char line[sizeof(ut.ut_line) + 1];
39#else
40struct utmpx *ut;
41char line[sizeof(ut->ut_line) + 1];
42#endif
43
44stralloc woof = {0};
45stralloc tofrom = {0};
46stralloc text = {0};
47
48static void doit(char *s, int n)
49{
50 if (!stralloc_catb(&text,s,n)) _exit(0);
51 if (text.len > 78) text.len = 78;
52}
53
54static void dobody(stralloc *h) { doit(h->s,h->len); }
55
56static void doheader(stralloc *h)
57{
58 int i;
59
60 if (hfield_known(h->s,h->len) == H_SUBJECT) {
61 i = hfield_skipname(h->s,h->len);
62 doit(h->s + i,h->len - i);
63 }
64}
65
66static void finishheader() { ; }
67
68int main()
69{
70 char *user;
71 char *sender;
72 char *userext;
73 struct stat st;
74 int i;
75
76 if (chdir("/dev") == -1) _exit(0);
77
78 if (!(user = env_get("USER"))) _exit(0);
79 if (!(sender = env_get("SENDER"))) _exit(0);
80 if (!(userext = env_get("LOCAL"))) _exit(0);
81#ifdef HASUTMP
82 if (str_len(user) > sizeof(ut.ut_name)) _exit(0);
83#else
84 if (str_len(user) > sizeof(ut->ut_user)) _exit(0);
85#endif
86
87 if (!stralloc_copys(&tofrom,"*** TO <")) _exit(0);
88 if (!stralloc_cats(&tofrom,userext)) _exit(0);
89 if (!stralloc_cats(&tofrom,"> FROM <")) _exit(0);
90 if (!stralloc_cats(&tofrom,sender)) _exit(0);
91 if (!stralloc_cats(&tofrom,">")) _exit(0);
92
93 for (i = 0; i < tofrom.len; ++i)
94 if ((tofrom.s[i] < 32) || (tofrom.s[i] > 126))
95 tofrom.s[i] = '_';
96
97 if (!stralloc_copys(&text," ")) _exit(0);
98 if (headerbody(buffer_0,doheader,finishheader,dobody) == -1) _exit(0);
99
100 for (i = 0; i < text.len; ++i)
101 if ((text.s[i] < 32) || (text.s[i] > 126))
102 text.s[i] = '/';
103
104 if (!stralloc_copys(&woof,"\015\n\007")) _exit(0);
105 if (!stralloc_cat(&woof,&tofrom)) _exit(0);
106 if (!stralloc_cats(&woof,"\015\n")) _exit(0);
107 if (!stralloc_cat(&woof,&text)) _exit(0);
108 if (!stralloc_cats(&woof,"\015\n")) _exit(0);
109
110#ifdef HASUTMP
111 fdutmp = open_read(UTMP_FILE);
112 if (fdutmp == -1) _exit(0);
113 buffer_init(&b,read,fdutmp,bufutmp,sizeof(bufutmp));
114
115 while (buffer_get(&b,(char *)&ut,sizeof(ut)) == sizeof(ut))
116 if (!str_diffn(ut.ut_name,user,sizeof(ut.ut_name))) {
117#else
118 while ((ut = getutxent()) != 0)
119 if (ut->ut_type == USER_PROCESS && !str_diffn(ut->ut_user,user,sizeof(ut->ut_user))) {
120#endif
121#ifdef HASUTMP
122 byte_copy(line,sizeof(ut.ut_line),ut.ut_line);
123 line[sizeof(ut.ut_line)] = 0;
124#else
125 byte_copy(line,sizeof(ut->ut_line),ut->ut_line);
126 line[sizeof(ut->ut_line)] = 0;
127#endif
128 if (line[0] == '/') continue;
129 if (!line[0]) continue;
130 if (line[str_chr(line,'.')]) continue;
131
132 fdtty = open_append(line);
133 if (fdtty == -1) continue;
134 if (fstat(fdtty,&st) == -1) { close(fdtty); continue; }
135 if (!(st.st_mode & 0100)) { close(fdtty); continue; }
136 if (st.st_uid != getuid()) { close(fdtty); continue; }
137 buffer_init(&b,write,fdtty,buftty,sizeof(buftty));
138 buffer_putflush(&b,woof.s,woof.len);
139 close(fdtty);
140 }
141 _exit(0);
142}
int stralloc_copys(stralloc *, char const *)
void _exit(int)
stralloc sender
Definition: fastforward.c:71
int headerbody(buffer *b, void(*dohf)(), void(*hdone)(), void(*dobl)())
Definition: headerbody.c:21
unsigned int hfield_skipname(char *s, int len)
Definition: hfield.c:96
int hfield_known(char *s, int len)
Definition: hfield.c:57
#define H_SUBJECT
Definition: hfield.h:16
void h(char *, char *, int, int, int)
Definition: install.c:15
struct utmpx * ut
Definition: qbiff.c:40
buffer b
Definition: qbiff.c:28
stralloc text
Definition: qbiff.c:46
char buftty[BUFSIZE_LINE]
Definition: qbiff.c:33
stralloc tofrom
Definition: qbiff.c:45
stralloc woof
Definition: qbiff.c:44
int main()
Definition: qbiff.c:68
char line[sizeof(ut->ut_line)+1]
Definition: qbiff.c:41
int fdtty
Definition: qbiff.c:34
stralloc user
#define BUFSIZE_LINE
Definition: qmail.h:8
void write()