summaryrefslogtreecommitdiff
path: root/users.c
blob: 006d0c7f5d26e08d32b9b26982ad58c8b310b74f (plain)
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
/*
    Mac-Telnet - Connect to RouterOS routers via MAC address
    Copyright (C) 2010, Håkon Nessjøen <haakon.nessjoen@gmail.com>

    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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "users.h"
#include "config.h"


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;
}