AllCommands.cxx 13.5 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 The Music Player Daemon Project
3
 * http://www.musicpd.org
Warren Dukes's avatar
Warren Dukes committed
4 5 6 7 8 9 10 11 12 13
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
14 15 16 17
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Warren Dukes's avatar
Warren Dukes committed
18 19
 */

20
#include "config.h"
Max Kellermann's avatar
Max Kellermann committed
21
#include "AllCommands.hxx"
22
#include "CommandError.hxx"
23
#include "Request.hxx"
24
#include "QueueCommands.hxx"
25
#include "TagCommands.hxx"
26 27
#include "PlayerCommands.hxx"
#include "PlaylistCommands.hxx"
28
#include "StorageCommands.hxx"
29
#include "DatabaseCommands.hxx"
30
#include "FileCommands.hxx"
31 32
#include "OutputCommands.hxx"
#include "MessageCommands.hxx"
33
#include "NeighborCommands.hxx"
34
#include "ClientCommands.hxx"
35
#include "PartitionCommands.hxx"
36
#include "OtherCommands.hxx"
37
#include "Permission.hxx"
38
#include "tag/Type.h"
39
#include "Partition.hxx"
40
#include "client/Client.hxx"
41
#include "client/Response.hxx"
42
#include "util/Macros.hxx"
43
#include "util/Tokenizer.hxx"
44
#include "util/StringAPI.hxx"
Warren Dukes's avatar
Warren Dukes committed
45

46
#ifdef ENABLE_SQLITE
Max Kellermann's avatar
Max Kellermann committed
47
#include "StickerCommands.hxx"
48
#include "sticker/StickerDatabase.hxx"
49 50
#endif

Max Kellermann's avatar
Max Kellermann committed
51
#include <assert.h>
52
#include <string.h>
Warren Dukes's avatar
Warren Dukes committed
53

54 55 56 57 58 59 60
/*
 * The most we ever use is for search/find, and that limits it to the
 * number of tags we can have.  Add one for the command, and one extra
 * to catch errors clients may send us
 */
#define COMMAND_ARGV_MAX	(2+(TAG_NUM_OF_ITEM_TYPES*2))

Warren Dukes's avatar
Warren Dukes committed
61 62
/* if min: -1 don't check args *
 * if max: -1 no max args      */
63
struct command {
Max Kellermann's avatar
Max Kellermann committed
64
	const char *cmd;
Max Kellermann's avatar
Max Kellermann committed
65
	unsigned permission;
Avuton Olrich's avatar
Avuton Olrich committed
66 67
	int min;
	int max;
68
	CommandResult (*handler)(Client &client, Request request, Response &response);
69 70
};

71
/* don't be fooled, this is the command handler for "commands" command */
72
static CommandResult
73
handle_commands(Client &client, Request request, Response &response);
74

75
static CommandResult
76
handle_not_commands(Client &client, Request request, Response &response);
77

78 79 80 81 82
/**
 * The command registry.
 *
 * This array must be sorted!
 */
83
static constexpr struct command commands[] = {
Max Kellermann's avatar
Max Kellermann committed
84 85
	{ "add", PERMISSION_ADD, 1, 1, handle_add },
	{ "addid", PERMISSION_ADD, 1, 2, handle_addid },
86
	{ "addtagid", PERMISSION_ADD, 3, 3, handle_addtagid },
87
	{ "channels", PERMISSION_READ, 0, 0, handle_channels },
Max Kellermann's avatar
Max Kellermann committed
88 89
	{ "clear", PERMISSION_CONTROL, 0, 0, handle_clear },
	{ "clearerror", PERMISSION_CONTROL, 0, 0, handle_clearerror },
90
	{ "cleartagid", PERMISSION_ADD, 1, 2, handle_cleartagid },
Max Kellermann's avatar
Max Kellermann committed
91 92
	{ "close", PERMISSION_NONE, -1, -1, handle_close },
	{ "commands", PERMISSION_NONE, 0, 0, handle_commands },
93
	{ "config", PERMISSION_ADMIN, 0, 0, handle_config },
94
	{ "consume", PERMISSION_CONTROL, 1, 1, handle_consume },
95
#ifdef ENABLE_DATABASE
Max Kellermann's avatar
Max Kellermann committed
96
	{ "count", PERMISSION_READ, 2, -1, handle_count },
97
#endif
Max Kellermann's avatar
Max Kellermann committed
98 99
	{ "crossfade", PERMISSION_CONTROL, 1, 1, handle_crossfade },
	{ "currentsong", PERMISSION_READ, 0, 0, handle_currentsong },
100
	{ "decoders", PERMISSION_READ, 0, 0, handle_decoders },
Max Kellermann's avatar
Max Kellermann committed
101 102 103 104
	{ "delete", PERMISSION_CONTROL, 1, 1, handle_delete },
	{ "deleteid", PERMISSION_CONTROL, 1, 1, handle_deleteid },
	{ "disableoutput", PERMISSION_ADMIN, 1, 1, handle_disableoutput },
	{ "enableoutput", PERMISSION_ADMIN, 1, 1, handle_enableoutput },
105
#ifdef ENABLE_DATABASE
Max Kellermann's avatar
Max Kellermann committed
106
	{ "find", PERMISSION_READ, 2, -1, handle_find },
107
	{ "findadd", PERMISSION_ADD, 2, -1, handle_findadd},
108
#endif
109
	{ "idle", PERMISSION_READ, 0, -1, handle_idle },
Max Kellermann's avatar
Max Kellermann committed
110
	{ "kill", PERMISSION_ADMIN, -1, -1, handle_kill },
111
#ifdef ENABLE_DATABASE
Max Kellermann's avatar
Max Kellermann committed
112 113 114
	{ "list", PERMISSION_READ, 1, -1, handle_list },
	{ "listall", PERMISSION_READ, 0, 1, handle_listall },
	{ "listallinfo", PERMISSION_READ, 0, 1, handle_listallinfo },
115 116 117
#endif
	{ "listfiles", PERMISSION_READ, 0, 1, handle_listfiles },
#ifdef ENABLE_DATABASE
118
	{ "listmounts", PERMISSION_READ, 0, 0, handle_listmounts },
119
#endif
120 121 122
#ifdef ENABLE_NEIGHBOR_PLUGINS
	{ "listneighbors", PERMISSION_READ, 0, 0, handle_listneighbors },
#endif
123
	{ "listpartitions", PERMISSION_READ, 0, 0, handle_listpartitions },
Max Kellermann's avatar
Max Kellermann committed
124 125 126
	{ "listplaylist", PERMISSION_READ, 1, 1, handle_listplaylist },
	{ "listplaylistinfo", PERMISSION_READ, 1, 1, handle_listplaylistinfo },
	{ "listplaylists", PERMISSION_READ, 0, 0, handle_listplaylists },
127
	{ "load", PERMISSION_ADD, 1, 2, handle_load },
Max Kellermann's avatar
Max Kellermann committed
128
	{ "lsinfo", PERMISSION_READ, 0, 1, handle_lsinfo },
129 130
	{ "mixrampdb", PERMISSION_CONTROL, 1, 1, handle_mixrampdb },
	{ "mixrampdelay", PERMISSION_CONTROL, 1, 1, handle_mixrampdelay },
131 132 133
#ifdef ENABLE_DATABASE
	{ "mount", PERMISSION_ADMIN, 2, 2, handle_mount },
#endif
Max Kellermann's avatar
Max Kellermann committed
134 135
	{ "move", PERMISSION_CONTROL, 2, 2, handle_move },
	{ "moveid", PERMISSION_CONTROL, 2, 2, handle_moveid },
136
	{ "newpartition", PERMISSION_ADMIN, 1, 1, handle_newpartition },
Max Kellermann's avatar
Max Kellermann committed
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
	{ "next", PERMISSION_CONTROL, 0, 0, handle_next },
	{ "notcommands", PERMISSION_NONE, 0, 0, handle_not_commands },
	{ "outputs", PERMISSION_READ, 0, 0, handle_devices },
	{ "password", PERMISSION_NONE, 1, 1, handle_password },
	{ "pause", PERMISSION_CONTROL, 0, 1, handle_pause },
	{ "ping", PERMISSION_NONE, 0, 0, handle_ping },
	{ "play", PERMISSION_CONTROL, 0, 1, handle_play },
	{ "playid", PERMISSION_CONTROL, 0, 1, handle_playid },
	{ "playlist", PERMISSION_READ, 0, 0, handle_playlist },
	{ "playlistadd", PERMISSION_CONTROL, 2, 2, handle_playlistadd },
	{ "playlistclear", PERMISSION_CONTROL, 1, 1, handle_playlistclear },
	{ "playlistdelete", PERMISSION_CONTROL, 2, 2, handle_playlistdelete },
	{ "playlistfind", PERMISSION_READ, 2, -1, handle_playlistfind },
	{ "playlistid", PERMISSION_READ, 0, 1, handle_playlistid },
	{ "playlistinfo", PERMISSION_READ, 0, 1, handle_playlistinfo },
	{ "playlistmove", PERMISSION_CONTROL, 3, 3, handle_playlistmove },
	{ "playlistsearch", PERMISSION_READ, 2, -1, handle_playlistsearch },
154 155
	{ "plchanges", PERMISSION_READ, 1, 2, handle_plchanges },
	{ "plchangesposid", PERMISSION_READ, 1, 2, handle_plchangesposid },
Max Kellermann's avatar
Max Kellermann committed
156
	{ "previous", PERMISSION_CONTROL, 0, 0, handle_previous },
157 158
	{ "prio", PERMISSION_CONTROL, 2, -1, handle_prio },
	{ "prioid", PERMISSION_CONTROL, 2, -1, handle_prioid },
Max Kellermann's avatar
Max Kellermann committed
159
	{ "random", PERMISSION_CONTROL, 1, 1, handle_random },
160
	{ "rangeid", PERMISSION_ADD, 2, 2, handle_rangeid },
161
	{ "readcomments", PERMISSION_READ, 1, 1, handle_read_comments },
162
	{ "readmessages", PERMISSION_READ, 0, 0, handle_read_messages },
Max Kellermann's avatar
Max Kellermann committed
163 164
	{ "rename", PERMISSION_CONTROL, 2, 2, handle_rename },
	{ "repeat", PERMISSION_CONTROL, 1, 1, handle_repeat },
165 166 167 168
	{ "replay_gain_mode", PERMISSION_CONTROL, 1, 1,
	  handle_replay_gain_mode },
	{ "replay_gain_status", PERMISSION_READ, 0, 0,
	  handle_replay_gain_status },
169
	{ "rescan", PERMISSION_CONTROL, 0, 1, handle_rescan },
Max Kellermann's avatar
Max Kellermann committed
170 171
	{ "rm", PERMISSION_CONTROL, 1, 1, handle_rm },
	{ "save", PERMISSION_CONTROL, 1, 1, handle_save },
172
#ifdef ENABLE_DATABASE
Max Kellermann's avatar
Max Kellermann committed
173
	{ "search", PERMISSION_READ, 2, -1, handle_search },
174 175
	{ "searchadd", PERMISSION_ADD, 2, -1, handle_searchadd },
	{ "searchaddpl", PERMISSION_CONTROL, 3, -1, handle_searchaddpl },
176
#endif
Max Kellermann's avatar
Max Kellermann committed
177
	{ "seek", PERMISSION_CONTROL, 2, 2, handle_seek },
178
	{ "seekcur", PERMISSION_CONTROL, 1, 1, handle_seekcur },
Max Kellermann's avatar
Max Kellermann committed
179
	{ "seekid", PERMISSION_CONTROL, 2, 2, handle_seekid },
180
	{ "sendmessage", PERMISSION_CONTROL, 2, 2, handle_send_message },
Max Kellermann's avatar
Max Kellermann committed
181
	{ "setvol", PERMISSION_CONTROL, 1, 1, handle_setvol },
182
	{ "shuffle", PERMISSION_CONTROL, 0, 1, handle_shuffle },
183
	{ "single", PERMISSION_CONTROL, 1, 1, handle_single },
Max Kellermann's avatar
Max Kellermann committed
184 185
	{ "stats", PERMISSION_READ, 0, 0, handle_stats },
	{ "status", PERMISSION_READ, 0, 0, handle_status },
186 187 188
#ifdef ENABLE_SQLITE
	{ "sticker", PERMISSION_ADMIN, 3, -1, handle_sticker },
#endif
Max Kellermann's avatar
Max Kellermann committed
189
	{ "stop", PERMISSION_CONTROL, 0, 0, handle_stop },
190
	{ "subscribe", PERMISSION_READ, 1, 1, handle_subscribe },
Max Kellermann's avatar
Max Kellermann committed
191 192
	{ "swap", PERMISSION_CONTROL, 2, 2, handle_swap },
	{ "swapid", PERMISSION_CONTROL, 2, 2, handle_swapid },
193
	{ "tagtypes", PERMISSION_READ, 0, -1, handle_tagtypes },
194
	{ "toggleoutput", PERMISSION_ADMIN, 1, 1, handle_toggleoutput },
195 196 197
#ifdef ENABLE_DATABASE
	{ "unmount", PERMISSION_ADMIN, 1, 1, handle_unmount },
#endif
198
	{ "unsubscribe", PERMISSION_READ, 1, 1, handle_unsubscribe },
199
	{ "update", PERMISSION_CONTROL, 0, 1, handle_update },
Max Kellermann's avatar
Max Kellermann committed
200
	{ "urlhandlers", PERMISSION_READ, 0, 0, handle_urlhandlers },
201
	{ "volume", PERMISSION_CONTROL, 1, 1, handle_volume },
202 203
};

204
static constexpr unsigned num_commands = ARRAY_SIZE(commands);
205

206
static bool
207 208
command_available(gcc_unused const Partition &partition,
		  gcc_unused const struct command *cmd)
209 210
{
#ifdef ENABLE_SQLITE
211
	if (StringIsEqual(cmd->cmd, "sticker"))
212 213 214
		return sticker_enabled();
#endif

215
#ifdef ENABLE_NEIGHBOR_PLUGINS
216
	if (StringIsEqual(cmd->cmd, "listneighbors"))
217
		return neighbor_commands_available(partition.instance);
218 219
#endif

220 221 222 223 224 225 226 227
	if (StringIsEqual(cmd->cmd, "save") ||
	    StringIsEqual(cmd->cmd, "rm") ||
	    StringIsEqual(cmd->cmd, "rename") ||
	    StringIsEqual(cmd->cmd, "playlistdelete") ||
	    StringIsEqual(cmd->cmd, "playlistmove") ||
	    StringIsEqual(cmd->cmd, "playlistclear") ||
	    StringIsEqual(cmd->cmd, "playlistadd") ||
	    StringIsEqual(cmd->cmd, "listplaylists"))
228 229
		return playlist_commands_available();

230 231 232
	return true;
}

233
static CommandResult
234 235
PrintAvailableCommands(Response &r, const Partition &partition,
		     unsigned permission)
236 237
{
	for (unsigned i = 0; i < num_commands; ++i) {
238
		const struct command *cmd = &commands[i];
239

240
		if (cmd->permission == (permission & cmd->permission) &&
241 242
		    command_available(partition, cmd))
			r.Format("command: %s\n", cmd->cmd);
243 244
	}

245
	return CommandResult::OK;
246 247
}

248
static CommandResult
249
PrintUnavailableCommands(Response &r, unsigned permission)
250 251
{
	for (unsigned i = 0; i < num_commands; ++i) {
252
		const struct command *cmd = &commands[i];
253

Max Kellermann's avatar
Max Kellermann committed
254
		if (cmd->permission != (permission & cmd->permission))
255
			r.Format("command: %s\n", cmd->cmd);
256 257
	}

258
	return CommandResult::OK;
259 260
}

261 262
/* don't be fooled, this is the command handler for "commands" command */
static CommandResult
263
handle_commands(Client &client, gcc_unused Request request, Response &r)
264
{
265
	return PrintAvailableCommands(r, client.GetPartition(),
266 267 268 269
				      client.GetPermission());
}

static CommandResult
270
handle_not_commands(Client &client, gcc_unused Request request, Response &r)
271 272 273 274
{
	return PrintUnavailableCommands(r, client.GetPermission());
}

275 276
void
command_init()
Avuton Olrich's avatar
Avuton Olrich committed
277
{
278 279 280 281 282
#ifndef NDEBUG
	/* ensure that the command list is sorted */
	for (unsigned i = 0; i < num_commands - 1; ++i)
		assert(strcmp(commands[i].cmd, commands[i + 1].cmd) < 0);
#endif
Avuton Olrich's avatar
Avuton Olrich committed
283 284
}

285 286
void
command_finish()
Avuton Olrich's avatar
Avuton Olrich committed
287
{
288 289 290 291 292 293 294 295 296 297 298
}

static const struct command *
command_lookup(const char *name)
{
	unsigned a = 0, b = num_commands, i;

	/* binary search */
	do {
		i = (a + b) / 2;

299
		const auto cmp = strcmp(name, commands[i].cmd);
300 301 302 303 304 305 306 307
		if (cmp == 0)
			return &commands[i];
		else if (cmp < 0)
			b = i;
		else if (cmp > 0)
			a = i + 1;
	} while (a < b);

308
	return nullptr;
Avuton Olrich's avatar
Avuton Olrich committed
309 310
}

311
static bool
312
command_check_request(const struct command *cmd, Response &r,
313
		      unsigned permission, Request args)
Avuton Olrich's avatar
Avuton Olrich committed
314
{
Max Kellermann's avatar
Max Kellermann committed
315
	if (cmd->permission != (permission & cmd->permission)) {
316
		r.FormatError(ACK_ERROR_PERMISSION,
317 318
			      "you don't have permission for \"%s\"",
			      cmd->cmd);
319
		return false;
Warren Dukes's avatar
Warren Dukes committed
320 321
	}

322 323 324 325
	const int min = cmd->min;
	const int max = cmd->max;

	if (min < 0)
326
		return true;
Warren Dukes's avatar
Warren Dukes committed
327

328
	if (min == max && unsigned(max) != args.size) {
329
		r.FormatError(ACK_ERROR_ARG,
330
			      "wrong number of arguments for \"%s\"",
331
			      cmd->cmd);
332
		return false;
333
	} else if (args.size < unsigned(min)) {
334
		r.FormatError(ACK_ERROR_ARG,
335
			      "too few arguments for \"%s\"", cmd->cmd);
336
		return false;
337
	} else if (max >= 0 && args.size > unsigned(max)) {
338
		r.FormatError(ACK_ERROR_ARG,
339
			      "too many arguments for \"%s\"", cmd->cmd);
340
		return false;
Avuton Olrich's avatar
Avuton Olrich committed
341
	} else
342
		return true;
Warren Dukes's avatar
Warren Dukes committed
343 344
}

345
static const struct command *
346
command_checked_lookup(Response &r, unsigned permission,
347
		       const char *cmd_name, Request args)
Warren Dukes's avatar
Warren Dukes committed
348
{
349
	const struct command *cmd = command_lookup(cmd_name);
350
	if (cmd == nullptr) {
351
		r.FormatError(ACK_ERROR_UNKNOWN,
352
			      "unknown command \"%s\"", cmd_name);
353
		return nullptr;
Avuton Olrich's avatar
Avuton Olrich committed
354
	}
Warren Dukes's avatar
Warren Dukes committed
355

356
	r.SetCommand(cmd->cmd);
Warren Dukes's avatar
Warren Dukes committed
357

358
	if (!command_check_request(cmd, r, permission, args))
359
		return nullptr;
360 361 362 363

	return cmd;
}

364
CommandResult
365
command_process(Client &client, unsigned num, char *line)
366
try {
367
	Response r(client, num);
368

369
	/* get the command name (first word on the line) */
370
	/* we have to set current_command because Response::Error()
371
	   expects it to be set */
372

373
	Tokenizer tokenizer(line);
374

375 376 377 378 379 380 381 382 383 384 385
	const char *cmd_name;
	try {
		cmd_name = tokenizer.NextWord();
		if (cmd_name == nullptr) {
			r.Error(ACK_ERROR_UNKNOWN, "No command given");
			/* this client does not speak the MPD
			   protocol; kick the connection */
			return CommandResult::FINISH;
		}
	} catch (const std::exception &e) {
		r.Error(ACK_ERROR_UNKNOWN, e.what());
386 387 388
		/* this client does not speak the MPD protocol; kick
		   the connection */
		return CommandResult::FINISH;
389 390
	}

391
	char *argv[COMMAND_ARGV_MAX];
392
	Request args(argv, 0);
393 394 395

	/* now parse the arguments (quoted or unquoted) */

396
	while (true) {
397
		if (args.size == COMMAND_ARGV_MAX) {
398
			r.Error(ACK_ERROR_ARG, "Too many arguments");
399 400 401
			return CommandResult::ERROR;
		}

402 403 404
		char *a = tokenizer.NextParam();
		if (a == nullptr)
			break;
405

406
		argv[args.size++] = a;
407 408 409
	}

	/* look up and invoke the command handler */
410

411
	const struct command *cmd =
412
		command_checked_lookup(r, client.GetPermission(),
413
				       cmd_name, args);
414

415 416 417
	CommandResult ret = cmd
		? cmd->handler(client, args, r)
		: CommandResult::ERROR;
Warren Dukes's avatar
Warren Dukes committed
418

419 420 421
	return ret;
} catch (const std::exception &e) {
	Response r(client, num);
422
	PrintError(r, std::current_exception());
423
	return CommandResult::ERROR;
Warren Dukes's avatar
Warren Dukes committed
424
}