forked from haakonnessjoen/MAC-Telnet
-
Notifications
You must be signed in to change notification settings - Fork 6
/
users.c
109 lines (90 loc) · 2.81 KB
/
users.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
/*
Mac-Telnet - Connect to RouterOS or mactelnetd devices via MAC address
Copyright (C) 2010, Håkon Nessjøen <[email protected]>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <libintl.h>
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <pwd.h>
#include "users.h"
#include "config.h"
#define _(String) gettext (String)
struct mt_credentials mt_users[MT_CRED_MAXNUM];
void read_userfile() {
FILE *file = fopen(USERSFILE, "r");
char line [BUFSIZ];
int i = 0;
if (file == NULL) {
perror(USERSFILE);
exit(1);
}
while ( fgets(line, sizeof line, file) ) {
char *user;
char *password;
user = strtok(line, ":");
password = strtok(NULL, "\n");
if (user == NULL || password == NULL) {
continue;
}
if (user[0] == '#')
continue;
memcpy(mt_users[i].username, user, strlen(user) < MT_CRED_LEN - 1? strlen(user) : MT_CRED_LEN);
memcpy(mt_users[i++].password, password, strlen(password) < MT_CRED_LEN - 1? strlen(password) : MT_CRED_LEN);
if (i == MT_CRED_MAXNUM)
break;
mt_users[i].username[0] = '\0';
}
fclose(file);
}
struct mt_credentials* find_user(char *username) {
int i = 0;
while (i < MT_CRED_MAXNUM && mt_users[i].username[0] != 0) {
if (strcmp(username, mt_users[i].username) == 0) {
return &(mt_users[i]);
}
i++;
}
return NULL;
}
void drop_privileges(char *username) {
struct passwd *user = (struct passwd *) getpwnam(username);
if (user == NULL) {
fprintf(stderr, _("Failed dropping privileges. The user %s is not a valid username on local system.\n"), username);
exit(1);
}
if (getuid() == 0) {
/* process is running as root, drop privileges */
if (setgid(user->pw_gid) != 0) {
perror("setgid: Error dropping group privileges");
exit(1);
}
if (setuid(user->pw_uid) != 0) {
perror("setuid: Error dropping user privileges");
exit(1);
}
/* Verify if the privileges were dropped. */
if (setuid(0) != -1) {
perror("Failed to drop privileges");
exit(1);
}
}
else {
fprintf(stderr, _("Failed dropping privileges. Not running as privileged user.\n"));
exit(1);
}
}