File: yhost.c

package info (click to toggle)
yiff 2.06-3
  • links: PTS
  • area: main
  • in suites: potato
  • size: 3,464 kB
  • ctags: 3,028
  • sloc: ansic: 47,463; makefile: 218; sh: 77
file content (241 lines) | stat: -rw-r--r-- 4,497 bytes parent folder | download
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
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>

#include <sys/types.h>
#include <sys/socket.h>

#include <netdb.h>	/* For gethostbyname(). */
extern int h_errno;

#include <netinet/in.h>

#include <Y2/Y.h>
#include <Y2/Ylib.h>

#include "../include/string.h"


#ifndef DEF_CON_ARG
# define DEF_CON_ARG    "127.0.0.1:9433"
#endif            



/*
 *	Print help.
 */
void PrintUsage()
{
	printf("\
Usage: yhost [-] <address> [options]\n\
\n\
    Specifying a `-' argument means to remove the <address>.\n\
\n\
    <address> is the numeric IP or named address of the host you\n\
    want to allow/disallow access to the YIFF server.\n\
    This command must be runned on the same physical computer as\n\
    the YIFF server is running on.\n\
\n\
    [options] can be any of the following:\n\
\n\
        --recorder <address:port>    Specify which YIFF server to connect to.\n\
\n"
	);


	return;
}


int main(int argc, char *argv[])
{
	int i, status;

	char *con_arg = NULL;

	int op = 1;

	YConnection *con = NULL;
	YIPUnion ip;

	struct hostent *he;
        struct sockaddr_in haddr;


	if(argc < 2)
	{
	    PrintUsage();
	    return(1);
	}


	/* Parse arguments. */
	for(i = 1; i < argc; i++)
	{
	    if(argv[i] == NULL)
		continue;

	    /* Help. */
	    if(strcasepfx(argv[i], "--h") ||
               strcasepfx(argv[i], "-h") ||
               !strcmp(argv[i], "?")
	    )
	    {
		PrintUsage();
		return(0);
	    }

	    /* Add or remove? */
	    if(i == 1)
	    {
		if(!strcmp(argv[i], "-"))
		{
		    op = 0;	/* We know it's remove, continue. */
		    continue;
		}
		else if(!strcmp(argv[i], "+"))
		{
		    op = 1;	/* We know it's add, continue. */
                    continue;
		}
		else
		{
		    op = 1;
		}
	    }

	    /* Address. */
	    if((i == 1) ||
               (i == 2)
            )
	    {
		/* Parse address. */
		he = gethostbyname(argv[i]);
		if(he == NULL)
		{
		    switch(h_errno)
		    {
                      case NO_ADDRESS:
                        fprintf(stderr,
                            "%s: No associated IP address.\n",
                            argv[i]
                        );
                        break;

                      case NO_RECOVERY:
                        fprintf(stderr,
                            "%s: Unrecoverable error.\n",
                            argv[i]
                        );
                        break;

                      case TRY_AGAIN:
                        fprintf(stderr,
                            "%s: Name server unavailable.\n",
                            argv[i]
                        );
                        break;

                      default:
                        fprintf(stderr,
                            "%s: Unknown host.\n",
                            argv[i]
                        );
                        break;
		    }

		    free(con_arg);
		    con_arg = NULL;

		    return(1);
		}

		haddr.sin_addr = *((struct in_addr *)he->h_addr);
		ip.whole = (u_int32_t)haddr.sin_addr.s_addr;

		continue;
	    }

	    /* Connect address. */
	    if(strcasepfx(argv[i], "--rec") ||
               strcasepfx(argv[i], "-rec")
	    )
	    {
		i++;
		if(i < argc)
		{
		    free(con_arg);
		    con_arg = StringCopyAlloc(argv[i]);
		}
		else
		{
		    fprintf(stderr,
			"%s: Requires argument.\n",
			argv[i - 1]
		    );
		}
	    }
	}


	/*
	 *	Connect to YIFF server.
	 */
	con = YOpenConnection(
	    NULL,		/* No start argument. */
	    con_arg
	);
	if(con == NULL)
	{
	    printf("%s: Cannot connect to YIFF server.\n",
		(con_arg == NULL) ? DEF_CON_ARG : con_arg
	    );

            free(con_arg);
            con_arg = NULL;

	    return(-1);
	}


	/*
	 *	Add or remove host.
	 */
	if(op)
	    status = YAddHost(con, &ip);
	else
	    status = YRemoveHost(con, &ip);        

	if(status)
            printf("Cannot %s %i.%i.%i.%i.\n",
		(op == 0) ? "remove" : "add",
                ip.charaddr[0],
                ip.charaddr[1],
                ip.charaddr[2],
                ip.charaddr[3]
            );
	else
            printf("%s %i.%i.%i.%i %s host access list.\n",
                ((op == 0) ? "Removing" : "Adding"),
                ip.charaddr[0],
                ip.charaddr[1],
                ip.charaddr[2],
                ip.charaddr[3],
		((op == 0) ? "from" : "to")
            );

	/*
	 *	Disconnect from YIFF server
	 */
	YCloseConnection(con, False);
	con = NULL;

	free(con_arg);
	con_arg = NULL;



	return(0);
}