qbiff.c (2625B)
1 #include <sys/types.h> 2 #include <sys/stat.h> 3 #include "qtmp.h" 4 #include "readwrite.h" 5 #include "stralloc.h" 6 #include "substdio.h" 7 #include "subfd.h" 8 #include "open.h" 9 #include "byte.h" 10 #include "str.h" 11 #include "headerbody.h" 12 #include "hfield.h" 13 #include "env.h" 14 #include "exit.h" 15 16 substdio sstty; 17 char buftty[1024]; 18 int fdtty; 19 20 UTMP_INIT; 21 char line[sizeof(ut->ut_line) + 1]; 22 stralloc woof = {0}; 23 stralloc tofrom = {0}; 24 stralloc text = {0}; 25 26 void doit(s,n) char *s; int n; 27 { 28 if (!stralloc_catb(&text,s,n)) _exit(0); 29 if (text.len > 78) text.len = 78; 30 } 31 void dobody(h) stralloc *h; { doit(h->s,h->len); } 32 void doheader(h) stralloc *h; 33 { 34 int i; 35 if (hfield_known(h->s,h->len) == H_SUBJECT) 36 { 37 i = hfield_skipname(h->s,h->len); 38 doit(h->s + i,h->len - i); 39 } 40 } 41 void finishheader() { ; } 42 43 int main(void) 44 { 45 char *user; 46 char *sender; 47 char *userext; 48 struct stat st; 49 int i; 50 51 if (chdir("/dev") == -1) return 0; 52 53 if (!(user = env_get("USER"))) return 0; 54 if (!(sender = env_get("SENDER"))) return 0; 55 if (!(userext = env_get("LOCAL"))) return 0; 56 if (str_len(user) > sizeof(ut->UTMP_USER)) return 0; 57 58 if (!stralloc_copys(&tofrom,"*** TO <")) return 0; 59 if (!stralloc_cats(&tofrom,userext)) return 0; 60 if (!stralloc_cats(&tofrom,"> FROM <")) return 0; 61 if (!stralloc_cats(&tofrom,sender)) return 0; 62 if (!stralloc_cats(&tofrom,">")) return 0; 63 64 for (i = 0;i < tofrom.len;++i) 65 if ((tofrom.s[i] < 32) || (tofrom.s[i] > 126)) 66 tofrom.s[i] = '_'; 67 68 if (!stralloc_copys(&text," ")) return 0; 69 if (headerbody(subfdin,doheader,finishheader,dobody) == -1) return 0; 70 71 for (i = 0;i < text.len;++i) 72 if ((text.s[i] < 32) || (text.s[i] > 126)) 73 text.s[i] = '/'; 74 75 if (!stralloc_copys(&woof,"\015\n\007")) return 0; 76 if (!stralloc_cat(&woof,&tofrom)) return 0; 77 if (!stralloc_cats(&woof,"\015\n")) return 0; 78 if (!stralloc_cat(&woof,&text)) return 0; 79 if (!stralloc_cats(&woof,"\015\n")) return 0; 80 81 UTMP_OPEN; 82 83 while (UTMP_READ_MORE) 84 if (UTMP_TYPE_MATCHES && !str_diffn(ut->UTMP_USER,user,sizeof(ut->UTMP_USER))) 85 { 86 byte_copy(line,sizeof(ut->ut_line),ut->ut_line); 87 line[sizeof(ut->ut_line)] = 0; 88 if (line[0] == '/') continue; 89 if (!line[0]) continue; 90 if (line[str_chr(line,'.')]) continue; 91 fdtty = open_append(line); 92 if (fdtty == -1) continue; 93 if (fstat(fdtty,&st) == -1) { close(fdtty); continue; } 94 if (!(st.st_mode & 0100)) { close(fdtty); continue; } 95 if (st.st_uid != getuid()) { close(fdtty); continue; } 96 substdio_fdbuf(&sstty,write,fdtty,buftty,sizeof(buftty)); 97 substdio_putflush(&sstty,woof.s,woof.len); 98 close(fdtty); 99 } 100 return 0; 101 }