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
|
/*
* (C) 2013-2020 by Christian Hesse <mail@eworm.de>
*
* 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 3 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, see <https://www.gnu.org/licenses/>.
*
*/
#include "config.h"
#include "version.h"
#include "cqrlogo.h"
/*** main ***/
int main(int argc, char **argv) {
const char * http_referer, * server_name, * uri;
char * uri_server_name, * pattern, * stolen;
regex_t preg;
regmatch_t pmatch[1];
uint8_t https;
struct timeval tv;
struct cqr_png * png;
struct cqr_bitmap * bitmap;
struct cqr_conf conf;
#if HAVE_FCGI
/* loop for requests */
while (FCGI_Accept() >= 0)
#endif
{
/* do the variable initialization within the loop! */
uri_server_name = NULL;
pattern = NULL;
stolen = NULL;
https = 0;
/* these default values are defined in config.h */
conf.scale = QRCODE_SCALE;
conf.border = QRCODE_BORDER;
conf.level = QRCODE_LEVEL;
conf.overwrite = ALLOW_OVERWRITE;
/* check if we have environment variables from CGI */
if ((server_name = getenv("SERVER_NAME")) == NULL) {
fprintf(stderr, "This is a CGI executable. Running without a web service is not supported.\n"
"Note that SERVER_NAME needs to be defined, for full features the client has\n"
"to send referer information.\n");
return EXIT_FAILURE;
}
/* check if we have https connection */
if (getenv("HTTPS") != NULL)
https = 1;
/* assemble uri for use when referer is missing or fails */
uri_server_name = malloc(10 + strlen(server_name));
sprintf(uri_server_name, "http%s://%s/", https ? "s" : "", server_name);
/* get http referer */
if ((http_referer = getenv("HTTP_REFERER")) != NULL) {
uri = http_referer;
/* prepare pattern matching */
pattern = malloc(sizeof(URLPATTERN) + strlen(server_name));
sprintf(pattern, URLPATTERN, server_name);
if (regcomp(&preg, pattern, 0) != 0) {
fprintf(stderr, "regcomp() failed, returning nonzero.\n");
return EXIT_FAILURE;
}
/* check if the QR-Code is for the correct server */
if (regexec(&preg, http_referer, 1, pmatch, 0) != 0) {
stolen = malloc(sizeof(TEXTSTOLEN) + strlen(server_name));
sprintf(stolen, TEXTSTOLEN, https ? "s" : "", server_name);
uri = stolen;
}
regfree(&preg);
free(pattern);
} else {
/* use uri assembled from server name */
uri = uri_server_name;
}
cqr_conf_file(server_name, &conf);
cqr_conf_string(getenv("QUERY_STRING"), &conf);
/* encode the QR-Code */
if ((bitmap = cqr_encode_qrcode_to_bitmap(uri, conf)) == NULL) {
/* uri too long? retry with uri from server name */
uri = uri_server_name;
if ((bitmap = cqr_encode_qrcode_to_bitmap(uri, conf)) == NULL) {
fprintf(stderr, "Could not generate QR-Code.\n");
return EXIT_FAILURE;
}
}
/* generate PNG data */
if ((png = cqr_bitmap_to_png(bitmap, uri, CQR_META_ALL)) == NULL) {
fprintf(stderr, "Failed to generate PNG.\n");
return EXIT_FAILURE;
}
/* print HTTP header */
fputs(CQR_HEADER_CONTENT_TYPE, stdout);
fputs(CQR_HEADER_CONTENT_DISPOSITION, stdout);
fputs(CQR_HEADER_CACHE_CONTROL, stdout);
fputs(CQR_HEADER_PRAGMA, stdout);
gettimeofday(&tv, NULL);
fprintf(stdout, "ETag: %lu%lu\n", tv.tv_sec, tv.tv_usec);
fputc('\n', stdout);
/* write PNG data to stdout */
if (fwrite(png->buffer, png->size, 1, stdout) != 1) {
fprintf(stderr, "Failed writing PNG data to stdout.\n");
return EXIT_FAILURE;
}
/* free memory we no longer need */
if (uri_server_name)
free(uri_server_name);
if (stolen)
free(stolen);
cqr_bitmap_free(bitmap);
if (png->size > 0)
free(png->buffer);
free(png);
}
return EXIT_SUCCESS;
}
// vim: set syntax=c:
|