1 /******************************************************************************
2  * FIDOCONFIG --- library for fidonet configs
3  ******************************************************************************
4  * fidoconfig to binkd config converter:
5  *     generate passwords file or
6  *     simple binkd config file (may include it into real config)
7  *
8  * Copyright (C) 2002
9  * Stas Degteff
10  * Fido:     2:5080/102
11  * Internet: g@grumbler.org
12  * Copyrigth (c) Husky Developers Team
13  * http://husky.sourceforge.net
14  *
15  * This file is part of FIDOCONFIG.
16  *
17  * This library is free software; you can redistribute it and/or
18  * modify it under the terms of the GNU Library General Public
19  * License as published by the Free Software Foundation; either
20  * version 2 of the License, or (at your option) any later version.
21  *
22  * This library is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
25  * Library General Public License for more details.
26  *
27  * You should have received a copy of the GNU Library General Public
28  * License along with this library; see file COPYING. If not, write to the Free
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
30  *
31  * See also http://www.gnu.org
32  *****************************************************************************/
33 
34 #include <stdlib.h>
35 #include <errno.h>
36 #include <stdio.h>
37 #include <string.h>
38 
39 #include <huskylib/compiler.h>
40 
41 #ifdef HAS_IO_H
42 #  include <io.h>
43 #endif
44 #ifdef HAS_UNISTD_H
45 #  include <unistd.h>
46 #endif
47 
48 #include "fidoconf.h"
49 #include "common.h"
50 
51 #ifndef VERSION_H
52 #define VERSION_H
53 
54 #include "version.h"
55 #include "../cvsdate.h"
56 
57 #endif
58 
59 /* ANSI C knows nothing about this constant */
60 #ifndef F_OK
61 #define F_OK 0
62 #endif
63 
64 char *program_name=NULL;
65 #define COPYRIGHT "(c) Stas Degteff 2:5080/102 g@grumbler.org\n(c) The Husky Team http://husky.sourceforge.net/team.html"
66 
67 #define BINKDLOG "binkd.log"
68 
69 int force_flag = 0; /* Force rewriting flag */
70 
printversion(FILE * fd)71 void printversion(FILE *fd){
72      char *temp;
73      fprintf(fd, "%s\n\n", temp=GenVersionStr( "fconf2binkd", FC_VER_MAJOR,
74 			FC_VER_MINOR, FC_VER_PATCH, FC_VER_BRANCH, cvs_date ));
75      nfree(temp);
76 }
77 
usage(int retcode)78 void usage(int retcode){
79       printversion(stdout);
80       printf(": fidoconfig to binkd config converter\n\n" COPYRIGHT "\n");
81       printf("\nUsage:\n");
82       printf("%s [-v] [-h] [-c path/to/fidoconfig] [-f] [-p] [output_file_name]\n", program_name);
83       printf("where\n");
84       printf("\t-v\tprint version and exit\n");
85       printf("\t-h\tprint usage information\n");
86       printf("\t-c\tspecify alternate fidoconfig\n");
87       printf("\t-f\tforce owerwrite file\n");
88       printf("\t-p\tgenerate passwords file\n");
89       exit(retcode);
90 }
91 
createOutputFile(const char * ofname)92 FILE *createOutputFile(const char *ofname){
93   FILE *ofd;
94 
95   if( ofname ){
96     if( !(access(ofname,F_OK) || force_flag) ){
97       fprintf( stderr, "File '%s' exist!", ofname );
98       return NULL;
99     }
100     if( (ofd=fopen(ofname, "w+t")) == NULL ){
101       fprintf( stderr, "Can't open file '%s': %s!", ofname, strerror(errno) );
102       return NULL;
103     }
104   }else ofd=stdout;
105   return ofd;
106 }
107 
108  /* doublicate backslash in output */
printPath(char * path,FILE * ofd)109 int printPath(char *path, FILE *ofd){
110   int rc=1;
111 
112   for( ; rc && *path; path++ ){
113       if( *path=='\\' ) rc = (EOF != fprintf( ofd, "\\\\"));
114       else rc = fputc(*path, ofd);
115   }
116   return !rc;
117 }
118 
119 /* Write passwords file:
120  * ;comment
121  * <address> <password>
122  * <address>
123  */
writePasswords(s_fidoconfig * config,char * ofname)124 int writePasswords( s_fidoconfig *config, char * ofname ){
125   FILE *ofd=NULL;
126   unsigned ll = config->linkCount;
127 
128   ofd = createOutputFile(ofname);
129 
130   if( !ofd ) /* can't create */
131     return 2;
132 
133   fprintf( ofd, "; binkd / t-mail passwords file\n; generated by ");
134   printversion( ofd );
135   fprintf( ofd, "\n\n" );
136 
137   for( ; ll-- ; ){
138     fprintf( ofd, "%s %s\n",
139             aka2str(config->links[ll]->hisAka),
140             config->links[ll]->sessionPwd ? config->links[ll]->sessionPwd
141                                          : (config->links[ll]->defaultPwd ?
142                                             config->links[ll]->defaultPwd : "" )
143            );
144   }
145 
146   return 0;
147 }
148 
149 /* Write max. possible binkd config
150  *
151  */
writeBinkdConfig(s_fidoconfig * config,const char * ofname)152 int writeBinkdConfig( s_fidoconfig *config, const char *ofname ){
153   FILE *ofd=NULL;
154   int rc=0;
155   unsigned ll;
156 
157   ofd = createOutputFile(ofname);
158 
159   if( !ofd ) /* can't create */
160     return 2;
161 
162   fprintf( ofd, "# binkd config file\n# generated by ");
163   printversion( ofd );
164 
165   /* Address, paths & etc. */
166     fprintf( ofd, "\n# The name of your system, it's location, and your name\n");
167     if( config->name )
168        fprintf( ofd, "sysname   \"%s\"\n", config->name );
169     if( config->location )
170       fprintf( ofd, "location  \"%s\"\n", config->location );
171     if( config->sysop )
172       fprintf( ofd, "sysop     \"%s\"\n\n", config->sysop );
173 
174     fprintf( ofd, "# Your addresses, 4D or 5D:\naddress" );
175     for( ll=config->addrCount ; ll-- ; ){
176       fprintf( ofd, " %s", aka2str(config->addr[ll]) );
177     }
178 
179     fprintf( ofd, "\n\n# Your FTN domains:\ndomain fidonet " );
180     printPath( config->outbound, ofd );
181     fprintf( ofd, " %u\n\n", config->addr->zone);
182 
183     fprintf( ofd, "# Path and name for the logfile\nlog " );
184     printPath( config->logFileDir, ofd );
185     fprintf( ofd, BINKDLOG "\n" );
186     if( config->protInbound ){
187       fprintf( ofd, "\n# Inbound directory for secure links\ninbound " );
188       printPath( config->protInbound, ofd );
189     }
190     if( config->inbound ){
191       fprintf( ofd, "\n\n# Inbound directory for non-secure links\ninbound-nonsecure " );
192       printPath( config->inbound, ofd );
193     }
194     if( config->minDiskFreeSpace )
195       fprintf( ofd, "\n\n# Free space limit for receive next file\nminfree %u\n", config->minDiskFreeSpace );
196     else fputc( '\n', ofd );
197 
198   /* Links:
199    Set out-box flavor to "crash"
200    */
201 
202   fprintf( ofd, "\n# Define a link:\n"
203           "# node [[z:]n/]n[.p][@domain] [-nr] [{hosts|-} [{pwd|-} [flavour [{obox|-} [{ibox|-}]]]]]\n"
204          );
205   for( ll=config->linkCount ; ll-- ; ){
206     fprintf( ofd, "Node %s * %s %c %s\n",
207             aka2str(config->links[ll]->hisAka),
208             config->links[ll]->sessionPwd ? config->links[ll]->sessionPwd
209                                          : (config->links[ll]->defaultPwd ?
210                                             config->links[ll]->defaultPwd : "" ),
211              config->links[ll]->fileBox ? 'c' : ' ',
212              config->links[ll]->fileBox ? config->links[ll]->fileBox : ""
213            );
214   }
215 
216   return rc;
217 }
218 
main(int argc,char * argv[])219 int main (int argc, char *argv[]) {
220    s_fidoconfig *config=NULL;
221    char *ofname=NULL;  /* output file name */
222    char *fcfname=NULL; /* fidoconfig file name */
223    char *p=NULL;
224    int i=1, rc=0;
225    unsigned passwords_list_only=0;
226 
227    /* Constucte program name from calling string */
228    p=strrchr(argv[0],PATH_DELIM);
229    if( p ) program_name = p+1;
230    else program_name = "fconf2binkd";
231 
232    for( ; i<argc; i++ ){
233      if( argv[i][0] != '-' ){ /* parameter: output filename */
234        ofname = sstrdup( argv[i] );
235        break;
236      }
237      switch( argv[i][1] ){
238      case 'v':
239      case 'V': /* version request */
240                printversion(stdout);
241                printf( "\n" );
242 
243                return 1;
244      case 'h':
245      case 'H': /* help request */
246                usage(1);
247      case 'f': /* Force rewriting file */
248                force_flag = 1;
249                break;
250      case 'c': /* fidoconfig file name */
251                if( ++i<argc ) fcfname = sstrdup( argv[i] );
252                else{
253                  fprintf(stderr, "'%s' option require parameter!\n", argv[i-1]);
254                  usage(-1);
255                }
256                break;
257      case 'p': /* generate passwords list */
258                passwords_list_only=1;
259                break;
260      default:  /* unknown option */
261                fprintf(stderr,"Illegal parameter: %s\n",argv[i]);
262                usage(1);
263      }
264    }
265 
266    config = readConfig(fcfname);
267    if ( config ) {
268      if( ofname ) /* if not defined output binkd config to stdout */
269        printf("Generate binkd %s file %s\n", passwords_list_only ? "passwords" : "config" , ofname);
270 
271      if( passwords_list_only )
272        rc = writePasswords( config, ofname );
273      else
274        rc = writeBinkdConfig( config, ofname );
275 
276      disposeConfig( config );
277      return rc;
278    }
279 
280    return 1;
281 }
282