Commit cce42c68822f605ff7297c136312af50bfd5fdad
re-indent
Yves Rutschle committed on 4/18/2017, 6:53:19 PMParent: b0f4e24ce016310b8cf7f4995632f5e282fa63ee
Files changed
systemd-sslh-generator.c | changed |
systemd-sslh-generator.c | ||
---|---|---|
@@ -4,11 +4,10 @@ | ||
4 | 4 | |
5 | 5 | |
6 | 6 | |
7 | 7 | static char* resolve_listen(const char *hostname, const char *port) { |
8 | - | |
9 | -/* Need room in the strcat for \0 and : | |
10 | - * the format in the socket unit file is hostname:port */ | |
8 | + /* Need room in the strcat for \0 and : | |
9 | + * the format in the socket unit file is hostname:port */ | |
11 | 10 | char *conn = (char*)malloc(strlen(hostname)+strlen(port)+2); |
12 | 11 | strcpy(conn, hostname); |
13 | 12 | strcat(conn, ":"); |
14 | 13 | strcat(conn, port); |
@@ -18,136 +17,130 @@ | ||
18 | 17 | } |
19 | 18 | |
20 | 19 | |
21 | 20 | static int get_listen_from_conf(const char *filename, char **listen) { |
21 | + config_t config; | |
22 | + config_setting_t *setting, *addr; | |
23 | + const char *hostname, *port; | |
24 | + int len = 0; | |
22 | 25 | |
23 | - config_t config; | |
24 | - config_setting_t *setting, *addr; | |
25 | - const char *hostname, *port; | |
26 | - int len = 0; | |
27 | - | |
28 | -/* look up the listen stanzas in the config file so these | |
29 | - * can be used in the socket file generated */ | |
30 | - | |
31 | - config_init(&config); | |
32 | - if (config_read_file(&config, filename) == CONFIG_FALSE) { | |
33 | - /* we don't care if file is missing, skip it */ | |
34 | - if (config_error_line(&config) != 0) { | |
35 | - fprintf(stderr, "%s:%d:%s\n", | |
36 | - filename, | |
37 | - config_error_line(&config), | |
38 | - config_error_text(&config)); | |
39 | - return -1; | |
40 | - } | |
41 | - } else { | |
42 | - setting = config_lookup(&config, "listen"); | |
43 | - if (setting) { | |
44 | - len = config_setting_length(setting); | |
45 | - for (int i = 0; i < len; i++) { | |
46 | - addr = config_setting_get_elem(setting, i); | |
47 | - if (! (config_setting_lookup_string(addr, "host", &hostname) && | |
48 | - config_setting_lookup_string(addr, "port", &port))) { | |
49 | - fprintf(stderr, | |
50 | - "line %d:Incomplete specification (hostname and port required)\n", | |
51 | - config_setting_source_line(addr)); | |
26 | + /* look up the listen stanzas in the config file so these | |
27 | + * can be used in the socket file generated */ | |
28 | + config_init(&config); | |
29 | + if (config_read_file(&config, filename) == CONFIG_FALSE) { | |
30 | + /* we don't care if file is missing, skip it */ | |
31 | + if (config_error_line(&config) != 0) { | |
32 | + fprintf(stderr, "%s:%d:%s\n", | |
33 | + filename, | |
34 | + config_error_line(&config), | |
35 | + config_error_text(&config)); | |
52 | 36 | return -1; |
53 | - } else { | |
54 | - | |
55 | - listen[i] = malloc(strlen(resolve_listen(hostname, port))); | |
56 | - strcpy(listen[i], resolve_listen(hostname, port)); | |
57 | 37 | } |
58 | - } | |
38 | + } else { | |
39 | + setting = config_lookup(&config, "listen"); | |
40 | + if (setting) { | |
41 | + len = config_setting_length(setting); | |
42 | + for (int i = 0; i < len; i++) { | |
43 | + addr = config_setting_get_elem(setting, i); | |
44 | + if (! (config_setting_lookup_string(addr, "host", &hostname) && | |
45 | + config_setting_lookup_string(addr, "port", &port))) { | |
46 | + fprintf(stderr, | |
47 | + "line %d:Incomplete specification (hostname and port required)\n", | |
48 | + config_setting_source_line(addr)); | |
49 | + return -1; | |
50 | + } else { | |
51 | + listen[i] = malloc(strlen(resolve_listen(hostname, port))); | |
52 | + strcpy(listen[i], resolve_listen(hostname, port)); | |
53 | + } | |
54 | + } | |
55 | + } | |
59 | 56 | } |
60 | - } | |
61 | 57 | |
62 | - return len; | |
58 | + return len; | |
63 | 59 | |
64 | 60 | } |
65 | 61 | |
66 | 62 | static int write_socket_unit(FILE *socket, char **listen, int num_addr, const char *source) { |
67 | 63 | |
68 | - fprintf(socket, | |
69 | - "# Automatically generated by systemd-sslh-generator\n\n" | |
70 | - "[Unit]\n" | |
71 | - "Before=sslh.service\n" | |
72 | - "SourcePath=%s\n" | |
73 | - "Documentation=man:sslh(8) man:systemd-sslh-generator(8)\n\n" | |
74 | - "[Socket]\n" | |
75 | - "FreeBind=true\n", | |
76 | - source); | |
64 | + fprintf(socket, | |
65 | + "# Automatically generated by systemd-sslh-generator\n\n" | |
66 | + "[Unit]\n" | |
67 | + "Before=sslh.service\n" | |
68 | + "SourcePath=%s\n" | |
69 | + "Documentation=man:sslh(8) man:systemd-sslh-generator(8)\n\n" | |
70 | + "[Socket]\n" | |
71 | + "FreeBind=true\n", | |
72 | + source); | |
77 | 73 | |
78 | - for (int i = 0; i < num_addr; i++) { | |
79 | - fprintf(socket, "ListenStream=%s\n", listen[i]); | |
80 | - } | |
74 | + for (int i = 0; i < num_addr; i++) { | |
75 | + fprintf(socket, "ListenStream=%s\n", listen[i]); | |
76 | + } | |
81 | 77 | |
82 | -return 0; | |
78 | + return 0; | |
83 | 79 | } |
84 | 80 | |
85 | 81 | static int gen_sslh_config(char *runtime_unit_dir) { |
82 | + char *sslh_conf; | |
83 | + int num_addr; | |
84 | + FILE *config; | |
85 | + char **listen; | |
86 | + FILE *runtime_conf_fd = stdout; | |
87 | + const char *unit_file; | |
86 | 88 | |
87 | - char *sslh_conf; | |
88 | - int num_addr; | |
89 | - FILE *config; | |
90 | - char **listen; | |
91 | - FILE *runtime_conf_fd = stdout; | |
92 | - const char *unit_file; | |
89 | + /* There are two default locations so check both with first given preference */ | |
90 | + sslh_conf = "/etc/sslh.cfg"; | |
93 | 91 | |
94 | -/* There are two default locations so check both with first given preference */ | |
95 | - sslh_conf = "/etc/sslh.cfg"; | |
96 | - | |
97 | - config = fopen(sslh_conf, "r"); | |
98 | - if (config == NULL) { | |
99 | - sslh_conf="/etc/sslh/sslh.cfg"; | |
100 | - config = fopen(sslh_conf, "r"); | |
101 | - if (config == NULL) { | |
102 | - return -1; | |
92 | + config = fopen(sslh_conf, "r"); | |
93 | + if (config == NULL) { | |
94 | + sslh_conf="/etc/sslh/sslh.cfg"; | |
95 | + config = fopen(sslh_conf, "r"); | |
96 | + if (config == NULL) { | |
97 | + return -1; | |
98 | + } | |
103 | 99 | } |
104 | - } | |
105 | 100 | |
106 | - fclose(config); | |
101 | + fclose(config); | |
107 | 102 | |
108 | 103 | |
109 | - num_addr = get_listen_from_conf(sslh_conf, listen); | |
110 | - if (num_addr < 0) | |
111 | - return -1; | |
104 | + num_addr = get_listen_from_conf(sslh_conf, listen); | |
105 | + if (num_addr < 0) | |
106 | + return -1; | |
112 | 107 | |
113 | -/* If this is run by systemd directly write to the location told to | |
114 | - * otherwise write to standard out so that it's trivial to check what | |
115 | - * will be written */ | |
116 | - if (runtime_unit_dir != "") { | |
117 | - unit_file = "/sslh.socket"; | |
118 | - size_t uf_len = strlen(unit_file); | |
119 | - size_t runtime_len = strlen(runtime_unit_dir) + uf_len + 1; | |
120 | - char *runtime_conf = malloc(runtime_len); | |
121 | - strcpy(runtime_conf, runtime_unit_dir); | |
122 | - strcat(runtime_conf, unit_file); | |
123 | - runtime_conf_fd = fopen(runtime_conf, "w"); | |
124 | - } | |
108 | + /* If this is run by systemd directly write to the location told to | |
109 | + * otherwise write to standard out so that it's trivial to check what | |
110 | + * will be written */ | |
111 | + if (runtime_unit_dir != "") { | |
112 | + unit_file = "/sslh.socket"; | |
113 | + size_t uf_len = strlen(unit_file); | |
114 | + size_t runtime_len = strlen(runtime_unit_dir) + uf_len + 1; | |
115 | + char *runtime_conf = malloc(runtime_len); | |
116 | + strcpy(runtime_conf, runtime_unit_dir); | |
117 | + strcat(runtime_conf, unit_file); | |
118 | + runtime_conf_fd = fopen(runtime_conf, "w"); | |
119 | + } | |
125 | 120 | |
126 | 121 | |
127 | - return write_socket_unit(runtime_conf_fd, listen, num_addr, sslh_conf); | |
128 | - | |
122 | + return write_socket_unit(runtime_conf_fd, listen, num_addr, sslh_conf); | |
129 | 123 | } |
130 | 124 | |
125 | + | |
131 | 126 | int main(int argc, char *argv[]){ |
127 | + int r = 0; | |
128 | + int k; | |
129 | + char *runtime_unit_dest = ""; | |
132 | 130 | |
133 | - int r = 0; | |
134 | - int k; | |
135 | - char *runtime_unit_dest = ""; | |
131 | + if (argc > 1 && (argc != 4) ) { | |
132 | + printf("This program takes three or no arguments.\n"); | |
133 | + return -1; | |
134 | + } | |
136 | 135 | |
137 | - if (argc > 1 && (argc != 4) ) { | |
138 | - printf("This program takes three or no arguments.\n"); | |
139 | - return -1; | |
140 | - } | |
136 | + if (argc > 1) | |
137 | + runtime_unit_dest = argv[1]; | |
141 | 138 | |
142 | - if (argc > 1) | |
143 | - runtime_unit_dest = argv[1]; | |
139 | + k = gen_sslh_config(runtime_unit_dest); | |
140 | + if (k < 0) | |
141 | + r = k; | |
144 | 142 | |
145 | - k = gen_sslh_config(runtime_unit_dest); | |
146 | - if (k < 0) | |
147 | - r = k; | |
148 | - | |
149 | - return r < 0 ? -1 : 0; | |
150 | - | |
143 | + return r < 0 ? -1 : 0; | |
151 | 144 | } |
152 | 145 | |
153 | 146 |
Built with git-ssb-web