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