1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
|
#include "config.h"
#include <pwd.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include "list.h"
#include "common.h"
#include "auth-libsecret.h"
#include "auth.h"
struct authitem{
char *name; // item name
time_t touch_time; // item touch time
struct authinfo *info;
struct{
int child_cnt; // number of subitem
int max_child_cnt; // maximum number of subitem
struct authitem **childs; // sorted list of subitems
};
};
static char auth_login[64] = "guest";
static char *auth_fake_password = "********";
static struct authinfo authinfo_default = {{NULL, NULL}, 1, "", auth_login, ""};
static LIST authinfo_list = STATIC_LIST_INITIALIZER(authinfo_list);
static struct authitem authroot = {NULL, (time_t) 0, NULL, {0, 0, NULL}};
static pthread_mutex_t m_auth = PTHREAD_MUTEX_INITIALIZER;
void auth_set_default_login_name(const char *name){
strncpy(auth_login, name, sizeof(auth_login));
auth_login[sizeof(auth_login) - 1] = '\0';
DPRINTF(5, "login=%s\n", auth_login);
}
static struct authinfo * authinfo_create_new(
const char *domain,
const char *user,
const char *password){
size_t len;
struct authinfo *info;
len = sizeof(struct authinfo) +
strlen(domain) + strlen(user) + strlen(password) + 3;
if ((info = malloc(len)) == NULL) return NULL;
memset(info, 0, len);
info->domain = (char *) (info + 1);
info->user = info->domain + strlen(domain) + 1;
info->password = info->user + strlen(user) + 1;
strcpy(info->domain, domain);
strcpy(info->user, user);
strcpy(info->password, password);
return info;
}
static inline void authinfo_delete(struct authinfo *info){
free(info);
}
static inline int authinfo_compare(struct authinfo *info,
const char *domain,
const char *user,
const char *password){
return ((strcmp(info->domain, domain) == 0) &&
(strcmp(info->user, user) == 0) &&
(strcmp(info->password, password) == 0));
}
static struct authinfo * authinfo_find_in_list(
const char *domain,
const char *user,
const char *password){
struct authinfo *info;
LIST *elem;
elem = first_list_elem(&authinfo_list);
while(is_valid_list_elem(&authinfo_list, elem)){
info = list_entry(elem, struct authinfo, entries);
if (authinfo_compare(info, domain, user, password)) return info;
elem = elem->next;
}
return NULL;
}
static struct authinfo * authinfo_store_list(
const char *domain,
const char *user,
const char *password){
struct authinfo *info;
DPRINTF(10, "domain=%s, user=%s, password=%s\n",
domain, user, auth_fake_password);
info = authinfo_find_in_list(domain, user, password);
if (info != NULL){
remove_from_list(&authinfo_list, &info->entries);
}else{
info = authinfo_create_new(domain, user, password);
if (info == NULL) return NULL;
}
info->ref_count++;
add_to_list_back(&authinfo_list, &info->entries);
return info;
}
static void authinfo_release(struct authinfo *info){
info->ref_count--;
if (info->ref_count == 0){
remove_from_list(&authinfo_list, &info->entries);
authinfo_delete(info);
}
}
static struct authitem* authitem_create_item(const char *name){
struct authitem *item;
item = malloc(sizeof(struct authitem) + strlen(name) + 1);
if (item == NULL) return NULL;
memset(item, 0 , sizeof(struct authitem));
item->name = (char *) (item + 1);
strcpy(item->name, name);
item->touch_time = time(NULL);
return item;
}
static inline void authitem_delete_item(struct authitem *item){
if (item->info != NULL) authinfo_release(item->info);
if (item->childs != NULL) free(item->childs);
free(item);
}
static void authitem_delete_obsolete_items(struct authitem *item, time_t threshold){
int i;
for(i = item->child_cnt - 1; i >= 0; i--){
authitem_delete_obsolete_items(item->childs[i], threshold);
if ((item->childs[i]->info == NULL) &&
(item->childs[i]->childs == NULL)){
authitem_delete_item(item->childs[i]);
if (i != item->child_cnt - 1){
memmove(&item->childs[i],
&item->childs[i + 1],
(item->child_cnt - i - 1) * sizeof(struct authitem *));
}
item->child_cnt--;
}
}
if ((item->touch_time < threshold) && (item->info != NULL)){
authinfo_release(item->info);
item->info = NULL;
}
if ((item->child_cnt == 0) && (item->childs != NULL)){
free(item->childs);
item->childs = NULL;
item->max_child_cnt = 0;
}
}
/*
* This function search for an item in specified authitem subitems,
* subitems MUST be sorted alphabetically, so we can bisect.
*
* return:
* a) item position, if item with specified name was found
* b) negative value -(pos+1), if item was not found,
* where 'pos' is the position to insert the new element
*/
static int authitem_find_subitem(struct authitem *item, const char *name){
int first = 0, last = item->child_cnt - 1;
while(first <= last){
int i = (first + last) >> 1;
int result = strcasecmp(item->childs[i]->name, name);
if (result == 0) return i;
if (result < 0) first = i + 1;
else last = i - 1;
}
return -(first + 1);
}
/*
* This function insert an element to specified authitem,
* insert position MUST be obtained with authitem_find_subitem().
*
* return:
* a) zero, if no errors
* b) (-1), if insertion failed
*/
static int authitem_insert_subitem(struct authitem *item,
struct authitem *subitem, int pos){
if ((pos > item->child_cnt) || (pos < 0)) return -1;
if (item->max_child_cnt == item->child_cnt){
struct authitem **new_childs;
int new_max_cnt;
new_max_cnt = (item->max_child_cnt == 0) ?
64 : 2 * item->max_child_cnt;
new_childs = realloc(item->childs,
new_max_cnt * sizeof(struct authitem *));
if (new_childs == NULL) return -1;
item->max_child_cnt = new_max_cnt;
item->childs = new_childs;
}
if (pos < item->child_cnt){
memmove(&item->childs[pos + 1],
&item->childs[pos],
(item->child_cnt - pos) * sizeof(struct authitem *));
}
item->childs[pos] = subitem;
item->child_cnt++;
return 0;
}
static struct authitem * authitem_get_subitem(
struct authitem *item,
const char *name){
int pos;
struct authitem *subitem;
pos = authitem_find_subitem(item, name);
if (pos < 0){
/* create new subitem and add it to item */
subitem = authitem_create_item(name);
if (subitem == NULL) return NULL;
pos = -(pos + 1);
if (authitem_insert_subitem(item, subitem, pos) != 0){
authitem_delete_item(subitem);
return NULL;
}
}
return item->childs[pos];
}
struct authinfo * auth_get_authinfo(
const char *domain,
const char *server,
const char *share,
int *suitability){
int pos;
struct authitem *item;
struct authinfo *info;
DPRINTF(10, "domain=%s, server=%s, share=%s\n", domain, server, share);
if ((server == NULL) || (*server == '\0')) return NULL;
if (domain == NULL) domain = "";
if (share == NULL) share = "";
item = &authroot;
info = &authinfo_default;
*suitability = AUTH_FALLBACK;
pthread_mutex_lock(&m_auth);
if (item->info != NULL){
info = item->info;
*suitability = AUTH_MATCH_DEFAULT;
}
if (*domain != '\0'){
pos = authitem_find_subitem(item, domain);
if ((pos >= 0) && (item->childs[pos]->info != NULL)){
info = item->childs[pos]->info;
*suitability = AUTH_MATCH_DOMAIN_COMPAT;
}
}
if ((pos = authitem_find_subitem(item, server)) < 0) goto end;
item = item->childs[pos];
if (item->info != NULL){
info = item->info;
*suitability = AUTH_MATCH_SERVER;
}
if (*share == '\0') goto end;
if ((pos = authitem_find_subitem(item, share)) < 0) goto end;
item = item->childs[pos];
if (item->info != NULL){
info = item->info;
*suitability = AUTH_MATCH_RESOURCE;
}
end:
info->ref_count++;
pthread_mutex_unlock(&m_auth);
DPRINTF(10, "domain=%s, user=%s, password=%s, suitability=%d\n",
info->domain, info->user, auth_fake_password, *suitability);
return info;
}
void auth_release_authinfo(struct authinfo *info){
pthread_mutex_lock(&m_auth);
authinfo_release(info);
pthread_mutex_unlock(&m_auth);
}
int auth_store_auth_data(const char *server,
const char *share,
const char *domain,
const char *user,
const char *password){
int result;
struct authinfo *info;
struct authitem *item;
DPRINTF(10, "smb://%s/%s, domain=%s, user=%s, password=%s\n",
server, share, domain, user, auth_fake_password);
if ((user == NULL) || (*user == '\0')) return -1;
if (server == NULL) server = "";
if (share == NULL) share = "";
if (domain == NULL) domain = "";
if (password == NULL) password = "";
if ((*server == '\0') && (*share != '\0')) return -1;
result = -1;
item = &authroot;
pthread_mutex_lock(&m_auth);
if (*server == '\0') goto update_info;
if ((item = authitem_get_subitem(item, server)) == NULL) goto error;
if (*share == '\0') goto update_info;
if ((item = authitem_get_subitem(item, share)) == NULL) goto error;
update_info:
if ((item->info == NULL) ||
! authinfo_compare(item->info, domain, user, password)){
info = authinfo_store_list(domain, user, password);
if (info == NULL) goto error;
if (item->info != NULL) authinfo_release(item->info);
item->info = info;
}
item->touch_time = time(NULL);
result = 0;
error:
pthread_mutex_unlock(&m_auth);
return result;
}
void auth_delete_obsolete(time_t threshold){
pthread_mutex_lock(&m_auth);
authitem_delete_obsolete_items(&authroot, threshold);
pthread_mutex_unlock(&m_auth);
}
|