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