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();
43 struct member *members;
57 if (!strcmp(argv[1], "-db")) {
58 strncpy(db, argv[2], sizeof(db)-1);
63 fprintf(stderr, "usage: %s [-db sms] outfile\n", whoami);
67 dbase_fd = open(argv[1], O_RDWR|O_CREAT|O_TRUNC, 0660);
69 perror("opening file %s", argv[1]);
74 initialize_sms_error_table();
75 initialize_pt_error_table();
76 Initdb(); /* Initialize prdb */
78 users = create_hash(10000);
79 groups = create_hash(15000);
82 ## set lockmode session where level = table
98 ## int uid, id, status;
100 struct prentry tentry;
104 fprintf(stderr, "Doing users: %s", ctime(&t));
106 ## range of u is users
107 ## retrieve (login = u.#login, uid = u.#uid, id = u.users_id)
108 ## where u.#uid > 0 and (u.#status = 1 or u.#status = 2) {
109 lowercase(strtrim(login));
114 status = CreateEntry(0,login,&uid,1/*idflag*/,0/*gflag*/,
115 SYSADMINID/*oid*/, SYSADMINID/*cid*/);
117 fprintf(stderr, "Error adding user %s uid %d: %s\n",
118 login, uid, error_message(status));
120 u = (struct entry *)malloc(sizeof(struct entry));
123 hash_store(users, id, u);
134 struct prentry gentry, uentry;
137 struct bucket **p, *b;
141 ## int gid, id, lid, hide;
144 fprintf(stderr, "Doing groups: %s", ctime(&t));
146 ## range of l is list
147 ## range of m is imembers
148 /* get lock records */
149 ## retrieve (name = l.modtime) where l.list_id = 0
150 ## retrieve (name = users.modtime) where users.users_id = 0
152 ## retrieve (name = l.#name, gid = l.#gid, lid = l.list_id, hide = l.hidden)
153 ## where l.group != 0 and l.active != 0 and l.#gid > 0 {
154 lowercase(strtrim(name));
155 sprintf(namebuf, "system:%s", name);
158 if (FindByID(0, aid))
161 status = CreateEntry(0,namebuf,&aid,1/*idflag*/,PRGRP/*gflag*/,
162 SYSADMINID/*oid*/, SYSADMINID/*cid*/);
164 fprintf(stderr, "Error adding group %s id %d: %s\n",
165 namebuf, aid, error_message(status));
167 if ((status==0 || status==PRIDEXIST) &&
168 (aid!=ANYUSERID && aid!=AUTHUSERID)) {
170 g = (struct entry *)malloc(sizeof(struct entry));
173 hash_store(groups, lid, g);
176 /* Set modes on hidden lists (S----) */
178 pos = FindByID(0, aid);
179 status = pr_Read(0, 0, pos, &gentry, sizeof(gentry));
181 gentry.flags = htonl(PRGRP|PRACCESS|PRP_STATUS_ANY);
182 status = pr_Write(0, 0, pos, &gentry, sizeof(gentry));
186 "Error setting flags on group %s: %s\n",
187 namebuf, error_message(status));
193 fprintf(stderr, "Doing members: %s", ctime(&t));
195 ## retrieve (lid = m.list_id, id = m.member_id)
196 ## where m.member_type = "USER" {
197 if ((u = (struct entry *)hash_lookup(users, id)) &&
198 (g = (struct entry *)hash_lookup(groups, lid))) {
199 m = (struct member *)malloc(sizeof(struct member));
202 m->unext = u->members;
203 m->gnext = g->members;
204 u->members = g->members = m;
209 /* Do the bulk of the membership quickly.
210 * Add PRSIZE members into the user/group record. After that, we
211 * require the use of continuation records, but we assume this is
212 * few enough that we do this the slow way (AddToEntry).
214 for (p = &(users->data[users->size - 1]); p >= users->data; p--) {
215 for (b = *p; b; b = b->next) {
216 if ((u = (struct entry *)b->data)->members == 0)
218 pos = FindByID(0, u->id);
219 pr_Read(0, 0, pos, &uentry, sizeof(uentry));
220 for (t=0, m=u->members; m && t<PRSIZE; m=m->unext, t++)
221 uentry.entries[t] = htonl(m->group->id);
222 uentry.count = htonl(t);
223 pr_Write(0, 0, pos, &uentry, sizeof(uentry));
225 pr_ReadEntry(0, 0, pos, &uentry);
227 AddToEntry(0, &uentry, pos, m->group->id);
233 for (p = &(groups->data[groups->size - 1]); p >= groups->data; p--) {
234 for (b = *p; b; b = b->next) {
235 if ((g = (struct entry *)b->data)->members == 0)
237 pos = FindByID(0, g->id);
238 pr_Read(0, 0, pos, &gentry, sizeof(gentry));
239 for (t=0, m=g->members; m && t<PRSIZE; m=m->gnext, t++)
240 gentry.entries[t] = htonl(m->user->id);
241 gentry.count = htonl(t);
242 pr_Write(0, 0, pos, &gentry, sizeof(gentry));
244 pr_ReadEntry(0, 0, pos, &gentry);
246 AddToEntry(0, &gentry, pos, m->user->id);
254 fprintf(stderr, "Done (%d users, %d groups, %d members): %s",
255 ucount, gcount, mcount, ctime(&t));
261 * ingerr: (supposedly) called when Ingres indicates an error.
262 * I have not yet been able to get this to work to intercept a
263 * database open error.
265 #define INGRES_DEADLOCK 4700
267 static int ingerr(num)
274 case INGRES_DEADLOCK:
275 ingres_errno = MR_DEADLOCK;
278 ingres_errno = MR_INGRES_ERR;
280 com_err(whoami, MR_INGRES_ERR, " code %d\n", *num);