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"
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
)
49 temp
= strtok_r(string
, PERMISSION_SEPERATOR
, &tok
);
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
;
60 FATAL("unknown permission \"%s\"\n", temp
);
63 temp
= strtok_r(NULL
, PERMISSION_SEPERATOR
, &tok
);
69 void initPermissions(void)
77 permission_passwords
= makeList(free
, 1);
79 permission_default
= PERMISSION_READ
| PERMISSION_ADD
|
80 PERMISSION_CONTROL
| PERMISSION_ADMIN
;
82 param
= getNextConfigParam(CONF_PASSWORD
, NULL
);
85 permission_default
= 0;
88 if (!strstr(param
->value
, PERMISSION_PASSWORD_CHAR
)) {
89 FATAL("\"%s\" not found in password string "
91 PERMISSION_PASSWORD_CHAR
,
92 param
->value
, param
->line
);
95 if (!(temp
= strtok_r(param
->value
,
96 PERMISSION_PASSWORD_CHAR
,
98 FATAL("something weird just happened in permission.c\n");
103 permission
= xmalloc(sizeof(int));
105 parsePermissions(strtok_r(NULL
, "", &cp2
));
107 insertInList(permission_passwords
, password
,
109 } while ((param
= getNextConfigParam(CONF_PASSWORD
, param
)));
112 param
= getConfigParam(CONF_DEFAULT_PERMS
);
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
);
132 void finishPermissions(void)
134 freeList(permission_passwords
);
137 int getDefaultPermissions(void)
139 return permission_default
;