forked from urbanserj/entente
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
134 lines (128 loc) · 3.94 KB
/
main.c
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
/*=
* Copyright (c) 2017 Donovan Baarda <[email protected]>
* Based on entente Copyright (c) 2010, 2011 Sergey Urbanovich
*
* Licensed under the GPLv3 License. See LICENSE file for details.
*/
#include "utils.h"
#include "ldap_server.h"
#include "ranges.h"
#include "pam.h"
#include "log.h"
#include <unistd.h>
#include <syslog.h>
char *setting_port = "389";
bool setting_loopback = 0;
bool setting_authnss = 0;
bool setting_daemon = 0;
char *setting_runuser = "root";
char *setting_chroot = NULL;
char *setting_basedn = "dc=lightldapd";
char *setting_rootuser = "root";
bool setting_anonok = 0;
char *setting_crtpath = NULL;
char *setting_caspath = NULL;
char *setting_keypath = NULL;
char *setting_uids = "1000-29999";
char *setting_gids = "100,1000-29999";
char *setting_loglevel = "4";
void settings(int argc, char **argv);
int main(int argc, char **argv)
{
ev_loop *loop = EV_DEFAULT;
mbedtls_net_context socket;
ldap_server server;
char *server_addr;
uid_t runuid;
ldap_ranges uids, gids;
int loglevel;
settings(argc, argv);
server_addr = setting_loopback ? "127.0.0.1" : NULL;
runuid = name2uid(setting_runuser);
loglevel = atoi(setting_loglevel);
if (loglevel < LOG_ALERT || loglevel > LOG_DEBUG)
lerrx(EX_USAGE, "Invalid -L loglevel value: \"%s\"", setting_loglevel);
if (!ldap_ranges_init(&uids, setting_uids))
lerrx(EX_USAGE, "Invalid -U value: \"%s\"", setting_uids);
if (!ldap_ranges_init(&gids, setting_gids))
lerrx(EX_USAGE, "Invalid -G value: \"%s\"", setting_gids);
if (ldap_server_init
(&server, loop, setting_basedn, setting_rootuser, setting_anonok, setting_crtpath, setting_caspath,
setting_keypath, &uids, &gids))
lerr(1, "ldap_server_init() failed");
if (mbedtls_net_bind(&socket, server_addr, setting_port, MBEDTLS_NET_PROTO_TCP))
lerr(1, "mbdedtls_net_bind() failed");
log_init("lightldapd", setting_daemon, loglevel);
if (setting_daemon && daemon(1, 0))
lerr(1, "daemon() failed");
if (setting_chroot && chroot(setting_chroot))
lerr(1, "chroot() failed");
if (chdir("/"))
lerr(1, "chdir() failed");
if (runuid && setuid(runuid))
lerr(1, "setuid() failed");
if (setting_authnss)
auth_user = auth_nss;
ldap_server_start(&server, socket);
ev_run(loop, 0);
return 0;
}
void settings(int argc, char **argv)
{
int c;
while ((c = getopt(argc, argv, "ab:dlp:r:u:A:C:G:K:L:NR:U:")) != -1) {
switch (c) {
case 'a':
setting_anonok = true;
break;
case 'b':
setting_basedn = optarg;
break;
case 'd':
setting_daemon = true;
break;
case 'l':
setting_loopback = true;
break;
case 'p':
setting_port = optarg;
break;
case 'r':
setting_rootuser = optarg;
break;
case 'u':
setting_runuser = optarg;
break;
case 'A':
setting_caspath = optarg;
break;
case 'C':
setting_crtpath = optarg;
break;
case 'G':
setting_gids = optarg;
break;
case 'K':
setting_keypath = optarg;
break;
case 'L':
setting_loglevel = optarg;
break;
case 'N':
setting_authnss = true;
break;
case 'R':
setting_chroot = optarg;
break;
case 'U':
setting_uids = optarg;
break;
default:
fprintf(stderr,
"Usage: %s [-a] [-b dc=lightldapd] [-r rootuser] [-l] [-p 389] [-d] \\\n"
" [-u runuser] [-R chroot] [-C crtfile] [-A ca-file] [-K keyfile] \\\n"
" [-U 1000-29999,...] [-G 100,1000-29999,...] [-N] [-L loglevel]", argv[0]);
exit(EX_USAGE);
}
}
}