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 if (!strcmp(argv[1], "-db")) {
46 strncpy(db, argv[2], sizeof(db)-1);
51 fprintf(stderr, "usage: %s [-db sms] outfile\n", whoami);
55 dbase_fd = open(argv[1], O_RDWR|O_CREAT|O_TRUNC, 0660);
57 perror("opening file %s", argv[1]);
62 initialize_sms_error_table();
63 initialize_pt_error_table();
64 Initdb(); /* Initialize prdb */
66 idpos = create_hash(20000);
67 users = create_hash(10000);
68 groups = create_hash(15000);
70 pos = ntohl(cheader.eofPtr);
71 usercount = ntohl(cheader.usercount);
72 groupcount = ntohl(cheader.groupcount);
75 ## set lockmode session where level = table
91 ## int uid, id, status;
93 struct prentry tentry;
96 fprintf(stderr, "Doing users: %s", ctime(&t));
98 ## range of u is users
99 ## retrieve (login = u.#login, uid = u.#uid, id = u.users_id)
100 ## where u.#uid > 0 and (u.#status = 1 or u.#status = 2) {
101 lowercase(strtrim(login));
103 if (uid==ANONYMOUSID)
106 bzero(&tentry, sizeof(tentry));
107 hash_store(idpos, pos);
108 strcpy(tentry.name, login);
110 tentry.owner = SYSADMINID;
111 tentry.creator = SYSADMINID;
112 tentry.flags = PRQUOTA;
113 tentry.ngroups = tentry.nusers = 20;
114 if (tentry.id > ntohl(cheader.maxID))
115 cheader.maxID = htonl(tentry.id);
118 i = IDHash(tentry.id);
119 tentry.nextID = ntohl(cheader.idHash[i]);
120 cheader.idHash[i] = htonl(pos);
122 i = NameHash(tentry.name);
123 tentry.nextName = ntohl(cheader.nameHash[i]);
124 cheader.nameHash[i] = htonl(pos);
126 pr_WriteEntry(0, 0, pos, &tentry);
132 fprintf(stderr, "Error adding user %s uid %d: %s\n",
133 login, uid, error_message(status));
135 hash_store(users, id, uid);
143 long u, g, status, gpos, upos;
144 struct prentry gentry, uentry;
148 ## int gid, id, lid, hide;
151 fprintf(stderr, "Doing groups: %s", ctime(&t));
153 ## range of l is list
154 ## range of m is imembers
156 /* get lock records */
157 ## retrieve (name = l.modtime) where l.list_id = 0
158 ## retrieve (name = users.modtime) where users.users_id = 0
159 ## retrieve (name = l.#name, gid = l.#gid, lid = l.list_id, hide = l.hidden)
160 ## where l.group != 0 and l.active != 0 and l.#gid > 0 {
161 lowercase(strtrim(name));
162 sprintf(namebuf, "system:%s", name);
165 if (aid==ANYUSERID || aid==AUTHUSERID)
168 bzero(&gentry, sizeof(gentry));
169 hash_store(idpos, pos);
170 strcpy(gentry.name, namebuf);
172 gentry.owner = SYSADMINID;
173 gentry.creator = SYSADMINID;
175 gentry.flags = PRGRP|PRACCESS|PRP_STATUS_ANY;
177 gentry.flags = PRGRP;
178 if (gentry.id < ntohl(cheader.maxGroup))
179 cheader.maxGroup = htonl(gentry.id);
182 i = IDHash(gentry.id);
183 gentry.nextID = ntohl(cheader.idHash[i]);
184 cheader.idHash[i] = htonl(pos);
186 i = NameHash(gentry.name);
187 gentry.nextName = ntohl(cheader.nameHash[i]);
188 cheader.nameHash[i] = htonl(pos);
190 pr_WriteEntry(0, 0, pos, &gentry);
191 AddToOwnerChain(0,gentry.id,gentry.owner);
196 fprintf(stderr, "Error adding group %s id %d: %s\n",
197 namebuf, aid, error_message(status));
199 hash_store(groups, lid, aid);
203 cheader.groupcount = htonl(groupcount);
204 cheader.usercount = htonl(usercount);
205 cheader.eofPtr = htonl(pos);
206 pr_Write(0, 0, 0, &cheader, sizeof(cheader));
209 fprintf(stderr, "Doing members: %s", ctime(&t));
211 ## retrieve (lid = m.list_id, id = m.member_id)
212 ## where m.member_type = "USER" {
213 if ((u = (long) hash_lookup(users, id)) &&
214 (g = (long) hash_lookup(groups, lid))) {
215 if ((gpos = get_id(g)) && (upos = get_id(u))) {
216 pr_ReadEntry(0,0,upos,&uentry);
217 pr_ReadEntry(0,0,gpos,&gentry);
218 AddToEntry (0, &gentry, gpos, u);
219 AddToEntry (0, &uentry, upos, g);
225 fprintf(stderr, "Error adding uid %d to group %d: %s\n",
226 u, -g, error_message(status));
231 fprintf(stderr, "Done: %s", ctime(&t));
240 if (i=(long)hash_lookup(idpos, id))
242 hash_store(idpos, id, i=FindByID(0, id));
249 * ingerr: (supposedly) called when Ingres indicates an error.
250 * I have not yet been able to get this to work to intercept a
251 * database open error.
253 #define INGRES_DEADLOCK 4700
255 static int ingerr(num)
262 case INGRES_DEADLOCK:
263 ingres_errno = MR_DEADLOCK;
266 ingres_errno = MR_INGRES_ERR;
268 com_err(whoami, MR_INGRES_ERR, " code %d\n", *num);