-
Notifications
You must be signed in to change notification settings - Fork 18
/
sftpconf.c
118 lines (113 loc) · 3.39 KB
/
sftpconf.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
110
111
112
113
114
115
116
117
118
/*
* This file is part of the Green End SFTP Server.
* Copyright (C) Richard Kettlewell
*
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
*/
#include "sftpserver.h"
#include "sftpconf.h"
#include "utils.h"
#include <stdio.h>
#include <errno.h>
#include <ctype.h>
#include <stdlib.h>
int sftpconf_nthreads = NTHREADS;
int sftpconf_reorder = 1;
static size_t sftpconf_split(char *line, char **bufferp, char **words,
size_t maxwords) {
size_t nwords = 0;
char *buffer = sftp_xmalloc(strlen(line) + 1), *bufptr = buffer;
// Keep going until we hit the end of the line, or start of a comment
while(*line && *line != '#') {
char *word;
if(isspace((unsigned char)*line)) {
++line;
continue;
}
if(*line == '"' || *line == '\'') {
// Quoted word
int quote = *line++; // Step over quote
word = bufptr;
while(*line && *line != quote) {
if(*line == '\\') // Backslash escapes anything
line++;
*bufptr++ = *line++;
}
if(*line) // Step over final quote if present
line++;
*bufptr++ = 0;
} else {
// Unquoted word
word = bufptr;
while(*line && !isspace((unsigned char)*line))
*bufptr++ = *line++;
*bufptr++ = 0;
}
if(nwords >= maxwords) {
free(buffer);
*bufferp = NULL;
return -1;
}
words[nwords++] = word;
}
if(!nwords) {
free(buffer);
buffer = NULL;
}
*bufferp = buffer;
return nwords;
}
void sftpconf_read(const char *path) {
FILE *fp = NULL;
char line[1024];
int lineno = 0;
char *words[8];
if(!(fp = fopen(path, "r"))) {
if(errno == ENOENT)
return;
sftp_fatal("%s: %s", path, strerror(errno));
}
while(fgets(line, sizeof line, fp)) {
char *buffer = NULL;
lineno++;
if(!strchr(line, '\n'))
sftp_fatal("%s:%d: line too long", path, lineno);
size_t nwords =
sftpconf_split(line, &buffer, words, sizeof words / sizeof *words);
if(nwords == (size_t)-1)
sftp_fatal("%s:%d: too many parts to line", path, lineno);
if(nwords == 0)
continue;
if(!strcmp(words[0], "threads")) {
if(nwords != 2)
sftp_fatal("%s:%d: invalid threads directive", path, lineno);
sftpconf_nthreads = atoi(words[1]);
} else if(!strcmp(words[0], "reorder")) {
if(nwords != 2)
sftp_fatal("%s:%d: invalid reorder directive", path, lineno);
if(!strcmp(words[1], "true"))
sftpconf_reorder = 1;
else if(!strcmp(words[1], "false"))
sftpconf_reorder = 0;
else
sftp_fatal("%s:%d: invalid reorder directive", path, lineno);
} else {
sftp_fatal("%s:%d: unrecognized directive: %s", path, lineno, words[0]);
}
free(buffer);
}
fclose(fp);
}