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