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
|
#include <stdio.h>
#include <string.h>
#define AUTOLOGIN_PATH ".mactelnet"
#define AUTOLOGIN_MAXSTR 100
#define AUTOLOGIN_MAXPROFILES 100
struct autologin {
char identifier[AUTOLOGIN_MAXSTR];
char username[AUTOLOGIN_MAXSTR];
char password[AUTOLOGIN_MAXSTR];
char inuse:1;
char hasUsername:1;
char hasPassword:1;
};
struct autologin logins[AUTOLOGIN_MAXPROFILES];
enum autologin_state {
ALS_NONE,
ALS_PREIDENTIFIER,
ALS_IDENTIFIER,
ALS_PREKEY,
ALS_KEY,
ALS_PREVALUE,
ALS_VALUE
};
#define AL_NONE 0
int main() {
FILE *fp;
char c;
int i = -1;
char *p;
char key[AUTOLOGIN_MAXSTR];
char value[AUTOLOGIN_MAXSTR];
int line_counter=1;
enum autologin_state state = ALS_NONE;
fp = fopen(AUTOLOGIN_PATH, "r");
while ((c = fgetc(fp)) && !feof(fp)) {
if (c == '#') {
while ((c = fgetc(fp)) != '\n' && !feof(fp));
}
switch (state) {
case ALS_PREIDENTIFIER:
i++;
if (i == AUTOLOGIN_MAXPROFILES) {
goto done;
}
p = logins[i].identifier;
state++;
break;
case ALS_PREKEY:
memset(key, 0, AUTOLOGIN_MAXSTR);
memset(value, 0, AUTOLOGIN_MAXSTR);
p = key;
logins[i].inuse = 1;
state++;
break;
case ALS_PREVALUE:
memset(value, 0, AUTOLOGIN_MAXSTR);
p = value;
state++;
break;
}
switch (state) {
case ALS_NONE:
if (c == '[') {
state = ALS_PREIDENTIFIER;
}
break;
case ALS_IDENTIFIER:
if (c == ']') {
//fprintf(stderr, "debug: identifier %s on line %d\n", logins[i].identifier, line_counter);
state = ALS_PREKEY;
break;
}
if (c == '\n') {
fprintf(stderr, "Error on line %d in %s: New line in middle of identifier\n", line_counter, AUTOLOGIN_PATH);
state = ALS_NONE;
break;
}
*p++ = c;
if (p - logins[i].identifier == AUTOLOGIN_MAXSTR-1) {
*p = 0;
fprintf(stderr, "Error on line %d in %s: Identifier string too long.\n", line_counter, AUTOLOGIN_PATH);
while ((c = fgetc(fp)) != '\n' && c != ']' && !feof(fp));
state = ALS_PREKEY;
break;
}
break;
case ALS_KEY:
if (p == key && c == '\n') break;
if (c == '=') {
state = ALS_PREVALUE;
break;
}
if (c == '[') {
state = ALS_PREIDENTIFIER;
break;
}
if (c == ' ') { // ignore whitespace
break;
}
if (c == '\n') {
fprintf(stderr, "Error on line %d in %s: Newline before '=' character\n", line_counter, AUTOLOGIN_PATH);
state = ALS_PREKEY;
break;
}
*p++ = c;
if (p - key == AUTOLOGIN_MAXSTR-1) {
*p = 0;
fprintf(stderr, "Error on line %d in %s: Key string too long.\n", line_counter, AUTOLOGIN_PATH);
while ((c = fgetc(fp)) != '\n' && c != '=' && !feof(fp));
if (c == '\n') {
state = ALS_PREKEY;
} else {
state = ALS_PREVALUE;
}
}
break;
case ALS_VALUE:
if (p == value && c == '\n') break;
if (c == '\n') {
if (strncasecmp(key, "user", AUTOLOGIN_MAXSTR) == 0) {
strncpy(logins[i].username, value, AUTOLOGIN_MAXSTR);
logins[i].hasUsername = 1;
} else if (strncasecmp(key, "password", AUTOLOGIN_MAXSTR) == 0) {
strncpy(logins[i].password, value, AUTOLOGIN_MAXSTR);
logins[i].hasPassword = 1;
} else {
fprintf(stderr, "Warning on line %d of %s: Unknown parameter %s, ignoring.\n", line_counter, AUTOLOGIN_PATH, key);
}
state = ALS_PREKEY;
break;
}
if (c == ' ') { // ignore whitespace
break;
}
*p++ = c;
if (p - value == AUTOLOGIN_MAXSTR-1) {
*p = 0;
fprintf(stderr, "Error on line %d in %s: Value string too long.\n", line_counter, AUTOLOGIN_PATH);
while ((c = fgetc(fp)) != '\n' && !feof(fp));
if (c == '\n') {
state = ALS_PREKEY;
}
}
break;
}
if (c == '\n') {
line_counter++;
}
if (feof(fp)) {
break;
}
}
done:
fclose(fp);
printf("\n\nConfig:\n");
for (i = 0; i < 100; ++i) {
if (logins[i].inuse) {
printf("Profile: '%s'\n", logins[i].identifier);
if (logins[i].hasUsername) {
printf("\tUsername: '%s'\n", logins[i].username);
}
if (logins[i].hasPassword) {
printf("\tPassword: '%s'\n", logins[i].password);
}
printf("\n");
}
}
}
|