1 /**
2 * HTTP authentication credentials
3 *
4 * Copyright (C) 2000-2014 by
5 * Jeffrey Fulmer - <jeff@joedog.org>, et al.
6 * This file is distributed as part of Siege
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 *--
22 */
23 #include <stdlib.h>
24 #include <string.h>
25 #include <memory.h>
26 #include <joedog/defs.h>
27 #include <joedog/boolean.h>
28 #include <creds.h>
29
30 struct CREDS_T {
31 SCHEME scheme;
32 char *username;
33 char *password;
34 char *realm;
35 };
36
37 size_t CREDSIZE = sizeof(struct CREDS_T);
38
39 private void __parse_input(CREDS this, char *str);
40
41
42 CREDS
new_creds(SCHEME scheme,char * str)43 new_creds(SCHEME scheme, char *str)
44 {
45 CREDS this;
46
47 this = calloc(sizeof(struct CREDS_T), 1);
48 this->scheme = scheme;
49 this->username = NULL;
50 this->password = NULL;
51 this->realm = NULL;
52 __parse_input(this, str);
53 return this;
54 }
55
56 CREDS
creds_destroy(CREDS this)57 creds_destroy(CREDS this)
58 {
59 xfree(this->username);
60 xfree(this->password);
61 xfree(this->realm);
62 xfree(this);
63 return NULL;
64 }
65
66 SCHEME
creds_get_scheme(CREDS this)67 creds_get_scheme(CREDS this)
68 {
69 return this->scheme;
70 }
71
72 char *
creds_get_username(CREDS this)73 creds_get_username(CREDS this)
74 {
75 return this->username;
76 }
77
78 char *
creds_get_password(CREDS this)79 creds_get_password(CREDS this)
80 {
81 return this->password;
82 }
83
84 char *
creds_get_realm(CREDS this)85 creds_get_realm(CREDS this)
86 {
87 return this->realm;
88 }
89
90 void
creds_set_username(CREDS this,char * username)91 creds_set_username(CREDS this, char *username)
92 {
93 size_t len = strlen(username);
94
95 this->username = malloc(len+1);
96 memset(this->username, '\0', len+1);
97 memcpy(this->username, username, len);
98 return;
99 }
100
101 void
creds_set_password(CREDS this,char * password)102 creds_set_password(CREDS this, char *password)
103 {
104 size_t len = strlen(password);
105
106 this->password = malloc(len+1);
107 memset(this->password, '\0', len+1);
108 memcpy(this->password, password, len);
109 return;
110 }
111
112 void
creds_set_realm(CREDS this,char * realm)113 creds_set_realm(CREDS this, char *realm)
114 {
115 size_t len = strlen(realm);
116
117 this->realm = malloc(len+1);
118 memset(this->realm, '\0', len+1);
119 memcpy(this->realm, realm, len);
120 return;
121 }
122
123
124 private void
__parse_input(CREDS this,char * str)125 __parse_input(CREDS this, char *str)
126 {
127 char *usr;
128 char *pwd;
129 char *rlm;
130 char *tmp;
131 char any[] = "any\0";
132
133 usr = tmp = str;
134 while (*tmp && *tmp != ':' && *tmp != '\0')
135 tmp++;
136
137 *tmp++=0;
138 pwd = tmp;
139 while (*tmp && *tmp != ':' && *tmp != '\0')
140 tmp++;
141
142 if ('\0' != *tmp) {
143 *tmp++=0;
144 rlm = tmp;
145 } else {
146 rlm = NULL;
147 }
148
149 creds_set_username(this, usr);
150 creds_set_password(this, pwd);
151 creds_set_realm(this, (rlm==NULL)?any:rlm);
152 }
153
154