permission.c 3.43 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 23
#include "permission.h"

#include "conf.h"
#include "list.h"
#include "log.h"
24
#include "utils.h"
Warren Dukes's avatar
Warren Dukes committed
25 26 27 28 29 30 31 32 33 34 35

#include <string.h>

#define PERMISSION_PASSWORD_CHAR	"@"
#define PERMISSION_SEPERATOR		","

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

Avuton Olrich's avatar
Avuton Olrich committed
36
static List *permission_passwords;
Warren Dukes's avatar
Warren Dukes committed
37

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

Avuton Olrich's avatar
Avuton Olrich committed
40 41
static int parsePermissions(char *string)
{
42
	int permission = 0;
Avuton Olrich's avatar
Avuton Olrich committed
43 44
	char *temp;
	char *tok;
Warren Dukes's avatar
Warren Dukes committed
45

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

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

Avuton Olrich's avatar
Avuton Olrich committed
63
		temp = strtok_r(NULL, PERMISSION_SEPERATOR, &tok);
Warren Dukes's avatar
Warren Dukes committed
64 65 66 67 68
	}

	return permission;
}

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

77
	permission_passwords = makeList(free, 1);
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 {
Avuton Olrich's avatar
Avuton Olrich committed
88
			if (!strstr(param->value, PERMISSION_PASSWORD_CHAR)) {
89
				FATAL("\"%s\" not found in password string "
Avuton Olrich's avatar
Avuton Olrich committed
90 91 92
				      "\"%s\", line %i\n",
				      PERMISSION_PASSWORD_CHAR,
				      param->value, param->line);
93
			}
Warren Dukes's avatar
Warren Dukes committed
94

Avuton Olrich's avatar
Avuton Olrich committed
95 96 97
			if (!(temp = strtok_r(param->value,
					      PERMISSION_PASSWORD_CHAR,
					      &cp2))) {
98
				FATAL("something weird just happened in permission.c\n");
99
			}
Warren Dukes's avatar
Warren Dukes committed
100

101
			password = temp;
Warren Dukes's avatar
Warren Dukes committed
102

103
			permission = xmalloc(sizeof(int));
Avuton Olrich's avatar
Avuton Olrich committed
104 105
			*permission =
			    parsePermissions(strtok_r(NULL, "", &cp2));
106

Avuton Olrich's avatar
Avuton Olrich committed
107 108 109
			insertInList(permission_passwords, password,
				     permission);
		} while ((param = getNextConfigParam(CONF_PASSWORD, param)));
Warren Dukes's avatar
Warren Dukes committed
110 111
	}

112
	param = getConfigParam(CONF_DEFAULT_PERMS);
Warren Dukes's avatar
Warren Dukes committed
113

Avuton Olrich's avatar
Avuton Olrich committed
114 115
	if (param)
		permission_default = parsePermissions(param->value);
116 117

	sortList(permission_passwords);
Warren Dukes's avatar
Warren Dukes committed
118 119
}

Avuton Olrich's avatar
Avuton Olrich committed
120 121 122
int getPermissionFromPassword(char *password, int *permission)
{
	void *foundPermission;
Warren Dukes's avatar
Warren Dukes committed
123

Avuton Olrich's avatar
Avuton Olrich committed
124
	if (findInList(permission_passwords, password, &foundPermission)) {
125
		*permission = *((int *)foundPermission);
Warren Dukes's avatar
Warren Dukes committed
126 127 128 129 130 131
		return 0;
	}

	return -1;
}

Avuton Olrich's avatar
Avuton Olrich committed
132 133
void finishPermissions(void)
{
Warren Dukes's avatar
Warren Dukes committed
134 135 136
	freeList(permission_passwords);
}

Avuton Olrich's avatar
Avuton Olrich committed
137 138
int getDefaultPermissions(void)
{
Warren Dukes's avatar
Warren Dukes committed
139 140
	return permission_default;
}