Nullidentd random username patch

From ThisBlueWiki

Jump to: navigation, search

This patch allows the venerable nullidentd to respond with a different random username every time it is queried if the username given to it as an argument is "RANDOM".

This patch was stripped out of Debian's patch to nullidentd 1.0 (the Debian version is 1.0-3).

Simply save this text to something like nullidentd.diff and then in the source dir run patch -p1 < nullidentd.diff (or whatever you named it). You don't want to apply the full Debian patch, obviously, if you're not running Debian. After you have patched nullidentd.c you can then run 'make' and go from there.

--- nullidentd-1.0.orig/nullidentd.c
+++ nullidentd-1.0/nullidentd.c
@@ -7,9 +7,11 @@
 */

 #include <stdio.h>
+#include <stdlib.h>
 #include <sys/types.h>
 #include <unistd.h>
 #include <signal.h>
+#include <time.h>

 #include "version.h"

@@ -18,6 +20,7 @@
 #define MAX_RESPONSE 200
 #define MAX_REQUEST 100
 #define MAX_USERID 50
+#define MAX_RANDOMID 8

 void usage()
 {
@@ -46,7 +49,6 @@

 int read_request( int fd, char *request, int maxlen )
 {
-    int		retval;
     char	c;
     int		bytesread = 0;

@@ -76,6 +78,22 @@
     return 1;
 }

+char *random_userid( void )
+{
+    static char buf[MAX_RANDOMID+1];
+    size_t i;
+    static const char valid[] =
+        "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
+
+    for (i = 0 ; i < MAX_RANDOMID ; i++)
+        buf[i] = valid[rand() % (sizeof(valid) - 1)];
+
+    buf[i] = '\0';
+
+    return buf;
+}
+
+
 void session_timeout( int foo )
 {
     exit( 0 );
@@ -84,12 +102,12 @@
 int main( int argc, const char *argv[] )
 {
     const char *	userid = "foobar";
-    char		c;
     int			infd;
     int			outfd;
     int			response_len;
     char		response[MAX_RESPONSE];
     char		request[MAX_REQUEST];
+    int			gen_random = 0;

     if( getgid() == 0 ) {
 	fprintf( stderr, "Group id is root, exitting.\n" );
@@ -114,6 +132,10 @@
 	}
     }

+    if (strcmp(userid, "RANDOM") == 0) {
+	    gen_random = 1;
+    }
+
     infd = fileno( stdin );
     outfd = fileno( stdout );

@@ -121,6 +143,8 @@
     signal( SIGALRM, session_timeout );
     alarm( SESSION_TIMEOUT );

+    srand(getpid() ^ time(NULL));
+
     for( ;; ) {
 	/* read the request */
 	if( !read_request( infd, request, MAX_REQUEST ) ) {
@@ -128,6 +152,10 @@
 	    goto done;
 	}

+	if (gen_random) {
+		userid = random_userid();
+	}
+
 	/* format the response */
 	response_len = snprintf( response, sizeof( response ), "%.20s : USERID : UNIX : %.20s\r\n", request, userid );

@@ -140,4 +168,3 @@
 done:
     return 0;
 }
-
This page was last modified on 7 January 2008, at 08:04. This page has been accessed 323 times.