1 /*
2  * ProFTPD: mod_vroot Alias API
3  * Copyright (c) 2002-2016 TJ Saunders
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 Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307, USA.
18  *
19  * As a special exemption, TJ Saunders and other respective copyright holders
20  * give permission to link this program with OpenSSL, and distribute the
21  * resulting executable, without including the source code for OpenSSL in the
22  * source distribution.
23  */
24 
25 #include "alias.h"
26 
27 static pool *alias_pool = NULL;
28 static pr_table_t *alias_tab = NULL;
29 
vroot_alias_count(void)30 unsigned int vroot_alias_count(void) {
31   int count;
32 
33   count = pr_table_count(alias_tab);
34   if (count < 0) {
35     return 0;
36   }
37 
38   return count;
39 }
40 
vroot_alias_do(int cb (const void * key_data,size_t key_datasz,const void * value_data,size_t value_datasz,void * user_data),void * user_data)41 int vroot_alias_do(int cb(const void *key_data, size_t key_datasz,
42     const void *value_data, size_t value_datasz, void *user_data),
43     void *user_data) {
44   int res;
45 
46   if (cb == NULL) {
47     errno = EINVAL;
48     return -1;
49   }
50 
51   res = pr_table_do(alias_tab, cb, user_data, PR_TABLE_DO_FL_ALL);
52   return res;
53 }
54 
vroot_alias_exists(const char * path)55 int vroot_alias_exists(const char *path) {
56   if (path == NULL) {
57     return FALSE;
58   }
59 
60   if (pr_table_get(alias_tab, path, 0) != NULL) {
61     return TRUE;
62   }
63 
64   return FALSE;
65 }
66 
vroot_alias_get(const char * path)67 const char *vroot_alias_get(const char *path) {
68   const void *v;
69 
70   if (path == NULL) {
71     errno = EINVAL;
72     return NULL;
73   }
74 
75   v = pr_table_get(alias_tab, path, NULL);
76   return v;
77 }
78 
vroot_alias_add(const char * dst_path,const char * src_path)79 int vroot_alias_add(const char *dst_path, const char *src_path) {
80   int res;
81 
82   if (dst_path == NULL ||
83       src_path == NULL) {
84     errno = EINVAL;
85     return -1;
86   }
87 
88   res = pr_table_add(alias_tab, pstrdup(alias_pool, dst_path),
89     pstrdup(alias_pool, src_path), 0);
90   return res;
91 }
92 
vroot_alias_init(pool * p)93 int vroot_alias_init(pool *p) {
94   if (p == NULL) {
95     errno = EINVAL;
96     return -1;
97   }
98 
99   if (alias_pool == NULL) {
100     alias_pool = make_sub_pool(p);
101     pr_pool_tag(alias_pool, "VRoot Alias Pool");
102 
103     alias_tab = pr_table_alloc(alias_pool, 0);
104   }
105 
106   return 0;
107 }
108 
vroot_alias_free(void)109 int vroot_alias_free(void) {
110   if (alias_pool != NULL) {
111     pr_table_empty(alias_tab);
112     destroy_pool(alias_pool);
113     alias_pool = NULL;
114     alias_tab = NULL;
115   }
116 
117   return 0;
118 }
119