-
-
Notifications
You must be signed in to change notification settings - Fork 159
/
Copy pathdomain.c
152 lines (126 loc) · 4.42 KB
/
domain.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
/*
* SNOOPY COMMAND LOGGER
*
* File: snoopy/datasource/domain.c
*
* Copyright (c) 2015 Bostjan Skufca <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/*
* Includes order: from local to global
*/
// This needs to be defined before including sys/utsname.h,
// or struct utsname does not return domainname member.
#define _GNU_SOURCE
#include "domain.h"
#include "snoopy.h"
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
/*
* Local defines
*/
#define HOST_NAME_BUF_SIZE HOST_NAME_MAX + 2 // +1 for terminal \0 and +1 because we'll be adding a trailing dot
#define HOSTS_PATH "/etc/hosts"
#define HOSTS_LINE_SIZE_MAX 1024
#define HOSTS_LINE_POS_MAX 1023
/*
* SNOOPY DATA SOURCE: domain
*
* Description:
* Returns domain of this system.
*
* Params:
* result: pointer to string, to write result into
* arg: (ignored)
*
* Return:
* number of characters in the returned string, or SNOOPY_DATASOURCE_FAILURE
*/
int snoopy_datasource_domain (char * const resultBuf, size_t resultBufSize, __attribute__((unused)) char const * const arg)
{
FILE *fp;
char hostname[HOST_NAME_BUF_SIZE];
char line[HOSTS_LINE_SIZE_MAX];
int retVal;
int hostnameLen;
/*
* START: COPY FROM datasource/hostname
*/
/* Get my hostname first */
retVal = gethostname(hostname, HOST_NAME_MAX);
if (0 != retVal) {
return snprintf(resultBuf, resultBufSize, "(error @ gethostname(): %d)", errno);
}
// If hostname was something alien (longer than HOST_NAME_MAX), then the
// last character may not be NULL (the behavior is unspecified).
// Let's avoid any surprises and null-terminate at the end of this buffer.
hostname[HOST_NAME_BUF_SIZE-1] = '\0';
/*
* END: Copy from datasource/hostname
*/
/* Check hostname length */
hostnameLen = (int) strlen(hostname);
if (0 == hostnameLen) {
snprintf(resultBuf, resultBufSize, "Got empty hostname");
return SNOOPY_DATASOURCE_FAILURE;
}
if (hostnameLen > HOST_NAME_BUF_SIZE - 2) {
snprintf(resultBuf, resultBufSize, "INTERNAL ERROR: Got too long hostname, length: %d", hostnameLen);
return SNOOPY_DATASOURCE_FAILURE;
}
/* Add a dot at the end of hostname, that is what we are searching for */
hostname[hostnameLen] = '.';
hostname[hostnameLen+1] = '\0';
/* Try to open file in read mode */
fp = fopen(HOSTS_PATH, "r");
if (NULL == fp) {
snprintf(resultBuf, resultBufSize, "Unable to open file for reading: %s", HOSTS_PATH);
return SNOOPY_OUTPUT_FAILURE;
}
/* Read line by line */
const char *linePtr;
char *hashPtr;
char *lineEntryPtr;
char *savePtr;
char *domainPtr = NULL;
while (NULL != (linePtr = fgets(line, sizeof(line), fp))) {
/* Is line a comment - ignore everything after '#' character */
hashPtr = strchr(linePtr, '#');
if (NULL != hashPtr) {
*hashPtr = '\0';
}
/* Try to find "hostname." there */
lineEntryPtr = strcasestr(linePtr, hostname);
if (NULL != lineEntryPtr) {
strtok_r(lineEntryPtr, " \t\n\r", &savePtr);
/* Does not matter whether token has been found or not. */
/* If it was, strtok creates \0 at the end of it and returns pointer to it. */
/* If it was not, we must have met the EOF and just use everything. */
domainPtr = lineEntryPtr + strlen(hostname);
break;
}
}
/* Cleanup and return */
fclose(fp);
if (NULL != domainPtr) {
return snprintf(resultBuf, resultBufSize, "%s", domainPtr);
} else {
return snprintf(resultBuf, resultBufSize, "(none)");
}
}