summaryrefslogtreecommitdiff
path: root/config.c
blob: d652147631bed31b87ba91aab65a38a6163f6d4f (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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#include "config.h"

// TODO - Multiple functions here (at least readnames(), replayseconds() and checkpassword()) have the file opening code, rewrite.

// TODO - Can isconf() and getconf() just be merged into one function?

// Check to see if a line is the configuration option specified by
// checking to see if str starts with conf followed by a space, a tab,
// or an equals sign.
// Returns the length of the name of the requested option if found, or
// zero if not.
long int isconf(char *str, char *conf) {
  char *ret;
  char substr[MAXCHAR];
  long int len = strlen(conf);

  // Ensure the next character is an equals sign, a space, or a tab
  if (str[len] != '=' && str[len] != ' ' && str[len] != ' ') {
    return 0;
  }

  strncpy(substr, str, len);
  substr[len] = '\0';


  if (!(ret = strstr(substr, conf)) == 0) {
    return len;
  }

  return 0;
}

// Extracts the value of a configuration option from a string.  Expects
// to receive the string and the number of characters long that the
// configuration option name is.
void getconf(char *str, long int start) {
  long int len = strlen(str);
  long int pos;
  char conf[MAXCHAR];

  for (long int i = start; i < len; i++) {
    if (str[i] == '=' || str[i] == ' ' || str[i] == ' ') {
      continue;
    } else {
      pos = i;
      break;
    }
  }

  strncpy(conf, str + pos, len - pos - 1); // Copy remainder to new string and lop off the newline
  conf[len - pos - 1] = '\0'; // Null terminate

   // Check for start and end quotes
  if (conf[0] != '"' || conf[strlen(conf) - 1] != '"') {
    printf("Error reading configuration option, not enclosed in double quotes!\n");
    exit(1);
  }

  strncpy(str, conf + 1, strlen(conf) - 2); // Copy result back to original string without quotes
  str[strlen(conf) - 2] = '\0'; // Null terminate
}

int readnames(char *nick, char *username, char *realname) {
  FILE *fp;
  char str[MAXCHAR];
  char* filename = "blabouncer.conf";

  // Set all strings to zero-length to begin
  nick[0] = '\0';
  username[0] = '\0';
  realname[0] = '\0';

  fp = fopen(filename, "r");

  if (fp == NULL) {
    printf("error: could not open configuration file '%s'.\n", filename);
    exit(1);
  }

  while (fgets(str, MAXCHAR, fp) != NULL) {
    long int len;
    if ((len = isconf(str, "nick"))) {
      getconf(str, len);
      strncpy(nick, str, strlen(str));
      nick[strlen(str)] = '\0';
      printf("Nick is: '%s', length '%ld'.\n", nick, strlen(nick));
    } else if ((len = isconf(str, "username"))) {
      getconf(str, len);
      strncpy(username, str, strlen(str));
      username[strlen(str)] = '\0';
      printf("Username is: '%s', length '%ld'.\n", username, strlen(username));
    } else if ((len = isconf(str, "realname"))) {
      getconf(str, len);
      strncpy(realname, str, strlen(str));
      realname[strlen(str)] = '\0';
      printf("Real name is: '%s', length '%ld'.\n", realname, strlen(realname));
    }
  }

  // Ensure we have everything required
  if (!strlen(nick) || !strlen(username) || !strlen(realname)) {
    printf("Failed to read nick and/or username and/or real name from configuration file.\n");
    exit(1);
  }

  fclose(fp);
  return 1;
}

// Return the replayseconds configuration option
// (How many seconds of replay should be sent to connecting clients)
int confreplayseconds() {
  FILE *fp;
  char str[MAXCHAR];
  char* filename = "blabouncer.conf";

  char secondsstr[MAXCHAR];
  int seconds;

  fp = fopen(filename, "r");

  if (fp == NULL) {
    printf("error: could not open configuration file '%s'.\n", filename);
    exit(1);
  }

  while (fgets(str, MAXCHAR, fp) != NULL) {
    long int len;
    if ((len = isconf(str, "replayseconds"))) {
      getconf(str, len);
      strncpy(secondsstr, str, strlen(str));
      secondsstr[strlen(str)] = '\0';
      printf("secondsstr is: '%s', length '%ld'.\n", secondsstr, strlen(secondsstr));
      seconds = strtol(secondsstr, NULL, 10); // Convert resulting string to an integer, base 10
      printf("seconds is: '%d'.\n", seconds);
    }
  }

  fclose(fp);
  return seconds;
}

// Check the password provided in the string 'str' against what is in
// the config file.
// Return 0 for password mismatch, or 1 for password match.
int checkpassword(char *password) {
  FILE *fp;
  char str[MAXCHAR];
  char* filename = "blabouncer.conf";

  fp = fopen(filename, "r");

  if (fp == NULL) {
    printf("error: could not open configuration file '%s'.\n", filename);
    exit(1);
  }

  while (fgets(str, MAXCHAR, fp) != NULL) {
    long int len;
    if ((len = isconf(str, "password"))) {
      getconf(str, len);
      printf("confpassword is: '%s', length '%ld'.\n", str, strlen(str));
      // Ensure password are the same length
      if (strlen(str) != strlen(password)) {
        printf("Password length mismatch!\n");
        fclose(fp);
        return 0;
      }
      // Ensure passwords match
      if (strncmp(str, password, strlen(password)) == 0) {
        printf("confpassword matches password.\n");
        fclose(fp);
        return 1;
      } else {
        printf("confpassword does NOT match password!\n");
        fclose(fp);
        return 0;
      }
    }
  }

  fclose(fp);
  printf("No password read from configuration file, quitting.\n");
  exit(1);
}

int confclientport(char *clientport) {
  FILE *fp;
  char str[MAXCHAR];
  char* filename = "blabouncer.conf";

  // Set all strings to zero-length to begin
  clientport[0] = '\0';

  fp = fopen(filename, "r");

  if (fp == NULL) {
    printf("error: could not open configuration file '%s'.\n", filename);
    exit(1);
  }

  while (fgets(str, MAXCHAR, fp) != NULL) {
    long int len;
    if ((len = isconf(str, "clientport"))) {
      getconf(str, len);
      strncpy(clientport, str, strlen(str));
      clientport[strlen(str)] = '\0';
      printf("clientport is: '%s', length '%ld'.\n", clientport, strlen(clientport));
    }
  }

  // Ensure we have everything required
  if (!strlen(clientport)) {
    printf("Failed to read clientport from configuration file.\n");
    exit(1);
  }

  int portint = strtol(clientport, NULL, 10); // Convert resulting string to an integer, base 10
  // Ensure port is valid
  if (portint < 1 || portint > 65535) {
    printf("Invalid clientport value in configuration file.\n");
    exit(1);
  }

  fclose(fp);
  return 1;
}