1 /************************************************************************
3 * voxelands - 3d voxel world sandbox game
4 * Copyright (C) Lisa 'darkrose' Milne 2016 <lisa@ltmnet.com>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 * See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>
18 ************************************************************************/
36 char* privstr
[PRIV_COUNT
];
53 /* convert privs into a human readable csv string */
54 int auth_priv2str(uint64_t privs
, char* buff
, int size
)
67 for (i
=0; i
<PRIV_COUNT
; i
++) {
71 l
= strlen(auth
.privstr
[i
]);
78 strcat(buff
,auth
.privstr
[i
]);
86 /* convert a string to privs */
87 uint64_t auth_str2privs(char* str
)
98 if (!strcpy(buff
,str
))
105 for (i
=0; i
<PRIV_COUNT
; i
++) {
106 if (strcmp(auth
.privstr
[i
],b
))
123 /* init auth system for the given file */
124 int auth_init(char* file
)
128 auth
.mutex
= mutex_create();
133 path
= path_get("game",file
,0,NULL
,0);
138 if (!strcmp(auth
.file
,path
)) {
147 nvp_free(&auth
.data
,1);
159 /* free auth memory, reset auth struct */
165 mutex_free(auth
.mutex
);
172 nvp_free(&auth
.data
,1);
178 /* load auth data from file */
189 mutex_lock(auth
.mutex
);
191 f
= file_load(NULL
,auth
.file
);
195 while (file_readline(f
,line
,512)) {
209 privs
= auth_str2privs(s
);
211 d
= malloc(sizeof(authdata_t
));
217 nvp_set(&auth
.data
,n
,"",d
);
220 mutex_unlock(auth
.mutex
);
227 /* save auth data to file */
237 mutex_lock(auth
.mutex
);
239 f
= file_create(NULL
,auth
.file
);
243 for (n
=auth
.data
; n
; n
= n
->next
) {
244 if (!n
->name
|| !n
->name
[0])
246 if (auth_priv2str(((authdata_t
*)n
->data
)->privs
,s
,512) < 0)
248 file_writef(f
,"%s:%s:%s\n",n
->name
,((authdata_t
*)n
->data
)->pwd
,s
);
253 mutex_unlock(auth
.mutex
);
260 int auth_exists(char* name
)
266 mutex_lock(auth
.mutex
);
268 n
= nvp_get(&auth
.data
,name
);
270 mutex_unlock(auth
.mutex
);
277 void auth_set(char* name
, authdata_t data
)
283 mutex_lock(auth
.mutex
);
285 n
= nvp_get(&auth
.data
,name
);
288 strcpy(((authdata_t
*)n
->data
)->pwd
,data
.pwd
);
289 ((authdata_t
*)n
->data
)->privs
= data
.privs
;
291 authdata_t
*d
= malloc(sizeof(authdata_t
));
293 mutex_unlock(auth
.mutex
);
297 strcpy(d
->pwd
,data
.pwd
);
298 d
->privs
= data
.privs
;
299 nvp_set(&auth
.data
,name
,"",d
);
302 mutex_unlock(auth
.mutex
);
307 void auth_add(char* name
)
311 d
.privs
= PRIV_DEFAULT
;
316 int auth_getpwd(char* name
, char buff
[64])
325 mutex_lock(auth
.mutex
);
327 n
= nvp_get(&auth
.data
,name
);
330 mutex_unlock(auth
.mutex
);
334 strcpy(buff
,((authdata_t
*)n
->data
)->pwd
);
336 mutex_unlock(auth
.mutex
);
341 void auth_setpwd(char* name
, char* pwd
)
347 mutex_lock(auth
.mutex
);
349 n
= nvp_get(&auth
.data
,name
);
352 strcpy(((authdata_t
*)n
->data
)->pwd
,pwd
);
354 authdata_t
*d
= malloc(sizeof(authdata_t
));
356 mutex_unlock(auth
.mutex
);
361 d
->privs
= PRIV_DEFAULT
;
362 nvp_set(&auth
.data
,name
,"",d
);
365 mutex_unlock(auth
.mutex
);
370 uint64_t auth_getprivs(char* name
)
373 uint64_t privs
= PRIV_INVALID
;
378 mutex_lock(auth
.mutex
);
380 n
= nvp_get(&auth
.data
,name
);
383 mutex_unlock(auth
.mutex
);
387 privs
= ((authdata_t
*)n
->data
)->privs
;
389 mutex_unlock(auth
.mutex
);
394 void auth_setprivs(char* name
, uint64_t privs
)
400 mutex_lock(auth
.mutex
);
402 n
= nvp_get(&auth
.data
,name
);
405 ((authdata_t
*)n
->data
)->privs
= privs
;
407 authdata_t
*d
= malloc(sizeof(authdata_t
));
409 mutex_unlock(auth
.mutex
);
415 nvp_set(&auth
.data
,name
,"",d
);
418 mutex_unlock(auth
.mutex
);