permission.c 3.45 KB
Newer Older
1
/* the Music Player Daemon (MPD)
2
 * Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com)
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * This project's homepage is: http://www.musicpd.org
 *
 * 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.
 * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

Warren Dukes's avatar
Warren Dukes committed
19 20 21 22
#include "permission.h"
#include "conf.h"
#include "log.h"

23
#include <glib.h>
Max Kellermann's avatar
Max Kellermann committed
24

25
#include <stdbool.h>
Max Kellermann's avatar
Max Kellermann committed
26
#include <string.h>
27

28
#define PERMISSION_PASSWORD_CHAR	'@'
Warren Dukes's avatar
Warren Dukes committed
29 30 31 32 33 34 35
#define PERMISSION_SEPERATOR		","

#define PERMISSION_READ_STRING		"read"
#define PERMISSION_ADD_STRING		"add"
#define PERMISSION_CONTROL_STRING	"control"
#define PERMISSION_ADMIN_STRING		"admin"

36
static GHashTable *permission_passwords;
Warren Dukes's avatar
Warren Dukes committed
37

38
static unsigned permission_default;
Warren Dukes's avatar
Warren Dukes committed
39

40
static unsigned parsePermissions(const char *string)
Avuton Olrich's avatar
Avuton Olrich committed
41
{
42
	unsigned permission = 0;
43
	gchar **tokens;
Warren Dukes's avatar
Warren Dukes committed
44

Avuton Olrich's avatar
Avuton Olrich committed
45 46
	if (!string)
		return 0;
Warren Dukes's avatar
Warren Dukes committed
47

48 49 50 51
	tokens = g_strsplit(string, PERMISSION_SEPERATOR, 0);
	for (unsigned i = 0; tokens[i] != NULL; ++i) {
		char *temp = tokens[i];

Avuton Olrich's avatar
Avuton Olrich committed
52
		if (strcmp(temp, PERMISSION_READ_STRING) == 0) {
Warren Dukes's avatar
Warren Dukes committed
53
			permission |= PERMISSION_READ;
Avuton Olrich's avatar
Avuton Olrich committed
54
		} else if (strcmp(temp, PERMISSION_ADD_STRING) == 0) {
Warren Dukes's avatar
Warren Dukes committed
55
			permission |= PERMISSION_ADD;
Avuton Olrich's avatar
Avuton Olrich committed
56
		} else if (strcmp(temp, PERMISSION_CONTROL_STRING) == 0) {
Warren Dukes's avatar
Warren Dukes committed
57
			permission |= PERMISSION_CONTROL;
Avuton Olrich's avatar
Avuton Olrich committed
58
		} else if (strcmp(temp, PERMISSION_ADMIN_STRING) == 0) {
Warren Dukes's avatar
Warren Dukes committed
59
			permission |= PERMISSION_ADMIN;
Avuton Olrich's avatar
Avuton Olrich committed
60
		} else {
61
			FATAL("unknown permission \"%s\"\n", temp);
Warren Dukes's avatar
Warren Dukes committed
62 63 64
		}
	}

65 66
	g_strfreev(tokens);

Warren Dukes's avatar
Warren Dukes committed
67 68 69
	return permission;
}

Avuton Olrich's avatar
Avuton Olrich committed
70 71 72
void initPermissions(void)
{
	char *password;
73
	unsigned permission;
Avuton Olrich's avatar
Avuton Olrich committed
74
	ConfigParam *param;
Warren Dukes's avatar
Warren Dukes committed
75

76 77
	permission_passwords = g_hash_table_new_full(g_str_hash, g_str_equal,
						     g_free, NULL);
78

Avuton Olrich's avatar
Avuton Olrich committed
79 80
	permission_default = PERMISSION_READ | PERMISSION_ADD |
	    PERMISSION_CONTROL | PERMISSION_ADMIN;
Warren Dukes's avatar
Warren Dukes committed
81

82
	param = getNextConfigParam(CONF_PASSWORD, NULL);
Warren Dukes's avatar
Warren Dukes committed
83

Avuton Olrich's avatar
Avuton Olrich committed
84 85
	if (param) {
		permission_default = 0;
Warren Dukes's avatar
Warren Dukes committed
86

87
		do {
88
			const char *separator =
89 90 91 92
				strchr(param->value, PERMISSION_PASSWORD_CHAR);

			if (separator == NULL)
				FATAL("\"%c\" not found in password string "
Avuton Olrich's avatar
Avuton Olrich committed
93 94 95
				      "\"%s\", line %i\n",
				      PERMISSION_PASSWORD_CHAR,
				      param->value, param->line);
Warren Dukes's avatar
Warren Dukes committed
96

97 98
			password = g_strndup(param->value,
					     separator - param->value);
Warren Dukes's avatar
Warren Dukes committed
99

100
			permission = parsePermissions(separator + 1);
101

102
			g_hash_table_replace(permission_passwords,
103
					     password,
104
					     GINT_TO_POINTER(permission));
Avuton Olrich's avatar
Avuton Olrich committed
105
		} while ((param = getNextConfigParam(CONF_PASSWORD, param)));
Warren Dukes's avatar
Warren Dukes committed
106 107
	}

108
	param = getConfigParam(CONF_DEFAULT_PERMS);
Warren Dukes's avatar
Warren Dukes committed
109

Avuton Olrich's avatar
Avuton Olrich committed
110 111
	if (param)
		permission_default = parsePermissions(param->value);
Warren Dukes's avatar
Warren Dukes committed
112 113
}

114
int getPermissionFromPassword(char *password, unsigned *permission)
Avuton Olrich's avatar
Avuton Olrich committed
115
{
116 117
	bool found;
	gpointer key, value;
Warren Dukes's avatar
Warren Dukes committed
118

119 120 121 122
	found = g_hash_table_lookup_extended(permission_passwords,
					     password, &key, &value);
	if (!found)
		return -1;
Warren Dukes's avatar
Warren Dukes committed
123

124 125
	*permission = GPOINTER_TO_INT(value);
	return 0;
Warren Dukes's avatar
Warren Dukes committed
126 127
}

Avuton Olrich's avatar
Avuton Olrich committed
128 129
void finishPermissions(void)
{
130
	g_hash_table_destroy(permission_passwords);
Warren Dukes's avatar
Warren Dukes committed
131 132
}

133
unsigned getDefaultPermissions(void)
Avuton Olrich's avatar
Avuton Olrich committed
134
{
Warren Dukes's avatar
Warren Dukes committed
135 136
	return permission_default;
}