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 EXEC SQL INCLUDE sqlca;
25 EXEC SQL BEGIN DECLARE SECTION;
26 char db[33] = "moira";
27 EXEC SQL END DECLARE SECTION;
34 #define min(x,y) ((x) < (y) ? (x) : (y))
36 char *whoami = "sync";
56 struct member *members;
59 int main(int argc, char **argv)
64 setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
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 data file");
82 initialize_sms_error_table();
83 initialize_pt_error_table();
84 Initdb(); /* Initialize prdb */
86 users = create_hash(10000);
87 groups = create_hash(15000);
89 EXEC SQL WHENEVER SQLERROR DO sqlerr();
90 EXEC SQL CONNECT :db IDENTIFIED BY :db;
96 fprintf(stderr, "Done (%d users, %d groups, %d kerberos, %d members): %s",
97 ucount, gcount, kcount, mcount, ctime(&t));
107 EXEC SQL BEGIN DECLARE SECTION;
108 char login[9], name[33];
110 EXEC SQL END DECLARE SECTION;
113 struct prentry tentry;
117 fprintf(stderr, "Doing users: %s", ctime(&t));
119 EXEC SQL DECLARE u_cursor CURSOR FOR
120 SELECT u.login, u.unix_uid, u.users_id
122 WHERE u.unix_uid > 0 AND (u.status = 1 OR u.status = 2)
124 EXEC SQL OPEN u_cursor;
126 EXEC SQL FETCH u_cursor INTO :login, :uid, :id;
127 if (sqlca.sqlcode != 0) break;
129 lowercase(strtrim(login));
134 status = CreateEntry(0,login,&uid,1 /*idflag*/,0/*gflag*/,
135 SYSADMINID /*oid*/, SYSADMINID/*cid*/);
137 fprintf(stderr, "Error adding user %s uid %d: %s\n",
138 login, uid, error_message(status));
140 u = (struct entry *)malloc(sizeof(struct entry));
143 hash_store(users, id, u);
147 EXEC SQL CLOSE u_cursor;
155 EXEC SQL BEGIN DECLARE SECTION;
156 char name[33], string[129];
157 int gid, id, lid, hide, ustatus;
158 EXEC SQL END DECLARE SECTION;
161 struct prentry gentry, uentry;
164 struct bucket **p, *b;
169 fprintf(stderr, "Doing groups: %s", ctime(&t));
171 EXEC SQL DECLARE l_cursor CURSOR FOR
172 SELECT l.name, l.gid, l.list_id, l.hidden
174 WHERE l.gid > 0 AND l.active != 0 AND l.grouplist != 0
176 EXEC SQL OPEN l_cursor;
178 EXEC SQL FETCH l_cursor INTO :name, :gid, :lid, :hide;
179 if (sqlca.sqlcode != 0) break;
181 lowercase(strtrim(name));
182 sprintf(namebuf, "system:%s", name);
185 if (FindByID(0, aid))
188 status = CreateEntry(0,namebuf,&aid,1 /*idflag*/,PRGRP/*gflag*/,
189 SYSADMINID /*oid*/, SYSADMINID/*cid*/);
191 fprintf(stderr, "Error adding group %s id %d: %s\n",
192 namebuf, aid, error_message(status));
194 if ((status==0 || status==PRIDEXIST) &&
195 (aid!=ANYUSERID && aid!=AUTHUSERID)) {
197 g = (struct entry *)malloc(sizeof(struct entry));
200 hash_store(groups, lid, g);
203 /* Set modes on hidden lists (S----) */
205 pos = FindByID(0, aid);
206 status = pr_Read(0, 0, pos, &gentry, sizeof(gentry));
208 gentry.flags = htonl(PRGRP|PRACCESS|PRP_STATUS_ANY);
209 status = pr_Write(0, 0, pos, &gentry, sizeof(gentry));
213 "Error setting flags on group %s: %s\n",
214 namebuf, error_message(status));
218 EXEC SQL CLOSE l_cursor;
221 fprintf(stderr, "Reading/preparing members: %s", ctime(&t));
223 EXEC SQL DECLARE m_cursor CURSOR FOR
224 SELECT m.list_id, m.member_id, m.member_type
227 EXEC SQL OPEN m_cursor;
229 EXEC SQL FETCH m_cursor INTO :lid, :id, :name;
230 if (sqlca.sqlcode != 0) break;
232 if (!(g = (struct entry *)hash_lookup(groups, lid)))
236 if (!strcmp(name, "USER")) {
237 if (u = (struct entry *)hash_lookup(users, id)) {
238 m = (struct member *)malloc(sizeof(struct member));
241 m->unext = u->members;
242 m->gnext = g->members;
243 u->members = g->members = m;
248 EXEC SQL CLOSE m_cursor;
251 fprintf(stderr, "Doing members: %s", ctime(&t));
253 /* Do the bulk of the membership quickly.
254 * Add PRSIZE members into the user/group record. After that, we
255 * require the use of continuation records, but we assume this is
256 * few enough that we do this the slow way (AddToEntry).
258 for (p = &(users->data[users->size - 1]); p >= users->data; p--) {
259 for (b = *p; b; b = b->next) {
260 if ((u = (struct entry *)b->data)->members == 0)
262 pos = FindByID(0, u->id);
263 pr_Read(0, 0, pos, &uentry, sizeof(uentry));
264 for (t=0, m=u->members; m && t<PRSIZE; m=m->unext, t++)
265 uentry.entries[t] = htonl(m->group->id);
266 uentry.count = htonl(t);
267 pr_Write(0, 0, pos, &uentry, sizeof(uentry));
269 pr_ReadEntry(0, 0, pos, &uentry);
271 AddToEntry(0, &uentry, pos, m->group->id);
277 for (p = &(groups->data[groups->size - 1]); p >= groups->data; p--) {
278 for (b = *p; b; b = b->next) {
279 if ((g = (struct entry *)b->data)->members == 0)
281 pos = FindByID(0, g->id);
282 pr_Read(0, 0, pos, &gentry, sizeof(gentry));
283 for (t=0, m=g->members; m && t<PRSIZE; m=m->gnext, t++)
284 gentry.entries[t] = htonl(m->user->id);
285 gentry.count = htonl(t);
286 pr_Write(0, 0, pos, &gentry, sizeof(gentry));
288 pr_ReadEntry(0, 0, pos, &gentry);
290 AddToEntry(0, &gentry, pos, m->user->id);
305 sqlglm(buf, &size, &len);
307 com_err(whoami, MR_DBMS_ERR, " code %d\n%s", sqlca.sqlcode, buf);