1 /**************************************************
2 OpengateM - MAC address authentication system
4 Module for sending warning mail to users at the expiration date.
5 This program searchs devices that are expired soon, and send out
6 warning mail to the registered mail address of their owners.
8 'conf/warningmail.sample' is the sample template file of mail.
9 Copy it to 'conf/warningmail' and modify to match your site.
10 Leave variables '%%DEVICENAME%%' and '%%LIMITDATE%%' in the file
11 untouched. These are replaced properly by this program.
13 Copyright (C) 2011 Opengate Project Team
14 Written by Yoshiaki Watanabe
16 This program is free software; you can redistribute it and/or
17 modify it under the terms of the GNU General Public License
18 as published by the Free Software Foundation; either version 2
19 of the License, or (at your option) any later version.
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
26 You should have received a copy of the GNU General Public License
27 along with this program; if not, write to the Free Software
28 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30 Email: watanaby@is.saga-u.ac.jp
31 **************************************************/
33 #include "opengatemmng.h"
35 /**********************************/
36 /* main routine run as cron */
37 /**********************************/
38 int main(int argc, char **argv)
40 char mailAddress[BUFFMAXLN]=""; /* mail address */
41 char limitDate[WORDMAXLN]=""; /* limit(expiration) date */
42 char device[WORDMAXLN]=""; /* device(terminal) name */
43 char deviceList[BUFFMAXLN]=""; /* device name list (concat of device) */
44 char mailAddressPrev[BUFFMAXLN]=""; /* mail address of previous record */
45 char limitDatePrev[WORDMAXLN]=""; /* limit date of previous record */
46 int count=0; /* count of sending mails */
47 char* progName; /* the name of this program in argv[0] */
49 /* drop root privilege */
52 /* if this is executed in shell with '-v' option, show makedir */
54 if(strcmp(argv[1],"-v")==0){
55 printf("makedir: %s\n", MAKEDIR);
57 printf("This is the program used with cron\n");
58 printf("To show version, run this on console with '-v' option\n");
63 /* save program load path */
64 saveLoadPath(argv[0]);
65 progName = getProgramName();
67 /* prepare config file */
68 if(OpenConfFile()==-1){
69 PutMessageToClient("Check config file by running this cgi on console");
74 errToSyslog(atoi(GetConfValue("Syslog/Enable")));
75 openlog(progName, LOG_PID, atoi(GetConfValue("Syslog/Facility")));
77 /* initialize config */
79 if(!InitMngDb()) return 0;
81 /* get first mail address near expiration date */
82 /* use same funciiton for first/next call (branch in the function) */
83 if(GetNextMailAddressFromMngDb(mailAddress, limitDate, device)){
85 /*** following code summarizes the information of devices having */
86 /* same mailAddress and limitDate to one mail */
88 /* if get, save the first record */
89 strncpy(mailAddressPrev, mailAddress, BUFFMAXLN);
90 strncpy(limitDatePrev, limitDate, WORDMAXLN);
91 strncpy(deviceList, device, WORDMAXLN);
93 /* get next mail address near expiration date */
94 while(GetNextMailAddressFromMngDb(mailAddress, limitDate, device)){
96 /* if mailAddress or limitDate are not same as previous */
97 if( strcmp(mailAddress, mailAddressPrev)!=0
98 || strcmp(limitDate, limitDatePrev)!=0 ){
100 /* send mail to the address and clear deviceList */
101 if(!isNull(mailAddressPrev)){
102 if(SendMail(mailAddressPrev, limitDatePrev, deviceList)) count++;
107 /* save the mail Address and limit date */
108 strncpy(mailAddressPrev, mailAddress, BUFFMAXLN);
109 strncpy(limitDatePrev, limitDate, WORDMAXLN);
111 /* add the device name to the deviceList
112 if list is too long, it is terminated by ", ......" */
113 if(isNull(deviceList)){
114 strncpy(deviceList, device, WORDMAXLN);
116 else if(strstr(deviceList, ", ......")!=NULL) ;
117 else if( strlen(deviceList) > BUFFMAXLN*0.8 ){
118 strncat(deviceList, ", ......", BUFFMAXLN);
121 strncat(deviceList, ", ", BUFFMAXLN);
122 strncat(deviceList, device, BUFFMAXLN);
126 /* post processing of residue at the end of get loop */
127 if( !isNull(mailAddressPrev) ){
128 if(SendMail(mailAddressPrev, limitDatePrev, deviceList)) count++;
135 /* report to syslog */
136 if(debug>0) err_msg("INFO: Sended %d mails",count);
141 /**************************************************/
143 /**************************************************/
144 int sendMail(char* mailAddress, char* limitDate, char* device)
148 char mailCmd[BUFFMAXLN];
149 char buff[BUFFMAXLN];
153 /* set mail command path (/bin/rmail) */
154 mailCmdPath=GetConfValue("Mail/CmdPath");
155 if(isNull(mailCmdPath)){
156 err_msg("ERR at %s#%d: cannot get Mail/CmdPath from conf",__FILE__,__LINE__);
160 /* if mail command is not exists, return */
161 if(stat(mailCmdPath, &st)!=0){
162 err_msg("ERR at %s#%d: mail command[%s] is not found",__FILE__,__LINE__,mailCmdPath);
166 /* prepare mail command as [/bin/rmail user@domain] */
167 strlcpy(mailCmd, mailCmdPath, BUFFMAXLN);
168 strlcat(mailCmd, " ", BUFFMAXLN);
169 strlcat(mailCmd, mailAddress, BUFFMAXLN);
171 /* prepare a pipe connected to the mail command */
172 mailer = popen(mailCmd, "w");
174 err_msg("ERR at %s#%d: cannot open pipe",__FILE__,__LINE__);
178 /* open the mail content file to read in */
179 if((fp=fopen(GetConfValue("Mail/Content"), "r"))==NULL){
180 err_msg("ERR at %s#%d: cannot find file %s",__FILE__,__LINE__,
181 GetConfValue("Mail/Content"));
185 /* read mail content file, replace variables, and write to the pipe */
186 while(fgets(buff,BUFFMAXLN,fp)!=NULL){
187 HtmlReplace(buff, "%%MAILADDRESS%%", mailAddress);
188 HtmlReplace(buff, "%%LIMITDATE%%", limitDate);
189 HtmlReplace(buff, "%%DEVICENAME%%", device);
197 /***********************************************
198 routines for debugging output
199 ***********************************************/
200 int SendMail(char* mailAddress, char* limitDate, char* device){
202 if(debug>1) err_msg("DEBUG:=>sendMail(%s,%s,%s)",
203 mailAddress, limitDate, device);
204 ret=sendMail(mailAddress, limitDate, device);
205 if(debug>1) err_msg("DEBUG:(%d)<=sendMail( )",ret);