summaryrefslogtreecommitdiff
path: root/config.c
blob: 12237b37d0579f3b87d91dce17da7dacaf35e981 (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
228
229
230
231
232
233
#include "config.h"

// Sets 'dest' to the value of the configuration option with name
// 'confname' from configuration file 'filename'.
// Returns 1 for success or 0 for error/failure.
int getconfstr(char *confname, char *filename, char* dest) {
  FILE *fp;
  char *ret;
  char str[MAXCHAR];
  int found = 0; // Have we found the configuration option?

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

  // Length of requested configuration option name
  long int namelen = strlen(confname);

  fp = fopen(filename, "r");

  if (fp == NULL) {
    printf("error: could not open configuration file '%s'.\n", filename);
    exit(1);
  }
  // Loop through the whole file, looking for the requested configuration option
  while (fgets(str, MAXCHAR, fp) != NULL) {
    char substr[MAXCHAR];

    // Check if the next character after the length of the requested option
    // name is an equals sign, a space, or a tab
    if (str[namelen] != '=' && str[namelen] != ' ' && str[namelen] != '\t') {
      // If it isn't this can't have been our option
      continue;
    }

    // Copy the number of characters that the requested option name is long
    // to a temporary string
    strncpy(substr, str, namelen);
    substr[namelen] = '\0';

    // If the resulting temporary string contains the requested option name,
    // we have found our configuration option and it is in the current 'str'
    if (!(ret = strstr(substr, confname)) == 0) {
      found = 1;
      break;
    }
  }

  // If we got here, then either we found the configuration option or we ran out of file

  if (!found) {
    printf("Error reading configuration option '%s', did not find it in configuration file '%s'.\n", confname, filename);
    fclose(fp);
    return 0;
  }

  long int pos;
  char conf[MAXCHAR]; // Temporary string to build configuration value in

  // Starting from the end of the option name, find the position of the start of the configuration value
  // (including its double quotes) by skipping over everything that isn't an equals sign, a space, or a tab
  for (size_t i = namelen; i < strlen(str); i++) {
    if (str[i] == '=' || str[i] == ' ' || str[i] == '\t') {
      continue;
    } else {
      // Record current/final position in string
      pos = i;
      break;
    }
  }

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

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

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

  printf("getconfstr(): returning '%s'.\n", dest);

  // Close fine and return success
  fclose(fp);
  return 1;
}

// Returns the value of the configuration option with name
// 'confname' from configuration file 'filename'.
int getconfint(char *confname, char *filename) {
  char result[MAXCHAR];
  if (!getconfstr(confname, filename, result)) {
    printf("getconfint(): error getting configuration option '%s' from configuration file '%s'.\n", confname, filename);
    // TODO - Do something useful here instead of exiting
    exit(1);
  }

  return strtol(result, NULL, 10); // Convert resulting string to an integer, base 10
}

// Check the password provided in the string 'str' against what is in
// the configuration file 'filename'.
// Return 0 for password mismatch, or 1 for password match.
int checkpassword(char *password, char *filename) {
  char confpassword[MAXCHAR];

  if (!getconfstr("password", filename, confpassword)) {
    printf("checkpassword(): error getting configuration option 'password' from configuration file '%s'.\n", filename);
    return 0;
  }

  // Ensure passwords are the same length
  if (strlen(password) != strlen(confpassword)) {
    printf("Password length mismatch!\n");
    return 0;
  }
  // Ensure passwords match
  if (strncmp(password, confpassword, strlen(password)) == 0) {
    printf("confpassword matches password.\n");
    return 1;
  } else {
    printf("confpassword does NOT match password!\n");
    return 0;
  }

  printf("checkpassword(): unexpectedly got to end of function, quitting.\n");
  exit(1);
}

// Create the default configuration file.
// Return 1 on success, 0 on failure.
int createconfigfile(char *filename) {
  char *dirtmp;
  char *dir;
  dirtmp = strdup(filename);
  dir = strdup(dirname(dirtmp));

  // Make sure the parent directory exists
  struct stat st = {0};
  if (stat(dir, &st) == -1) {
    if (mkdir(dir, 0700)) {
      printf("Error creating config directory '%s'.\n", dir);
      exit(1);
    } else {
      printf("Created config directory '%s'.\n", dir);
    }
  }

  FILE *fp;

  fp = fopen(filename, "a");

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

  // Prepare the string
  char *string =
  "# blabouncer configuration file\n"
  "#\n"
  "# Entries must be in the form:\n"
  "# option name, space, equals sign, space, double quote, option value, double quote\n"
  "# e.g.\n"
  "# realname = \"Mr Bla Bouncer\"\n"
  "#\n"
  "# Shell expansion is not supported, so do not try and specify e.g.\n"
  "# \"~/.blabouncer/\" or \"%HOME/.blabouncer/\", instead use \"/home/foo/.blabouncer\"\n"
  "\n"
  "nick = \"blabounce\"\n"
  "nick2 = \"bbounce2\"\n"
  "nick3 = \"bbounce3\"\n"
  "username = \"bounceusr\"\n"
  "realname = \"Mr Bla Bouncer\"\n"
  "\n"
  "# Channels to automatically join (comma-separated list, defaults to none)\n"
  "#channels = \"#blabouncer,#test\"\n"
  "\n"
  "# How many seconds of replay log should be sent to connecting clients\n"
  "replayseconds = \"600\"\n"
  "\n"
  "# Connect password clients must provided to connect\n"
  "password = \"bananas\"\n"
  "\n"
  "# Port the bouncer should listen on\n"
  "clientport = \"1234\"\n"
  "\n"
  "# Enable TLS for clients connecting to the bouncer (\"1\" for yes or \"0\" for no)\n"
  "# If \"0\" then certfile and keyfile need not be set\n"
  "clienttls = \"1\"\n"
  "\n"
  "# Enable TLS for the bouncer connecting to the IRC server (\"1\" for yes or \"0\" for no)\n"
  "servertls = \"1\"\n"
  "\n"
  "# Real IRC server the bouncer connects to\n"
  "ircserver = \"irc.blatech.net\"\n"
  "\n"
  "# Real IRC server port\n"
  "ircserverport = \"6697\"\n"
  "\n"
  "# Certificate file (defaults to $HOME/.blabouncer/cert.pem)\n"
  "# If clienttls = \"0\" then this need not be set\n"
  "#certfile = \"/home/foo/.blabouncer/cert.pem\"\n"
  "\n"
  "# Certificate key file (defaults to $HOME/.blabouncer/key.pem)\n"
  "# If clienttls = \"0\" then this need not be set\n"
  "#keyfile = \"/home/foo/.blabouncer/key.pem\"\n"
  "\n"
  "# Base directory (defaults to $HOME/.blabouncer/)\n"
  "# Things such as the logs directory will be placed below this\n"
  "#basedir = \"/home/foo/.blabouncer/\"\n"
  "\n"
  "# Enable logging (\"1\" for yes or \"0\" for no)\n"
  "# Logs go to basedir/logs/ with one file per channel/nick\n"
  "logging = \"1\"\n"
  "\n"
  "# Enable replay logging (\"1\" for yes or \"0\" for no)\n"
  "# Replay log goes to basedir/replay.log\n"
  "replaylogging = \"1\"\n";

  // Write complete string to file
  if ((fprintf(fp, string)) < 0) {
    printf("error: could not write to replay log file.\n");
    exit(1);
  }

  fclose(fp);
  free(dirtmp);
  free(dir);
  return 0;
}