OSDN Git Service

Ver.0.8.1
[opengatem/opengatem.git] / mngsrc / opengatemmail.c
1 /**************************************************
2 OpengateM - MAC address authentication system 
3  module for sending mail to warn limit date 
4
5 Copyright (C) 2011 Opengate Project Team
6 Written by Yoshiaki Watanabe
7
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.
12
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.
17
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.
21
22 Email: watanaby@is.saga-u.ac.jp
23 **************************************************/
24
25 #include        "opengatemmng.h"
26
27 /***************************************************/
28 /*  main routine called as cgi from Web server     */
29 /***************************************************/
30 int  main(int argc, char **argv)
31 {
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] */
37
38   /* drop root privilege */
39   seteuid(getuid());
40
41   /* if this is executed in shell with '-v' option, show makedir */  
42   if(argc>1){
43     if(strcmp(argv[1],"-v")==0){
44       printf("makedir: %s\n", MAKEDIR);
45     }else{
46       printf("This is the program used with cron\n");
47       printf("To show version, run this on console with '-v' option\n");
48     }
49     exit(0);
50   }
51
52   /* save program load path */
53   saveLoadPath(argv[0]);
54   progName = getProgramName();
55
56   /* prepare config file */
57   if(OpenConfFile()==-1){
58     PutMessageToClient("Check config file by running this cgi on console");
59     return 0;
60   }
61  
62   /* start log */
63   errToSyslog(atoi(GetConfValue("Syslog/Enable")));
64   openlog(progName, LOG_PID, atoi(GetConfValue("Syslog/Facility")));
65
66   /* initialize config */
67   InitConf();
68   if(!InitMngDb()) return 0;
69
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++;
73   }
74
75   /* finalize */
76   CloseMngDb();
77
78   /* report to syslog */
79   if(debug>0)  err_msg("INFO: Sended %d mails",count); 
80
81   return 0;
82 }
83
84 /**************************************************/
85 /* send mail                                      */
86 /**************************************************/
87 int sendMail(char* mailAddress, char* limitDate, char* device)
88 {
89   FILE* mailer;
90   FILE* fp;
91   char mailCmd[BUFFMAXLN];
92   char buff[BUFFMAXLN];
93   char* mailCmdPath;
94   struct stat st;
95
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__);
100     return FALSE;
101   }
102
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);
106     return FALSE;
107   }
108
109   /* prepare mail command [/bin/rmail user@domain] */
110   strncpy(mailCmd, mailCmdPath, BUFFMAXLN);
111   strncat(mailCmd, " ", BUFFMAXLN);
112   strncat(mailCmd, mailAddress, BUFFMAXLN);
113
114   /* prepare a pipe connected to the mail command */
115   mailer = popen(mailCmd, "w");
116   if(mailer==NULL){
117     err_msg("ERR at %s#%d: cannot open pipe",__FILE__,__LINE__);
118     return FALSE;
119   }
120
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"));
125     return FALSE;
126   }
127
128   while(fgets(buff,BUFFMAXLN,fp)!=NULL){
129     HtmlReplace(buff, "%%MAILADDRESS%%", mailAddress);
130     HtmlReplace(buff, "%%LIMITDATE%%", limitDate);
131     HtmlReplace(buff, "%%DEVICENAME%%", device);
132     fputs(buff, mailer);    
133   }
134   fclose(fp);
135   pclose(mailer);
136   return TRUE;
137 }
138
139 /***********************************************
140  routines for debugging output
141 ***********************************************/
142 int SendMail(char* mailAddress, char* limitDate, char* device){
143     int ret;
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);
148   return ret;
149 }