Commit | Line | Data |
---|---|---|
3b600ead TL |
1 | /* |
2 | * write to another user | |
3 | */ | |
4 | ||
5 | #include <stdio.h> | |
6 | #include <sys/types.h> | |
7 | #include <sys/stat.h> | |
8 | #include <signal.h> | |
9 | #include <utmp.h> | |
10 | ||
11 | char *strcat(); | |
12 | char *strcpy(); | |
13 | struct utmp ubuf; | |
14 | int signum[] = {SIGHUP, SIGINT, SIGQUIT, 0}; | |
15 | char me[10] = "???"; | |
16 | char *him; | |
17 | char *mytty; | |
18 | char histty[32]; | |
19 | char *histtya; | |
20 | char *ttyname(); | |
21 | char *rindex(); | |
22 | int logcnt; | |
23 | int eof(); | |
24 | int timout(); | |
25 | FILE *tf; | |
26 | ||
27 | main(argc, argv) | |
28 | char *argv[]; | |
29 | { | |
30 | struct stat stbuf; | |
31 | register i; | |
32 | register FILE *uf; | |
33 | int c1, c2; | |
34 | ||
35 | if(argc < 2) { | |
36 | printf("usage: write user [ttyname]\n"); | |
37 | exit(1); | |
38 | } | |
39 | him = argv[1]; | |
40 | if(argc > 2) | |
41 | histtya = argv[2]; | |
42 | if ((uf = fopen("/etc/utmp", "r")) == NULL) { | |
43 | printf("cannot open /etc/utmp\n"); | |
44 | goto cont; | |
45 | } | |
46 | mytty = ttyname(2); | |
47 | if (mytty == NULL) { | |
48 | printf("Can't find your tty\n"); | |
49 | exit(1); | |
50 | } | |
51 | mytty = rindex(mytty, '/') + 1; | |
52 | if (histtya) { | |
53 | strcpy(histty, "/dev/"); | |
54 | strcat(histty, histtya); | |
55 | } | |
56 | while (fread((char *)&ubuf, sizeof(ubuf), 1, uf) == 1) { | |
57 | if (strcmp(ubuf.ut_line, mytty)==0) { | |
58 | for(i=0; i<8; i++) { | |
59 | c1 = ubuf.ut_name[i]; | |
60 | if(c1 == ' ') | |
61 | c1 = 0; | |
62 | me[i] = c1; | |
63 | if(c1 == 0) | |
64 | break; | |
65 | } | |
66 | } | |
67 | if(him[0] != '-' || him[1] != 0) | |
68 | for(i=0; i<8; i++) { | |
69 | c1 = him[i]; | |
70 | c2 = ubuf.ut_name[i]; | |
71 | if(c1 == 0) | |
72 | if(c2 == 0 || c2 == ' ') | |
73 | break; | |
74 | if(c1 != c2) | |
75 | goto nomat; | |
76 | } | |
77 | logcnt++; | |
78 | if (histty[0]==0) { | |
79 | strcpy(histty, "/dev/"); | |
80 | strcat(histty, ubuf.ut_line); | |
81 | } | |
82 | nomat: | |
83 | ; | |
84 | } | |
85 | cont: | |
86 | if (logcnt==0 && histty[0]=='\0') { | |
87 | printf("%s not logged in.\n", him); | |
88 | exit(1); | |
89 | } | |
90 | fclose(uf); | |
91 | if (histtya==0 && logcnt > 1) { | |
92 | printf("%s logged more than once\nwriting to %s\n", him, histty+5); | |
93 | } | |
94 | if(histty[0] == 0) { | |
95 | printf(him); | |
96 | if(logcnt) | |
97 | printf(" not on that tty\n"); else | |
98 | printf(" not logged in\n"); | |
99 | exit(1); | |
100 | } | |
101 | if (access(histty, 0) < 0) { | |
102 | printf("No such tty\n"); | |
103 | exit(1); | |
104 | } | |
105 | signal(SIGALRM, timout); | |
106 | alarm(5); | |
107 | if ((tf = fopen(histty, "w")) == NULL) | |
108 | goto perm; | |
109 | alarm(0); | |
110 | if (fstat(fileno(tf), &stbuf) < 0) | |
111 | goto perm; | |
112 | if ((stbuf.st_mode&02) == 0) | |
113 | goto perm; | |
114 | sigs(eof); | |
115 | fprintf(tf, "Message from "); | |
116 | #ifdef interdata | |
117 | fprintf(tf, "(Interdata) " ); | |
118 | #endif | |
119 | fprintf(tf, "%s %s...\n\a\a\a", me, mytty); | |
120 | fflush(tf); | |
121 | for(;;) { | |
122 | char buf[128]; | |
123 | i = read(0, buf, 128); | |
124 | if(i <= 0) | |
125 | eof(); | |
126 | if(buf[0] == '!') { | |
127 | buf[i] = 0; | |
128 | ex(buf); | |
129 | continue; | |
130 | } | |
131 | write(fileno(tf), buf, i); | |
132 | } | |
133 | ||
134 | perm: | |
135 | printf("Permission denied\n"); | |
136 | exit(1); | |
137 | } | |
138 | ||
139 | timout() | |
140 | { | |
141 | ||
142 | printf("Timeout opening his tty\n"); | |
143 | exit(1); | |
144 | } | |
145 | ||
146 | eof() | |
147 | { | |
148 | ||
149 | fprintf(tf, "EOF\n"); | |
150 | exit(0); | |
151 | } | |
152 | ||
153 | ex(bp) | |
154 | char *bp; | |
155 | { | |
156 | register i; | |
157 | ||
158 | sigs(SIG_IGN); | |
159 | i = fork(); | |
160 | if(i < 0) { | |
161 | printf("Try again\n"); | |
162 | goto out; | |
163 | } | |
164 | if(i == 0) { | |
165 | sigs((int (*)())0); | |
166 | execl("/bin/sh", "sh", "-c", bp+1, 0); | |
167 | exit(0); | |
168 | } | |
169 | while(wait((int *)NULL) != i) | |
170 | ; | |
171 | printf("!\n"); | |
172 | out: | |
173 | sigs(eof); | |
174 | } | |
175 | ||
176 | sigs(sig) | |
177 | int (*sig)(); | |
178 | { | |
179 | register i; | |
180 | ||
181 | for(i=0;signum[i];i++) | |
182 | signal(signum[i],sig); | |
183 | } |