xref: /csrg-svn/libexec/fingerd/fingerd.c (revision 36199)
122492Sdist /*
235556Sbostic  * Copyright (c) 1983 The Regents of the University of California.
335556Sbostic  * All rights reserved.
435556Sbostic  *
535556Sbostic  * Redistribution and use in source and binary forms are permitted
635556Sbostic  * provided that the above copyright notice and this paragraph are
735556Sbostic  * duplicated in all such forms and that any documentation,
835556Sbostic  * advertising materials, and other materials related to such
935556Sbostic  * distribution and use acknowledge that the software was developed
1035556Sbostic  * by the University of California, Berkeley.  The name of the
1135556Sbostic  * University may not be used to endorse or promote products derived
1235556Sbostic  * from this software without specific prior written permission.
1335556Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1435556Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1535556Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1622492Sdist  */
1722492Sdist 
1816471Ssam #ifndef lint
1922492Sdist char copyright[] =
2035556Sbostic "@(#) Copyright (c) 1983 The Regents of the University of California.\n\
2122492Sdist  All rights reserved.\n";
2235556Sbostic #endif /* not lint */
2316471Ssam 
2422492Sdist #ifndef lint
25*36199Sbostic static char sccsid[] = "@(#)fingerd.c	5.3 (Berkeley) 11/03/88";
2635556Sbostic #endif /* not lint */
2722492Sdist 
2816471Ssam /*
2916471Ssam  * Finger server.
3016471Ssam  */
3116471Ssam #include <sys/types.h>
3216471Ssam #include <netinet/in.h>
3316471Ssam #include <stdio.h>
3416471Ssam #include <ctype.h>
3516471Ssam 
3616471Ssam main(argc, argv)
3735556Sbostic 	int argc;
3816471Ssam 	char *argv[];
3916471Ssam {
4016770Sralph 	register char *sp;
4116770Sralph 	char line[512];
4216471Ssam 	struct sockaddr_in sin;
4316770Sralph 	int i, p[2], pid, status;
4416595Sralph 	FILE *fp;
4516770Sralph 	char *av[4];
4616471Ssam 
4716471Ssam 	i = sizeof (sin);
4816471Ssam 	if (getpeername(0, &sin, &i) < 0)
4916471Ssam 		fatal(argv[0], "getpeername");
50*36199Sbostic 	if (fgets(line, sizeof(line), stdin) == NULL)
51*36199Sbostic 		exit(1);
5216595Sralph 	sp = line;
5316770Sralph 	av[0] = "finger";
54*36199Sbostic 	for (i = 1;;) {
5516595Sralph 		while (isspace(*sp))
5616595Sralph 			sp++;
5716595Sralph 		if (!*sp)
5816471Ssam 			break;
5916595Sralph 		if (*sp == '/' && (sp[1] == 'W' || sp[1] == 'w')) {
6016595Sralph 			sp += 2;
6116770Sralph 			av[i++] = "-l";
6216471Ssam 		}
6316770Sralph 		if (*sp && !isspace(*sp)) {
6416770Sralph 			av[i++] = sp;
6516770Sralph 			while (*sp && !isspace(*sp))
6616770Sralph 				sp++;
6716770Sralph 			*sp = '\0';
6816770Sralph 		}
6916471Ssam 	}
7016770Sralph 	av[i] = 0;
7116770Sralph 	if (pipe(p) < 0)
7216770Sralph 		fatal(argv[0], "pipe");
7316770Sralph 	if ((pid = fork()) == 0) {
7416770Sralph 		close(p[0]);
7516770Sralph 		if (p[1] != 1) {
7616770Sralph 			dup2(p[1], 1);
7716770Sralph 			close(p[1]);
7816770Sralph 		}
7916770Sralph 		execv("/usr/ucb/finger", av);
8016770Sralph 		_exit(1);
8116770Sralph 	}
8216770Sralph 	if (pid == -1)
8316770Sralph 		fatal(argv[0], "fork");
8416770Sralph 	close(p[1]);
8516770Sralph 	if ((fp = fdopen(p[0], "r")) == NULL)
8616770Sralph 		fatal(argv[0], "fdopen");
8716595Sralph 	while ((i = getc(fp)) != EOF) {
8816595Sralph 		if (i == '\n')
8916595Sralph 			putchar('\r');
9016595Sralph 		putchar(i);
9116595Sralph 	}
9216770Sralph 	fclose(fp);
9316770Sralph 	while ((i = wait(&status)) != pid && i != -1)
9416770Sralph 		;
9516595Sralph 	return(0);
9616471Ssam }
9716471Ssam 
9816471Ssam fatal(prog, s)
9916471Ssam 	char *prog, *s;
10016471Ssam {
10116471Ssam 	fprintf(stderr, "%s: ", prog);
10216471Ssam 	perror(s);
10316471Ssam 	exit(1);
10416471Ssam }
105