📄 smsd.c
字号:
/* -------------------------------------------------------------------- *//* SMS Client, send messages to mobile phones and pagers *//* *//* smsd.c *//* *//* Copyright (C) 1997,1998 Angelo Masci *//* *//* This library is free software; you can redistribute it and/or *//* modify it under the terms of the GNU Library General Public *//* License as published by the Free Software Foundation; either *//* version 2 of the License, or (at your option) any later version. *//* *//* This library is distributed in the hope that it will be useful, *//* but WITHOUT ANY WARRANTY; without even the implied warranty of *//* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *//* Library General Public License for more details. *//* *//* You should have received a copy of the GNU Library General Public *//* License along with this library; if not, write to the Free *//* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *//* *//* You can contact the author at this e-mail address: *//* *//* angelo@styx.demon.co.uk *//* *//* -------------------------------------------------------------------- $Id$ -------------------------------------------------------------------- */#include <stdio.h>#include <stdlib.h>#include <string.h>#include <ctype.h>#include <sys/types.h>#include <sys/socket.h>#include <sys/stat.h>#include <fcntl.h>#include <unistd.h>#include <errno.h>#if !defined(AIX) && !defined(SOLARIS) && !defined(UNIXWARE)#include <getopt.h>#endif#include "server.h"#include "logfile.h"#include "common.h"#include "resource.h"/* -------------------------------------------------------------------- */#if !defined(MSMSDLOGFILE)#error "MSMSDLOGFILE undefined"#else#define LOGFILE MSMSDLOGFILE#endif#if !defined(MSMSDLOGLEVEL)#error "MSMSDLOGLEVEL undefined" #else#define LOGLEVEL MSMSDLOGLEVEL#endif/* -------------------------------------------------------------------- */#define ERROR_STATE -2#define EXIT_STATE -1#define NULL_STATE 0#define UNKNOWN_STATE 1/* -------------------------------------------------------------------- */struct sms_control_data_struct { char *user, *host, *mail, *alen, *destination, *control_file, *data_file, *id; long len; };typedef struct sms_control_data_struct SMS_CONTROL_DATA;struct sms_data_struct { char *data; };typedef struct sms_data_struct SMS_DATA;/* -------------------------------------------------------------------- */SMS_DATA sms_data;/* -------------------------------------------------------------------- */static int smsd_port, smsd_maxid;static char *smsd_spool, *smsd_incoming, *smsd_services;static RESOURCE resource_list[] = { { RESOURCE_NUMERIC, "port", 0, 1, NULL, 0, NULL, 4563, &smsd_port }, { RESOURCE_NUMERIC, "maxid", 0, 1, NULL, 0, NULL, 32000, &smsd_maxid }, { RESOURCE_STRING, "directories.spool", 0, 1, NULL, 0, "/var/spool/sms", 0, &smsd_spool }, { RESOURCE_STRING, "directories.incoming", 0, 1, NULL, 0, "incoming", 0, &smsd_incoming }, { RESOURCE_STRING, "directories.services", 0, 1, NULL, 0, "services", 0, &smsd_services }, { RESOURCE_NULL, NULL, 0, 1, NULL, 0, NULL, 0, NULL } };#define SMS_PORT smsd_port /* Standard port number */ /* assigned for sms traffic */#define SMS_SPOOLDIR smsd_spool#define SMS_INCOMINGDIR smsd_incoming#define SMS_SERVICESDIR smsd_services#define SMS_MAXID smsd_maxid/* -------------------------------------------------------------------- */#define SAFEOUTPUT(X) ((X == NULL)?("<NULL>"):(X))#define SAFECONTROLOUTPUT(X) ((X == NULL)?(""):(X))/* -------------------------------------------------------------------- */void cleanup_files(SMS_CONTROL_DATA *control_data);void display_current(int fd, SMS_CONTROL_DATA *control_data);void gateway(int new_fd);void usage(char *file);char *get_data(char *buf);int gen_controlfile(SMS_CONTROL_DATA *control_data);int copy_data(int dst_fd, int src_fd, long dlen);/* -------------------------------------------------------------------- *//* -------------------------------------------------------------------- */void display_current(int fd, SMS_CONTROL_DATA *control_data){ hprintf(fd, " Current Control Record info:\r\n"); hprintf(fd, " Username: %s\r\n", SAFEOUTPUT(control_data->user)); hprintf(fd, " Hostname: %s\r\n", SAFEOUTPUT(control_data->host)); hprintf(fd, " Mail on Delivery: %s\r\n", SAFEOUTPUT(control_data->mail)); hprintf(fd, " Destination: %s\r\n", SAFEOUTPUT(control_data->destination)); hprintf(fd, " Control File: %s\r\n", SAFEOUTPUT(control_data->control_file)); hprintf(fd, " Data File: %s\r\n", SAFEOUTPUT(control_data->data_file)); hprintf(fd, " ID: %s\r\n", SAFEOUTPUT(control_data->id)); hprintf(fd, " Length: %ld\r\n", control_data->len);}/* -------------------------------------------------------------------- *//* -------------------------------------------------------------------- */void write_control_data(SMS_CONTROL_DATA *control_data){ FILE *fp; fp = fopen(control_data->control_file, "w"); if (fp == NULL) { lprintf(LOG_ERROR, "opening control_file %s\n", control_data->control_file); exit(-1); } fprintf(fp, "{\n"); fprintf(fp, " Username = \"%s\";\n", SAFECONTROLOUTPUT(control_data->user)); fprintf(fp, " Hostname = \"%s\";\n", SAFECONTROLOUTPUT(control_data->host)); fprintf(fp, " Mail_on_Delivery = \"%s\";\n", SAFECONTROLOUTPUT(control_data->mail)); fprintf(fp, " Destination = \"%s\";\n", SAFECONTROLOUTPUT(control_data->destination)); fprintf(fp, " Control_File = \"%s\";\n", SAFECONTROLOUTPUT(control_data->control_file)); fprintf(fp, " Data_File = \"%s\";\n", SAFECONTROLOUTPUT(control_data->data_file)); fprintf(fp, " ID = \"%s\";\n", SAFECONTROLOUTPUT(control_data->id)); fprintf(fp, " Length = \"%ld\"\n", control_data->len); fprintf(fp, "}\n"); fclose(fp);}/* -------------------------------------------------------------------- *//* Parse string of form: *//* *//* COMMAND<SP>DATA<EOL> *//* *//* Copy data to newly allocated buffer and return *//* a pointer to it. If no data available return NULL *//* -------------------------------------------------------------------- */char *get_data(char *buf){ char *src, *dst, *data; int i, j, cmd; cmd = TRUE; src = buf; i = 0; j = 0; while ((*src != '\n') && (*src != '\0') && (*src != '\r')) { if ((cmd) && (*src == ' ')) { cmd = FALSE; j = i+1; i = 0; } else { i++; } src++; }#if 0 if ((j == 0) || ((i == 1) && ((*src == '\n') || (*src == '\r') || (isspace(*src)) || (*src == '\0')))) { return NULL; }#else if (j == 0) { return NULL; }#endif data = (char *)malloc(sizeof(char) * i + 1); if (dst == NULL) { lprintf(LOG_ERROR, "Allocating memory\n"); exit(-1); } dst = data; src = &buf[j]; while ((*src != '\n') && (*src != '\0') && (*src != '\r')) { *dst++ = *src++; } *dst = '\0'; return data;}/* -------------------------------------------------------------------- *//* Return Values: *//* *//* 0 - If the current control record is incomplete *//* 1 - if the current control record is complete *//* *//* -------------------------------------------------------------------- */int is_complete(SMS_CONTROL_DATA *control_data){ if ((control_data->user == NULL) || (control_data->host == NULL) || (control_data->mail == NULL) || (control_data->alen == NULL) || (control_data->id == NULL) || (control_data->destination == NULL) || (control_data->control_file == NULL) || (control_data->data_file == NULL)) { return 0; } return 1;}/* -------------------------------------------------------------------- *//* -------------------------------------------------------------------- */SMS_CONTROL_DATA *init_control_data(void){ SMS_CONTROL_DATA *control_data; control_data = (SMS_CONTROL_DATA *)malloc(sizeof(SMS_CONTROL_DATA)); if (control_data == NULL) { lprintf(LOG_ERROR, "Allocating memory\n"); exit(-1); } control_data->user = NULL; control_data->host = NULL; control_data->mail = NULL; control_data->alen = NULL; control_data->id = NULL; control_data->destination = NULL; control_data->control_file = NULL; control_data->data_file = NULL; control_data->len = 0; return control_data;}/* -------------------------------------------------------------------- *//* Generate a control file in the incoming directory. *//* The control file is empty. *//* The control_data structure is updated to include the full path *//* to the generated control file and it's corresponding data file. *//* Note that memory is allocated for each of control_file, *//* data_file and id. It is the responsibility of the caller to *//* free this memory. *//* *//* Return Value: *//* *//* -1 error *//* 0 Success *//* -------------------------------------------------------------------- */int gen_controlfile(SMS_CONTROL_DATA *control_data){ int n, fd; cleanup_files(control_data); control_data->id = (char *)malloc(sizeof(char) * 8); if (control_data->id == NULL) { lprintf(LOG_ERROR, "Allocating memory\n"); exit(-1); } control_data->control_file = (char *)malloc(sizeof(char) * (sms_strlen(SMS_SPOOLDIR) + sms_strlen(SMS_INCOMINGDIR) + 10)); if (control_data->control_file == NULL) { lprintf(LOG_ERROR, "Allocating memory\n"); exit(-1); } control_data->data_file = (char *)malloc(sizeof(char) * (sms_strlen(SMS_SPOOLDIR) + sms_strlen(SMS_INCOMINGDIR) + 10)); if (control_data->data_file == NULL) { lprintf(LOG_ERROR, "Allocating memory\n"); exit(-1); } sms_strcpy(control_data->control_file, SMS_SPOOLDIR); sms_strcpy(control_data->data_file, SMS_SPOOLDIR); if (control_data->control_file[sms_strlen(control_data->control_file) -1] != '/') { sms_strcat(control_data->data_file, "/"); sms_strcat(control_data->control_file, "/"); } sms_strcat(control_data->control_file, SMS_INCOMINGDIR); sms_strcat(control_data->data_file, SMS_INCOMINGDIR); if (control_data->control_file[sms_strlen(control_data->control_file) -1] != '/') { sms_strcat(control_data->data_file, "/"); sms_strcat(control_data->control_file, "/"); } for (n = getpid(); n < SMS_MAXID; n++) { sprintf(control_data->id, "%05d", n); sprintf(control_data->control_file, "%scf%s", control_data->control_file, control_data->id); sprintf(control_data->data_file, "%sdf%s", control_data->data_file, control_data->id); if (access(control_data->control_file, F_OK) == 0) { continue; } else { fd = open(control_data->control_file, O_CREAT|O_WRONLY|O_EXCL, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH); if (fd == -1) { if (errno == EEXIST) { continue; } lprintf(LOG_WARNING, "Cannot create file %s\n", control_data->control_file); free(control_data->control_file); free(control_data->data_file); free(control_data->id); return -1; } else { close(fd); break; } } } if (n == SMS_MAXID) { lprintf(LOG_WARNING, "Cannot create file as MAXID exceeded\n"); free(control_data->control_file); free(control_data->data_file); free(control_data->id); return -1; } return 0;}/* -------------------------------------------------------------------- *//* -------------------------------------------------------------------- */void cleanup_files(SMS_CONTROL_DATA *control_data){ if (control_data->control_file != NULL) { unlink(control_data->control_file); free(control_data->control_file); control_data->control_file = NULL; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -