-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpath.c
136 lines (115 loc) · 2.93 KB
/
path.c
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
* This file is part of John the Ripper password cracker,
* Copyright (c) 1996-2000,2010 by Solar Designer
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted.
*
* There's ABSOLUTELY NO WARRANTY, express or implied.
*/
#include <string.h>
#include "misc.h"
#include "params.h"
#include "memory.h"
#include "path.h"
static char *john_home_path = NULL;
static int john_home_length;
#if JOHN_SYSTEMWIDE
#include <unistd.h>
#include <errno.h>
#include <pwd.h>
#include <sys/types.h>
#include <sys/stat.h>
static char *user_home_path = NULL;
static int user_home_length;
#endif
void path_init(char **argv)
{
#if JOHN_SYSTEMWIDE
struct passwd *pw;
#ifdef JOHN_PRIVATE_HOME
const char *private;
#endif
#else
char *pos;
#endif
#if JOHN_SYSTEMWIDE
john_home_path = mem_alloc(PATH_BUFFER_SIZE);
strnzcpy(john_home_path, JOHN_SYSTEMWIDE_HOME "/", PATH_BUFFER_SIZE);
john_home_length = strlen(john_home_path);
if (user_home_path) return;
pw = getpwuid(getuid());
endpwent();
if (!pw) return;
user_home_length = strlen(pw->pw_dir) + 1;
if (user_home_length >= PATH_BUFFER_SIZE) return;
user_home_path = mem_alloc(PATH_BUFFER_SIZE);
memcpy(user_home_path, pw->pw_dir, user_home_length - 1);
user_home_path[user_home_length - 1] = '/';
#ifdef JOHN_PRIVATE_HOME
private = path_expand(JOHN_PRIVATE_HOME);
if (mkdir(private, S_IRUSR | S_IWUSR | S_IXUSR)) {
if (errno != EEXIST) pexit("mkdir: %s", private);
} else
fprintf(stderr, "Created directory: %s\n", private);
#endif
#else
if (argv[0])
if (!john_home_path && (pos = strrchr(argv[0], '/'))) {
john_home_length = pos - argv[0] + 1;
if (john_home_length >= PATH_BUFFER_SIZE) return;
john_home_path = mem_alloc(PATH_BUFFER_SIZE);
memcpy(john_home_path, argv[0], john_home_length);
}
#endif
}
const char *path_expand(const char *name)
{
if (!strncmp(name, "$JOHN/", 6)) {
if (john_home_path &&
john_home_length + strlen(name) - 6 < PATH_BUFFER_SIZE) {
strnzcpy(&john_home_path[john_home_length], &name[6],
PATH_BUFFER_SIZE - john_home_length);
return john_home_path;
}
return name + 6;
}
#if JOHN_SYSTEMWIDE
if (!strncmp(name, "~/", 2)) {
if (user_home_path &&
user_home_length + strlen(name) - 2 < PATH_BUFFER_SIZE) {
strnzcpy(&user_home_path[user_home_length], &name[2],
PATH_BUFFER_SIZE - user_home_length);
return user_home_path;
}
return name + 2;
}
#endif
return name;
}
char *path_session(const char *session, const char *suffix)
{
int keep, add;
char *p;
keep = strlen(session);
#ifdef __DJGPP__
if ((p = strchr(session, '.')))
keep = p - session;
#endif
if (!keep) {
fprintf(stderr, "Invalid session name requested\n");
error();
}
add = strlen(suffix) + 1;
p = mem_alloc_tiny(keep + add, MEM_ALIGN_NONE);
memcpy(p, session, keep);
memcpy(p + keep, suffix, add);
return p;
}
void path_done(void)
{
MEM_FREE(john_home_path);
#if JOHN_SYSTEMWIDE
MEM_FREE(user_home_path);
#endif
}