-
Notifications
You must be signed in to change notification settings - Fork 29
/
types.c
132 lines (109 loc) · 2.64 KB
/
types.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
/* types.c
* By Ron Bowes
* Created September 1, 2008
*
* (See LICENSE.txt)
*/
#define _BSD_SOURCE /* For strdup(). */
#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef WIN32
#include <winsock2.h>
#else
#include <pwd.h> /* Required for dropping privileges. */
#include <unistd.h>
#endif
#include "memory.h"
#include "types.h"
void drop_privileges(char *username)
{
#ifdef WIN32
fprintf(stderr, "Skipping privilege drop since we're on Windows.\n");
#else
/* Drop privileges. */
if(getuid() == 0)
{
/* Get the user id for the given user. */
struct passwd *user = getpwnam(username);
if(!user)
{
fprintf(stderr, "Error: couldn't drop privileges to '%s': user not found. Please create the\n", username);
fprintf(stderr, "user or specify a better one with -u.\n");
}
else if(user->pw_uid == 0)
{
fprintf(stderr, "Error: dropped user account has root privileges; please specify a better\n");
fprintf(stderr, "one with -u.\n");
}
else
{
/* fprintf(stderr, "Dropping privileges to account %s:%d.\n", user->pw_name, user->pw_uid); */
setuid(user->pw_uid);
}
/* Ensure it succeeded. */
if(setuid(0) == 0)
{
fprintf(stderr, "Privilege drop failed, sorry!\n");
exit(1);
}
}
#endif
}
int getlasterror()
{
#ifdef WIN32
if(errno)
return errno;
if(GetLastError())
return GetLastError();
return WSAGetLastError();
#else
return errno;
#endif
}
/* Displays an error and doesn't die. */
void nberror(char *str)
{
int error = getlasterror();
#ifdef WIN32
char error_str[1024];
FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, NULL, error, 0, error_str, 1024, NULL);
if(str)
fprintf(stderr, "%s\n", str);
fprintf(stderr, "Error %d: %s", error, error_str);
#else
char *error_str = strerror(error);
if(str)
fprintf(stderr, "%s (error %d: %s)\n", str, error, error_str);
else
fprintf(stderr, "Error %d: %s\n", error, error_str);
#endif
}
void nbdie(char *str)
{
nberror(str);
exit(EXIT_FAILURE);
}
char *nbstrcasestr(char *haystack, char *needle)
{
size_t i;
char *haystack_lc = safe_strdup(haystack);
char *needle_lc = safe_strdup(needle);
char *result;
/* Make them both lowercase. Note: 'int' typecast fixes compiler warning on cygwin. */
for(i = 0; i < strlen(haystack_lc); i++)
haystack_lc[i] = tolower((int)haystack_lc[i]);
for(i = 0; i < strlen(needle_lc); i++)
needle_lc[i] = tolower((int)needle_lc[i]);
printf("Searching for '%s' in '%s'...\n", needle_lc, haystack_lc);
result = strstr(haystack_lc, needle_lc);
if(!result)
return NULL;
result = haystack + (haystack_lc - result);
safe_free(haystack_lc);
safe_free(needle_lc);
return result;
}