xref: /csrg-svn/usr.bin/finger/net.c (revision 61995)
137660Sbostic /*
2*61995Sbostic  * Copyright (c) 1989, 1993
3*61995Sbostic  *	The Regents of the University of California.  All rights reserved.
437660Sbostic  *
540027Sbostic  * This code is derived from software contributed to Berkeley by
640027Sbostic  * Tony Nardo of the Johns Hopkins University/Applied Physics Lab.
740027Sbostic  *
842731Sbostic  * %sccs.include.redist.c%
937660Sbostic  */
1037660Sbostic 
1137660Sbostic #ifndef lint
12*61995Sbostic static char sccsid[] = "@(#)net.c	8.1 (Berkeley) 06/06/93";
1337660Sbostic #endif /* not lint */
1437660Sbostic 
1537660Sbostic #include <sys/types.h>
1637660Sbostic #include <sys/socket.h>
1737660Sbostic #include <netinet/in.h>
1850596Sbostic #include <arpa/inet.h>
1937660Sbostic #include <netdb.h>
2050613Sbostic #include <db.h>
2150596Sbostic #include <unistd.h>
2250596Sbostic #include <pwd.h>
2350596Sbostic #include <utmp.h>
2437660Sbostic #include <stdio.h>
2537660Sbostic #include <ctype.h>
2650596Sbostic #include <string.h>
2750596Sbostic #include "finger.h"
2837660Sbostic 
2950596Sbostic void
3037660Sbostic netfinger(name)
3137660Sbostic 	char *name;
3237660Sbostic {
3337660Sbostic 	extern int lflag;
3437660Sbostic 	register FILE *fp;
3537660Sbostic 	register int c, lastc;
3637660Sbostic 	struct in_addr defaddr;
3737660Sbostic 	struct hostent *hp, def;
3837660Sbostic 	struct servent *sp;
3937660Sbostic 	struct sockaddr_in sin;
4037660Sbostic 	int s;
4150596Sbostic 	char *alist[1], *host;
4237660Sbostic 
4337660Sbostic 	if (!(host = rindex(name, '@')))
4437660Sbostic 		return;
4537660Sbostic 	*host++ = NULL;
4637660Sbostic 	if (!(hp = gethostbyname(host))) {
4737660Sbostic 		defaddr.s_addr = inet_addr(host);
4837660Sbostic 		if (defaddr.s_addr == -1) {
4937660Sbostic 			(void)fprintf(stderr,
5037660Sbostic 			    "finger: unknown host: %s\n", host);
5137660Sbostic 			return;
5237660Sbostic 		}
5337660Sbostic 		def.h_name = host;
5437660Sbostic 		def.h_addr_list = alist;
5537660Sbostic 		def.h_addr = (char *)&defaddr;
5637660Sbostic 		def.h_length = sizeof(struct in_addr);
5737660Sbostic 		def.h_addrtype = AF_INET;
5837660Sbostic 		def.h_aliases = 0;
5937660Sbostic 		hp = &def;
6037660Sbostic 	}
6137660Sbostic 	if (!(sp = getservbyname("finger", "tcp"))) {
6237660Sbostic 		(void)fprintf(stderr, "finger: tcp/finger: unknown service\n");
6337660Sbostic 		return;
6437660Sbostic 	}
6537660Sbostic 	sin.sin_family = hp->h_addrtype;
6637660Sbostic 	bcopy(hp->h_addr, (char *)&sin.sin_addr, hp->h_length);
6737660Sbostic 	sin.sin_port = sp->s_port;
6837660Sbostic 	if ((s = socket(hp->h_addrtype, SOCK_STREAM, 0)) < 0) {
6937660Sbostic 		perror("finger: socket");
7037660Sbostic 		return;
7137660Sbostic 	}
7237660Sbostic 
7337660Sbostic 	/* have network connection; identify the host connected with */
7437660Sbostic 	(void)printf("[%s]\n", hp->h_name);
7537660Sbostic 	if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
7637660Sbostic 		perror("finger: connect");
7737660Sbostic 		(void)close(s);
7837660Sbostic 		return;
7937660Sbostic 	}
8037660Sbostic 
8137660Sbostic 	/* -l flag for remote fingerd  */
8237660Sbostic 	if (lflag)
8337660Sbostic 		write(s, "/W ", 3);
8437660Sbostic 	/* send the name followed by <CR><LF> */
8537660Sbostic 	(void)write(s, name, strlen(name));
8637660Sbostic 	(void)write(s, "\r\n", 2);
8737660Sbostic 
8837660Sbostic 	/*
8940190Sbostic 	 * Read from the remote system; once we're connected, we assume some
9040190Sbostic 	 * data.  If none arrives, we hang until the user interrupts.
9137660Sbostic 	 *
9240190Sbostic 	 * If we see a <CR> or a <CR> with the high bit set, treat it as
9340190Sbostic 	 * a newline; if followed by a newline character, only output one
9440190Sbostic 	 * newline.
9537660Sbostic 	 *
9637660Sbostic 	 * Otherwise, all high bits are stripped; if it isn't printable and
9737660Sbostic 	 * it isn't a space, we can simply set the 7th bit.  Every ASCII
9837660Sbostic 	 * character with bit 7 set is printable.
9937660Sbostic 	 */
10037660Sbostic 	if (fp = fdopen(s, "r"))
10137660Sbostic 		while ((c = getc(fp)) != EOF) {
10240190Sbostic 			c &= 0x7f;
10340190Sbostic 			if (c == 0x0d) {
10437660Sbostic 				c = '\n';
10540190Sbostic 				lastc = '\r';
10640190Sbostic 			} else {
10740190Sbostic 				if (!isprint(c) && !isspace(c))
10840190Sbostic 					c |= 0x40;
10940190Sbostic 				if (lastc != '\r' || c != '\n')
11040190Sbostic 					lastc = c;
11140190Sbostic 				else {
11240190Sbostic 					lastc = '\n';
11340190Sbostic 					continue;
11440190Sbostic 				}
11540190Sbostic 			}
11637660Sbostic 			putchar(c);
11737660Sbostic 		}
11837660Sbostic 	if (lastc != '\n')
11937660Sbostic 		putchar('\n');
12050613Sbostic 	putchar('\n');
12137660Sbostic 	(void)fclose(fp);
12237660Sbostic }
123