]> code.citadel.org Git - citadel.git/blob - citadel/migratenet.c
* more work on the net migrator
[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
21 void logoff(int code)
22 {
23         exit(code);
24 }
25
26 int main(int argc, char **argv)
27 {
28         char buf[SIZ];
29         char hostbuf[SIZ];
30         char portbuf[SIZ];
31         DIR *dp;
32         struct dirent *d;
33         long highest = 0L;
34         char node[SIZ];
35         char room[SIZ];
36         long thighest;
37         FILE *fp;
38         FILE *roomfp;
39         char roomfilename[SIZ];
40         FILE *nodefp;
41         char nodefilename[SIZ];
42
43         printf("\n\n\n\n\n"
44 "This utility migrates your network settings (room sharing with other\n"
45 "Citadel systems) from 5.80 to 5.90.  You should only do this ONCE.  It\n"
46 "will ERASE your 5.80 configuration files when it is finished, and it will\n"
47 "ERASE any 5.90 configuration files that you have already set up.\n\n"
48 "Are you sure you want to do this? ");
49
50         gets(buf);
51         if (tolower(buf[0]) != 'y') exit(0);
52
53         get_config();
54
55         attach_to_server(argc, argv, hostbuf, portbuf);
56         serv_gets(buf);
57         printf("%s\n", &buf[4]);
58         if ( (buf[0]!='2') && (strncmp(buf,"551",3)) ) {
59                 fprintf(stderr, "%s: %s\n", argv[0], &buf[4]);
60                 logoff(atoi(buf));
61         }
62
63         sprintf(buf, "IPGM %d", config.c_ipgm_secret);
64         serv_puts(buf);
65         serv_gets(buf);
66         fprintf(stderr, "%s\n", &buf[4]);
67         if (buf[0] != '2') {
68                 exit(2);
69         }
70
71         if (chdir("network/systems") != 0) {
72                 perror("cannot chdir network/systems");
73                 exit(errno);
74         }
75
76         strcpy(roomfilename, tmpnam(NULL));
77         roomfp = fopen(roomfilename, "w");
78         if (roomfp == NULL) {
79                 perror("cannot open temp file");
80                 exit(errno);
81         }
82
83         strcpy(nodefilename, tmpnam(NULL));
84         nodefp = fopen(nodefilename, "w");
85         if (roomfp == NULL) {
86                 perror("cannot open temp file");
87                 exit(errno);
88         }
89
90         dp = opendir(".");
91         if (dp == NULL) {
92                 perror("cannot open directory");
93                 exit(errno);
94         }
95
96         while (d = readdir(dp), d != NULL) {
97                 fp = NULL;
98                 if ( (d->d_name[0] != '.') && strcasecmp(d->d_name, "CVS")) {
99                         fp = fopen(d->d_name, "r");
100                 }
101                 if (fp != NULL) {
102                         printf("\n*** Processing '%s'\n", d->d_name);
103
104                         fprintf(nodefp, "%s|", d->d_name);
105                         printf("Enter shared secret: ");
106                         gets(buf);
107                         fprintf(nodefp, "%s|", buf);
108                         printf("Enter host name/IP : ");
109                         gets(buf);
110                         fprintf(nodefp, "%s|", buf);
111                         printf("Enter port number  : ");
112                         gets(buf);
113                         fprintf(nodefp, "%s\n", buf);
114
115                         while (fgets(room, sizeof room, fp),
116                                 fscanf(fp, "%ld\n", &thighest),
117                                 !feof(fp) ) {
118                                         room[strlen(room) - 1] = 0;
119                                         fprintf(roomfp, "%s|%s\n",
120                                                 d->d_name, room);
121                                         if (thighest > highest) {
122                                                 highest = thighest;
123                                         }
124                         }
125                         fclose(fp);
126                 }
127         }
128
129         closedir(dp);
130         fclose(roomfp);
131         fclose(nodefp);
132
133         /* Point of no return */
134
135         /* Set up the node table */
136         printf("Creating neighbor node table\n");
137         sprintf(buf, "CONF putsys|%s", IGNETCFG);
138         serv_puts(buf);
139         serv_gets(buf);
140         if (buf[0] == '4') {
141                 nodefp = fopen(nodefilename, "r");
142                 if (nodefp != NULL) {
143                         while (fgets(buf, sizeof buf, nodefp) != NULL) {
144                                 buf[strlen(buf)-1] = 0;
145                                 serv_puts(buf);
146                         }
147                         fclose(nodefp);
148                 }
149                 serv_puts("000");
150         }
151         else {
152                 printf("%s\n", &buf[4]);
153         }
154
155
156         /* Now go through the table looking for node names to enter */
157
158         sprintf(buf, "cat %s |awk '{ FS=\"|\"; print $2 }' |sort -f |uniq -i",
159                 roomfilename);
160         roomfp = popen(buf, "r");
161         if (roomfp == NULL) {
162                 perror("cannot open pipe");
163                 unlink(roomfilename);
164         }
165
166         while (fgets(room, sizeof room, roomfp) != NULL) {
167                 room[strlen(room)-1] = 0;
168                 if (strlen(room) > 0) {
169                         printf("Room <%s>\n", room);
170                 }
171         }
172
173         pclose(roomfp);
174
175         unlink(roomfilename);
176         unlink(nodefilename);
177         return(0);
178 }
179
180
181