1 #include <libconfig.h> 2 #include "compat.h" 3 #include "backuppcd-auth.h" 4 5 struct bpcd_auth_userinfo; 6 struct bpcd_auth_userinfo { 7 const char *username; 8 const char *passhash; 9 backuppc_privs_t privs; 10 struct bpcd_auth_userinfo *_next; 11 }; 12 13 struct bpcd_auth_userinfo *userlist = NULL; 14 15 static int bpcd_auth_opt_user(const char *shortvar, const char *var, const char *arguments, const char *value, lc_flags_t flags, void *extra) { 16 struct bpcd_auth_userinfo *newnode; 17 char *valcopy_s, *valcopy; 18 char *privstr; 19 20 newnode = malloc(sizeof(*newnode)); 21 if (newnode == NULL) { 22 return(LC_CBRET_ERROR); 23 } 24 25 valcopy_s = valcopy = strdup(value); 26 27 newnode->username = strsep(&valcopy, " ,\t"); 28 if (newnode->username == NULL) { 29 free(valcopy_s); 30 free(newnode); 31 fprintf(stderr, "error: usage: USER <Username> <Password> <Privilegs>\n"); 32 return(LC_CBRET_ERROR); 33 } 34 35 newnode->passhash = strsep(&valcopy, " ,\t"); 36 if (newnode->passhash == NULL) { 37 free(valcopy_s); 38 free(newnode); 39 fprintf(stderr, "error: usage: USER <Username> <Password> <Privilegs>\n"); 40 return(LC_CBRET_ERROR); 41 } 42 43 privstr = strsep(&valcopy, " ,\t"); 44 if (privstr == NULL) { 45 free(valcopy_s); 46 free(newnode); 47 fprintf(stderr, "error: usage: USER <Username> <Password> <Privilegs>\n"); 48 return(LC_CBRET_ERROR); 49 } 50 51 if (strlen(newnode->passhash) != 40) { 52 free(valcopy_s); 53 free(newnode); 54 fprintf(stderr, "error: Password hash must be exactly 40 charectars long.\n"); 55 return(LC_CBRET_ERROR); 56 } 57 58 if (strcasecmp(privstr, "Read") == 0) { 59 newnode->privs = BPC_PRIV_READ; 60 } else if (strcasecmp(privstr, "Write") == 0) { 61 newnode->privs = BPC_PRIV_WRITE; 62 } else if (strcasecmp(privstr, "ReadWrite") == 0) { 63 newnode->privs = BPC_PRIV_RDWR; 64 } else if (strcasecmp(privstr, "RD") == 0) { 65 newnode->privs = BPC_PRIV_READ; 66 } else if (strcasecmp(privstr, "WR") == 0) { 67 newnode->privs = BPC_PRIV_WRITE; 68 } else if (strcasecmp(privstr, "RDWR") == 0) { 69 newnode->privs = BPC_PRIV_RDWR; 70 } else if (strcasecmp(privstr, "r") == 0) { 71 newnode->privs = BPC_PRIV_READ; 72 } else if (strcasecmp(privstr, "w") == 0) { 73 newnode->privs = BPC_PRIV_WRITE; 74 } else if (strcasecmp(privstr, "rw") == 0) { 75 newnode->privs = BPC_PRIV_RDWR; 76 } else { 77 free(valcopy_s); 78 free(newnode); 79 fprintf(stderr, "error: usage: Privileges must be one of: READ, WRITE, or READWRITE\n"); 80 return(LC_CBRET_ERROR); 81 } 82 83 newnode->_next = userlist; 84 userlist = newnode; 85 86 return(LC_CBRET_OKAY); 87 } 88 89 void bpcd_auth_init(void) { 90 lc_register_callback("User", 'u', LC_VAR_STRING, bpcd_auth_opt_user, NULL); 91 return; 92 } 93 94 backuppc_privs_t bpcd_auth_verify(const char *username, const char *passhash, uint32_t address) { 95 struct bpcd_auth_userinfo *tmp; 96 97 for (tmp = userlist; tmp; tmp = tmp->_next) { 98 99 /* 100 * Should the username be case-sensitive ? (XXX) 101 */ 102 if (strcasecmp(tmp->username, username) == 0) { 103 if (strcasecmp(tmp->passhash, passhash) == 0) { 104 return(tmp->privs); 105 } else { 106 return(BPC_PRIV_ERROR); 107 } 108 } 109 } 110 111 return(BPC_PRIV_ERROR); 112 } |