1 /* cvm/sql-query.c - SQL query parsing and insertion framework.
2 * Copyright (C) 2010 Bruce Guenter <bruce@untroubled.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18 #include <ctype.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <bglibs/str.h>
22 #include "module.h"
23 #include "sql.h"
24
25 #define QUOTE '\''
26 #define BACKSLASH '\\'
27
str_catb_quoted(str * s,const char * ptr,unsigned long len)28 static int str_catb_quoted(str* s, const char* ptr, unsigned long len)
29 {
30 if (!str_catc(s, QUOTE)) return 0;
31 for (; len > 0; ++ptr, --len) {
32 switch (*ptr) {
33 case 0: if (!str_cats(s, "\\0")) return 0; continue;
34 case QUOTE: if (!str_catc(s, QUOTE)) return 0; break;
35 case BACKSLASH: if (!str_catc(s, BACKSLASH)) return 0; break;
36 }
37 str_catc(s, *ptr);
38 }
39 return str_catc(s, QUOTE);
40 }
41
sql_query_validate(const char * template)42 int sql_query_validate(const char* template)
43 {
44 while ((template = strchr(template, '$')) != 0) {
45 ++template;
46 switch (*template) {
47 case '$':
48 ++template;
49 break;
50 case '{':
51 ++template;
52 if ((template = strchr(template, '}')) == 0) return 0;
53 ++template;
54 default:
55 while (isalnum(*template) || *template == '_') ++template;
56 }
57 }
58 return 1;
59 }
60
sql_query_build(const char * template,str * q)61 int sql_query_build(const char* template, str* q)
62 {
63 static str name;
64 const char* ptr;
65 if (!str_truncate(q, 0)) return 0;
66 while ((ptr = strchr(template, '$')) != 0) {
67 if (!str_catb(q, template, ptr - template)) return 0;
68 template = ptr + 1;
69 switch (*template) {
70 case '$':
71 ++template;
72 if (!str_truncate(&name, 0)) return 0;
73 break;
74 case '{':
75 ++template;
76 if ((ptr = strchr(template, '}')) == 0) return 0;
77 if (!str_copyb(&name, template, ptr-template)) return 0;
78 template = ptr + 1;
79 break;
80 default:
81 if (!str_truncate(&name, 0)) return 0;
82 while (isalnum(*template) || *template == '_')
83 if (!str_catc(&name, *template++)) return 0;
84 }
85 if (name.len == 0) {
86 if (!str_catc(q, '$')) return 0;
87 }
88 else {
89 if (str_diffs(&name, "account") == 0) {
90 if (!str_catb_quoted(q,
91 cvm_module_credentials[CVM_CRED_ACCOUNT].s,
92 cvm_module_credentials[CVM_CRED_ACCOUNT].len))
93 return 0;
94 }
95 else if (str_diffs(&name, "domain") == 0) {
96 if (!str_catb_quoted(q,
97 cvm_module_credentials[CVM_CRED_DOMAIN].s,
98 cvm_module_credentials[CVM_CRED_DOMAIN].len))
99 return 0;
100 }
101 else {
102 ptr = getenv(name.s);
103 if (ptr != 0)
104 if (!str_catb_quoted(q, ptr, strlen(ptr)))
105 return 0;
106 }
107 }
108 }
109 if (!str_cats(q, template)) return 0;
110 return 1;
111 }
112