1 /**************************************************
2 OpengateM - MAC address authentication system
3 module for sending mail to warn limit date
5 Copyright (C) 2011 Opengate Project Team
6 Written by Yoshiaki Watanabe
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 Email: watanaby@is.saga-u.ac.jp
23 **************************************************/
25 #include "opengatemmng.h"
27 /***************************************************/
28 /* main routine called as cgi from Web server */
29 /***************************************************/
30 int main(int argc, char **argv)
32 char mailAddress[BUFFMAXLN]; /* mail address */
33 char limitDate[WORDMAXLN]; /* limit date */
34 char device[WORDMAXLN]; /* device name */
35 int count=0; /* count for sending mails */
36 char* progName; /* the name of this program in argv[0] */
38 /* drop root privilege */
41 /* if this is executed in shell with '-v' option, show makedir */
43 if(strcmp(argv[1],"-v")==0){
44 printf("makedir: %s\n", MAKEDIR);
46 printf("This is the program used with cron\n");
47 printf("To show version, run this on console with '-v' option\n");
52 /* save program load path */
53 saveLoadPath(argv[0]);
54 progName = getProgramName();
56 /* prepare config file */
57 if(OpenConfFile()==-1){
58 PutMessageToClient("Check config file by running this cgi on console");
63 errToSyslog(atoi(GetConfValue("Syslog/Enable")));
64 openlog(progName, LOG_PID, atoi(GetConfValue("Syslog/Facility")));
66 /* initialize config */
68 if(!InitMngDb()) return 0;
70 /* get mail address for users near expiration date and send mail */
71 while(GetNextMailAddressFromMngDb(mailAddress, limitDate, device)){
72 if(SendMail(mailAddress, limitDate, device)) count++;
78 /* report to syslog */
79 if(debug>0) err_msg("INFO: Sended %d mails",count);
84 /**************************************************/
86 /**************************************************/
87 int sendMail(char* mailAddress, char* limitDate, char* device)
91 char mailCmd[BUFFMAXLN];
96 /* set mail command path (/bin/rmail) */
97 mailCmdPath=GetConfValue("Mail/CmdPath");
98 if(isNull(mailCmdPath)){
99 err_msg("ERR at %s#%d: cannot get Mail/CmdPath from conf",__FILE__,__LINE__);
103 /* if mail command is not exists, return */
104 if(stat(mailCmdPath, &st)!=0){
105 err_msg("ERR at %s#%d: mail command[%s] is not found",__FILE__,__LINE__,mailCmdPath);
109 /* prepare mail command [/bin/rmail user@domain] */
110 strncpy(mailCmd, mailCmdPath, BUFFMAXLN);
111 strncat(mailCmd, " ", BUFFMAXLN);
112 strncat(mailCmd, mailAddress, BUFFMAXLN);
114 /* prepare a pipe connected to the mail command */
115 mailer = popen(mailCmd, "w");
117 err_msg("ERR at %s#%d: cannot open pipe",__FILE__,__LINE__);
121 /* write the mail content to the pipe */
122 if((fp=fopen(GetConfValue("Mail/Content"), "r"))==NULL){
123 err_msg("ERR at %s#%d: cannot find file %s",__FILE__,__LINE__,
124 GetConfValue("Mail/Content"));
128 while(fgets(buff,BUFFMAXLN,fp)!=NULL){
129 HtmlReplace(buff, "%%MAILADDRESS%%", mailAddress);
130 HtmlReplace(buff, "%%LIMITDATE%%", limitDate);
131 HtmlReplace(buff, "%%DEVICENAME%%", device);
139 /***********************************************
140 routines for debugging output
141 ***********************************************/
142 int SendMail(char* mailAddress, char* limitDate, char* device){
144 if(debug>1) err_msg("DEBUG:=>sendMail(%s,%s,%s)",
145 mailAddress, limitDate, device);
146 ret=sendMail(mailAddress, limitDate, device);
147 if(debug>1) err_msg("DEBUG:(%d)<=sendMail( )",ret);