shithub: fingerd

Download patch

ref: 75a944b4791241499ef3ad85ff88add6e4a7ff4a
author: sirjofri <sirjofri@sirjofri.de>
date: Wed Apr 14 13:26:40 EDT 2021

adds fingerd client. should generally work.

features:
/lib/finger/motd
/lib/finger/usernotfound
/lib/finger/filenotfound
/usr/$user/finger/finger - $user@server
/usr/$user/finger/file - $user/file@server

--- /dev/null
+++ b/LICENSE.txt
@@ -1,0 +1,7 @@
+Copyright © 2020 sirjofri
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
--- /dev/null
+++ b/fingerd.c
@@ -1,0 +1,143 @@
+#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 = strrchr(req, '/');
+	if (file == nil || strlen(file) == 0)
+		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;
+		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(path);
+		free(path);
+	}
+}
--- /dev/null
+++ b/mkfile
@@ -1,0 +1,6 @@
+</$objtype/mkfile
+
+TARG=fingerd
+OFILES=fingerd.$O
+
+</sys/src/cmd/mkone