Commit | Line | Data |
---|---|---|
31b42ab1 | 1 | #ifndef lint |
25a06dbc | 2 | static char *sccsid = "@(#)write.c 4.10 %G%"; |
31b42ab1 | 3 | #endif |
cd784246 BJ |
4 | /* |
5 | * write to another user | |
6 | */ | |
7 | ||
8 | #include <stdio.h> | |
31b42ab1 | 9 | #include <ctype.h> |
cd784246 BJ |
10 | #include <sys/types.h> |
11 | #include <sys/stat.h> | |
12 | #include <signal.h> | |
13 | #include <utmp.h> | |
72cf2549 | 14 | #include <sys/time.h> |
cd784246 | 15 | |
cf288731 BJ |
16 | #define NMAX sizeof(ubuf.ut_name) |
17 | #define LMAX sizeof(ubuf.ut_line) | |
cd784246 BJ |
18 | |
19 | char *strcat(); | |
20 | char *strcpy(); | |
21 | struct utmp ubuf; | |
22 | int signum[] = {SIGHUP, SIGINT, SIGQUIT, 0}; | |
31b42ab1 | 23 | char me[NMAX + 1] = "???"; |
cd784246 BJ |
24 | char *him; |
25 | char *mytty; | |
26 | char histty[32]; | |
27 | char *histtya; | |
28 | char *ttyname(); | |
29 | char *rindex(); | |
30 | int logcnt; | |
31 | int eof(); | |
32 | int timout(); | |
33 | FILE *tf; | |
34 | char *getenv(); | |
35 | ||
36 | main(argc, argv) | |
cf288731 BJ |
37 | int argc; |
38 | char *argv[]; | |
cd784246 BJ |
39 | { |
40 | struct stat stbuf; | |
41 | register i; | |
42 | register FILE *uf; | |
43 | int c1, c2; | |
cf288731 | 44 | long clock = time(0); |
25a06dbc | 45 | int suser = getuid() == 0; |
cd784246 BJ |
46 | struct tm *localtime(); |
47 | struct tm *localclock = localtime( &clock ); | |
48 | ||
cf288731 | 49 | if (argc < 2) { |
31b42ab1 | 50 | fprintf(stderr, "Usage: write user [ttyname]\n"); |
cd784246 BJ |
51 | exit(1); |
52 | } | |
53 | him = argv[1]; | |
cf288731 | 54 | if (argc > 2) |
cd784246 BJ |
55 | histtya = argv[2]; |
56 | if ((uf = fopen("/etc/utmp", "r")) == NULL) { | |
31b42ab1 | 57 | perror("write: Can't open /etc/utmp"); |
cd784246 BJ |
58 | goto cont; |
59 | } | |
60 | mytty = ttyname(2); | |
61 | if (mytty == NULL) { | |
31b42ab1 | 62 | fprintf(stderr, "write: Can't find your tty\n"); |
cd784246 BJ |
63 | exit(1); |
64 | } | |
cf288731 | 65 | if (stat(mytty, &stbuf) < 0) { |
31b42ab1 | 66 | perror("write: Can't stat your tty"); |
cf288731 | 67 | exit(1); |
b40712b7 | 68 | } |
25a06dbc | 69 | if (!suser && (stbuf.st_mode&02) == 0) { |
31b42ab1 S |
70 | fprintf(stderr, |
71 | "write: You have write permission turned off\n"); | |
cf288731 | 72 | exit(1); |
b40712b7 | 73 | } |
cd784246 BJ |
74 | mytty = rindex(mytty, '/') + 1; |
75 | if (histtya) { | |
76 | strcpy(histty, "/dev/"); | |
77 | strcat(histty, histtya); | |
78 | } | |
79 | while (fread((char *)&ubuf, sizeof(ubuf), 1, uf) == 1) { | |
b85a75a1 EA |
80 | if (ubuf.ut_name[0] == '\0') |
81 | continue; | |
cd784246 | 82 | if (strcmp(ubuf.ut_line, mytty)==0) { |
cf288731 | 83 | for (i=0; i<NMAX; i++) { |
cd784246 | 84 | c1 = ubuf.ut_name[i]; |
cf288731 | 85 | if (c1 == ' ') |
cd784246 BJ |
86 | c1 = 0; |
87 | me[i] = c1; | |
cf288731 | 88 | if (c1 == 0) |
cd784246 BJ |
89 | break; |
90 | } | |
91 | } | |
31b42ab1 S |
92 | if (him[0] == '-' && him[1] == 0) |
93 | goto nomat; | |
cf288731 | 94 | for (i=0; i<NMAX; i++) { |
cd784246 BJ |
95 | c1 = him[i]; |
96 | c2 = ubuf.ut_name[i]; | |
cf288731 BJ |
97 | if (c1 == 0) |
98 | if (c2 == 0 || c2 == ' ') | |
cd784246 | 99 | break; |
cf288731 | 100 | if (c1 != c2) |
cd784246 BJ |
101 | goto nomat; |
102 | } | |
103 | logcnt++; | |
104 | if (histty[0]==0) { | |
105 | strcpy(histty, "/dev/"); | |
106 | strcat(histty, ubuf.ut_line); | |
107 | } | |
108 | nomat: | |
109 | ; | |
110 | } | |
111 | cont: | |
112 | if (logcnt==0 && histty[0]=='\0') { | |
31b42ab1 | 113 | fprintf(stderr, "write: %s not logged in\n", him); |
cd784246 BJ |
114 | exit(1); |
115 | } | |
31b42ab1 S |
116 | if (uf != NULL) |
117 | fclose(uf); | |
cd784246 | 118 | if (histtya==0 && logcnt > 1) { |
31b42ab1 S |
119 | fprintf(stderr, |
120 | "write: %s logged in more than once ... writing to %s\n", | |
121 | him, histty+5); | |
cd784246 | 122 | } |
cf288731 | 123 | if (histty[0] == 0) { |
cd784246 | 124 | printf(him); |
cf288731 | 125 | if (logcnt) |
cd784246 BJ |
126 | printf(" not on that tty\n"); else |
127 | printf(" not logged in\n"); | |
128 | exit(1); | |
129 | } | |
130 | if (access(histty, 0) < 0) { | |
31b42ab1 | 131 | fprintf(stderr, "write: No such tty\n"); |
cd784246 BJ |
132 | exit(1); |
133 | } | |
134 | signal(SIGALRM, timout); | |
135 | alarm(5); | |
136 | if ((tf = fopen(histty, "w")) == NULL) | |
137 | goto perm; | |
138 | alarm(0); | |
139 | if (fstat(fileno(tf), &stbuf) < 0) | |
140 | goto perm; | |
25a06dbc | 141 | if (!suser && (stbuf.st_mode&02) == 0) |
cd784246 BJ |
142 | goto perm; |
143 | sigs(eof); | |
cf288731 BJ |
144 | { char hostname[32]; |
145 | gethostname(hostname, sizeof (hostname)); | |
31b42ab1 S |
146 | fprintf(tf, |
147 | "\r\nMessage from %s@%s on %s at %d:%02d ...\r\n\007\007\007", | |
148 | me, hostname, mytty, localclock->tm_hour, localclock->tm_min); | |
cd784246 | 149 | fflush(tf); |
31b42ab1 | 150 | } |
cf288731 | 151 | for (;;) { |
31b42ab1 S |
152 | char buf[BUFSIZ]; |
153 | register char *bp; | |
154 | i = read(0, buf, sizeof buf); | |
cf288731 | 155 | if (i <= 0) |
cd784246 | 156 | eof(); |
cf288731 | 157 | if (buf[0] == '!') { |
cd784246 BJ |
158 | buf[i] = 0; |
159 | ex(buf); | |
160 | continue; | |
161 | } | |
31b42ab1 S |
162 | for (bp = buf; --i >= 0; bp++) { |
163 | if (*bp == '\n') | |
164 | putc('\r', tf); | |
165 | ||
166 | if (!isascii(*bp)) { | |
167 | putc('M', tf); | |
168 | putc('-', tf); | |
169 | *bp = toascii(*bp); | |
170 | } | |
171 | ||
172 | if (isprint(*bp) || | |
173 | *bp == ' ' || *bp == '\t' || *bp == '\n') { | |
174 | putc(*bp, tf); | |
175 | } else { | |
176 | putc('^', tf); | |
177 | putc(*bp ^ 0100, tf); | |
178 | } | |
179 | ||
180 | if (*bp == '\n') | |
181 | fflush(tf); | |
182 | ||
183 | if (ferror(tf) || feof(tf)) { | |
184 | printf("\n\007Write failed (%s logged out?)\n", | |
185 | him); | |
186 | exit(1); | |
187 | } | |
93665ffd | 188 | } |
cd784246 | 189 | } |
cd784246 | 190 | perm: |
31b42ab1 | 191 | fprintf(stderr, "write: Permission denied\n"); |
cd784246 BJ |
192 | exit(1); |
193 | } | |
194 | ||
195 | timout() | |
196 | { | |
197 | ||
31b42ab1 | 198 | fprintf(stderr, "write: Timeout opening their tty\n"); |
cd784246 BJ |
199 | exit(1); |
200 | } | |
201 | ||
202 | eof() | |
203 | { | |
204 | ||
205 | fprintf(tf, "EOF\r\n"); | |
206 | exit(0); | |
207 | } | |
208 | ||
209 | ex(bp) | |
cf288731 | 210 | char *bp; |
cd784246 BJ |
211 | { |
212 | register i; | |
213 | ||
214 | sigs(SIG_IGN); | |
215 | i = fork(); | |
cf288731 | 216 | if (i < 0) { |
cd784246 BJ |
217 | printf("Try again\n"); |
218 | goto out; | |
219 | } | |
cf288731 | 220 | if (i == 0) { |
cd784246 | 221 | sigs((int (*)())0); |
cf288731 BJ |
222 | execl(getenv("SHELL") ? |
223 | getenv("SHELL") : "/bin/sh", "sh", "-c", bp+1, 0); | |
cd784246 BJ |
224 | exit(0); |
225 | } | |
cf288731 | 226 | while (wait((int *)NULL) != i) |
cd784246 BJ |
227 | ; |
228 | printf("!\n"); | |
229 | out: | |
230 | sigs(eof); | |
231 | } | |
232 | ||
233 | sigs(sig) | |
cf288731 | 234 | int (*sig)(); |
cd784246 BJ |
235 | { |
236 | register i; | |
237 | ||
cf288731 BJ |
238 | for (i=0; signum[i]; i++) |
239 | signal(signum[i], sig); | |
cd784246 | 240 | } |