6 #define UTMP_FILE _PATH_UTMP
8 #define UTMP_FILE "/etc/utmp"
11 #include "readwrite.h"
18 #include "headerbody.h"
24 char bufutmp
[sizeof(struct utmp
) * 16];
31 char line
[sizeof(ut
.ut_line
) + 1];
33 stralloc tofrom
= {0};
36 void doit(s
,n
) char *s
; int n
;
38 if (!stralloc_catb(&text
,s
,n
)) _exit(0);
39 if (text
.len
> 78) text
.len
= 78;
41 void dobody(h
) stralloc
*h
; { doit(h
->s
,h
->len
); }
42 void doheader(h
) stralloc
*h
;
45 if (hfield_known(h
->s
,h
->len
) == H_SUBJECT
)
47 i
= hfield_skipname(h
->s
,h
->len
);
48 doit(h
->s
+ i
,h
->len
- i
);
51 void finishheader() { ; }
61 if (chdir("/dev") == -1) _exit(0);
63 if (!(user
= env_get("USER"))) _exit(0);
64 if (!(sender
= env_get("SENDER"))) _exit(0);
65 if (!(userext
= env_get("LOCAL"))) _exit(0);
66 if (str_len(user
) > sizeof(ut
.ut_name
)) _exit(0);
68 if (!stralloc_copys(&tofrom
,"*** TO <")) _exit(0);
69 if (!stralloc_cats(&tofrom
,userext
)) _exit(0);
70 if (!stralloc_cats(&tofrom
,"> FROM <")) _exit(0);
71 if (!stralloc_cats(&tofrom
,sender
)) _exit(0);
72 if (!stralloc_cats(&tofrom
,">")) _exit(0);
74 for (i
= 0;i
< tofrom
.len
;++i
)
75 if ((tofrom
.s
[i
] < 32) || (tofrom
.s
[i
] > 126))
78 if (!stralloc_copys(&text
," ")) _exit(0);
79 if (headerbody(subfdin
,doheader
,finishheader
,dobody
) == -1) _exit(0);
81 for (i
= 0;i
< text
.len
;++i
)
82 if ((text
.s
[i
] < 32) || (text
.s
[i
] > 126))
85 if (!stralloc_copys(&woof
,"\015\n\007")) _exit(0);
86 if (!stralloc_cat(&woof
,&tofrom
)) _exit(0);
87 if (!stralloc_cats(&woof
,"\015\n")) _exit(0);
88 if (!stralloc_cat(&woof
,&text
)) _exit(0);
89 if (!stralloc_cats(&woof
,"\015\n")) _exit(0);
91 fdutmp
= open_read(UTMP_FILE
);
92 if (fdutmp
== -1) _exit(0);
93 substdio_fdbuf(&ssutmp
,read
,fdutmp
,bufutmp
,sizeof(bufutmp
));
95 while (substdio_get(&ssutmp
,&ut
,sizeof(ut
)) == sizeof(ut
))
96 if (!str_diffn(ut
.ut_name
,user
,sizeof(ut
.ut_name
)))
98 byte_copy(line
,sizeof(ut
.ut_line
),ut
.ut_line
);
99 line
[sizeof(ut
.ut_line
)] = 0;
100 if (line
[0] == '/') continue;
101 if (!line
[0]) continue;
102 if (line
[str_chr(line
,'.')]) continue;
103 fdtty
= open_append(line
);
104 if (fdtty
== -1) continue;
105 if (fstat(fdtty
,&st
) == -1) { close(fdtty
); continue; }
106 if (!(st
.st_mode
& 0100)) { close(fdtty
); continue; }
107 if (st
.st_uid
!= getuid()) { close(fdtty
); continue; }
108 substdio_fdbuf(&sstty
,write
,fdtty
,buftty
,sizeof(buftty
));
109 substdio_putflush(&sstty
,woof
.s
,woof
.len
);