File: nullidentd.c

package info (click to toggle)
nullidentd 1.0-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 172 kB
  • sloc: ansic: 332; makefile: 89; sh: 26
file content (171 lines) | stat: -rw-r--r-- 3,273 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
    nullidentd.c

    Copyright 1999 Brian Young <bayoung@acm.org>

    Licensed under the GPL, see the file COPYING included with this distribution.
*/

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

#include "version.h"

#define SESSION_TIMEOUT 20

#define MAX_RESPONSE 200
#define MAX_REQUEST 100
#define MAX_USERID 50
#define MAX_RANDOMID 8

void usage()
{
    fprintf( stderr, "nullidentd -- version %s\n", VERSION );
    fprintf( stderr, "Copyright 1999 Brian Young <bayoung@acm.org>\n" );
    fprintf( stderr, "usage: nullidentd [userid]\n" );
}

int write_response( int fd, char *response, int len )
{
    int		retval;
    int		byteswritten = 0;

    while( byteswritten < len ) {
	retval = write( fd, response + byteswritten, len - byteswritten );
	if( retval <= 0 ) {
	    /* error */
	    return 0;
	}
	byteswritten += retval;
    }

    /* success */
    return 1;
}

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

    /* read until \n */
    while( bytesread < maxlen - 1 ) {
	if( read( fd, &c, 1 ) != 1 ) {
	    /* error */
	    return 0;
	}
	if( c == '\n' ) {
	    /* end of line */
	    break;
	}

	request[bytesread] = c;
	bytesread += 1;
    }

    if( bytesread > 0 && request[bytesread-1] == '\r' ) {
	/* strip trailing \r */
	bytesread -= 1;
    }
    /* null terminate */
    request[bytesread] = '\0';

    /* success */
    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 );
}

int main( int argc, const char *argv[] )
{
    const char *	userid = "foobar";
    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" );
	return 1;
    }
    if( getuid() == 0 ) {
	fprintf( stderr, "User id is root, exiting.\n" );
	return 1;
    }

    if( argc > 2 ) {
	usage();
	return 1;
    }

    if( argc == 2 ) {
	/* userid parameter specified */
	userid = argv[1];
	if( strlen( userid ) > MAX_USERID ) {
	    fprintf( stderr, "Userid parameter is too long.\n" );
	    return 1;
	}
    }

    if (strcmp(userid, "RANDOM") == 0) {
	    gen_random = 1;
    }

    infd = fileno( stdin );
    outfd = fileno( stdout );

    // set the session timeout
    signal( SIGALRM, session_timeout );
    alarm( SESSION_TIMEOUT );

    srand(getpid() ^ time(NULL));

    for( ;; ) {
	/* read the request */
	if( !read_request( infd, request, MAX_REQUEST ) ) {
	    /* error or timed out */
	    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 );

	/* send the line */
	if( !write_response( outfd, response, response_len ) ) {
	    goto done;
	}
    }

done:
    return 0;
}