1 /* 2 * Copyright (c) 1989, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Tony Nardo of the Johns Hopkins University/Applied Physics Lab. 7 * 8 * %sccs.include.redist.c% 9 */ 10 11 #ifndef lint 12 static char sccsid[] = "@(#)net.c 8.2 (Berkeley) 11/16/93"; 13 #endif /* not lint */ 14 15 #include <sys/types.h> 16 #include <sys/socket.h> 17 #include <netinet/in.h> 18 #include <arpa/inet.h> 19 #include <netdb.h> 20 #include <db.h> 21 #include <unistd.h> 22 #include <pwd.h> 23 #include <utmp.h> 24 #include <stdio.h> 25 #include <ctype.h> 26 #include <string.h> 27 #include "finger.h" 28 29 void 30 netfinger(name) 31 char *name; 32 { 33 extern int lflag; 34 register FILE *fp; 35 register int c, lastc; 36 struct in_addr defaddr; 37 struct hostent *hp, def; 38 struct servent *sp; 39 struct sockaddr_in sin; 40 int s; 41 char *alist[1], *host; 42 43 if (!(host = rindex(name, '@'))) 44 return; 45 *host++ = NULL; 46 if (!(hp = gethostbyname(host))) { 47 defaddr.s_addr = inet_addr(host); 48 if (defaddr.s_addr == -1) { 49 (void)fprintf(stderr, 50 "finger: unknown host: %s\n", host); 51 return; 52 } 53 def.h_name = host; 54 def.h_addr_list = alist; 55 def.h_addr = (char *)&defaddr; 56 def.h_length = sizeof(struct in_addr); 57 def.h_addrtype = AF_INET; 58 def.h_aliases = 0; 59 hp = &def; 60 } 61 if (!(sp = getservbyname("finger", "tcp"))) { 62 (void)fprintf(stderr, "finger: tcp/finger: unknown service\n"); 63 return; 64 } 65 sin.sin_family = hp->h_addrtype; 66 bcopy(hp->h_addr, (char *)&sin.sin_addr, hp->h_length); 67 sin.sin_port = sp->s_port; 68 if ((s = socket(hp->h_addrtype, SOCK_STREAM, 0)) < 0) { 69 perror("finger: socket"); 70 return; 71 } 72 73 /* have network connection; identify the host connected with */ 74 (void)printf("[%s]\n", hp->h_name); 75 if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) { 76 perror("finger: connect"); 77 (void)close(s); 78 return; 79 } 80 81 /* -l flag for remote fingerd */ 82 if (lflag) 83 write(s, "/W ", 3); 84 /* send the name followed by <CR><LF> */ 85 (void)write(s, name, strlen(name)); 86 (void)write(s, "\r\n", 2); 87 88 /* 89 * Read from the remote system; once we're connected, we assume some 90 * data. If none arrives, we hang until the user interrupts. 91 * 92 * If we see a <CR> or a <CR> with the high bit set, treat it as 93 * a newline; if followed by a newline character, only output one 94 * newline. 95 * 96 * Otherwise, all high bits are stripped; if it isn't printable and 97 * it isn't a space, we can simply set the 7th bit. Every ASCII 98 * character with bit 7 set is printable. 99 */ 100 if (fp = fdopen(s, "r")) 101 while ((c = getc(fp)) != EOF) { 102 c &= 0x7f; 103 if (c == 0x0d) { 104 if (lastc == '\r') /* ^M^M - skip dupes */ 105 continue; 106 c = '\n'; 107 lastc = '\r'; 108 } else { 109 if (!isprint(c) && !isspace(c)) 110 c |= 0x40; 111 if (lastc != '\r' || c != '\n') 112 lastc = c; 113 else { 114 lastc = '\n'; 115 continue; 116 } 117 } 118 putchar(c); 119 } 120 if (lastc != '\n') 121 putchar('\n'); 122 putchar('\n'); 123 (void)fclose(fp); 124 } 125