-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutility.c
276 lines (211 loc) · 4.32 KB
/
utility.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
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
/*
Copyright (c) 2015 Colum Paget <[email protected]>
* SPDX-License-Identifier: GPL-3.0
*/
//utility functions, mostly string handling
//if you are looking for PAM module example code, then look in pam_module.c
#include "utility.h"
#include <fnmatch.h>
void TSettingsDestroy(TSettings *Settings)
{
if (! Settings) return;
Destroy(Settings->Prompt);
Destroy(Settings->CredsFiles);
Destroy(Settings->SortedFiles);
Destroy(Settings->User);
Destroy(Settings->PamUser);
Destroy(Settings->PamHost);
Destroy(Settings->PamTTY);
Destroy(Settings->Script);
free(Settings);
}
void THoneyCredDestroy(THoneyCred *Cred)
{
if (! Cred) return;
Destroy(Cred->User);
Destroy(Cred->Pass);
Destroy(Cred->Salt);
free(Cred);
}
//Get items from the MatchList, and use them as fnmatch patterns, returning TRUE
//if we find one that matches. However, if the match pattern starts with '!', then
//return TRUE if the match fails
int ItemMatches(const char *Item, const char *MatchList)
{
const char *ptr, *mptr;
char *Match=NULL;
int result=FALSE;
if (StrLen(Item) ==0) return(FALSE);
ptr=GetTok(MatchList, ',', &Match);
while (ptr)
{
mptr=Match;
if (*mptr=='!') mptr++;
if (fnmatch(mptr, Item, 0)==0)
{
if (*Match!='!')
{
result=TRUE;
break;
}
}
else if (*Match=='!')
{
result=TRUE;
break;
}
ptr=GetTok(ptr, ',', &Match);
}
Destroy(Match);
return(result);
}
//call 'ItemMatches' for each item in the list 'ItemList'
int ItemListMatches(const char *ItemList, const char *MatchList)
{
char *Item=NULL;
const char *ptr;
int result=FALSE;
ptr=GetTok(ItemList, ' ', &Item);
while (ptr)
{
if (ItemMatches(Item, MatchList))
{
result=TRUE;
break;
}
ptr=GetTok(ptr, ' ', &Item);
}
Destroy(Item);
return(result);
}
#ifndef va_copy
#define va_copy(dest, src) (dest) = (src)
#endif
char *VCatStr(char *Dest, const char *Str1, va_list args)
{
//initialize these to keep valgrind happy
size_t len=0;
char *ptr=NULL;
const char *sptr=NULL;
if (Dest !=NULL)
{
len=StrLen(Dest);
ptr=Dest;
}
else
{
len=10;
ptr=(char *) calloc(10,1);
}
if (! Str1) return(ptr);
for (sptr=Str1; sptr !=NULL; sptr=va_arg(args,const char *))
{
len+=StrLen(sptr)+1;
len=len*2;
ptr=(char *) realloc(ptr,len);
if (ptr && sptr) strcat(ptr,sptr);
}
return(ptr);
}
char *MCatStr(char *Dest, const char *Str1, ...)
{
char *ptr=NULL;
va_list args;
va_start(args,Str1);
ptr=VCatStr(Dest,Str1,args);
va_end(args);
return(ptr);
}
char *MCopyStr(char *Dest, const char *Str1, ...)
{
char *ptr=NULL;
va_list args;
ptr=Dest;
if (ptr) *ptr='\0';
va_start(args,Str1);
ptr=VCatStr(ptr,Str1,args);
va_end(args);
return(ptr);
}
char *CatStr(char *Dest, const char *Src)
{
return(MCatStr(Dest,Src,NULL));
}
char *CopyStr(char *Dest, const char *Src)
{
return(MCopyStr(Dest,Src,NULL));
}
void StripTrailingWhitespace(char *str)
{
size_t len;
char *ptr;
len=StrLen(str);
if (len==0) return;
for(ptr=str+len-1; (ptr >= str) && isspace(*ptr); ptr--) *ptr='\0';
}
void StripLeadingWhitespace(char *str)
{
char *ptr, *start=NULL;
if (! str) return;
for(ptr=str; *ptr !='\0'; ptr++)
{
if ((! start) && (! isspace(*ptr))) start=ptr;
}
if (!start) start=ptr;
memmove(str,start,ptr+1-start);
}
void StripQuotes(char *Str)
{
int len;
char *ptr, StartQuote='\0';
ptr=Str;
while (isspace(*ptr)) ptr++;
if ((*ptr=='"') || (*ptr=='\''))
{
StartQuote=*ptr;
len=StrLen(ptr);
if ((len > 0) && (StartQuote != '\0') && (ptr[len-1]==StartQuote))
{
if (ptr[len-1]==StartQuote) ptr[len-1]='\0';
memmove(Str,ptr+1,len);
}
}
}
//I don't trust strtok, it's not reentrant, and this handles quotes
const char *GetTok(const char *In, char Delim, char **Token)
{
char quot='\0';
const char *ptr;
int i=0;
*Token=realloc(*Token,258);
//When input is exhausted return null
if ((! In) || (*In=='\0')) return(NULL);
for (ptr=In; *ptr != '\0'; ptr++)
{
if (*ptr=='\0') break;
if (quot != '\0')
{
if (*ptr==quot) quot='\0';
}
else if ((*ptr=='"') || (*ptr=='\'')) quot=*ptr;
else if (*ptr==Delim) break;
else
{
if (*ptr=='\\') ptr++;
(*Token)[i]=*ptr;
i++;
}
if (i > 256) break;
}
(*Token)[i]='\0';
StripQuotes(*Token);
//if it's not '\0', then it must be a delim, so go past it
if (*ptr !='\0') ptr++;
//Don't return null if ptr=='\0' here, because there's probably
//still something in Token
return(ptr);
}
void Destroy(void *Item)
{
if (Item) free(Item);
}