add support for dropping root privileges
[scrobby.git] / src / misc.cpp
blob10129328c1cd21f17dff6935082c18351dd42329
1 /***************************************************************************
2 * Copyright (C) 2008 by Andrzej Rybczak *
3 * electricityispower@gmail.com *
4 * *
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. *
9 * *
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. *
14 * *
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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
21 #include <cerrno>
22 #include <cstdlib>
23 #include <cstring>
24 #include <fstream>
25 #include <iostream>
26 #include <sstream>
27 #include <openssl/evp.h>
28 #include <pwd.h>
30 #include "configuration.h"
31 #include "misc.h"
33 extern ScrobbyConfig config;
35 pthread_mutex_t log_file = PTHREAD_MUTEX_INITIALIZER;
37 size_t write_data(char *buffer, size_t size, size_t nmemb, std::string data)
39 size_t result = size * nmemb;
40 data.append(buffer, result);
41 return result;
44 void ChangeToUser()
46 if (config.dedicated_user.empty() || getuid() != 0)
47 return;
49 passwd *userinfo;
50 if (!(userinfo = getpwnam(config.dedicated_user.c_str())))
52 std::cerr << "user " << config.dedicated_user << " not found!\n";
53 exit(1);
55 if (setgid(userinfo->pw_gid) == -1)
57 std::cerr << "cannot set gid for user " << config.dedicated_user << ": " << strerror(errno) << std::endl;
58 exit(1);
60 if (setuid(userinfo->pw_uid) == -1)
62 std::cerr << "cannot change to uid of user " << config.dedicated_user << ": " << strerror(errno) << std::endl;
63 exit(1);
67 bool Daemonize()
69 if (daemon(0, 0) < 0)
70 return false;
72 std::ofstream f(config.file_pid.c_str(), std::ios_base::app);
73 if (f.is_open())
75 pid_t pid = getpid();
76 f << pid;
77 f.close();
78 return true;
80 else
81 return false;
84 void ClearCache()
86 std::ofstream f(config.file_cache.c_str(), std::ios::trunc);
87 f.close();
90 void GetCachedSongs(std::vector<std::string> &v)
92 std::ifstream f(config.file_cache.c_str());
93 if (f.is_open())
95 std::string line;
96 while (!f.eof())
98 getline(f, line);
99 if (!line.empty())
100 v.push_back(line);
105 void Cache(const std::string &s)
107 std::ofstream f(config.file_cache.c_str(), std::ios::app);
108 if (f.is_open())
110 f << s << std::endl;
111 f.close();
115 void Log(LogLevel ll, const char *format, ...)
117 if (config.log_level < ll)
118 return;
119 pthread_mutex_lock(&log_file);
120 FILE *f = fopen(config.file_log.c_str(), "a");
121 if (!f)
123 perror("Cannot open log file!\n");
124 exit(1);
126 fprintf(f, "[%s] ", DateTime().c_str());
127 va_list list;
128 va_start(list, format);
129 vfprintf(f, format, list);
130 va_end(list);
131 fprintf(f, "\n");
132 fclose(f);
133 pthread_mutex_unlock(&log_file);
136 void ignore_newlines(std::string &s)
138 for (size_t i = s.find("\n"); i != std::string::npos; i = s.find("\n"))
139 s.replace(i, 1, "");
142 std::string md5sum(const std::string &s)
144 unsigned char md_value[EVP_MAX_MD_SIZE];
145 unsigned int md_len;
147 EVP_MD_CTX mdctx;
148 EVP_DigestInit(&mdctx, EVP_md5());
149 EVP_DigestUpdate(&mdctx, s.c_str(), s.length());
150 EVP_DigestFinal_ex(&mdctx, md_value, &md_len);
151 EVP_MD_CTX_cleanup(&mdctx);
153 char result[md_len*2+1];
154 for (unsigned i = 0; i < md_len; i++)
155 sprintf(&result[i*2], "%02x", md_value[i]);
156 result[md_len*2] = 0;
158 return result;
161 std::string DateTime()
163 char result[32];
164 time_t raw;
165 tm *t;
166 time(&raw);
167 t = localtime(&raw);
168 result[strftime(result, 31, "%Y/%m/%d %X", t)] = 0;
169 return result;
172 int StrToInt(const std::string &s)
174 return atoi(s.c_str());