-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsr_credentials.c
More file actions
149 lines (117 loc) · 3.31 KB
/
Copy pathsr_credentials.c
File metadata and controls
149 lines (117 loc) · 3.31 KB
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
/*
This file is part of sarracenia.
The sarracenia suite is Free and is proudly provided by the Government of Canada
Copyright (C) Her Majesty The Queen in Right of Canada, Environment Canada, 2008-2015
Questions or bugs report: dps-client@ec.gc.ca
sarracenia repository: https://github.com/MetPX/Sarrac
Documentation: https://github.com/MetPX/sarracenia
Code contributed by:
Peter Silva - 2017-2019
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
char *sr_credentials = NULL;
char *sr_credentials_fetch(char *s)
/* search for the first credential that matches the search spec given.
*/
{
char *start = sr_credentials;
char *end = sr_credentials;
char *result = NULL;
int slen;
if (!s)
return (NULL);
slen = strlen(s);
//fprintf(stderr, "\nfetching: %s\n", s );
while (*end != '\0') {
//fprintf( stderr, "try:\n%s\n", start );
int i = 0;
int smatching = 0;
while (start[i] == s[i]) {
//fprintf( stderr, "start[i]=%c, s[i]=%c\n", start[i], s[i] );
i++;
}
//fprintf( stderr, "out of loop 1: start[i]=%c, s[i]=%c\n", start[i], s[i] );
if (i == slen) {
result = (char *)malloc(i + 1);
strncpy(result, start, i);
result[i] = '\0';
//fprintf( stderr, "result: %s\n", result );
return (result);
}
if (((start[i] == ':') && (s[i] == '@')) || (!strchr(s, '@'))) {
smatching = i;
//fprintf( stderr, "skipping password..\n" );
while (start[i] != '@')
i++;
// we can't compare @ when we don't have a username in the config uri
if (!strchr(s, '@'))
++i;
//fprintf( stderr, "rest of url, start[i]=%c, s[smatching]=%c\n",
// start[i], s[smatching] );
while ((smatching < slen) && start[i] == s[smatching]) {
//fprintf( stderr, "start[i]=%c, i=%d s[smatching]=%c smatching=%d\n",
// start[i], i, s[smatching], smatching );
i++;
smatching++;
}
//fprintf( stderr, "out of loop 2, slen=%d, start[i]=%c, i=%d s[smatching]=%c smatching=%d\n",
// slen, start[i], i, s[smatching], smatching );
if ((smatching >= slen - 1)
&& ((start[i] == ' ') || (start[i] == '/')
|| (start[i] == '\t') || (start[i] == '\n'))) {
result = (char *)malloc(i + 1);
strncpy(result, start, i);
result[i] = '\0';
//fprintf( stderr, "result: %s\n", result );
return (result);
};
}
//fprintf( stderr, "nope!\n" );
end = start + i;
while ((*end != '\n') && (*end != '\0'))
end++;
start = end + 1;
}
return (NULL);
}
void sr_credentials_init()
{
FILE *f;
char cfnbuf[1024];
struct stat sb;
int status;
strcpy(cfnbuf, getenv("HOME"));
strcat(cfnbuf, "/.config/" SR_APPNAME "/credentials.conf");
status = stat(cfnbuf, &sb);
if (status) {
sb.st_size = 1;
}
sr_credentials = (char *)malloc(sb.st_size + 1);
//fprintf( stderr, "opening %s\n", cfnbuf );
f = fopen(cfnbuf, "r");
if (f != NULL) {
fread(sr_credentials, sb.st_size, 1, f);
sr_credentials[sb.st_size] = '\0';
fclose(f);
} else {
sr_credentials[0] = '\0';
}
}
void sr_credentials_cleanup()
{
if (sr_credentials)
free(sr_credentials);
}
/*
void main() {
sr_credentials_init();
sr_credentials_fetch( "amqp://guest:guest@localhost" );
sr_credentials_fetch( "amqp://guest@localhost" );
sr_credentials_fetch( "amqp://tsource@localhost/" );
}
*/