Update to 6762
[qball-mpd.git] / src / permission.c
blob3d597052cb283f87bb5369f1f6356cd66e36f794
1 /* the Music Player Daemon (MPD)
2 * Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com)
3 * This project's homepage is: 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.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include "permission.h"
21 #include "conf.h"
22 #include "list.h"
23 #include "log.h"
24 #include "utils.h"
26 #include <string.h>
28 #define PERMISSION_PASSWORD_CHAR "@"
29 #define PERMISSION_SEPERATOR ","
31 #define PERMISSION_READ_STRING "read"
32 #define PERMISSION_ADD_STRING "add"
33 #define PERMISSION_CONTROL_STRING "control"
34 #define PERMISSION_ADMIN_STRING "admin"
36 static List *permission_passwords;
38 static int permission_default;
40 static int parsePermissions(char *string)
42 int permission = 0;
43 char *temp;
44 char *tok;
46 if (!string)
47 return 0;
49 temp = strtok_r(string, PERMISSION_SEPERATOR, &tok);
50 while (temp) {
51 if (strcmp(temp, PERMISSION_READ_STRING) == 0) {
52 permission |= PERMISSION_READ;
53 } else if (strcmp(temp, PERMISSION_ADD_STRING) == 0) {
54 permission |= PERMISSION_ADD;
55 } else if (strcmp(temp, PERMISSION_CONTROL_STRING) == 0) {
56 permission |= PERMISSION_CONTROL;
57 } else if (strcmp(temp, PERMISSION_ADMIN_STRING) == 0) {
58 permission |= PERMISSION_ADMIN;
59 } else {
60 FATAL("unknown permission \"%s\"\n", temp);
63 temp = strtok_r(NULL, PERMISSION_SEPERATOR, &tok);
66 return permission;
69 void initPermissions(void)
71 char *temp;
72 char *cp2;
73 char *password;
74 int *permission;
75 ConfigParam *param;
77 permission_passwords = makeList(free, 1);
79 permission_default = PERMISSION_READ | PERMISSION_ADD |
80 PERMISSION_CONTROL | PERMISSION_ADMIN;
82 param = getNextConfigParam(CONF_PASSWORD, NULL);
84 if (param) {
85 permission_default = 0;
87 do {
88 if (!strstr(param->value, PERMISSION_PASSWORD_CHAR)) {
89 FATAL("\"%s\" not found in password string "
90 "\"%s\", line %i\n",
91 PERMISSION_PASSWORD_CHAR,
92 param->value, param->line);
95 if (!(temp = strtok_r(param->value,
96 PERMISSION_PASSWORD_CHAR,
97 &cp2))) {
98 FATAL("something weird just happened in permission.c\n");
101 password = temp;
103 permission = xmalloc(sizeof(int));
104 *permission =
105 parsePermissions(strtok_r(NULL, "", &cp2));
107 insertInList(permission_passwords, password,
108 permission);
109 } while ((param = getNextConfigParam(CONF_PASSWORD, param)));
112 param = getConfigParam(CONF_DEFAULT_PERMS);
114 if (param)
115 permission_default = parsePermissions(param->value);
117 sortList(permission_passwords);
120 int getPermissionFromPassword(char *password, int *permission)
122 void *foundPermission;
124 if (findInList(permission_passwords, password, &foundPermission)) {
125 *permission = *((int *)foundPermission);
126 return 0;
129 return -1;
132 void finishPermissions(void)
134 freeList(permission_passwords);
137 int getDefaultPermissions(void)
139 return permission_default;