about summary refs log tree commit diff stats
path: root/src/main.c
blob: 3701c792d72a3f183a7a8e8f3eb6d245668d0ee1 (plain) (blame)
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
#include <unistd.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <syslog.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netdb.h>
#include <pthread.h>
#include <sys/types.h>
#include <pwd.h>
#include <grp.h>
#include <errno.h>
#include <signal.h>

#include "packet.h"
#include "netascii.h"

static int stop = 0;

void stop_handler() {
	stop = 1;
	return;
}

void usage(char *name) {
	printf("USAGE: %s -d -u nobody -g nobody -a 127.0.0.1 -p 69\n", name);
	printf("\td: daemonize.\n");
	printf("\tu: username to run as (default: nobody).  Must be specified after -d.\n");
	printf("\tg: group to run as (default: nobody).  Must be specified after -d.\n");
	printf("\ta: address to bind to (default: 127.0.0.1)\n");
	printf("\tp: port to bind to (default: 69)\n");
}

int main(int argc, char **argv) {
	int daemonize = 0;
	char addr[16], user[32], group[32];
	memset(addr, '\0', sizeof(addr));
	memset(user, '\0', sizeof(user));
	memset(group, '\0', sizeof(group));
	strcpy(addr, "127.0.0.1");
	strcpy(user, "nobody");
	strcpy(group, "nobody");
	int port = 69;
	
	int ch = 0;
	while ((ch = getopt(argc, argv, "da:p:u:g:h")) != -1) {
		switch (ch) {
			case 'a':
				//TODO: this is unsafe
				strcpy(addr, optarg);
				break;
			case 'p':
				port = atoi(optarg);
				if ((port <= 0) || (port >= 65536)) {
					fprintf(stderr, "invalid port specified.\n");
					return -1;
				}
				break;
			case 'd':
				daemonize = 1;
				break;
			case 'g':
				if (daemonize) {
					//TODO: this is unsafe
					strcpy(group, optarg);
				} else {
					fprintf(stderr, "-g requires -d\n");
					return -1;
				}
				break;
			case 'u':
				if (daemonize) {
					//TODO: this is unsafe
					strcpy(user, optarg);
				} else {
					fprintf(stderr, "-u requires -d\n");
					return -1;
				}
				break;
			 case 'h': case '?':
				usage(argv[0]);
				return -1;
		}
	}

	setlogmask(LOG_UPTO(LOG_INFO));
	openlog(argv[0], LOG_PID | LOG_PERROR | LOG_NDELAY, LOG_FTP);
	syslog(LOG_INFO, "starting up...");

	if (daemonize) {
		if (daemon(1, 0)) {
			syslog(LOG_ERR, "failed to daemonize as requested!");
			return -1;
		} else {
			syslog(LOG_INFO, "daemonized");
		}
	}

	int s = socket(AF_INET, SOCK_DGRAM, 0);
	if (s <= 0) {
		syslog(LOG_ERR, "unable to bind socket!");
		return -1;
	}
	struct sockaddr_in saddr;
	saddr.sin_family = AF_INET;
	saddr.sin_port = htons(port);
	saddr.sin_addr.s_addr = inet_addr(addr);

	if (bind(s, (struct sockaddr*)&saddr, sizeof(saddr)) < 0) {
		switch (errno) {
			case EACCES:
				syslog(LOG_ERR, "socket bind failed: permission denied");
				break;
			case EADDRINUSE:
				syslog(LOG_ERR, "socket bind failed: address already in use");
				break;
			case EADDRNOTAVAIL:
				syslog(LOG_ERR, "socket bind failed: address not available");
				break;
			default:
				syslog(LOG_ERR, "socket bind failed");
		}
		return -1;
	}
	syslog(LOG_INFO, "socket bind success");

	//TODO: use getpwnam_r() and getgrnam_r()
	struct passwd *u = getpwnam((const char *)&user);
	if (setuid((*u).pw_uid) == -1) {
		syslog(LOG_ERR, "failed to drop user privileges");
		return -1;
	}
	struct group *g = getgrnam((const char *)&group);
	if (setgid((*g).gr_gid) == -1) {
		syslog(LOG_ERR, "failed to drop group privileges");
		return -1;
	}

	// create persistent buffer
	char *buf;
	buf = malloc(1024);
	if (buf == NULL) {
		syslog(LOG_CRIT, "unable to allocate memory!");
		close(s);
		return -1;
	}
	while (!stop) {
		struct sockaddr_in caddr;
		unsigned int clen = sizeof(caddr);
		memset(buf, '\0', 1024);
		if (recvfrom(s, buf, 1024, 0, (struct sockaddr*)&caddr, &clen) < 0) {
			syslog(LOG_ERR,
					"got a client connection, but unable to receive data!");
			free(buf);
			continue;
		}
		//TODO: process packet

		// we will never actually join on this thread, and don't care to keep it around.
		//pthread_t *_thread; // since this hasn't been implemented yet, clang is complaining

	}

	// free our persistent buffer
	free(buf);
	// close socket
	close(s);
	// wait for threads to exit
	pthread_exit(NULL);
	// exit program
	return 0;
}