137660Sbostic /*
261995Sbostic * Copyright (c) 1989, 1993
361995Sbostic * 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*69101Sbostic static char sccsid[] = "@(#)net.c 8.4 (Berkeley) 04/28/95";
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
netfinger(name)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;
4665318Sbostic if (isdigit(*host) && (defaddr.s_addr = inet_addr(host)) != -1) {
4737660Sbostic def.h_name = host;
4837660Sbostic def.h_addr_list = alist;
4937660Sbostic def.h_addr = (char *)&defaddr;
5037660Sbostic def.h_length = sizeof(struct in_addr);
5137660Sbostic def.h_addrtype = AF_INET;
5237660Sbostic def.h_aliases = 0;
5337660Sbostic hp = &def;
5465318Sbostic } else if (!(hp = gethostbyname(host))) {
5565318Sbostic (void)fprintf(stderr,
5665318Sbostic "finger: unknown host: %s\n", host);
5765318Sbostic return;
5837660Sbostic }
5937660Sbostic if (!(sp = getservbyname("finger", "tcp"))) {
6037660Sbostic (void)fprintf(stderr, "finger: tcp/finger: unknown service\n");
6137660Sbostic return;
6237660Sbostic }
6337660Sbostic sin.sin_family = hp->h_addrtype;
6437660Sbostic bcopy(hp->h_addr, (char *)&sin.sin_addr, hp->h_length);
6537660Sbostic sin.sin_port = sp->s_port;
6637660Sbostic if ((s = socket(hp->h_addrtype, SOCK_STREAM, 0)) < 0) {
6737660Sbostic perror("finger: socket");
6837660Sbostic return;
6937660Sbostic }
7037660Sbostic
7137660Sbostic /* have network connection; identify the host connected with */
7237660Sbostic (void)printf("[%s]\n", hp->h_name);
7337660Sbostic if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
7437660Sbostic perror("finger: connect");
7537660Sbostic (void)close(s);
7637660Sbostic return;
7737660Sbostic }
7837660Sbostic
7937660Sbostic /* -l flag for remote fingerd */
8037660Sbostic if (lflag)
8137660Sbostic write(s, "/W ", 3);
8237660Sbostic /* send the name followed by <CR><LF> */
8337660Sbostic (void)write(s, name, strlen(name));
8437660Sbostic (void)write(s, "\r\n", 2);
8537660Sbostic
8637660Sbostic /*
8740190Sbostic * Read from the remote system; once we're connected, we assume some
8840190Sbostic * data. If none arrives, we hang until the user interrupts.
8937660Sbostic *
9040190Sbostic * If we see a <CR> or a <CR> with the high bit set, treat it as
9140190Sbostic * a newline; if followed by a newline character, only output one
9240190Sbostic * newline.
9337660Sbostic *
9437660Sbostic * Otherwise, all high bits are stripped; if it isn't printable and
9537660Sbostic * it isn't a space, we can simply set the 7th bit. Every ASCII
9637660Sbostic * character with bit 7 set is printable.
9737660Sbostic */
98*69101Sbostic lastc = 0;
99*69101Sbostic if ((fp = fdopen(s, "r")) != NULL)
10037660Sbostic while ((c = getc(fp)) != EOF) {
10140190Sbostic c &= 0x7f;
10240190Sbostic if (c == 0x0d) {
10364885Sbostic if (lastc == '\r') /* ^M^M - skip dupes */
10464885Sbostic continue;
10537660Sbostic c = '\n';
10640190Sbostic lastc = '\r';
10740190Sbostic } else {
10840190Sbostic if (!isprint(c) && !isspace(c))
10940190Sbostic c |= 0x40;
11040190Sbostic if (lastc != '\r' || c != '\n')
11140190Sbostic lastc = c;
11240190Sbostic else {
11340190Sbostic lastc = '\n';
11440190Sbostic continue;
11540190Sbostic }
11640190Sbostic }
11737660Sbostic putchar(c);
11837660Sbostic }
11937660Sbostic if (lastc != '\n')
12037660Sbostic putchar('\n');
12150613Sbostic putchar('\n');
12237660Sbostic (void)fclose(fp);
12337660Sbostic }
124