configure.ac: Move OggVorbis Encoder to Encoder Plugins.
[mpd-mk.git] / src / permission.c
blobce47effe81c4af81afc77e6757368e99d97f3b95
1 /*
2 * Copyright (C) 2003-2010 The Music Player Daemon Project
3 * http://www.musicpd.org
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "config.h"
21 #include "permission.h"
22 #include "conf.h"
24 #include <glib.h>
26 #include <stdbool.h>
27 #include <string.h>
29 #define PERMISSION_PASSWORD_CHAR '@'
30 #define PERMISSION_SEPERATOR ","
32 #define PERMISSION_READ_STRING "read"
33 #define PERMISSION_ADD_STRING "add"
34 #define PERMISSION_CONTROL_STRING "control"
35 #define PERMISSION_ADMIN_STRING "admin"
37 static GHashTable *permission_passwords;
39 static unsigned permission_default;
41 static unsigned parsePermissions(const char *string)
43 unsigned permission = 0;
44 gchar **tokens;
46 if (!string)
47 return 0;
49 tokens = g_strsplit(string, PERMISSION_SEPERATOR, 0);
50 for (unsigned i = 0; tokens[i] != NULL; ++i) {
51 char *temp = tokens[i];
53 if (strcmp(temp, PERMISSION_READ_STRING) == 0) {
54 permission |= PERMISSION_READ;
55 } else if (strcmp(temp, PERMISSION_ADD_STRING) == 0) {
56 permission |= PERMISSION_ADD;
57 } else if (strcmp(temp, PERMISSION_CONTROL_STRING) == 0) {
58 permission |= PERMISSION_CONTROL;
59 } else if (strcmp(temp, PERMISSION_ADMIN_STRING) == 0) {
60 permission |= PERMISSION_ADMIN;
61 } else {
62 g_error("unknown permission \"%s\"", temp);
66 g_strfreev(tokens);
68 return permission;
71 void initPermissions(void)
73 char *password;
74 unsigned permission;
75 const struct config_param *param;
77 permission_passwords = g_hash_table_new_full(g_str_hash, g_str_equal,
78 g_free, NULL);
80 permission_default = PERMISSION_READ | PERMISSION_ADD |
81 PERMISSION_CONTROL | PERMISSION_ADMIN;
83 param = config_get_next_param(CONF_PASSWORD, NULL);
85 if (param) {
86 permission_default = 0;
88 do {
89 const char *separator =
90 strchr(param->value, PERMISSION_PASSWORD_CHAR);
92 if (separator == NULL)
93 g_error("\"%c\" not found in password string "
94 "\"%s\", line %i",
95 PERMISSION_PASSWORD_CHAR,
96 param->value, param->line);
98 password = g_strndup(param->value,
99 separator - param->value);
101 permission = parsePermissions(separator + 1);
103 g_hash_table_replace(permission_passwords,
104 password,
105 GINT_TO_POINTER(permission));
106 } while ((param = config_get_next_param(CONF_PASSWORD, param)));
109 param = config_get_param(CONF_DEFAULT_PERMS);
111 if (param)
112 permission_default = parsePermissions(param->value);
115 int getPermissionFromPassword(char const* password, unsigned* permission)
117 bool found;
118 gpointer key, value;
120 found = g_hash_table_lookup_extended(permission_passwords,
121 password, &key, &value);
122 if (!found)
123 return -1;
125 *permission = GPOINTER_TO_INT(value);
126 return 0;
129 void finishPermissions(void)
131 g_hash_table_destroy(permission_passwords);
134 unsigned getDefaultPermissions(void)
136 return permission_default;