Merge pull request #66 from BenceJanosSzabo/master
[deliverable/titan.core.git] / licensegen / renew_license.c
1 /******************************************************************************
2 * Copyright (c) 2000-2016 Ericsson Telecom AB
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * Balasko, Jeno
10 * Gecse, Roland
11 *
12 ******************************************************************************/
13 #include <stdio.h>
14 #include "cgic.h"
15 #include <string.h>
16 #include <stdlib.h>
17
18 #include "mysql.h"
19
20 #define CGI_INPUT_UNIQUE_ID "unique_id"
21 #define CGI_INPUT_DEFAULT_UNIQUE_ID 0
22 #define CGI_INPUT_LICENSEE_EMAIL "licensee_email"
23 #define CGI_INPUT_LICENSEE_EMAIL_MAX_LENGTH 40+1
24
25 #define MYSQL_HOST "mwlx122"
26 #define MYSQL_DB_NAME "ttcn3"
27 #define MYSQL_USERID "ttcn3"
28 #define MYSQL_PASSWD "ttcn3"
29
30 #define COMMAND_LINE_BUFFER_LENGTH 80
31
32 /*
33 The content of CGI_INPUT_UNIQUE_ID should be checked before submitting
34 the content to this program. Missing CGI_INPUT_UNIQUE_ID defaults to 0.
35
36 Do not forget to set mySQL database and user parameters below!
37
38 Requires cgic.h and libcgic.a for compiling (http://boutell.com/cgic/).
39
40 Requires mysql.h and libmysql.a for compiling.
41 */
42
43 int cgiMain() {
44 /* Send the content type, letting the browser know this is HTML */
45 cgiHeaderContentType("text/html");
46 /* Top of the page */
47 fprintf(cgiOut, "<HTML><HEAD>\n");
48 fprintf(cgiOut, "<TITLE>renew_license</TITLE></HEAD>\n");
49 fprintf(cgiOut, "<BODY><H1>renew_license</H1>\n");
50
51 int unique_id;
52 if(cgiFormInteger(CGI_INPUT_UNIQUE_ID, &unique_id,
53 CGI_INPUT_DEFAULT_UNIQUE_ID) != cgiFormSuccess) {
54 fprintf(cgiOut,
55 "<P>ERROR: Missing or invalid input field <code>%s</code>.",
56 CGI_INPUT_UNIQUE_ID);
57 goto the_end;
58 };
59
60 char licensee_email[CGI_INPUT_LICENSEE_EMAIL_MAX_LENGTH];
61 if(cgiFormStringNoNewlines(CGI_INPUT_LICENSEE_EMAIL, licensee_email,
62 CGI_INPUT_LICENSEE_EMAIL_MAX_LENGTH)
63 != cgiFormSuccess) {
64 fprintf(cgiOut,
65 "<P>ERROR: Missing or invalid input field <code>%s</code>.",
66 CGI_INPUT_LICENSEE_EMAIL);
67 goto the_end;
68 };
69
70 /* Connect to mySQL server */
71 MYSQL *mysql = mysql_init(NULL);
72 if(!mysql) {
73 fprintf(cgiOut,
74 "<P>ERROR: Could not initialize mySQL handle.");
75 goto the_end;
76 }
77 if(!(mysql_real_connect(mysql, MYSQL_HOST, MYSQL_USERID, MYSQL_PASSWD,
78 MYSQL_DB_NAME, 0, NULL, CLIENT_ODBC))) {
79 fprintf(cgiOut,
80 "<P>ERROR: Could not connect to database.");
81 goto the_end;
82 }
83 /* Execute query */
84 char query[1024];
85 snprintf(query, 1024,
86 "SELECT * FROM licenses WHERE unique_id=%d AND "
87 "licensee_email='%s' AND "
88 "valid_until < DATE_ADD(CURDATE(), INTERVAL 1 MONTH);\0",
89 unique_id, licensee_email);
90 if(mysql_query(mysql, query)) {
91 fprintf(cgiOut,
92 "<P>ERROR: Problem executing query.");
93 goto before_the_end;
94 };
95 MYSQL_RES *result;
96 result = mysql_store_result(mysql);
97 if(result) {
98 /* mysql_num_rows() returns my_ulonglong==unsigned long */
99 my_ulonglong num_records = mysql_num_rows(result);
100 if(num_records == 0) {
101 fprintf(cgiOut,
102 "<P>ERROR: Did not find license with "
103 "number <code>%d</code>, "
104 "e-mail <code>%s</code>, expiring within 1 month. "
105 "Re-check data validity and try again.",
106 unique_id,
107 licensee_email);
108 goto before_the_end;
109 }
110 MYSQL_ROW row = mysql_fetch_row(result);
111 fprintf(cgiOut, "Debug info (remove it from final build):<br>");
112 fprintf(cgiOut, "<p>unique_id: <code>%s</code>", row[0]);
113 fprintf(cgiOut, "<p>licensee_email: <code>%s</code>", row[2]);
114 fprintf(cgiOut, "<p>valid_until: <code>%s</code>", row[7]);
115 fprintf(cgiOut, "<p>notes: <code>%s</code>",
116 row[33] ? row[33] : "NULL");
117 /* Update mySQL database with new expiry date and add note */
118 snprintf(query, 1024,
119 "UPDATE licenses SET "
120 "valid_until=DATE_ADD(NOW(), INTERVAL 1 YEAR), "
121 "notes=CONCAT(COALESCE(notes, ''), '\nProlonged on ', CURDATE(), '.') "
122 "WHERE unique_id=%d;", unique_id);
123 fprintf(cgiOut,
124 "<P>Should execute the followin query now "
125 "(uncomment next line in source to do so): <BR>"
126 "<code>%s</code>\n", query);
127 /* if(mysql_query(mysql, query)) {
128 fprintf(cgiOut, "<P>ERROR: Problem executing query.");
129 goto before_the_end;
130 } */
131 /* Generate and email license */
132 char command_line[COMMAND_LINE_BUFFER_LENGTH];
133 snprintf(command_line, COMMAND_LINE_BUFFER_LENGTH,
134 "license_gen -m %d", unique_id);
135 if(!(system(command_line))) {
136 fprintf(cgiOut,
137 "<P>Your new license key (%s) was generated and "
138 "e-mailed to your filed e-mail address (%s).",
139 row[0],
140 row[2]);
141 } else {
142 fprintf(cgiOut, "<P>ERROR: Could not launch subshell." );
143 }
144 mysql_free_result(result);
145 } else {
146 fprintf(cgiOut,
147 "<P>ERROR: Problem fetching results from database.");
148 goto before_the_end;
149 }
150
151 /* Terminate mysql session */
152 before_the_end:
153 mysql_close(mysql);
154
155 /* Finish up the page */
156 the_end:
157 fprintf(cgiOut, "</BODY></HTML>\n");
158 return 0;
159 }
160
161
162
This page took 0.056905 seconds and 5 git commands to generate.