1 /***************************************************************************
2 * Copyright (C) 2008-2009 by Andrzej Rybczak *
3 * electricityispower@gmail.com *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
26 #include <openssl/evp.h>
29 #include "configuration.h"
32 size_t write_data(char *buffer
, size_t size
, size_t nmemb
, void *data
)
34 size_t result
= size
* nmemb
;
35 static_cast<std::string
*>(data
)->append(buffer
, result
);
41 if (Config
.dedicated_user
.empty() || getuid() != 0)
45 if (!(userinfo
= getpwnam(Config
.dedicated_user
.c_str())))
47 std::cerr
<< "user " << Config
.dedicated_user
<< " not found!\n";
50 if (setgid(userinfo
->pw_gid
) == -1)
52 std::cerr
<< "cannot set gid for user " << Config
.dedicated_user
<< ": " << strerror(errno
) << std::endl
;
55 if (setuid(userinfo
->pw_uid
) == -1)
57 std::cerr
<< "cannot change to uid of user " << Config
.dedicated_user
<< ": " << strerror(errno
) << std::endl
;
60 Config
.user_home_folder
= userinfo
->pw_dir
? userinfo
->pw_dir
: "";
68 std::ofstream
f(Config
.file_pid
.c_str(), std::ios_base::trunc
);
82 std::ofstream
f(Config
.file_cache
.c_str(), std::ios::trunc
);
86 void WriteCache(const std::string
&s
)
88 std::ofstream
f(Config
.file_cache
.c_str(), std::ios::app
);
96 void Log(LogLevel ll
, const char *format
, ...)
98 if (Config
.log_level
< ll
)
100 FILE *f
= fopen(Config
.file_log
.c_str(), "a");
103 perror("Cannot open log file!\n");
106 fprintf(f
, "[%s] ", DateTime().c_str());
108 va_start(list
, format
);
109 vfprintf(f
, format
, list
);
115 void IgnoreNewlines(std::string
&s
)
117 for (size_t i
= s
.find("\n"); i
!= std::string::npos
; i
= s
.find("\n"))
121 std::string
md5sum(const std::string
&s
)
123 unsigned char md_value
[EVP_MAX_MD_SIZE
];
127 EVP_DigestInit(&mdctx
, EVP_md5());
128 EVP_DigestUpdate(&mdctx
, s
.c_str(), s
.length());
129 EVP_DigestFinal_ex(&mdctx
, md_value
, &md_len
);
130 EVP_MD_CTX_cleanup(&mdctx
);
132 char result
[md_len
*2+1];
133 for (unsigned i
= 0; i
< md_len
; i
++)
134 sprintf(&result
[i
*2], "%02x", md_value
[i
]);
135 result
[md_len
*2] = 0;
140 std::string
DateTime()
142 static char result
[32];
147 result
[strftime(result
, 31, "%Y/%m/%d %X", t
)] = 0;
151 int StrToInt(const std::string
&s
)
153 return atoi(s
.c_str());