rfc931.c (44744) | rfc931.c (56977) |
---|---|
1 /* 2 * rfc931() speaks a common subset of the RFC 931, AUTH, TAP, IDENT and RFC 3 * 1413 protocols. It queries an RFC 931 etc. compatible daemon on a remote 4 * host to look up the owner of a connection. The information should not be 5 * used for authentication purposes. This routine intercepts alarm signals. 6 * 7 * Diagnostics are reported through syslog(3). 8 * 9 * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands. | 1 /* 2 * rfc931() speaks a common subset of the RFC 931, AUTH, TAP, IDENT and RFC 3 * 1413 protocols. It queries an RFC 931 etc. compatible daemon on a remote 4 * host to look up the owner of a connection. The information should not be 5 * used for authentication purposes. This routine intercepts alarm signals. 6 * 7 * Diagnostics are reported through syslog(3). 8 * 9 * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands. |
10 * 11 * $FreeBSD: head/contrib/tcp_wrappers/rfc931.c 56977 2000-02-03 10:27:03Z shin $ |
|
10 */ 11 12#ifndef lint 13static char sccsid[] = "@(#) rfc931.c 1.10 95/01/02 16:11:34"; 14#endif 15 16/* System libraries. */ 17 --- 45 unchanged lines hidden (view full) --- 63int sig; 64{ 65 longjmp(timebuf, sig); 66} 67 68/* rfc931 - return remote user name, given socket structures */ 69 70void rfc931(rmt_sin, our_sin, dest) | 12 */ 13 14#ifndef lint 15static char sccsid[] = "@(#) rfc931.c 1.10 95/01/02 16:11:34"; 16#endif 17 18/* System libraries. */ 19 --- 45 unchanged lines hidden (view full) --- 65int sig; 66{ 67 longjmp(timebuf, sig); 68} 69 70/* rfc931 - return remote user name, given socket structures */ 71 72void rfc931(rmt_sin, our_sin, dest) |
73#ifdef INET6 74struct sockaddr *rmt_sin; 75struct sockaddr *our_sin; 76#else |
|
71struct sockaddr_in *rmt_sin; 72struct sockaddr_in *our_sin; | 77struct sockaddr_in *rmt_sin; 78struct sockaddr_in *our_sin; |
79#endif |
|
73char *dest; 74{ 75 unsigned rmt_port; 76 unsigned our_port; | 80char *dest; 81{ 82 unsigned rmt_port; 83 unsigned our_port; |
84#ifdef INET6 85 struct sockaddr_storage rmt_query_sin; 86 struct sockaddr_storage our_query_sin; 87 int alen; 88#else |
|
77 struct sockaddr_in rmt_query_sin; 78 struct sockaddr_in our_query_sin; | 89 struct sockaddr_in rmt_query_sin; 90 struct sockaddr_in our_query_sin; |
91#endif |
|
79 char user[256]; /* XXX */ 80 char buffer[512]; /* XXX */ 81 char *cp; 82 char *result = unknown; 83 FILE *fp; 84 | 92 char user[256]; /* XXX */ 93 char buffer[512]; /* XXX */ 94 char *cp; 95 char *result = unknown; 96 FILE *fp; 97 |
98#ifdef INET6 99 /* address family must be the same */ 100 if (rmt_sin->sa_family != our_sin->sa_family) { 101 STRN_CPY(dest, result, STRING_LENGTH); 102 return; 103 } 104 switch (our_sin->sa_family) { 105 case AF_INET: 106 alen = sizeof(struct sockaddr_in); 107 break; 108 case AF_INET6: 109 alen = sizeof(struct sockaddr_in6); 110 break; 111 default: 112 STRN_CPY(dest, result, STRING_LENGTH); 113 return; 114 } 115#endif 116 |
|
85 /* 86 * Use one unbuffered stdio stream for writing to and for reading from 87 * the RFC931 etc. server. This is done because of a bug in the SunOS 88 * 4.1.x stdio library. The bug may live in other stdio implementations, 89 * too. When we use a single, buffered, bidirectional stdio stream ("r+" 90 * or "w+" mode) we read our own output. Such behaviour would make sense 91 * with resources that support random-access operations, but not with 92 * sockets. 93 */ 94 | 117 /* 118 * Use one unbuffered stdio stream for writing to and for reading from 119 * the RFC931 etc. server. This is done because of a bug in the SunOS 120 * 4.1.x stdio library. The bug may live in other stdio implementations, 121 * too. When we use a single, buffered, bidirectional stdio stream ("r+" 122 * or "w+" mode) we read our own output. Such behaviour would make sense 123 * with resources that support random-access operations, but not with 124 * sockets. 125 */ 126 |
127#ifdef INET6 128 if ((fp = fsocket(our_sin->sa_family, SOCK_STREAM, 0)) != 0) { 129#else |
|
95 if ((fp = fsocket(AF_INET, SOCK_STREAM, 0)) != 0) { | 130 if ((fp = fsocket(AF_INET, SOCK_STREAM, 0)) != 0) { |
131#endif |
|
96 setbuf(fp, (char *) 0); 97 98 /* 99 * Set up a timer so we won't get stuck while waiting for the server. 100 */ 101 102 if (setjmp(timebuf) == 0) { 103 signal(SIGALRM, timeout); 104 alarm(rfc931_timeout); 105 106 /* 107 * Bind the local and remote ends of the query socket to the same 108 * IP addresses as the connection under investigation. We go 109 * through all this trouble because the local or remote system 110 * might have more than one network address. The RFC931 etc. 111 * client sends only port numbers; the server takes the IP 112 * addresses from the query socket. 113 */ 114 | 132 setbuf(fp, (char *) 0); 133 134 /* 135 * Set up a timer so we won't get stuck while waiting for the server. 136 */ 137 138 if (setjmp(timebuf) == 0) { 139 signal(SIGALRM, timeout); 140 alarm(rfc931_timeout); 141 142 /* 143 * Bind the local and remote ends of the query socket to the same 144 * IP addresses as the connection under investigation. We go 145 * through all this trouble because the local or remote system 146 * might have more than one network address. The RFC931 etc. 147 * client sends only port numbers; the server takes the IP 148 * addresses from the query socket. 149 */ 150 |
151#ifdef INET6 152 memcpy(&our_query_sin, our_sin, alen); 153 memcpy(&rmt_query_sin, rmt_sin, alen); 154 switch (our_sin->sa_family) { 155 case AF_INET: 156 ((struct sockaddr_in *)&our_query_sin)->sin_port = htons(ANY_PORT); 157 ((struct sockaddr_in *)&rmt_query_sin)->sin_port = htons(RFC931_PORT); 158 break; 159 case AF_INET6: 160 ((struct sockaddr_in6 *)&our_query_sin)->sin6_port = htons(ANY_PORT); 161 ((struct sockaddr_in6 *)&rmt_query_sin)->sin6_port = htons(RFC931_PORT); 162 break; 163 } 164 165 if (bind(fileno(fp), (struct sockaddr *) & our_query_sin, 166 alen) >= 0 && 167 connect(fileno(fp), (struct sockaddr *) & rmt_query_sin, 168 alen) >= 0) { 169#else |
|
115 our_query_sin = *our_sin; 116 our_query_sin.sin_port = htons(ANY_PORT); 117 rmt_query_sin = *rmt_sin; 118 rmt_query_sin.sin_port = htons(RFC931_PORT); 119 120 if (bind(fileno(fp), (struct sockaddr *) & our_query_sin, 121 sizeof(our_query_sin)) >= 0 && 122 connect(fileno(fp), (struct sockaddr *) & rmt_query_sin, 123 sizeof(rmt_query_sin)) >= 0) { | 170 our_query_sin = *our_sin; 171 our_query_sin.sin_port = htons(ANY_PORT); 172 rmt_query_sin = *rmt_sin; 173 rmt_query_sin.sin_port = htons(RFC931_PORT); 174 175 if (bind(fileno(fp), (struct sockaddr *) & our_query_sin, 176 sizeof(our_query_sin)) >= 0 && 177 connect(fileno(fp), (struct sockaddr *) & rmt_query_sin, 178 sizeof(rmt_query_sin)) >= 0) { |
179#endif |
|
124 125 /* 126 * Send query to server. Neglect the risk that a 13-byte 127 * write would have to be fragmented by the local system and 128 * cause trouble with buggy System V stdio libraries. 129 */ 130 131 fprintf(fp, "%u,%u\r\n", | 180 181 /* 182 * Send query to server. Neglect the risk that a 13-byte 183 * write would have to be fragmented by the local system and 184 * cause trouble with buggy System V stdio libraries. 185 */ 186 187 fprintf(fp, "%u,%u\r\n", |
188#ifdef INET6 189 ntohs(((struct sockaddr_in *)rmt_sin)->sin_port), 190 ntohs(((struct sockaddr_in *)our_sin)->sin_port)); 191#else |
|
132 ntohs(rmt_sin->sin_port), 133 ntohs(our_sin->sin_port)); | 192 ntohs(rmt_sin->sin_port), 193 ntohs(our_sin->sin_port)); |
194#endif |
|
134 fflush(fp); 135 136 /* 137 * Read response from server. Use fgets()/sscanf() so we can 138 * work around System V stdio libraries that incorrectly 139 * assume EOF when a read from a socket returns less than 140 * requested. 141 */ 142 143 if (fgets(buffer, sizeof(buffer), fp) != 0 144 && ferror(fp) == 0 && feof(fp) == 0 145 && sscanf(buffer, "%u , %u : USERID :%*[^:]:%255s", 146 &rmt_port, &our_port, user) == 3 | 195 fflush(fp); 196 197 /* 198 * Read response from server. Use fgets()/sscanf() so we can 199 * work around System V stdio libraries that incorrectly 200 * assume EOF when a read from a socket returns less than 201 * requested. 202 */ 203 204 if (fgets(buffer, sizeof(buffer), fp) != 0 205 && ferror(fp) == 0 && feof(fp) == 0 206 && sscanf(buffer, "%u , %u : USERID :%*[^:]:%255s", 207 &rmt_port, &our_port, user) == 3 |
208#ifdef INET6 209 && ntohs(((struct sockaddr_in *)rmt_sin)->sin_port) == rmt_port 210 && ntohs(((struct sockaddr_in *)our_sin)->sin_port) == our_port) { 211#else |
|
147 && ntohs(rmt_sin->sin_port) == rmt_port 148 && ntohs(our_sin->sin_port) == our_port) { | 212 && ntohs(rmt_sin->sin_port) == rmt_port 213 && ntohs(our_sin->sin_port) == our_port) { |
214#endif |
|
149 150 /* 151 * Strip trailing carriage return. It is part of the 152 * protocol, not part of the data. 153 */ 154 155 if (cp = strchr(user, '\r')) 156 *cp = 0; 157 result = user; 158 } 159 } 160 alarm(0); 161 } 162 fclose(fp); 163 } 164 STRN_CPY(dest, result, STRING_LENGTH); 165} | 215 216 /* 217 * Strip trailing carriage return. It is part of the 218 * protocol, not part of the data. 219 */ 220 221 if (cp = strchr(user, '\r')) 222 *cp = 0; 223 result = user; 224 } 225 } 226 alarm(0); 227 } 228 fclose(fp); 229 } 230 STRN_CPY(dest, result, STRING_LENGTH); 231} |