shithub: fingerd

ref: 23aaf92ee4cc8844a5390e3048a23b39c917730c
dir: /fingerd.c/

View raw version
#include <u.h>
#include <libc.h>
#include <bio.h>

#define BUFSIZ 8192

char *usernotfound = "/lib/finger/usernotfound";
char *filenotfound = "/lib/finger/filenotfound";
char *motd = "/lib/finger/motd";

char *req;

int puserlist = 1;

int
vaccess(char *file)
{
	int fd;
	fd = open(file, OREAD);
	if (fd < 0){
		return 0;
	}
	close(fd);
	return -1;
}

int
cat(char *file, int perr)
{
	int n, fd;
	char buf[BUFSIZ];
	fd = open(file, OREAD);
	if (fd < 0){
		if (perr)
			print("File %s not Found.\n", file);
		return 0;
	}
	while ((n = read(fd, buf, BUFSIZ)) > 0)
		if (write(1, buf, n) != n)
			print("Error writing data!\n");
	close(fd);
	return 1;
}

char*
splitpath(char *req)
{
	char *file;
	file = strchr(req, '/');
	if (file == nil || strlen(file) <= 1)
		return "finger";
	file[0] = 0;
	return file+1;
}

void
printuserinfo(char *user)
{
	print("%s\n", user);
}

void
printuserlist(void)
{
	Dir *dirbuf;
	char *path;
	int fd, n, i;
	fd = open("/usr", OREAD);
	if (fd < 0){
		print("Can't list users.\n");
		return;
	}
	n = dirreadall(fd, &dirbuf);
	close(fd);
	if (n)
		print("User list:\n");
	else
		print("No users.\n");
	for (i = 0; i < n; i++){
		path = smprint("/usr/%s/finger/finger", dirbuf[i].name);
		if (vaccess(path)){
			free(path);
			printuserinfo(dirbuf[i].name);
		}
	}
}

void
printnouser(char *user)
{
	if (!cat(usernotfound, 0))
		print("User %s not found or private.\n", user);
}

void
printnofile(char *file)
{
	if (!cat(usernotfound, 0))
		print("File %s not found.\n", file);
}

void
main(int argc, char **argv)
{
	int n;
	Biobuf *bin;
	char *user, *file, *path;

	ARGBEGIN{
	case 'l':
		puserlist = 0;
		break;
	} ARGEND

	bin = Bfdopen(0, OREAD);
	req = Brdline(bin, '\n');
	if (req == nil)
		sysfatal("bad read: %r");
	n = Blinelen(bin);
	req[n-1] = 0;

	switch(n){
	case 1:  /* server request */
		if (!cat(motd, 0))
			print("motd not found.\n");
		if (puserlist)
			printuserlist();
		break;
	default: /* user request   */
		file = splitpath(req);
		user = req;
		if (strlen(user) <= 0) {
			print("bad request.\n");
			break;
		}
		path = smprint("/usr/%s/finger/finger", user);
		if (!vaccess(path)){
			printnouser(user);
			break;
		}
		free(path);
		path = smprint("/usr/%s/finger/%s", user, file);
		if (!cat(path, OREAD))
			printnofile(file);
		free(path);
	}
}