1/* 2 * Copyright 2004, ASUSTek Inc. 3 * All Rights Reserved. 4 * 5 * THIS SOFTWARE IS OFFERED "AS IS", AND ASUS GRANTS NO WARRANTIES OF ANY 6 * KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE. BROADCOM 7 * SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS 8 * FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE. 9 * 10 * $Id: sendalarm.c,v 1.1.1.1 2008/10/15 03:28:48 james26_jang Exp $ 11 */ 12 13#ifdef USB_SUPPORT 14 15#include <stdio.h> 16#include <stdlib.h> 17#include <string.h> 18#include <signal.h> 19#include <unistd.h> 20#include <errno.h> 21#include <sys/fcntl.h> 22#include <dirent.h> 23#include <sys/mount.h> 24#include <bcmnvram.h> 25#include <netconf.h> 26#include <shutils.h> 27#include <rc.h> 28#include <syslog.h> 29 30#define logs(s) syslog(LOG_NOTICE, s) 31 32void filecat(FILE *fp, char *catted) 33{ 34 FILE *cfp; 35 char line[1024]; 36 size_t i; 37 38 if ((cfp=fopen(catted, "r"))==NULL) return; 39 40 while((i=fread(line, 1, sizeof(line), cfp))) 41 { 42 //printf("write: %s\n", line); 43 fwrite(line, 1, i, fp); 44 } 45 fclose(cfp); 46 return; 47} 48 49char *nslookup(char *name, char qtype, char *ret) 50{ 51 #define MAX_LINE_SIZE 512 52 FILE *fp; 53 char buf[MAX_LINE_SIZE]; 54 55 fp=fopen("/etc/resolv.conf", "r+"); 56 57 if (fp!=NULL && 58 fgets(buf, MAX_LINE_SIZE, fp)!=NULL && 59 strncmp(buf,"nameserver", 10)==0) 60 { 61 dns_query(&buf[11], name, qtype, ret); 62 fclose(fp); 63 } 64 return ret; 65} 66 67int 68sendalarm_main(int argc, char *argv[]) 69{ 70 FILE *fp; 71 char serverip[32], servername[32]; 72 char command[128]; 73 char image[64], *imagebase; 74 char *boundry="alarmmailalarmmail"; 75 int i; 76 77 if (nvram_invmatch("usb_websecurity_x", "1")) return -1; 78 79 *servername = *serverip = 0x0; 80 81 if (nvram_invmatch("usb_websendto_x", "")) 82 { 83 char *mname; 84 85 mname = strstr(nvram_safe_get("usb_websendto_x"), "@"); 86 87 if (mname!=NULL) 88 nslookup(mname+1, 'M', servername); 89 90 if (*servername) 91 nslookup(servername, 'A', serverip); 92 } 93 94 if (!*serverip && nvram_invmatch("usb_webmserver_x", "")) 95 { 96 nslookup(nvram_safe_get("web_webmserver_x"), 'A' , serverip); 97 } 98 99 if (!*serverip) 100 { 101 logs("send email alarm, but can not resolve ip of email server!"); 102 return -1; 103 } 104 else dprintf("send alarm to : %s\n", serverip); 105 // Build mail source file 106 107 if ((fp = fopen("/var/tmp/alarmmail", "w"))==NULL) return -1; 108 109 fprintf(fp, "To: %s\n", nvram_safe_get("usb_websendto_x")); 110 fprintf(fp, "Subject: %s\n", nvram_safe_get("usb_websubject_x")); 111 fprintf(fp, "X-Mailer: Mailer\n"); 112 fprintf(fp, "Mime-Version: 1.0\n"); 113 fprintf(fp, "Content-Type: multipart/mixed; boundary=\"%s\"\n", boundry); 114 fprintf(fp, "Content-Disposition: inline\n\n\n"); 115 fprintf(fp, "--%s\n", boundry); 116 fprintf(fp, "Content-Type: text/plain; charset=us-ascii\n"); 117 fprintf(fp, "Content-Disposition: inline\n\n"); 118 fprintf(fp, "Image motion detected!!!\n\n"); 119 120 if (nvram_match("usb_webattach_x", "1")) 121 { 122 for(i=1;i<argc;i++) 123 { 124 strcpy(image, argv[i]); 125 126 if (!(imagebase=rindex(image, '/'))) imagebase = image; 127 else imagebase++; 128 129 //eval("uuencode", "-m", image, imagebase, "-f", "/var/tmp/uuencode"); 130 sprintf(command, "uuencode -m %s %s > /var/tmp/uuencode", image, imagebase); 131 system(command); 132 133 fprintf(fp, "--%s\n", boundry); 134 fprintf(fp, "Content-Type: application/unknown\n"); 135 fprintf(fp, "Content-Disposition: attachment; filename=\"%s\"\n", imagebase); 136 137 fprintf(fp, "Content-Transfer-Encoding: base64\n\n"); 138 filecat(fp, "/var/tmp/uuencode"); 139 fprintf(fp, "\n\n\n"); 140 } 141 fprintf(fp, "--%s--\n\n", boundry); 142 } 143 fclose(fp); 144 145 sprintf(command, "cat /var/tmp/alarmmail | mini_sendmail -s%s %s", serverip, nvram_safe_get("usb_websendto_x")); 146 system(command); 147 148 //eval("mini_sendmail", "-f", "/var/tmp/alarmmail", "-s", serverip, nvram_safe_get("usb_websendto")); 149 // log 150 logs("send mail alert"); 151 kill_pidfile_s("/var/run/watchdog.pid", SIGUSR2); 152 153 // clean temp file 154 // unlink("/var/tmp/uuencode"); 155 unlink("/var/tmp/alarmmail"); 156 157 dprintf("done\n"); 158 159 return 0; 160} 161 162#endif 163