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>
19 #include <moira_site.h>
22 #define min(x,y) ((x) < (y) ? (x) : (y))
23 char *whoami = "sync";
25 char *malloc(), *strsave();
28 long pos; /* cheader.eofPtr */
29 long usercount; /* cheader.usercount */
30 long groupcount; /* cheader.groupcount */
45 struct member *members;
59 if (!strcmp(argv[1], "-db")) {
60 strncpy(db, argv[2], sizeof(db)-1);
65 fprintf(stderr, "usage: %s [-db sms] outfile\n", whoami);
69 dbase_fd = open(argv[1], O_RDWR|O_CREAT|O_TRUNC, 0660);
71 perror("opening file %s", argv[1]);
76 initialize_sms_error_table();
77 initialize_pt_error_table();
78 Initdb(); /* Initialize prdb */
80 users = create_hash(10000);
81 groups = create_hash(15000);
83 pos = ntohl(cheader.eofPtr);
84 usercount = ntohl(cheader.usercount);
85 groupcount = ntohl(cheader.groupcount);
88 ## set lockmode session where level = table
106 struct prentry tentry;
110 fprintf(stderr, "Doing users: %s", ctime(&t));
112 ## range of u is users
113 ## retrieve (login = u.#login, uid = u.#uid, id = u.users_id)
114 ## where u.#uid > 0 and (u.#status = 1 or u.#status = 2) {
115 lowercase(strtrim(login));
117 if (uid==ANONYMOUSID)
120 bzero(&tentry, sizeof(tentry));
121 strcpy(tentry.name, login);
122 tentry.id = htonl(uid);
123 tentry.owner = htonl(SYSADMINID);
124 tentry.creator = htonl(SYSADMINID);
125 tentry.flags = htonl(PRQUOTA);
126 tentry.ngroups = tentry.nusers = htonl(20);
127 if (uid > ntohl(cheader.maxID))
128 cheader.maxID = tentry.id;
132 tentry.nextID = cheader.idHash[i];
133 cheader.idHash[i] = htonl(pos);
135 i = NameHash(tentry.name);
136 tentry.nextName = cheader.nameHash[i];
137 cheader.nameHash[i] = htonl(pos);
139 pr_Write(0, 0, pos, &tentry, sizeof(tentry));
141 u = (struct entry *)malloc(sizeof(struct entry));
151 fprintf(stderr, "Error adding user %s uid %d: %s\n",
152 login, uid, error_message(status));
154 hash_store(users, id, u);
163 struct prentry gentry, uentry;
165 struct bucket **p, *b;
170 ## int gid, id, lid, hide;
173 fprintf(stderr, "Doing groups: %s", ctime(&t));
175 ## range of l is list
176 ## range of m is imembers
178 /* get lock records */
179 ## retrieve (name = l.modtime) where l.list_id = 0
180 ## retrieve (name = users.modtime) where users.users_id = 0
181 ## retrieve (name = l.#name, gid = l.#gid, lid = l.list_id, hide = l.hidden)
182 ## where l.group != 0 and l.active != 0 and l.#gid > 0 {
183 lowercase(strtrim(name));
184 sprintf(namebuf, "system:%s", name);
187 if (aid==ANYUSERID || aid==AUTHUSERID || aid==SYSADMINID)
190 bzero(&gentry, sizeof(gentry));
191 strcpy(gentry.name, namebuf);
192 gentry.id = htonl(aid);
193 gentry.owner = htonl(SYSADMINID);
194 gentry.creator = htonl(SYSADMINID);
196 gentry.flags = htonl(PRGRP|PRACCESS|PRP_STATUS_ANY);
198 gentry.flags = htonl(PRGRP);
199 if (aid < ntohl(cheader.maxGroup))
200 cheader.maxGroup = gentry.id;
204 gentry.nextID = cheader.idHash[i];
205 cheader.idHash[i] = htonl(pos);
207 i = NameHash(gentry.name);
208 gentry.nextName = cheader.nameHash[i];
209 cheader.nameHash[i] = htonl(pos);
211 pr_Write(0, 0, pos, &gentry, sizeof(gentry));
212 AddToOwnerChain(0, aid, SYSADMINID);
214 g = (struct entry *)malloc(sizeof(struct entry));
223 fprintf(stderr, "Error adding group %s id %d: %s\n",
224 namebuf, aid, error_message(status));
225 if (status==0 || (status==PRIDEXIST && aid==SYSADMINID))
226 hash_store(groups, lid, g);
230 cheader.groupcount = htonl(groupcount);
231 cheader.usercount = htonl(usercount);
232 cheader.eofPtr = htonl(pos);
233 pr_Write(0, 0, 0, &cheader, sizeof(cheader));
236 fprintf(stderr, "Doing members: %s", ctime(&t));
238 ## retrieve (lid = m.list_id, id = m.member_id)
239 ## where m.member_type = "USER" {
240 if ((u = (struct entry *)hash_lookup(users, id)) &&
241 (g = (struct entry *)hash_lookup(groups, lid))) {
242 m = (struct member *)malloc(sizeof(struct member));
245 m->unext = u->members;
246 m->gnext = g->members;
247 u->members = g->members = m;
251 for (p = &(users->data[users->size - 1]); p >= users->data; p--) {
252 for (b = *p; b; b = b->next) {
253 if ((u = (struct entry *)b->data)->members == 0)
255 pr_Read(0, 0, u->pos, &uentry, sizeof(uentry));
256 for (i=0, m=u->members; m && i<PRSIZE; m=m->unext, i++)
257 uentry.entries[i] = htonl(m->group->id);
258 uentry.count = htonl(i);
259 pr_Write(0, 0, u->pos, &uentry, sizeof(uentry));
261 pr_ReadEntry(0, 0, u->pos, &uentry);
263 AddToEntry(0, &uentry, u->pos, m->group->id);
269 for (p = &(groups->data[groups->size - 1]); p >= groups->data; p--) {
270 for (b = *p; b; b = b->next) {
271 if ((g = (struct entry *)b->data)->members == 0)
273 pr_Read(0, 0, g->pos, &gentry, sizeof(gentry));
274 for (i=0, m=g->members; m && i<PRSIZE; m=m->gnext, i++)
275 gentry.entries[i] = htonl(m->user->id);
276 gentry.count = htonl(i);
277 pr_Write(0, 0, g->pos, &gentry, sizeof(gentry));
279 pr_ReadEntry(0, 0, g->pos, &gentry);
281 AddToEntry(0, &gentry, g->pos, m->user->id);
289 fprintf(stderr, "Done: %s", ctime(&t));
295 * ingerr: (supposedly) called when Ingres indicates an error.
296 * I have not yet been able to get this to work to intercept a
297 * database open error.
299 #define INGRES_DEADLOCK 4700
301 static int ingerr(num)
308 case INGRES_DEADLOCK:
309 ingres_errno = MR_DEADLOCK;
312 ingres_errno = MR_INGRES_ERR;
314 com_err(whoami, MR_INGRES_ERR, " code %d\n", *num);