-
Notifications
You must be signed in to change notification settings - Fork 2
/
user.cc
82 lines (67 loc) · 2.09 KB
/
user.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Copyright (c) 2010-2023 David Caldwell <david@porkrind.org>
// Licenced under the GPL 3.0 or any later version. See LICENSE file for details.
#include "user.h"
#include "strprintf.h"
#include "permissions.h"
#include "posix-util.h"
#include "passwd.h"
#include <string>
#include <glob.h>
using namespace std;
map<string,user*> users;
user::user(string name, string daemondir, string logdir)
{
pwent pw = pwent(name);
if (!pw.valid) throw_str("No user named \"%s\"", name.c_str());
init(pw, daemondir, logdir);
}
void user::init(const pwent &pw, string daemondir, string logdir)
{
name = pw.name;
uid = pw.uid;
gid = pw.gid;
homedir = string(pw.dir);
if (daemondir.size() && daemondir.back() == '/') daemondir.pop_back();
if (logdir.size() && logdir.back() == '/') logdir.pop_back();
this->daemondir = daemondir;
this->logdir = logdir;
}
string user::replace_dir_patterns(string pattern)
{
if (pattern.substr(0,2) == "~/")
pattern.replace(0, 1, homedir);
size_t start;
if ((start = pattern.find("%username%")) != string::npos)
pattern.replace(start, 10, name);
return pattern;
}
void user::create_dirs()
{
// Only create hierarchy in home directories, since that's the only one that's guessable.
if (daemondir.substr(0,2) == "~/")
mkdir_pug(homedir, daemondir.substr(2,daemondir.length()), 0750, uid, gid);
if (logdir.substr(0,2) == "~/")
mkdir_pug(homedir, logdir.substr(2,logdir.length()), 0750, uid, gid);
}
string user::config_path()
{
return replace_dir_patterns(daemondir);
}
string user::log_dir()
{
return replace_dir_patterns(logdir);
}
vector<string> user::config_files()
{
permissions::check(config_path(), 0002, uid);
// check permissions
vector<string> files;
glob_t gl;
int status = glob((config_path() + "/*.conf").c_str(), 0, NULL, &gl);
for (size_t i=0; i<gl.gl_pathc; i++)
files.push_back(string(gl.gl_pathv[i]));
globfree(&gl);
if (status != GLOB_NOMATCH && status != 0)
throw_strerr("Glob failed: %d", status);
return files;
}