AllCommands.cxx 13.9 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2019 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 "FingerprintCommands.hxx"
37
#include "OtherCommands.hxx"
38
#include "Permission.hxx"
39
#include "tag/Type.h"
40
#include "Partition.hxx"
41
#include "Instance.hxx"
42
#include "client/Client.hxx"
43
#include "client/Response.hxx"
44
#include "util/Tokenizer.hxx"
45
#include "util/StringAPI.hxx"
Warren Dukes's avatar
Warren Dukes committed
46

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

51 52
#include <iterator>

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

56 57 58 59 60 61 62
/*
 * 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
63 64
/* if min: -1 don't check args *
 * if max: -1 no max args      */
65
struct command {
Max Kellermann's avatar
Max Kellermann committed
66
	const char *cmd;
Max Kellermann's avatar
Max Kellermann committed
67
	unsigned permission;
Avuton Olrich's avatar
Avuton Olrich committed
68 69
	int min;
	int max;
70
	CommandResult (*handler)(Client &client, Request request, Response &response);
71 72
};

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

77
static CommandResult
78
handle_not_commands(Client &client, Request request, Response &response);
79

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

213
static constexpr unsigned num_commands = std::size(commands);
214

215
static bool
216 217
command_available(gcc_unused const Partition &partition,
		  gcc_unused const struct command *cmd)
218 219
{
#ifdef ENABLE_SQLITE
220
	if (StringIsEqual(cmd->cmd, "sticker"))
221
		return partition.instance.HasStickerDatabase();
222 223
#endif

224
#ifdef ENABLE_NEIGHBOR_PLUGINS
225
	if (StringIsEqual(cmd->cmd, "listneighbors"))
226
		return neighbor_commands_available(partition.instance);
227 228
#endif

229 230 231 232 233 234 235 236
	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"))
237 238
		return playlist_commands_available();

239 240 241
	return true;
}

242
static CommandResult
243 244
PrintAvailableCommands(Response &r, const Partition &partition,
		     unsigned permission)
245 246
{
	for (unsigned i = 0; i < num_commands; ++i) {
247
		const struct command *cmd = &commands[i];
248

249
		if (cmd->permission == (permission & cmd->permission) &&
250 251
		    command_available(partition, cmd))
			r.Format("command: %s\n", cmd->cmd);
252 253
	}

254
	return CommandResult::OK;
255 256
}

257
static CommandResult
258
PrintUnavailableCommands(Response &r, unsigned permission)
259 260
{
	for (unsigned i = 0; i < num_commands; ++i) {
261
		const struct command *cmd = &commands[i];
262

Max Kellermann's avatar
Max Kellermann committed
263
		if (cmd->permission != (permission & cmd->permission))
264
			r.Format("command: %s\n", cmd->cmd);
265 266
	}

267
	return CommandResult::OK;
268 269
}

270 271
/* don't be fooled, this is the command handler for "commands" command */
static CommandResult
272
handle_commands(Client &client, gcc_unused Request request, Response &r)
273
{
274
	return PrintAvailableCommands(r, client.GetPartition(),
275 276 277 278
				      client.GetPermission());
}

static CommandResult
279
handle_not_commands(Client &client, gcc_unused Request request, Response &r)
280 281 282 283
{
	return PrintUnavailableCommands(r, client.GetPermission());
}

284 285
void
command_init()
Avuton Olrich's avatar
Avuton Olrich committed
286
{
287 288 289 290 291
#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
292 293
}

294 295 296 297 298 299 300 301 302
static const struct command *
command_lookup(const char *name)
{
	unsigned a = 0, b = num_commands, i;

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

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

312
	return nullptr;
Avuton Olrich's avatar
Avuton Olrich committed
313 314
}

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

326 327 328 329
	const int min = cmd->min;
	const int max = cmd->max;

	if (min < 0)
330
		return true;
Warren Dukes's avatar
Warren Dukes committed
331

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

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

360
	r.SetCommand(cmd->cmd);
Warren Dukes's avatar
Warren Dukes committed
361

362
	if (!command_check_request(cmd, r, permission, args))
363
		return nullptr;
364 365 366 367

	return cmd;
}

368
CommandResult
369
command_process(Client &client, unsigned num, char *line)
370
try {
371
	Response r(client, num);
372

373
	/* get the command name (first word on the line) */
374
	/* we have to set current_command because Response::Error()
375
	   expects it to be set */
376

377
	Tokenizer tokenizer(line);
378

379 380 381 382 383 384 385 386 387 388 389
	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());
390 391 392
		/* this client does not speak the MPD protocol; kick
		   the connection */
		return CommandResult::FINISH;
393 394
	}

395
	char *argv[COMMAND_ARGV_MAX];
396
	Request args(argv, 0);
397 398 399

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

400
	while (true) {
401
		if (args.size == COMMAND_ARGV_MAX) {
402
			r.Error(ACK_ERROR_ARG, "Too many arguments");
403 404 405
			return CommandResult::ERROR;
		}

406 407 408
		char *a = tokenizer.NextParam();
		if (a == nullptr)
			break;
409

410
		argv[args.size++] = a;
411 412 413
	}

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

415
	const struct command *cmd =
416
		command_checked_lookup(r, client.GetPermission(),
417
				       cmd_name, args);
418

419 420 421
	CommandResult ret = cmd
		? cmd->handler(client, args, r)
		: CommandResult::ERROR;
Warren Dukes's avatar
Warren Dukes committed
422

423 424 425
	return ret;
} catch (const std::exception &e) {
	Response r(client, num);
426
	PrintError(r, std::current_exception());
427
	return CommandResult::ERROR;
Warren Dukes's avatar
Warren Dukes committed
428
}