1 /* tools/convertilines.c
2 * Copyright (c) 2002 Hybrid Development Team
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * 1.Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2.Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3.The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
20 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
25 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
28 * $Id: convertilines.c 6 2005-09-10 01:02:21Z nenolod $
38 #define FLAGS_RESTRICTED 0x001
39 #define FLAGS_EXCEEDLIMIT 0x002
40 #define FLAGS_KLINEEXEMPT 0x004
41 #define FLAGS_NEEDIDENT 0x010
42 #define FLAGS_NOTILDE 0x020
44 struct flag_table_struct
49 static struct flag_table_struct flag_table
[] =
51 { "restricted", FLAGS_RESTRICTED
},
52 { "exceed_limit", FLAGS_EXCEEDLIMIT
},
53 { "kline_exempt", FLAGS_KLINEEXEMPT
},
54 { "need_ident", FLAGS_NEEDIDENT
},
55 { "no_tilde", FLAGS_NOTILDE
},
61 struct AuthBlock
*next
;
71 /* indicates one of above */
76 static struct AuthBlock
*auth_spoof
= NULL
;
77 static struct AuthBlock
*auth_special
= NULL
;
78 static struct AuthBlock
*auth_passwd
= NULL
;
79 static struct AuthBlock
*auth_general
= NULL
;
80 static struct AuthBlock
*auth_restricted
= NULL
;
82 static void ConvertConf(FILE* file
,FILE *out
);
83 static void set_flags(struct AuthBlock
*, const char *, const char *);
84 static void usage(void);
85 static char *getfield(char *);
86 static struct AuthBlock
*find_matching_conf(struct AuthBlock
*);
87 static void ReplaceQuotes(char *out
, char *in
);
88 static void oldParseOneLine(FILE *out
, char *in
);
89 static void write_auth_entries(FILE *out
);
90 static void write_specific(FILE *out
, struct AuthBlock
*);
91 static int match(struct AuthBlock
*, struct AuthBlock
*);
93 int main(int argc
,char *argv
[])
101 if((in
= fopen(argv
[1],"r")) == NULL
)
103 fprintf(stderr
, "Can't open %s for reading\n", argv
[1]);
107 if((out
= fopen(argv
[2],"w")) == NULL
)
109 fprintf(stderr
, "Can't open %s for writing\n", argv
[2]);
113 ConvertConf(in
, out
);
120 fprintf(stderr
, "convertilines conf.old conf.new\n");
126 * Read configuration file.
129 * Inputs - FILE* to config file to convert
130 * - FILE* to output for new style conf
134 #define MAXCONFLINKS 150
136 static void ConvertConf(FILE* file
, FILE *out
)
139 char quotedLine
[BUFSIZE
];
142 while (fgets(line
, sizeof(line
), file
))
144 if ((p
= strchr(line
, '\n')))
147 ReplaceQuotes(quotedLine
,line
);
149 if(!*quotedLine
|| quotedLine
[0] == '#' || quotedLine
[0] == '\n' ||
150 quotedLine
[0] == ' ' || quotedLine
[0] == '\t')
153 if(quotedLine
[0] == '.')
158 if(!strncmp(quotedLine
+1,"include ",8))
160 if( (filename
= strchr(quotedLine
+8,'"')) )
164 fprintf(stderr
, "Bad config line: %s", quotedLine
);
168 if((back
= strchr(filename
,'"')))
172 fprintf(stderr
, "Bad config line: %s", quotedLine
);
179 /* Could we test if it's conf line at all? -Vesa */
180 if (quotedLine
[1] == ':')
181 oldParseOneLine(out
,quotedLine
);
187 write_auth_entries(out
);
193 * Inputs - input line to quote
194 * Output - quoted line
195 * Side Effects - All quoted chars in input are replaced
196 * with quoted values in output, # chars replaced with '\0'
197 * otherwise input is copied to output.
199 static void ReplaceQuotes(char* quotedLine
,char *inputLine
)
203 static char quotes
[] = {
235 * Do quoting of characters and # detection.
237 for (out
= quotedLine
,in
= inputLine
; *in
; out
++, in
++)
247 *out
= quotes
[ (unsigned int) (*in
& 0x1F) ];
262 * Inputs - pointer to line to parse
263 * - pointer to output to write
265 * Side Effects - Parse one old style conf line.
268 static void oldParseOneLine(FILE *out
,char* line
)
272 const char* host_field
=NULL
;
273 const char* passwd_field
=NULL
;
274 const char* user_field
=NULL
;
275 const char* port_field
= NULL
;
276 const char* classconf_field
= NULL
;
279 tmp
= getfield(line
);
283 for (;;) /* Fake loop, that I can use break here --msa */
286 if ((host_field
= getfield(NULL
)) == NULL
)
290 if ((passwd_field
= getfield(NULL
)) == NULL
)
294 if ((user_field
= getfield(NULL
)) == NULL
)
298 if ((port_field
= getfield(NULL
)) == NULL
)
302 if ((classconf_field
= getfield(NULL
)) == NULL
)
315 class_field
= atoi(classconf_field
);
317 switch( conf_letter
)
322 struct AuthBlock
*ptr
;
323 struct AuthBlock
*tempptr
;
325 tempptr
= malloc(sizeof(struct AuthBlock
));
326 memset(tempptr
, 0, sizeof(*tempptr
));
328 if(conf_letter
== 'i')
330 tempptr
->flags
|= FLAGS_RESTRICTED
;
331 tempptr
->specialk
= 1;
334 if(passwd_field
&& *passwd_field
)
335 tempptr
->passwd
= strdup(passwd_field
);
337 tempptr
->class = class_field
;
339 set_flags(tempptr
, user_field
, host_field
);
341 /* dont add specials/passworded ones to existing auth blocks */
342 if((ptr
= find_matching_conf(tempptr
)))
346 authindex
= ptr
->hostnum
;
349 ptr
->hostname
= realloc((void *)ptr
->hostname
, ptr
->hostnum
* sizeof(void *));
351 ptr
->hostname
[authindex
] = strdup(tempptr
->hostname
[0]);
353 free(tempptr
->hostname
[0]);
354 free(tempptr
->hostname
);
363 ptr
->next
= auth_spoof
;
366 else if(ptr
->special
)
368 ptr
->next
= auth_special
;
373 ptr
->next
= auth_passwd
;
376 else if(ptr
->specialk
)
378 ptr
->next
= auth_restricted
;
379 auth_restricted
= ptr
;
383 ptr
->next
= auth_general
;
395 static void write_auth_entries(FILE *out
)
397 struct AuthBlock
*ptr
;
399 for(ptr
= auth_spoof
; ptr
; ptr
= ptr
->next
)
400 write_specific(out
, ptr
);
402 for(ptr
= auth_special
; ptr
; ptr
= ptr
->next
)
403 write_specific(out
, ptr
);
405 for(ptr
= auth_passwd
; ptr
; ptr
= ptr
->next
)
406 write_specific(out
, ptr
);
408 for(ptr
= auth_general
; ptr
; ptr
= ptr
->next
)
409 write_specific(out
, ptr
);
411 for(ptr
= auth_restricted
; ptr
; ptr
= ptr
->next
)
412 write_specific(out
, ptr
);
416 static void write_specific(FILE *out
, struct AuthBlock
*ptr
)
421 fprintf(out
, "auth {\n");
423 for(i
= 0; i
< ptr
->hostnum
; i
++)
424 fprintf(out
, "\tuser = \"%s\";\n", ptr
->hostname
[i
]);
427 fprintf(out
, "\tspoof = \"%s\";\n", ptr
->spoof
);
430 fprintf(out
, "\tpassword = \"%s\";\n", ptr
->passwd
);
434 fprintf(out
, "\tflags = ");
436 for(i
= 0; flag_table
[i
].flag
; i
++)
438 if(ptr
->flags
& flag_table
[i
].flag
)
450 fprintf(out
, "\tclass = \"%d\";\n", ptr
->class);
451 fprintf(out
, "};\n");
455 * field breakup for ircd.conf file.
457 static char *getfield(char *newline
)
459 static char *line
= NULL
;
469 if ((end
= strchr(line
,':')) == NULL
)
472 if ((end
= strchr(field
,'\n')) == NULL
)
473 end
= field
+ strlen(field
);
481 struct AuthBlock
*find_matching_conf(struct AuthBlock
*acptr
)
483 struct AuthBlock
*ptr
;
485 for(ptr
= auth_spoof
; ptr
; ptr
= ptr
->next
)
487 if(match(ptr
, acptr
))
491 for(ptr
= auth_special
; ptr
; ptr
= ptr
->next
)
493 if(match(ptr
, acptr
))
497 for(ptr
= auth_passwd
; ptr
; ptr
= ptr
->next
)
499 if(match(ptr
, acptr
))
503 for(ptr
= auth_restricted
; ptr
; ptr
= ptr
->next
)
505 if(match(ptr
, acptr
))
509 for(ptr
= auth_general
; ptr
; ptr
= ptr
->next
)
511 if(match(ptr
, acptr
))
519 static int match(struct AuthBlock
*ptr
, struct AuthBlock
*acptr
)
521 if((ptr
->class == acptr
->class) &&
522 (ptr
->flags
== acptr
->flags
))
526 /* check the spoofs match.. */
540 /* now check the passwords match.. */
560 void set_flags(struct AuthBlock
*ptr
, const char *user_field
, const char *host_field
)
562 for(; *user_field
; user_field
++)
568 ptr
->spoof
= strdup(host_field
);
574 ptr
->flags
|= FLAGS_NOTILDE
;
579 ptr
->flags
|= FLAGS_NEEDIDENT
;
583 case '^': /* is exempt from k/g lines */
584 ptr
->flags
|= FLAGS_KLINEEXEMPT
;
589 ptr
->flags
|= FLAGS_EXCEEDLIMIT
;
603 authindex
= ptr
->hostnum
;
606 ptr
->hostname
= realloc((void *)ptr
->hostname
, ptr
->hostnum
* sizeof(void *));
608 /* if the IP field contains something useful, use that */
609 if(strcmp(host_field
, "NOMATCH") && (*host_field
!= 'x') &&
610 strcmp(host_field
, "*") && !ptr
->spoof
)
611 ptr
->hostname
[authindex
] = strdup(host_field
);
613 ptr
->hostname
[authindex
] = strdup(user_field
);