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