OSDN Git Service

modified comments
[opengatem/opengatem.git] / mngsrc / opengatemmail.c
1 /**************************************************
2 OpengateM - MAC address authentication system 
3
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.
7
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.
12  
13 Copyright (C) 2011 Opengate Project Team
14 Written by Yoshiaki Watanabe
15
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.
20
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.
25
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.
29
30 Email: watanaby@is.saga-u.ac.jp
31 **************************************************/
32
33 #include        "opengatemmng.h"
34
35 /**********************************/
36 /*  main routine run as cron      */
37 /**********************************/
38 int  main(int argc, char **argv)
39 {
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] */
48
49   /* drop root privilege */
50   seteuid(getuid());
51
52   /* if this is executed in shell with '-v' option, show makedir */  
53   if(argc>1){
54     if(strcmp(argv[1],"-v")==0){
55       printf("makedir: %s\n", MAKEDIR);
56     }else{
57       printf("This is the program used with cron\n");
58       printf("To show version, run this on console with '-v' option\n");
59     }
60     exit(0);
61   }
62
63   /* save program load path */
64   saveLoadPath(argv[0]);
65   progName = getProgramName();
66
67   /* prepare config file */
68   if(OpenConfFile()==-1){
69     PutMessageToClient("Check config file by running this cgi on console");
70     return 0;
71   }
72  
73   /* start log */
74   errToSyslog(atoi(GetConfValue("Syslog/Enable")));
75   openlog(progName, LOG_PID, atoi(GetConfValue("Syslog/Facility")));
76
77   /* initialize config */
78   InitConf();
79   if(!InitMngDb()) return 0;
80
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)){
84
85     /*** following code summarizes the information of devices having */
86     /*   same mailAddress and limitDate to one mail */
87     
88     /* if get, save the first record */
89     strncpy(mailAddressPrev, mailAddress, BUFFMAXLN);
90     strncpy(limitDatePrev, limitDate, WORDMAXLN);
91     strncpy(deviceList, device, WORDMAXLN);
92
93     /* get next mail address near expiration date */
94     while(GetNextMailAddressFromMngDb(mailAddress, limitDate, device)){
95
96       /* if mailAddress or limitDate are not same as previous */
97       if( strcmp(mailAddress, mailAddressPrev)!=0 
98          || strcmp(limitDate, limitDatePrev)!=0 ){
99
100         /* send mail to the address and clear deviceList */
101         if(!isNull(mailAddressPrev)){
102           if(SendMail(mailAddressPrev, limitDatePrev, deviceList)) count++;
103         }
104         deviceList[0]='\0';
105       }
106
107       /* save the mail Address and limit date */
108       strncpy(mailAddressPrev, mailAddress, BUFFMAXLN);
109       strncpy(limitDatePrev, limitDate, WORDMAXLN);
110
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);
115       }
116       else if(strstr(deviceList, ", ......")!=NULL) ;
117       else if( strlen(deviceList) > BUFFMAXLN*0.8 ){
118         strncat(deviceList, ", ......", BUFFMAXLN); 
119       }
120       else{
121         strncat(deviceList, ", ", BUFFMAXLN); 
122         strncat(deviceList, device, BUFFMAXLN);
123       }
124     }
125
126     /* post processing of residue at the end of get loop */
127     if( !isNull(mailAddressPrev) ){
128       if(SendMail(mailAddressPrev, limitDatePrev, deviceList)) count++;
129     }
130   }
131
132   /* finalize */
133   CloseMngDb();
134
135   /* report to syslog */
136   if(debug>0)  err_msg("INFO: Sended %d mails",count); 
137
138   return 0;
139 }
140
141 /**************************************************/
142 /* send mail                                      */
143 /**************************************************/
144 int sendMail(char* mailAddress, char* limitDate, char* device)
145 {
146   FILE* mailer;
147   FILE* fp;
148   char mailCmd[BUFFMAXLN];
149   char buff[BUFFMAXLN];
150   char* mailCmdPath;
151   struct stat st;
152
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__);
157     return FALSE;
158   }
159
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);
163     return FALSE;
164   }
165
166   /* prepare mail command as [/bin/rmail user@domain] */
167   strlcpy(mailCmd, mailCmdPath, BUFFMAXLN);
168   strlcat(mailCmd, " ", BUFFMAXLN);
169   strlcat(mailCmd, mailAddress, BUFFMAXLN);
170
171   /* prepare a pipe connected to the mail command */
172   mailer = popen(mailCmd, "w");
173   if(mailer==NULL){
174     err_msg("ERR at %s#%d: cannot open pipe",__FILE__,__LINE__);
175     return FALSE;
176   }
177
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"));
182     return FALSE;
183   }
184
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);
190     fputs(buff, mailer);    
191   }
192   fclose(fp);
193   pclose(mailer);
194   return TRUE;
195 }
196
197 /***********************************************
198  routines for debugging output
199 ***********************************************/
200 int SendMail(char* mailAddress, char* limitDate, char* device){
201     int ret;
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);
206   return ret;
207 }