]> andersk Git - moira.git/blame - afssync/sync.fast.qc
Initial revision
[moira.git] / afssync / sync.fast.qc
CommitLineData
4b329233 1/* $Header$
2 *
3 *
4 * (c) Copyright 1989 by the Massachusetts Institute of Technology.
5 * For copying and distribution information, please see the file
6 * <mit-copyright.h>.
7 */
8
9#include <mit-copyright.h>
10#include <stdio.h>
11#include <sys/file.h>
12
13#include <rx/xdr.h>
14#include "ptint.h"
15#include "ptserver.h"
16#include "pterror.h"
17
18#include <moira.h>
19#include <moira_site.h>
20#include <ctype.h>
21
22#define min(x,y) ((x) < (y) ? (x) : (y))
23char *whoami = "sync";
24
25char *malloc(), *strsave();
26int dbase_fd;
27
28long pos; /* cheader.eofPtr */
29long usercount; /* cheader.usercount */
30long groupcount; /* cheader.groupcount */
31
32struct hash *users;
33struct hash *groups;
34struct hash *idpos;
35
36main(argc, argv)
37int argc;
38char **argv;
39{
40## char db[9];
41
42 int status;
43 int ingerr();
44
45 if (!strcmp(argv[1], "-db")) {
46 strncpy(db, argv[2], sizeof(db)-1);
47 argc -= 2;
48 argv += 2;
49 }
50 if (argc != 2) {
51 fprintf(stderr, "usage: %s [-db sms] outfile\n", whoami);
52 exit(MR_ARGS);
53 }
54
55 dbase_fd = open(argv[1], O_RDWR|O_CREAT|O_TRUNC, 0660);
56 if (dbase_fd < 0) {
57 perror("opening file %s", argv[1]);
58 exit(1);
59 }
60 IIseterr(ingerr);
61
62 initialize_sms_error_table();
63 initialize_pt_error_table();
64 Initdb(); /* Initialize prdb */
65
66 idpos = create_hash(20000);
67 users = create_hash(10000);
68 groups = create_hash(15000);
69
70 pos = ntohl(cheader.eofPtr);
71 usercount = ntohl(cheader.usercount);
72 groupcount = ntohl(cheader.groupcount);
73
74## ingres db
75## set lockmode session where level = table
76## begin transaction
77
78 do_passwd();
79 do_groups();
80
81## end transaction
82## exit
83
84 exit(MR_SUCCESS);
85}
86
87
88do_passwd()
89##{
90## char login[9];
91## int uid, id, status;
92 int t, i;
93 struct prentry tentry;
94
95 t = time(0);
96 fprintf(stderr, "Doing users: %s", ctime(&t));
97
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));
102
103 if (uid==ANONYMOUSID)
104 status = PRIDEXIST;
105 else {
106 bzero(&tentry, sizeof(tentry));
107 hash_store(idpos, pos);
108 strcpy(tentry.name, login);
109 tentry.id = uid;
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);
116 usercount++;
117
118 i = IDHash(tentry.id);
119 tentry.nextID = ntohl(cheader.idHash[i]);
120 cheader.idHash[i] = htonl(pos);
121
122 i = NameHash(tentry.name);
123 tentry.nextName = ntohl(cheader.nameHash[i]);
124 cheader.nameHash[i] = htonl(pos);
125
126 pr_WriteEntry(0, 0, pos, &tentry);
127 pos += ENTRYSIZE;
128 status = 0;
129 }
130
131 if (status)
132 fprintf(stderr, "Error adding user %s uid %d: %s\n",
133 login, uid, error_message(status));
134 else
135 hash_store(users, id, uid);
136## }
137##}
138
139
140
141do_groups()
142##{
143 long u, g, status, gpos, upos;
144 struct prentry gentry, uentry;
145 char namebuf[41];
146 int aid, t, i;
147## char name[33];
148## int gid, id, lid, hide;
149
150 t = time(0);
151 fprintf(stderr, "Doing groups: %s", ctime(&t));
152
153## range of l is list
154## range of m is imembers
155
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);
163 aid = -gid;
164
165 if (aid==ANYUSERID || aid==AUTHUSERID)
166 status = PRIDEXIST;
167 else {
168 bzero(&gentry, sizeof(gentry));
169 hash_store(idpos, pos);
170 strcpy(gentry.name, namebuf);
171 gentry.id = aid;
172 gentry.owner = SYSADMINID;
173 gentry.creator = SYSADMINID;
174 if (hide)
175 gentry.flags = PRGRP|PRACCESS|PRP_STATUS_ANY;
176 else
177 gentry.flags = PRGRP;
178 if (gentry.id < ntohl(cheader.maxGroup))
179 cheader.maxGroup = htonl(gentry.id);
180 groupcount++;
181
182 i = IDHash(gentry.id);
183 gentry.nextID = ntohl(cheader.idHash[i]);
184 cheader.idHash[i] = htonl(pos);
185
186 i = NameHash(gentry.name);
187 gentry.nextName = ntohl(cheader.nameHash[i]);
188 cheader.nameHash[i] = htonl(pos);
189
190 pr_WriteEntry(0, 0, pos, &gentry);
191 AddToOwnerChain(0,gentry.id,gentry.owner);
192 pos += ENTRYSIZE;
193 status = 0;
194 }
195 if (status)
196 fprintf(stderr, "Error adding group %s id %d: %s\n",
197 namebuf, aid, error_message(status));
198 else
199 hash_store(groups, lid, aid);
200
201## }
202
203 cheader.groupcount = htonl(groupcount);
204 cheader.usercount = htonl(usercount);
205 cheader.eofPtr = htonl(pos);
206 pr_Write(0, 0, 0, &cheader, sizeof(cheader));
207
208 t = time(0);
209 fprintf(stderr, "Doing members: %s", ctime(&t));
210
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);
220 status = 0;
221 } else {
222 status = PRNOENT;
223 }
224 if (status)
225 fprintf(stderr, "Error adding uid %d to group %d: %s\n",
226 u, -g, error_message(status));
227 }
228## }
229
230 t = time(0);
231 fprintf(stderr, "Done: %s", ctime(&t));
232
233##}
234
235
236get_id(id)
237{
238 long i;
239
240 if (i=(long)hash_lookup(idpos, id))
241 return i;
242 hash_store(idpos, id, i=FindByID(0, id));
243 return i;
244}
245
246
247
248/*
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.
252 */
253#define INGRES_DEADLOCK 4700
254
255static int ingerr(num)
256 int *num;
257{
258 char buf[256];
259 int ingres_errno;
260
261 switch (*num) {
262 case INGRES_DEADLOCK:
263 ingres_errno = MR_DEADLOCK;
264 break;
265 default:
266 ingres_errno = MR_INGRES_ERR;
267 }
268 com_err(whoami, MR_INGRES_ERR, " code %d\n", *num);
269 exit(ingres_errno);
270}
This page took 0.082876 seconds and 5 git commands to generate.