4 * (c) Copyright 1989 by the Massachusetts Institute of Technology.
5 * For copying and distribution information, please see the file
9 #include <mit-copyright.h>
20 #include <moira_site.h>
23 /* The following enables the processing of .root instances */
26 EXEC SQL INCLUDE sqlca;
28 EXEC SQL BEGIN DECLARE SECTION;
29 char db[33] = "moira";
30 EXEC SQL END DECLARE SECTION;
32 #define min(x,y) ((x) < (y) ? (x) : (y))
33 char *whoami = "sync";
56 struct member *members;
66 if (argc > 2 && !strcmp(argv[1], "-db")) {
67 strncpy(db, argv[2], sizeof(db)-1);
72 fprintf(stderr, "usage: %s [-db moira] outfile\n", whoami);
76 dbase_fd = open(argv[1], O_RDWR|O_CREAT|O_TRUNC, 0660);
78 perror("opening file %s", argv[1]);
82 initialize_sms_error_table();
83 initialize_pt_error_table();
84 Initdb(); /* Initialize prdb */
87 users = create_hash(10000);
88 groups = create_hash(15000);
90 strings = create_hash(1000);
93 EXEC SQL WHENEVER SQLERROR CALL sqlerr;
96 EXEC SQL SET LOCKMODE SESSION WHERE LEVEL=TABLE, READLOCK=SHARED;
99 EXEC SQL DATABASE :db;
106 fprintf(stderr, "Done (%d users, %d groups, %d kerberos, %d members): %s",
107 ucount, gcount, kcount, mcount, ctime(&t));
113 EXEC SQL CLOSE DATABASE;
122 EXEC SQL BEGIN DECLARE SECTION;
123 char login[9], name[33];
125 EXEC SQL END DECLARE SECTION;
128 struct prentry tentry;
132 fprintf(stderr, "Doing users: %s", ctime(&t));
135 EXEC SQL SELECT modtime INTO :name FROM users WHERE users_id = 0;
137 EXEC SQL DECLARE u_cursor CURSOR FOR
138 SELECT u.login, u.uid, u.users_id
140 WHERE u.uid > 0 AND (u.status = 1 OR u.status = 2)
142 EXEC SQL OPEN u_cursor;
144 EXEC SQL FETCH u_cursor INTO :login, :uid, :id;
145 if (sqlca.sqlcode != 0) break;
147 lowercase(strtrim(login));
152 status = CreateEntry(0,login,&uid,1 /*idflag*/,0/*gflag*/,
153 SYSADMINID /*oid*/, SYSADMINID/*cid*/);
155 fprintf(stderr, "Error adding user %s uid %d: %s\n",
156 login, uid, error_message(status));
158 u = (struct entry *)malloc(sizeof(struct entry));
161 hash_store(users, id, u);
165 EXEC SQL CLOSE u_cursor;
173 EXEC SQL BEGIN DECLARE SECTION;
174 char name[33], string[129];
175 int gid, id, lid, hide, ustatus;
176 EXEC SQL END DECLARE SECTION;
179 struct prentry gentry, uentry;
182 struct bucket **p, *b;
187 fprintf(stderr, "Doing groups: %s", ctime(&t));
190 EXEC SQL SELECT modtime INTO :name FROM list WHERE list_id = 0;
192 EXEC SQL DECLARE l_cursor CURSOR FOR
193 SELECT l.name, l.gid, l.list_id, l.hidden
195 WHERE l.gid > 0 AND l.active != 0 AND l.grouplist != 0
197 EXEC SQL OPEN l_cursor;
199 EXEC SQL FETCH l_cursor INTO :name, :gid, :lid, :hide;
200 if (sqlca.sqlcode != 0) break;
202 lowercase(strtrim(name));
203 sprintf(namebuf, "system:%s", name);
206 if (FindByID(0, aid))
209 status = CreateEntry(0,namebuf,&aid,1 /*idflag*/,PRGRP/*gflag*/,
210 SYSADMINID /*oid*/, SYSADMINID/*cid*/);
212 fprintf(stderr, "Error adding group %s id %d: %s\n",
213 namebuf, aid, error_message(status));
215 if ((status==0 || status==PRIDEXIST) &&
216 (aid!=ANYUSERID && aid!=AUTHUSERID)) {
218 g = (struct entry *)malloc(sizeof(struct entry));
221 hash_store(groups, lid, g);
224 /* Set modes on hidden lists (S----) */
226 pos = FindByID(0, aid);
227 status = pr_Read(0, 0, pos, &gentry, sizeof(gentry));
229 gentry.flags = htonl(PRGRP|PRACCESS|PRP_STATUS_ANY);
230 status = pr_Write(0, 0, pos, &gentry, sizeof(gentry));
234 "Error setting flags on group %s: %s\n",
235 namebuf, error_message(status));
239 EXEC SQL CLOSE l_cursor;
242 fprintf(stderr, "Reading/preparing members: %s", ctime(&t));
244 EXEC SQL DECLARE m_cursor CURSOR FOR
245 SELECT m.list_id, m.member_id, m.member_type
248 EXEC SQL OPEN m_cursor;
250 EXEC SQL FETCH m_cursor INTO :lid, :id, :name;
251 if (sqlca.sqlcode != 0) break;
253 if (!(g = (struct entry *)hash_lookup(groups, lid)))
257 if (!strcmp(name, "USER")) {
258 if (u = (struct entry *)hash_lookup(users, id)) {
259 m = (struct member *)malloc(sizeof(struct member));
262 m->unext = u->members;
263 m->gnext = g->members;
264 u->members = g->members = m;
269 else if (!strcmp(name, "KERBEROS")) {
270 if (!(u = (struct entry *)hash_lookup(strings, id))) {
273 EXEC SQL SELECT string INTO :string
274 FROM strings WHERE string_id = :id;
279 if (p = (char *)index(string, '@')) {
281 if (strcmp(p+1, "ATHENA.MIT.EDU")) aid = -1;
284 if ((p = (char *)index(string, '.')) && (p-string) < 9 &&
285 !strcmp(p+1, "root"))
287 strncpy(name, string, p-string);
292 EXEC SQL DECLARE k_cursor2 CURSOR FOR
297 EXEC SQL OPEN k_cursor2;
299 if (sqlca.sqlcode) break;
300 EXEC SQL FETCH k_cursor2 INTO :lid, :ustatus;
301 if (ustatus==1 || ustatus==2) aid = lid+65536;
303 EXEC SQL CLOSE k_cursor2;
310 status = CreateEntry(0, string, &aid, 1 /*idflag*/,
311 0 /*gflag*/, SYSADMINID /*oid*/,
312 SYSADMINID /*cid */);
314 fprintf(stderr, "Error adding %s (id %d): %s\n",
315 string, aid, error_message(status));
316 if (status != PRIDEXIST) aid = 0;
321 u = (struct entry *)malloc(sizeof(struct entry));
324 hash_store(strings, id, u);
327 if (u->id == 0) continue;
329 m = (struct member *)malloc(sizeof(struct member));
332 m->unext = u->members;
333 m->gnext = g->members;
334 u->members = g->members = m;
339 EXEC SQL CLOSE m_cursor;
342 fprintf(stderr, "Doing members: %s", ctime(&t));
344 /* Do the bulk of the membership quickly.
345 * Add PRSIZE members into the user/group record. After that, we
346 * require the use of continuation records, but we assume this is
347 * few enough that we do this the slow way (AddToEntry).
349 for (p = &(users->data[users->size - 1]); p >= users->data; p--) {
350 for (b = *p; b; b = b->next) {
351 if ((u = (struct entry *)b->data)->members == 0)
353 pos = FindByID(0, u->id);
354 pr_Read(0, 0, pos, &uentry, sizeof(uentry));
355 for (t=0, m=u->members; m && t<PRSIZE; m=m->unext, t++)
356 uentry.entries[t] = htonl(m->group->id);
357 uentry.count = htonl(t);
358 pr_Write(0, 0, pos, &uentry, sizeof(uentry));
360 pr_ReadEntry(0, 0, pos, &uentry);
362 AddToEntry(0, &uentry, pos, m->group->id);
369 for (p = &(strings->data[strings->size - 1]); p >= strings->data; p--) {
370 for (b = *p; b; b = b->next) {
371 if ((u = (struct entry *)b->data)->members == 0)
373 pos = FindByID(0, u->id);
374 pr_Read(0, 0, pos, &uentry, sizeof(uentry));
375 for (t=0, m=u->members; m && t<PRSIZE; m=m->unext, t++)
376 uentry.entries[t] = htonl(m->group->id);
377 uentry.count = htonl(t);
378 pr_Write(0, 0, pos, &uentry, sizeof(uentry));
380 pr_ReadEntry(0, 0, pos, &uentry);
382 AddToEntry(0, &uentry, pos, m->group->id);
389 for (p = &(groups->data[groups->size - 1]); p >= groups->data; p--) {
390 for (b = *p; b; b = b->next) {
391 if ((g = (struct entry *)b->data)->members == 0)
393 pos = FindByID(0, g->id);
394 pr_Read(0, 0, pos, &gentry, sizeof(gentry));
395 for (t=0, m=g->members; m && t<PRSIZE; m=m->gnext, t++)
396 gentry.entries[t] = htonl(m->user->id);
397 gentry.count = htonl(t);
398 pr_Write(0, 0, pos, &gentry, sizeof(gentry));
400 pr_ReadEntry(0, 0, pos, &gentry);
402 AddToEntry(0, &gentry, pos, m->user->id);
414 com_err(whoami, MR_INGRES_ERR, " code %d\n", sqlca.sqlcode);