| 1 | /* |
| 2 | * This file is part of DisOrder |
| 3 | * Copyright (C) 2009 Richard Kettlewell |
| 4 | * |
| 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. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
| 18 | * USA |
| 19 | */ |
| 20 | /** @file lib/validity.c |
| 21 | * @brief Various validity checks |
| 22 | */ |
| 23 | #include "common.h" |
| 24 | #include "validity.h" |
| 25 | |
| 26 | #include "mem.h" |
| 27 | |
| 28 | /** @brief Parse a playlist name |
| 29 | * @param name Playlist name |
| 30 | * @param ownerp Where to put owner, or NULL |
| 31 | * @param sharep Where to put default sharing, or NULL |
| 32 | * @return 0 on success, -1 on error |
| 33 | * |
| 34 | * Playlists take the form USER.PLAYLIST or just PLAYLIST. The PLAYLIST part |
| 35 | * is alphanumeric and nonempty. USER is a username (see valid_username()). |
| 36 | */ |
| 37 | int playlist_parse_name(const char *name, |
| 38 | char **ownerp, |
| 39 | char **sharep) { |
| 40 | const char *dot = strchr(name, '.'), *share; |
| 41 | char *owner; |
| 42 | |
| 43 | if(dot) { |
| 44 | /* Owned playlist */ |
| 45 | owner = xstrndup(name, dot - name); |
| 46 | if(!valid_username(owner)) |
| 47 | return -1; |
| 48 | if(!valid_username(dot + 1)) |
| 49 | return -1; |
| 50 | share = "private"; |
| 51 | } else { |
| 52 | /* Shared playlist */ |
| 53 | if(!valid_username(name)) |
| 54 | return -1; |
| 55 | owner = 0; |
| 56 | share = "shared"; |
| 57 | } |
| 58 | if(ownerp) |
| 59 | *ownerp = owner; |
| 60 | if(sharep) |
| 61 | *sharep = xstrdup(share); |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | /** @brief Return non-zero for a valid username |
| 66 | * @param user Candidate username |
| 67 | * @return Nonzero if it's valid |
| 68 | * |
| 69 | * Currently we only allow the letters and digits in ASCII. We could be more |
| 70 | * liberal than this but it is a nice simple test. It is critical that |
| 71 | * semicolons are never allowed. |
| 72 | * |
| 73 | * NB also used by playlist_parse_name() to validate playlist names! |
| 74 | */ |
| 75 | int valid_username(const char *user) { |
| 76 | if(!*user) |
| 77 | return 0; |
| 78 | while(*user) { |
| 79 | const uint8_t c = *user++; |
| 80 | /* For now we are very strict */ |
| 81 | if((c >= 'a' && c <= 'z') |
| 82 | || (c >= 'A' && c <= 'Z') |
| 83 | || (c >= '0' && c <= '9')) |
| 84 | /* ok */; |
| 85 | else |
| 86 | return 0; |
| 87 | } |
| 88 | return 1; |
| 89 | } |
| 90 | |
| 91 | /* |
| 92 | Local Variables: |
| 93 | c-basic-offset:2 |
| 94 | comment-column:40 |
| 95 | fill-column:79 |
| 96 | indent-tabs-mode:nil |
| 97 | End: |
| 98 | */ |