f940602e0f049f53d95b184296401b3366eb51b1
[citadel.git] / citadel / migratenet.c
1 /*
2  * $Id$
3  * 
4  * 5.80 to 5.90 migration utility for network files
5  *
6  */
7
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <stdio.h>
11 #include <string.h>
12 #include <sys/types.h>
13 #include <dirent.h>
14 #include <errno.h>
15 #ifdef HAVE_LIMITS_H
16 #include <limits.h>
17 #endif
18 #include "citadel.h"
19 #include "ipc.h"
20 #include "tools.h"
21 #include "config.h"
22
23 struct mn_roomlist {
24         struct mn_roomlist *next;
25         char roomname[SIZ];
26 };
27
28 void logoff(int code)
29 {
30         exit(code);
31 }
32
33 int main(int argc, char **argv)
34 {
35         char buf[SIZ];
36         char hostbuf[SIZ];
37         char portbuf[SIZ];
38         DIR *dp;
39         struct dirent *d;
40         long highest = 0L;
41         char node[SIZ];
42         char room[SIZ];
43         long thighest;
44         FILE *fp;
45         FILE *roomfp;
46         char roomfilename[SIZ];
47         FILE *nodefp;
48         char nodefilename[SIZ];
49         struct mn_roomlist *mn = NULL;
50         struct mn_roomlist *mnptr = NULL;
51
52         printf("\n\n\n\n\n"
53 "This utility migrates your network settings (room sharing with other\n"
54 "Citadel systems) from 5.80 to 5.90.  You should only do this ONCE.  It\n"
55 "will ERASE your 5.80 configuration files when it is finished, and it will\n"
56 "ERASE any 5.90 configuration files that you have already set up.\n\n"
57 "Are you sure you want to do this? ");
58
59         gets(buf);
60         if (tolower(buf[0]) != 'y') exit(0);
61
62         get_config();
63
64         attach_to_server(argc, argv, hostbuf, portbuf);
65         serv_gets(buf);
66         printf("%s\n", &buf[4]);
67         if ( (buf[0]!='2') && (strncmp(buf,"551",3)) ) {
68                 fprintf(stderr, "%s: %s\n", argv[0], &buf[4]);
69                 logoff(atoi(buf));
70         }
71
72         sprintf(buf, "IPGM %d", config.c_ipgm_secret);
73         serv_puts(buf);
74         serv_gets(buf);
75         fprintf(stderr, "%s\n", &buf[4]);
76         if (buf[0] != '2') {
77                 exit(2);
78         }
79
80         if (chdir("network/systems") != 0) {
81                 perror("cannot chdir network/systems");
82                 exit(errno);
83         }
84
85         strcpy(roomfilename, tmpnam(NULL));
86         roomfp = fopen(roomfilename, "w");
87         if (roomfp == NULL) {
88                 perror("cannot open temp file");
89                 exit(errno);
90         }
91
92         strcpy(nodefilename, tmpnam(NULL));
93         nodefp = fopen(nodefilename, "w");
94         if (roomfp == NULL) {
95                 perror("cannot open temp file");
96                 exit(errno);
97         }
98
99         dp = opendir(".");
100         if (dp == NULL) {
101                 perror("cannot open directory");
102                 exit(errno);
103         }
104
105         while (d = readdir(dp), d != NULL) {
106                 fp = NULL;
107                 if ( (d->d_name[0] != '.') && strcasecmp(d->d_name, "CVS")) {
108                         fp = fopen(d->d_name, "r");
109                 }
110                 if (fp != NULL) {
111                         printf("\n*** Processing '%s'\n", d->d_name);
112
113                         fprintf(nodefp, "%s|", d->d_name);
114                         printf("Enter shared secret: ");
115                         gets(buf);
116                         if (buf[0] == 0) strcpy(buf, config.c_net_password);
117                         fprintf(nodefp, "%s|", buf);
118                         printf("Enter host name/IP : ");
119                         gets(buf);
120                         if (buf[0] == 0) sprintf(buf, "%s.citadel.org",
121                                 d->d_name);
122                         fprintf(nodefp, "%s|", buf);
123                         printf("Enter port number  : ");
124                         gets(buf);
125                         if (buf[0] == 0) strcpy(buf, "504");
126                         fprintf(nodefp, "%s\n", buf);
127
128                         fgets(buf, sizeof buf, fp);
129                         printf("skipping: %s", buf);
130                         while (fgets(room, sizeof room, fp),
131                               (fgets(buf, sizeof buf, fp) != NULL) ) {
132                                 thighest = atol(buf),
133                                 room[strlen(room) - 1] = 0;
134                                 fprintf(roomfp, "%s|%s\n",
135                                         d->d_name, room);
136                                 if (thighest > highest) {
137                                         highest = thighest;
138                                 }
139                         }
140                         fclose(fp);
141                 }
142         }
143
144         closedir(dp);
145         fclose(roomfp);
146         fclose(nodefp);
147
148         /* Point of no return */
149
150         /* Set up the node table */
151         printf("Creating neighbor node table\n");
152         sprintf(buf, "CONF putsys|%s", IGNETCFG);
153         serv_puts(buf);
154         serv_gets(buf);
155         if (buf[0] == '4') {
156                 nodefp = fopen(nodefilename, "r");
157                 if (nodefp != NULL) {
158                         while (fgets(buf, sizeof buf, nodefp) != NULL) {
159                                 buf[strlen(buf)-1] = 0;
160                                 serv_puts(buf);
161                         }
162                         fclose(nodefp);
163                 }
164                 serv_puts("000");
165         }
166         else {
167                 printf("%s\n", &buf[4]);
168         }
169
170
171         /* Now go through the table looking for node names to enter */
172
173         sprintf(buf, "cat %s |awk -F \"|\" '{ print $2 }' |sort -f |uniq -i",
174                 roomfilename);
175         roomfp = popen(buf, "r");
176         if (roomfp == NULL) {
177                 perror("cannot open pipe");
178                 unlink(roomfilename);
179         }
180
181         while (fgets(room, sizeof room, roomfp) != NULL) {
182                 room[strlen(room)-1] = 0;
183                 if (strlen(room) > 0) {
184                         mnptr = (struct mn_roomlist *)
185                                 malloc(sizeof (struct mn_roomlist));
186                         strcpy(mnptr->roomname, room);
187                         mnptr->next = mn;
188                         mn = mnptr;
189                 }
190         }
191
192         pclose(roomfp);
193
194         /* Enter configurations for each room... */
195         while (mn != NULL) {
196                 printf("Room <%s>\n", mn->roomname);
197
198                 sprintf(buf, "GOTO %s", mn->roomname);
199                 serv_puts(buf);
200                 serv_gets(buf);
201                 printf("%s\n", &buf[4]);
202                 if (buf[0] != '2') goto roomerror;
203
204                 serv_puts("SNET");
205                 serv_gets(buf);
206                 if (buf[0] != '4') goto roomerror;
207
208                 sprintf(buf, "lastsent|%ld", highest);
209                 serv_puts(buf);
210
211                 roomfp = fopen(roomfilename, "r");
212                 if (roomfp != NULL) {
213                         while (fgets(buf, sizeof buf, roomfp) != NULL) {
214                                 buf[strlen(buf)-1] = 0;
215                                 extract(node, buf, 0);
216                                 extract(room, buf, 1);
217                                 if (!strcasecmp(room, mn->roomname)) {
218                                         sprintf(buf, 
219                                                 "ignet_push_share|%s", node);
220                                         serv_puts(buf);
221                                 }
222                         }
223                         fclose(roomfp);
224                 }
225
226                 serv_puts("000");
227
228 roomerror:      /* free this record */
229                 mnptr = mn->next;
230                 free(mn);
231                 mn = mnptr;
232         }
233
234         unlink(roomfilename);
235         unlink(nodefilename);
236
237         printf("\n\n"
238                 "If this conversion was successful, you do not need your\n"
239                 "old network configuration files.  Delete them now? "
240         );
241
242         gets(buf);
243         if (tolower(buf[0]) != 'y') exit(0);
244
245         get_config();
246         system("rm -fr ./network/systems");
247         system("rm -f ./network/mail.sysinfo");
248         system("rm -f ./network/internetmail.config");
249
250         exit(0);
251 }