-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrtoken.c
74 lines (66 loc) · 1.23 KB
/
rtoken.c
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
// SPDX-License-Identifier: GPL-3.0-or-later
/*
* Copyright (C) 2019-2022 Junjiro R. Okajima
*/
#include <sys/stat.h>
#include <sys/types.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "xoauth2.h"
int refresh_token(char *user, char *rbuf, size_t rsz)
{
int err;
FILE *fp;
char *p;
size_t len;
struct stat st;
Dpri("user %s\n", user);
err = -1;
fp = fopen(XOAUTH2_USER, "r");
if (!fp)
goto out;
err = fstat(fileno(fp), &st);
if (err)
goto out_fp;
if (st.st_uid
|| (st.st_mode & (S_IWGRP | S_IXGRP | S_IRWXO))) {
err = -1;
Dpri(XOAUTH2_USER ", owner %d, mode 0%o\n",
st.st_uid, st.st_mode);
goto out_fp;
}
len = strlen(user);
do {
p = fgets(rbuf, rsz, fp);
if (!p) {
if (feof(fp))
goto out_no;
err = ferror(fp);
if (err)
goto out_fp;
/* should not happen */
abort();
}
if (!*p || !isalpha(*p))
continue;
if (strncmp(rbuf, user, len)
|| rbuf[len] != ':')
continue;
/* found */
Dpri("rbuf %s", rbuf);
p += len + 1;
len = strlen(rbuf) - 1 - len - 1;
memmove(rbuf, p, len);
rbuf[len] = '\0';
goto out_fp; /* success */
} while (1);
out_no:
*rbuf = '\0';
out_fp:
fclose(fp);
out:
return err;
}