2014-12-01 23:55:08 +01:00
|
|
|
|
// swad_config.c: configuration
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
SWAD (Shared Workspace At a Distance),
|
|
|
|
|
is a web platform developed at the University of Granada (Spain),
|
|
|
|
|
and used to support university teaching.
|
|
|
|
|
|
|
|
|
|
This file is part of SWAD core.
|
2020-01-01 14:53:57 +01:00
|
|
|
|
Copyright (C) 1999-2020 Antonio Ca<EFBFBD>as Vargas
|
2014-12-01 23:55:08 +01:00
|
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
|
it under the terms of the GNU Affero General 3 License as
|
|
|
|
|
published by the Free Software Foundation, either version 3 of the
|
|
|
|
|
License, or (at your option) any later version.
|
|
|
|
|
|
|
|
|
|
This program 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 Affero General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/*********************************** Headers *********************************/
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
2015-10-16 02:24:29 +02:00
|
|
|
|
#include <stdbool.h> // For boolean type
|
|
|
|
|
#include <stdio.h> // For fseek, ftell...
|
2016-01-26 13:45:45 +01:00
|
|
|
|
#include <stdlib.h> // For malloc and free
|
2015-10-16 02:24:29 +02:00
|
|
|
|
#include <string.h> // For strcasecmp...
|
2014-12-01 23:55:08 +01:00
|
|
|
|
|
|
|
|
|
#include "swad_config.h"
|
|
|
|
|
#include "swad_global.h"
|
|
|
|
|
#include "swad_layout.h"
|
|
|
|
|
#include "swad_string.h"
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/****************************** Public constants *****************************/
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
2019-11-21 16:47:07 +01:00
|
|
|
|
/***************************** Private constants *****************************/
|
2014-12-01 23:55:08 +01:00
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
2019-11-21 16:47:07 +01:00
|
|
|
|
/******************************* Private types *******************************/
|
2014-12-01 23:55:08 +01:00
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/************** External global variables from others modules ****************/
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
extern struct Globals Gbl;
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
2019-11-21 16:47:07 +01:00
|
|
|
|
/************************** Private global variables *************************/
|
2014-12-01 23:55:08 +01:00
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
2019-11-21 16:47:07 +01:00
|
|
|
|
/***************************** Private prototypes ****************************/
|
2014-12-01 23:55:08 +01:00
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/******************* Get SMTP password from config file **********************/
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#define Cfg_MAX_BYTES_STR 256
|
|
|
|
|
|
|
|
|
|
void Cfg_GetConfigFromFile (void)
|
|
|
|
|
{
|
|
|
|
|
FILE *FileCfg;
|
|
|
|
|
size_t Length;
|
|
|
|
|
char *Config;
|
|
|
|
|
const char *Ptr;
|
2017-01-28 15:58:46 +01:00
|
|
|
|
char Str[Cfg_MAX_BYTES_STR + 1];
|
2014-12-01 23:55:08 +01:00
|
|
|
|
|
|
|
|
|
/***** Read config from file to string *****/
|
|
|
|
|
/* Open config file */
|
|
|
|
|
if ((FileCfg = fopen (Cfg_FILE_CONFIG,"rb")) == NULL)
|
|
|
|
|
Lay_ShowErrorAndExit ("Can not open config file.");
|
|
|
|
|
|
|
|
|
|
/* Compute file size */
|
|
|
|
|
fseek (FileCfg,0L,SEEK_END);
|
|
|
|
|
Length = (size_t) ftell (FileCfg);
|
|
|
|
|
fseek (FileCfg,0L,SEEK_SET);
|
|
|
|
|
|
|
|
|
|
/* Allocate memory for buffer */
|
2017-01-28 15:58:46 +01:00
|
|
|
|
if ((Config = (char *) malloc (Length + 1)) == NULL)
|
2014-12-01 23:55:08 +01:00
|
|
|
|
{
|
|
|
|
|
fclose (FileCfg);
|
2018-10-18 20:06:54 +02:00
|
|
|
|
Lay_NotEnoughMemoryExit ();
|
2014-12-01 23:55:08 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Copy file content into buffer */
|
2019-11-06 19:45:20 +01:00
|
|
|
|
if (fread (Config,sizeof (char),Length,FileCfg) != Length)
|
2014-12-01 23:55:08 +01:00
|
|
|
|
{
|
|
|
|
|
fclose (FileCfg);
|
|
|
|
|
Lay_ShowErrorAndExit ("Can not read config.");
|
|
|
|
|
}
|
|
|
|
|
Config[Length] = '\0';
|
|
|
|
|
|
|
|
|
|
/* Close config file */
|
|
|
|
|
fclose (FileCfg);
|
|
|
|
|
|
|
|
|
|
/***** Search SMTP password *****/
|
|
|
|
|
Ptr = Config;
|
|
|
|
|
while (*Ptr)
|
|
|
|
|
{
|
|
|
|
|
Str_GetNextStringUntilSpace (&Ptr,Str,Cfg_MAX_BYTES_STR);
|
|
|
|
|
if (!strcasecmp (Str,"DATABASE_PASSWORD"))
|
|
|
|
|
Str_GetNextStringUntilSpace (&Ptr,Gbl.Config.DatabasePassword,Cfg_MAX_BYTES_DATABASE_PASSWORD);
|
|
|
|
|
else if (!strcasecmp (Str,"SMTP_PASSWORD"))
|
|
|
|
|
Str_GetNextStringUntilSpace (&Ptr,Gbl.Config.SMTPPassword,Cfg_MAX_BYTES_SMTP_PASSWORD);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!Gbl.Config.DatabasePassword[0] ||
|
|
|
|
|
!Gbl.Config.SMTPPassword[0])
|
|
|
|
|
Lay_ShowErrorAndExit ("Bad config format.");
|
|
|
|
|
}
|