wm: ircd

Download patch

ref: 1d094b1cb0ca77f1daa5017ff783bbf41bcb66b3
parent: 2d7dbddc67c655b8661a2860f77ca76af1513a35
author: mkf <mkf@cloud9p.org>
date: Wed Sep 20 13:36:00 EDT 2023

add PASS

--- a/ircd.c
+++ b/ircd.c
@@ -1,6 +1,8 @@
 #include <u.h>
 #include <libc.h>
 
+char *password = nil; /* nil to disable server password */
+
 enum {
 	Maxclients = 5,
 	Maxchannels = 5,
@@ -57,6 +59,7 @@
 	int pid;
 	int pings; /* unreplied pings */
 	long msgtimer; /* for flood */
+	int passok; 
 };
 
 /* not to mix with thread(2) Channel:s */
@@ -413,11 +416,19 @@
 	}
 }
 
-void
+int
 setprefix(Client *c)
 {
+	/* fatal */
+	if(password != nil && !c->passok)
+	{
+		delclient(c);
+		return 1;
+	}
+
+	/* not fatal */
 	if(c->user == nil || c->nick == nil)
-		return;
+		return 0;
 
 	if(c->prefix != nil)
 		free(c->prefix);
@@ -425,6 +436,7 @@
 	c->prefix = smprint("%s!~%s@%s", c->nick, c->user, c->conninfo->rsys);
 	if(c->prefix == nil)
 		sysfatal("setprefix: smprint: %r");
+	return 0;
 }
 
 /* allowed chars: [0-9A-Za-z_] */
@@ -472,7 +484,10 @@
 
 	qlock(&clock);
 	c->user = estrdup(user);
-	setprefix(c);
+	
+	if(setprefix(c))
+		return 0;
+
 	qunlock(&clock);
 
 	dprint("%d: user=%s\n", c->fd, c->user);
@@ -527,7 +542,8 @@
 			first = 1;
 
 		c->nick = estrdup(nick);
-		setprefix(c);
+		if(setprefix(c))
+			return 0;
 		dprint("%d: nick=%s\n", c->fd, c->nick);
 	}
 	qunlock(&clock);
@@ -875,6 +891,31 @@
 }
 
 int
+pass(Client *c, char *p)
+{
+	if(password == nil)
+		return 1;
+
+	if(c->passok)
+	{
+		dprint("here\n");
+		close(c->fd);
+		return 0;
+	}
+	else
+	{
+		if(strcmp(p, password) != 0)
+		{
+			close(c->fd);
+			dprint("pass: %s, p: %s\n", password, p);
+		}
+		else
+			c->passok = 1;
+	}
+	return 1;
+}
+
+int
 process(Client *c, char *msg)
 {
 	char *cmd, *tmp, *tmp2;
@@ -905,6 +946,11 @@
 			return 0;
 		return user(c, tmp);
 	}
+	if(strcmp(cmd, "PASS") == 0){
+		tmp = strtok(0, " \r"); /* password */
+		return pass(c, tmp);
+	}
+
 	if(c->prefix == nil) /* not registered */
 		return 1;