]>
Commit | Line | Data |
---|---|---|
62b40972 | 1 | /* $Header$ |
2 | * | |
62b40972 | 3 | * |
f00f36ed | 4 | * (c) Copyright 1989 by the Massachusetts Institute of Technology. |
62b40972 | 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> | |
8ec23c21 | 12 | |
62b40972 | 13 | #include <rx/xdr.h> |
8ec23c21 | 14 | #include "ptint.h" |
15 | #include "ptserver.h" | |
16 | #include "pterror.h" | |
17 | ||
f00f36ed | 18 | #include <moira.h> |
19 | #include <moira_site.h> | |
62b40972 | 20 | #include <ctype.h> |
21 | ||
62b40972 | 22 | #define min(x,y) ((x) < (y) ? (x) : (y)) |
62b40972 | 23 | char *whoami = "sync"; |
24 | ||
25 | char *malloc(), *strsave(); | |
26 | int dbase_fd; | |
27 | ||
30c31534 | 28 | int ucount = 0; |
29 | int gcount = 0; | |
30 | int mcount = 0; | |
31 | ||
67919620 | 32 | struct hash *users; |
33 | struct hash *groups; | |
62b40972 | 34 | |
30c31534 | 35 | struct member { |
36 | struct entry *user; | |
37 | struct entry *group; | |
38 | struct member *unext; | |
39 | struct member *gnext; | |
40 | }; | |
41 | struct entry { | |
42 | long id; | |
43 | struct member *members; | |
44 | }; | |
45 | ||
62b40972 | 46 | main(argc, argv) |
47 | int argc; | |
48 | char **argv; | |
49 | { | |
67919620 | 50 | ## char db[9]; |
51 | ||
278dd826 | 52 | int status; |
62b40972 | 53 | int ingerr(); |
54 | ||
2def5ef2 | 55 | strcpy(db, "sms"); |
56 | ||
67919620 | 57 | if (!strcmp(argv[1], "-db")) { |
58 | strncpy(db, argv[2], sizeof(db)-1); | |
59 | argc -= 2; | |
60 | argv += 2; | |
61 | } | |
62b40972 | 62 | if (argc != 2) { |
67919620 | 63 | fprintf(stderr, "usage: %s [-db sms] outfile\n", whoami); |
f00f36ed | 64 | exit(MR_ARGS); |
62b40972 | 65 | } |
66 | ||
67919620 | 67 | dbase_fd = open(argv[1], O_RDWR|O_CREAT|O_TRUNC, 0660); |
62b40972 | 68 | if (dbase_fd < 0) { |
69 | perror("opening file %s", argv[1]); | |
70 | exit(1); | |
71 | } | |
72 | IIseterr(ingerr); | |
67919620 | 73 | |
da6101af | 74 | initialize_sms_error_table(); |
75 | initialize_pt_error_table(); | |
67919620 | 76 | Initdb(); /* Initialize prdb */ |
77 | ||
67919620 | 78 | users = create_hash(10000); |
79 | groups = create_hash(15000); | |
80 | ||
81 | ## ingres db | |
62b40972 | 82 | ## set lockmode session where level = table |
83 | ## begin transaction | |
84 | ||
85 | do_passwd(); | |
86 | do_groups(); | |
87 | ||
88 | ## end transaction | |
89 | ## exit | |
90 | ||
f00f36ed | 91 | exit(MR_SUCCESS); |
62b40972 | 92 | } |
93 | ||
94 | ||
62b40972 | 95 | do_passwd() |
96 | ##{ | |
97 | ## char login[9]; | |
98 | ## int uid, id, status; | |
67919620 | 99 | int t; |
100 | struct prentry tentry; | |
30c31534 | 101 | struct entry *u; |
62b40972 | 102 | |
67919620 | 103 | t = time(0); |
104 | fprintf(stderr, "Doing users: %s", ctime(&t)); | |
105 | ||
62b40972 | 106 | ## range of u is users |
107 | ## retrieve (login = u.#login, uid = u.#uid, id = u.users_id) | |
da6101af | 108 | ## where u.#uid > 0 and (u.#status = 1 or u.#status = 2) { |
67919620 | 109 | lowercase(strtrim(login)); |
110 | ||
111 | if (FindByID(0,uid)) | |
da6101af | 112 | status = PRIDEXIST; |
113 | else | |
67919620 | 114 | status = CreateEntry(0,login,&uid,1/*idflag*/,0/*gflag*/, |
da6101af | 115 | SYSADMINID/*oid*/, SYSADMINID/*cid*/); |
116 | if (status) | |
117 | fprintf(stderr, "Error adding user %s uid %d: %s\n", | |
118 | login, uid, error_message(status)); | |
30c31534 | 119 | else { |
120 | u = (struct entry *)malloc(sizeof(struct entry)); | |
121 | u->id = uid; | |
122 | u->members = 0; | |
123 | hash_store(users, id, u); | |
124 | ucount++; | |
125 | } | |
62b40972 | 126 | ## } |
127 | ##} | |
128 | ||
129 | ||
130 | ||
131 | do_groups() | |
132 | ##{ | |
30c31534 | 133 | long status, pos; |
da6101af | 134 | struct prentry gentry, uentry; |
30c31534 | 135 | struct entry *u, *g; |
136 | struct member *m; | |
137 | struct bucket **p, *b; | |
138 | char namebuf[40]; | |
67919620 | 139 | int aid, t; |
140 | ## char name[33]; | |
8ec23c21 | 141 | ## int gid, id, lid, hide; |
62b40972 | 142 | |
67919620 | 143 | t = time(0); |
144 | fprintf(stderr, "Doing groups: %s", ctime(&t)); | |
62b40972 | 145 | |
62b40972 | 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 | |
151 | ||
8ec23c21 | 152 | ## retrieve (name = l.#name, gid = l.#gid, lid = l.list_id, hide = l.hidden) |
da6101af | 153 | ## where l.group != 0 and l.active != 0 and l.#gid > 0 { |
67919620 | 154 | lowercase(strtrim(name)); |
62b40972 | 155 | sprintf(namebuf, "system:%s", name); |
da6101af | 156 | aid = -gid; |
67919620 | 157 | |
158 | if (FindByID(0, aid)) | |
da6101af | 159 | status = PRIDEXIST; |
160 | else | |
161 | status = CreateEntry(0,namebuf,&aid,1/*idflag*/,PRGRP/*gflag*/, | |
162 | SYSADMINID/*oid*/, SYSADMINID/*cid*/); | |
62b40972 | 163 | if (status) |
da6101af | 164 | fprintf(stderr, "Error adding group %s id %d: %s\n", |
165 | namebuf, aid, error_message(status)); | |
30c31534 | 166 | else { |
167 | g = (struct entry *)malloc(sizeof(struct entry)); | |
168 | g->id = aid; | |
169 | g->members = 0; | |
170 | hash_store(groups, lid, g); | |
171 | gcount++; | |
172 | } | |
da6101af | 173 | |
725411bc | 174 | /* Set modes on hidden lists (S----) */ |
da6101af | 175 | if (hide && (status==0 || status==PRIDEXIST)) { |
30c31534 | 176 | pos = FindByID(0, aid); |
177 | status = pr_Read(0, 0, pos, &gentry, sizeof(gentry)); | |
da6101af | 178 | if (!status) { |
725411bc | 179 | gentry.flags = htonl(PRGRP|PRACCESS|PRP_STATUS_ANY); |
30c31534 | 180 | status = pr_Write(0, 0, pos, &gentry, sizeof(gentry)); |
da6101af | 181 | } |
8ec23c21 | 182 | if (status) |
67919620 | 183 | fprintf(stderr, |
184 | "Error setting flags on group %s: %s\n", | |
da6101af | 185 | namebuf, error_message(status)); |
8ec23c21 | 186 | } |
62b40972 | 187 | ## } |
188 | ||
67919620 | 189 | t = time(0); |
190 | fprintf(stderr, "Doing members: %s", ctime(&t)); | |
62b40972 | 191 | |
192 | ## retrieve (lid = m.list_id, id = m.member_id) | |
193 | ## where m.member_type = "USER" { | |
30c31534 | 194 | if ((u = (struct entry *)hash_lookup(users, id)) && |
195 | (g = (struct entry *)hash_lookup(groups, lid))) { | |
196 | m = (struct member *)malloc(sizeof(struct member)); | |
197 | m->user = u; | |
198 | m->group = g; | |
199 | m->unext = u->members; | |
200 | m->gnext = g->members; | |
201 | u->members = g->members = m; | |
202 | mcount++; | |
62b40972 | 203 | } |
204 | ## } | |
30c31534 | 205 | for (p = &(users->data[users->size - 1]); p >= users->data; p--) { |
206 | for (b = *p; b; b = b->next) { | |
207 | if ((u = (struct entry *)b->data)->members == 0) | |
208 | continue; | |
209 | pos = FindByID(0, u->id); | |
210 | pr_Read(0, 0, pos, &uentry, sizeof(uentry)); | |
211 | for (t=0, m=u->members; m && t<PRSIZE; m=m->unext, t++) | |
212 | uentry.entries[t] = htonl(m->group->id); | |
213 | uentry.count = htonl(t); | |
214 | pr_Write(0, 0, pos, &uentry, sizeof(uentry)); | |
215 | if (m) { | |
216 | pr_ReadEntry(0, 0, pos, &uentry); | |
217 | while (m) { | |
218 | AddToEntry(0, &uentry, pos, m->group->id); | |
219 | m = m->unext; | |
220 | } | |
221 | } | |
222 | } | |
223 | } | |
224 | for (p = &(groups->data[groups->size - 1]); p >= groups->data; p--) { | |
225 | for (b = *p; b; b = b->next) { | |
226 | if ((g = (struct entry *)b->data)->members == 0) | |
227 | continue; | |
228 | pos = FindByID(0, g->id); | |
229 | pr_Read(0, 0, pos, &gentry, sizeof(gentry)); | |
230 | for (t=0, m=g->members; m && t<PRSIZE; m=m->gnext, t++) | |
231 | gentry.entries[t] = htonl(m->user->id); | |
232 | gentry.count = htonl(t); | |
233 | pr_Write(0, 0, pos, &gentry, sizeof(gentry)); | |
234 | if (m) { | |
235 | pr_ReadEntry(0, 0, pos, &gentry); | |
236 | while (m) { | |
237 | AddToEntry(0, &gentry, pos, m->user->id); | |
238 | m = m->gnext; | |
239 | } | |
240 | } | |
241 | } | |
242 | } | |
62b40972 | 243 | |
67919620 | 244 | t = time(0); |
30c31534 | 245 | fprintf(stderr, "Done (%d users, %d groups, %d members): %s", |
246 | ucount, gcount, mcount, ctime(&t)); | |
67919620 | 247 | |
62b40972 | 248 | ##} |
249 | ||
67919620 | 250 | |
67919620 | 251 | /* |
252 | * ingerr: (supposedly) called when Ingres indicates an error. | |
253 | * I have not yet been able to get this to work to intercept a | |
254 | * database open error. | |
255 | */ | |
256 | #define INGRES_DEADLOCK 4700 | |
257 | ||
258 | static int ingerr(num) | |
259 | int *num; | |
260 | { | |
261 | char buf[256]; | |
262 | int ingres_errno; | |
263 | ||
264 | switch (*num) { | |
265 | case INGRES_DEADLOCK: | |
266 | ingres_errno = MR_DEADLOCK; | |
267 | break; | |
268 | default: | |
269 | ingres_errno = MR_INGRES_ERR; | |
270 | } | |
271 | com_err(whoami, MR_INGRES_ERR, " code %d\n", *num); | |
272 | exit(ingres_errno); | |
273 | } |