|
|
1.1 root 1: #ifdef TTYD
2: /* ttyw.c - the writer */
3:
4: #include <errno.h>
5: #include <stdio.h>
6: #include <strings.h>
7: #include <sys/types.h>
8: #include <sys/socket.h>
9: #include <netinet/in.h>
10: #include <netdb.h>
11: #include <arpa/inet.h>
12: #include "ttyd.h"
13: #include "ttym.c"
14:
15: static int getport();
16:
17: /* */
18:
19: ttyw(command, host, line, user)
20: char *command, *host, *line, *user;
21: {
22: int privd,
23: sd;
24: unsigned times;
25: char buffer[BUFSIZ];
26: struct hostent *hp;
27: struct servent *sp;
28: struct sockaddr_in tty_socket,
29: *tsock = &tty_socket;
30:
31: if (command == NULL) {
32: errno = EINVAL;
33: return NOTOK;
34: }
35:
36: if ((sp = getservbyname ("ttyserver", "tcp")) == NULL) {
37: errno = ENETDOWN;
38: return NOTOK;
39: }
40: if (host == NULL)
41: (void) gethostname (host = buffer, sizeof buffer);
42: if ((hp = gethostbyname (host))==NULL) {
43: errno = ENETDOWN;
44: return NOTOK;
45: }
46:
47: if (line && strncmp (line, "/dev/", strlen ("/dev/")) == 0)
48: line += strlen ("/dev/");
49:
50: privd = *command >= 'A' && *command <= 'Z';/* crude */
51:
52: /* */
53:
54: for (times = 1; times <= 16; times *= 2) {
55: if ((sd = getport (0, privd)) == NOTOK)
56: return NOTOK;
57:
58: bzero ((char *) tsock, sizeof *tsock);
59: tsock -> sin_family = hp -> h_addrtype;
60: tsock -> sin_port = sp -> s_port;
61: bcopy (hp -> h_addr, (char *) &tsock -> sin_addr, hp -> h_length);
62:
63: if (connect (sd, (struct sockaddr *) tsock, sizeof *tsock) == NOTOK) {
64: (void) close (sd);
65: if (errno == ECONNREFUSED || errno == EINTR) {
66: sleep (times);
67: continue;
68: }
69: break;
70: }
71:
72: ttym (sd, command, line, user, NULL);
73: if (ttyv (sd) == NOTOK || ttyv (sd) == NOTOK) {
74: (void) close (sd);
75: errno = EPERM; /* what else??? */
76: return NOTOK;
77: }
78: else
79: return sd;
80: }
81:
82: return NOTOK;
83: }
84:
85: /* */
86:
87: static int
88: getport(options, privd)
89: unsigned options;
90: int privd;
91: {
92: int sd,
93: port;
94: struct sockaddr_in unx_socket,
95: *usock = &unx_socket;
96:
97: if ((sd = socket (AF_INET, SOCK_STREAM, 0)) == NOTOK)
98: return sd;
99:
100: if (options & SO_DEBUG)
101: (void) setsockopt (sd, SOL_SOCKET, SO_DEBUG, NULL, 0);
102: (void) setsockopt (sd, SOL_SOCKET, SO_KEEPALIVE, NULL, 0);
103:
104: if (!privd)
105: return sd;
106:
107: usock -> sin_family = AF_INET;
108: usock -> sin_addr.s_addr = INADDR_ANY;
109:
110: for (port = IPPORT_RESERVED - 1; port > IPPORT_RESERVED / 2; port--) {
111: usock -> sin_port = htons (port);
112:
113: switch (bind (sd, (struct sockaddr *) usock, sizeof *usock)) {
114: case NOTOK:
115: if (errno != EADDRINUSE && errno != EADDRNOTAVAIL)
116: return NOTOK;
117: continue;
118:
119: default:
120: return sd;
121: }
122: }
123:
124: return NOTOK;
125: }
126: #endif TTYD
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.