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.
|
2024-02-07 00:40:28 +01:00
|
|
|
|
Copyright (C) 1999-2024 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"
|
2021-04-26 15:27:27 +02:00
|
|
|
|
#include "swad_error.h"
|
2014-12-01 23:55:08 +01:00
|
|
|
|
#include "swad_layout.h"
|
|
|
|
|
#include "swad_string.h"
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
2022-10-20 10:45:07 +02:00
|
|
|
|
/************************* Private global variables **************************/
|
2014-12-01 23:55:08 +01:00
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
2022-10-20 10:45:07 +02:00
|
|
|
|
static struct
|
|
|
|
|
{
|
|
|
|
|
char Database[Cfg_MAX_BYTES_DATABASE_PASSWORD + 1];
|
|
|
|
|
char SMTP[Cfg_MAX_BYTES_SMTP_PASSWORD + 1];
|
|
|
|
|
} Cfg_Passwords;
|
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)
|
2021-04-26 17:29:04 +02:00
|
|
|
|
Err_FileFolderNotFoundExit ();
|
2014-12-01 23:55:08 +01:00
|
|
|
|
|
|
|
|
|
/* Compute file size */
|
|
|
|
|
fseek (FileCfg,0L,SEEK_END);
|
|
|
|
|
Length = (size_t) ftell (FileCfg);
|
|
|
|
|
fseek (FileCfg,0L,SEEK_SET);
|
|
|
|
|
|
|
|
|
|
/* Allocate memory for buffer */
|
2021-02-15 16:25:55 +01:00
|
|
|
|
if ((Config = malloc (Length + 1)) == NULL)
|
2014-12-01 23:55:08 +01:00
|
|
|
|
{
|
|
|
|
|
fclose (FileCfg);
|
2021-04-26 15:27:27 +02:00
|
|
|
|
Err_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);
|
2021-04-26 15:27:27 +02:00
|
|
|
|
Err_ShowErrorAndExit ("Can not read config.");
|
2014-12-01 23:55:08 +01:00
|
|
|
|
}
|
|
|
|
|
Config[Length] = '\0';
|
|
|
|
|
|
|
|
|
|
/* Close config file */
|
|
|
|
|
fclose (FileCfg);
|
|
|
|
|
|
2022-10-20 10:45:07 +02:00
|
|
|
|
/***** Search passwords *****/
|
|
|
|
|
Cfg_Passwords.Database[0] =
|
|
|
|
|
Cfg_Passwords.SMTP[0] = '\0';
|
2014-12-01 23:55:08 +01:00
|
|
|
|
Ptr = Config;
|
|
|
|
|
while (*Ptr)
|
|
|
|
|
{
|
|
|
|
|
Str_GetNextStringUntilSpace (&Ptr,Str,Cfg_MAX_BYTES_STR);
|
|
|
|
|
if (!strcasecmp (Str,"DATABASE_PASSWORD"))
|
2022-10-20 10:45:07 +02:00
|
|
|
|
Str_GetNextStringUntilSpace (&Ptr,Cfg_Passwords.Database,Cfg_MAX_BYTES_DATABASE_PASSWORD);
|
2014-12-01 23:55:08 +01:00
|
|
|
|
else if (!strcasecmp (Str,"SMTP_PASSWORD"))
|
2022-10-20 10:45:07 +02:00
|
|
|
|
Str_GetNextStringUntilSpace (&Ptr,Cfg_Passwords.SMTP ,Cfg_MAX_BYTES_SMTP_PASSWORD);
|
2014-12-01 23:55:08 +01:00
|
|
|
|
}
|
2022-10-20 10:45:07 +02:00
|
|
|
|
if (!Cfg_Passwords.Database[0] ||
|
|
|
|
|
!Cfg_Passwords.SMTP[0])
|
2021-04-26 15:27:27 +02:00
|
|
|
|
Err_ShowErrorAndExit ("Bad config format.");
|
2014-12-01 23:55:08 +01:00
|
|
|
|
}
|
2022-10-20 10:45:07 +02:00
|
|
|
|
|
|
|
|
|
const char *Cfg_GetDatabasePassword (void)
|
|
|
|
|
{
|
|
|
|
|
return Cfg_Passwords.Database;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const char *Cfg_GetSMTPPassword (void)
|
|
|
|
|
{
|
|
|
|
|
return Cfg_Passwords.SMTP;
|
|
|
|
|
}
|