1 /*
2  * ProFTPD - FTP server daemon
3  * Copyright (c) 2004-2017 The ProFTPD Project team
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., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
18  *
19  * As a special exemption, The ProFTPD Project team and other respective
20  * copyright holders give permission to link this program with OpenSSL, and
21  * distribute the resulting executable, without including the source code for
22  * OpenSSL in the source distribution.
23  */
24 
25 /* HELP management code */
26 
27 #include "conf.h"
28 
29 struct help_rec {
30   const char *cmd;
31   const char *syntax;
32   int impl;
33 };
34 
35 static pool *help_pool = NULL;
36 static array_header *help_list = NULL;
37 
pr_help_add(const char * cmd,const char * syntax,int impl)38 void pr_help_add(const char *cmd, const char *syntax, int impl) {
39   struct help_rec *help;
40 
41   if (cmd == NULL ||
42       syntax == NULL) {
43     return;
44   }
45 
46   /* If no list has been allocated, create one. */
47   if (help_pool == NULL) {
48     help_pool = make_sub_pool(permanent_pool);
49     pr_pool_tag(help_pool, "Help Pool");
50     help_list = make_array(help_pool, 0, sizeof(struct help_rec));
51   }
52 
53   /* Make sure that the command being added isn't already in the list.
54    * However, if it _is_ already in the list, but it's marked as not
55    * implemented, _and_ the given impl flag is TRUE, then handle it
56    * accordingly.
57    */
58   if (help_list->nelts > 0) {
59     register unsigned int i = 0;
60     struct help_rec *helps = help_list->elts;
61 
62     for (i = 0; i < help_list->nelts; i++) {
63       if (strcmp(helps[i].cmd, cmd) == 0) {
64         if (helps[i].impl == FALSE &&
65             impl == TRUE) {
66           helps[i].impl = impl;
67         }
68 
69         return;
70       }
71     }
72   }
73 
74   help = push_array(help_list);
75   help->cmd = pstrdup(help_pool, cmd);
76   help->syntax = pstrdup(help_pool, syntax);
77   help->impl = impl;
78 }
79 
pr_help_add_response(cmd_rec * cmd,const char * target)80 int pr_help_add_response(cmd_rec *cmd, const char *target) {
81   if (cmd == NULL) {
82     errno = EINVAL;
83     return -1;
84   }
85 
86   if (help_list) {
87     register unsigned int i;
88     struct help_rec *helps = help_list->elts;
89     char *outa[8], *outstr;
90     char buf[9] = {'\0'};
91     int col = 0;
92 
93     if (target == NULL) {
94       const char *server_admin = "ftp-admin";
95 
96       pr_response_add(R_214,
97         _("The following commands are recognized (* =>'s unimplemented):"));
98 
99       memset(outa, '\0', sizeof(outa));
100 
101       for (i = 0; i < help_list->nelts; i++) {
102         outstr = "";
103 
104         if (helps[i].impl) {
105           outa[col++] = (char *) helps[i].cmd;
106 
107         } else {
108           outa[col++] = pstrcat(cmd->tmp_pool, helps[i].cmd, "*", NULL);
109         }
110 
111         /* 8 rows */
112         if ((i + 1) % 8 == 0 ||
113             (i+1 == help_list->nelts)) {
114           register unsigned int j;
115 
116           for (j = 0; j < 8; j++) {
117             if (outa[j]) {
118               pr_snprintf(buf, sizeof(buf), "%-8s", outa[j]);
119               buf[sizeof(buf)-1] = '\0';
120               outstr = pstrcat(cmd->tmp_pool, outstr, buf, NULL);
121 
122             } else {
123               break;
124             }
125           }
126 
127           if (*outstr) {
128             pr_response_add(R_DUP, "%s", outstr);
129           }
130 
131           memset(outa, '\0', sizeof(outa));
132           col = 0;
133         }
134       }
135 
136       if (cmd->server != NULL &&
137           cmd->server->ServerAdmin != NULL) {
138         server_admin = cmd->server->ServerAdmin;
139       }
140 
141       pr_response_add(R_DUP, _("Direct comments to %s"), server_admin);
142       return 0;
143     }
144 
145     /* List the syntax for the given target command. */
146     for (i = 0; i < help_list->nelts; i++) {
147       if (strcasecmp(helps[i].cmd, target) == 0) {
148         pr_response_add(R_214, "Syntax: %s %s", helps[i].cmd,
149           helps[i].syntax);
150         return 0;
151       }
152     }
153   }
154 
155   errno = ENOENT;
156   return -1;
157 }
158