1 // Copyright (C) 1999,2000 Bruce Guenter <bruceg@em.ca>
2 //
3 // This program is free software; you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation; either version 2 of the License, or
6 // (at your option) any later version.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16
17 #include <config.h>
18 #include "daemon.h"
19 #include "misc/lookup.h"
20
21 // declare the commands
22 extern CMD(adduser2);
23 extern CMD(adduser3);
24 extern CMD(autoresponse);
25 extern CMD(chattr);
26 extern CMD(check);
27 extern CMD(deluser);
28 extern CMD(listdomain);
29 extern CMD(lookup);
30 extern CMD(stat);
31
32 #ifdef TEST_DAEMON
CMD(echo)33 CMD(echo)
34 {
35 logcommand(args);
36 mystring msg;
37 for(unsigned i = 0; i < args.count(); i++) {
38 if(i > 0) msg += " ";
39 msg += args[i];
40 }
41 RETURN(ok, msg);
42 }
CMD(fecho)43 CMD(fecho)
44 {
45 logcommand(args);
46 mystring msg;
47 for(unsigned i = 0; i < args.count(); i++) {
48 if(i > 0) msg += " ";
49 msg += args[i];
50 }
51 RETURN(ok, msg);
52 }
53 #endif // TEST_DAEMON
54
55 //CMD(stat);
56 //CMD(statall);
57
58 struct dispatch
59 {
60 const char* name;
61 response (*function)(command&, int);
62 unsigned arg_min;
63 unsigned arg_max;
64 bool decode_virtual;
65 //unsigned count;
66 };
67
68 #define ENTRY(NAME,MIN,MAX,DV) { #NAME , NAME##_cmd , unsigned(MIN), unsigned(MAX), DV }
69 dispatch dispatch_table[] = {
70 ENTRY(lookup, 3, 3, true),
71 ENTRY(check, 3, 3, true),
72 ENTRY(chattr, 5, -1, true),
73 ENTRY(adduser2, 4, -1, true),
74 ENTRY(adduser3, 5, -1, true),
75 ENTRY(autoresponse, 4, 5, true),
76 ENTRY(deluser, 3, 3, true),
77 ENTRY(stat, 3, 3, true),
78 ENTRY(listdomain, 2, 2, false),
79 #ifdef TEST_DAEMON
80 ENTRY(echo, 0, -1, false), // For testing purposes only
81 ENTRY(fecho, 0, -1, false), // For testing purposes only
82 #endif // TEST_DAEMON
83 { "", 0, 0, false, 0 }
84 };
85 //ENTRY(stat, 1, , false),
86 //ENTRY(statall, 0, 0, false),
87
find_dispatch(mystring name)88 static dispatch* find_dispatch(mystring name)
89 {
90 for(dispatch* ptr = dispatch_table; ptr->function != 0; ptr++) {
91 if(name == ptr->name)
92 return ptr;
93 }
94 return 0;
95 }
96
97 #if 0
98 CMD(stat)
99 // Usage: stat function
100 // Returns: count
101 {
102 logcommand(args);
103 const dispatch* ptr = find_dispatch(args[0]);
104 if(!ptr)
105 RETURN(err, "Unknown operation to stat");
106 RETURN(ok, itoa(ptr->count));
107 }
108
109 CMD(statall)
110 // Usage: statall
111 // Returns: name:count;name:count...
112 {
113 logcommand(args);
114 mystring r;
115 for(const dispatch* ptr = dispatch_table; ptr->function != 0; ptr++) {
116 if(!r.empty())
117 r += ";";
118 r += ptr->name;
119 r += ":";
120 r += itoa(ptr->count);
121 }
122 RETURN(ok, r);
123 }
124 #endif
125
dispatch_cmd(command & args,int fd)126 response dispatch_cmd(command& args, int fd)
127 {
128 dispatch* ptr = find_dispatch(args.name());
129 if(ptr) {
130 if(args.count() < ptr->arg_min ||
131 (ptr->arg_max != unsigned(-1) && args.count() > ptr->arg_max))
132 RETURN(bad, "Incorrect number of parameters to command " + args.name());
133 //++ptr->count;
134 if(ptr->decode_virtual) {
135 if(is_local(args[0]))
136 args.replace_first_two(args[1]);
137 else {
138 mystring baseuser(find_virtual(args[0]));
139 if(baseuser.empty())
140 RETURN(err, "Invalid or unknown domain name: " + args[0]);
141 else if(!args[1])
142 args.replace_first_two(baseuser);
143 else
144 args.replace_first_two(baseuser + "-" + args[1]);
145 }
146 }
147 return ptr->function(args, fd);
148 }
149 else
150 RETURN(bad, "Invalid operation");
151 }
152