File: falselogin.c

package info (click to toggle)
falselogin 0.3-4
  • links: PTS
  • area: main
  • in suites: bullseye, buster, jessie, jessie-kfreebsd, lenny, sid, squeeze, stretch, wheezy
  • size: 56 kB
  • ctags: 15
  • sloc: ansic: 125; makefile: 57; perl: 20
file content (216 lines) | stat: -rw-r--r-- 5,757 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
 Falselogin by Tibor Koleszar <oldw@debian.org> GNU/GPL
 Further development made by Jose Parrella <joseparrella@cantv.net>
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <publib.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pwd.h>
#include <sys/types.h>
#include <sys/utsname.h>

// This is the maximum length of line buffers for
// configuration files and mailbox files.
#define MAXLEN_BUF 1024

// This is the maximum length of the user names.
#define MAXLEN_USERNAME 50

// This is the default configuration file on Debian,
// attending to FHS.
#define CONFIG_FILE "/etc/falselogin.conf"

// This is the default pattern for counting messages
// in a mailbox.
#define MESSAGE_ID "Message-ID"

// Maximum length for the hostname
#define MAXLEN_HOSTNAME 512

// Maximum length for the Debian version information
#define MAXLEN_DEBVER 50

// Maximum length for quantity of mails
#define MAIL_LEN 50

// Function definitions
int countMail (char userName[]);

int main (int argc, char *argv[]) {
	FILE *f, *g;					// f will hold the configuration file
	struct passwd *pwd;				// this holds the pwent
	char s_mail[MAIL_LEN];				// length of mail number
	char *temp, host_n[MAXLEN_HOSTNAME], 
	deb_ver[MAXLEN_DEBVER] = "";			// temp will hold a conffile line
	struct utsname u_name;				// this holds the u_name
	size_t max_buf = MAXLEN_BUF;			// maximum buffer length
	int ok = 0;					// lame control (see passwd filling)
	long wait = 0;					// seconds (negative to wait for ENTER)

	temp = malloc(MAXLEN_BUF);			// initial allocation (could happen later)
							// thou shalt not cast

	// This fills the passwd struct with the
	// pwent until it ends.
	// (it might be a better way)
	while (!ok) {
		pwd = getpwent ();
		if (pwd && pwd->pw_uid == getuid ())
			ok = 1;
	}
	
	// This fills the uname struct with the
	// data and exits if it fails.
	if (uname ((struct utsname *) &u_name) == -1) {
		perror ("Error at calling uname()");
		exit (EXIT_FAILURE);
	}
	
	// Proceeds to open the configuration
	// file specified as a macro constant.
	if ((f = fopen (CONFIG_FILE, "rt")) == NULL) {
		perror ("Can't parse config file");
		exit (EXIT_FAILURE);
	}

	// While we have fun...
	while (!feof (f)) {
		// Main iteration
		// f: The opened CONFIG_FILE in rt mode.
		// &temp: A line, taken from the CONFIG_FILE
		// &max_buf: A reference to the maximum buffer length

		// Now I reset the buffer size
		// (but I don't really resize &temp)
		max_buf = MAXLEN_BUF;

		// This does the same that fgets (temp, 1024, f) does in less iterations,
		// but with a variable buffer size.
		if (getline (&temp, &max_buf, f) == -1) {
			continue;
		}

		// If we still have something to work on...
		// ...and this is not a comment
		if (!feof (f) && strncmp(temp, "#", 1)) {

			// Substitutions follows: they happen on the temp
			// buffer which is 1024 bytes long, so only 1023
			// characters will be allowed.

			// Search for a wait parameter

			if ( !strncmp(temp, "wait=", 5) ) {
				if ( !strncmp(temp+5, "enter", 5) )
					wait = -1;
				else if ( atol(temp+5) > 0 ) 
					wait = atol(temp+5);
				continue;
			}

			// Substitutions using passwd and uname structs

			if (strstr (temp, "%user%") != NULL)
				strgsub (temp, "%user%", pwd->pw_name, max_buf);

			if (strstr (temp, "%sysname%") != NULL)
				strgsub (temp, "%sysname%", u_name.sysname, max_buf);

			if (strstr (temp, "%nodename%") != NULL)
				strgsub (temp, "%nodename%", u_name.nodename, max_buf);

			if (strstr (temp, "%release%") != NULL)
				strgsub (temp, "%release%", u_name.release, max_buf);

			if (strstr (temp, "%version%") != NULL)
				strgsub (temp, "%version%", u_name.version, max_buf);

			if (strstr (temp, "%machine%") != NULL)
				strgsub (temp, "%machine%", u_name.machine, max_buf);

			// Self-contained substitutions

			if (strstr (temp, "%host%") != NULL) {
				gethostname(host_n, MAXLEN_HOSTNAME);
				strgsub (temp, "%host%", host_n, max_buf);
			}

			if (strstr (temp, "%mail%") != NULL) {
				snprintf (s_mail, MAXLEN_BUF, "%d", countMail(pwd->pw_name)); 
				strgsub (temp, "%mail%", s_mail, max_buf);
			}

			if (strstr (temp, "%debian_version%") != NULL) {
				if ((g = fopen ("/etc/debian_version", "rt")) != NULL) {
					fgets(deb_ver, sizeof(deb_ver), g);
					if (*deb_ver && deb_ver[strlen(deb_ver)-1] == '\n')
						deb_ver[strlen(deb_ver)-1] = 0;
					fclose(g);
				};
				strgsub (temp, "%debian_version%", deb_ver, max_buf);
			}
		
			// Prints the new line with substitutions

			printf ("%s", temp);
		}

	}

	// Closes the configuration file after the feof
	fclose (f);

	// Frees the buffer
	free (temp);

	// Now we can check the arguments
	if (argc > 2) {
		// Supersedes the eventual wait value in the conf file
		if ( !strncmp(argv[1], "--wait", 6) || !strncmp(argv[1], "-w", 2) ) {
			if ( !strncmp(argv[2], "enter", 5) )
				wait = -1;
			else if ( atol(argv[2]) > 0 ) 
				wait = atol(argv[2]);
		}
		else {
			printf("Unknown arguments were given\n");
		}
	}
	else if (argc > 3 || argc == 2) {
		printf("Wrong number of arguments given\n");
		exit(EXIT_FAILURE);
	}

	// Wait for some time or the user to press ENTER.
	// If no wait where specified it doesn't wait (0 sec).
	if (wait >= 0)
		sleep(wait);
	else
		while (getchar() != '\n');

	// Bye-bye if we don't have arguments
	exit(EXIT_SUCCESS);
}

int countMail (char userName[MAXLEN_USERNAME]) {
	int total = 0;
	char *temp;
	FILE *f;

	temp = (char *) malloc (MAXLEN_BUF);
	snprintf (temp, MAXLEN_BUF, "/var/mail/%s", userName);

	if ( (f = fopen (temp, "rt")) != NULL) {
		while (!feof (f)) {
			fgets (temp, MAXLEN_BUF, f);
			if (!strncasecmp (temp, MESSAGE_ID, strlen (MESSAGE_ID)))
				total++;
		}
		fclose (f);
	}

	return total;
}