]> andersk Git - moira.git/blob - afssync/sync.qc
Optimized synchronization code (temporarily removed cross-cell support)
[moira.git] / afssync / sync.qc
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))
23 struct hash *users = NULL;
24 char *whoami = "sync";
25
26 char *malloc(), *strsave();
27 int dbase_fd;
28
29
30 main(argc, argv)
31 int argc;
32 char **argv;
33 {
34     int status;
35     int ingerr();
36
37     if (argc != 2) {
38         fprintf(stderr, "usage: %s outfile\n", argv[0]);
39         exit(MR_ARGS);
40     }
41
42     dbase_fd = open(argv[1], O_RDWR|O_CREAT, 0660);
43     if (dbase_fd < 0) {
44         perror("opening file %s", argv[1]);
45         exit(1);
46     }   
47     IIseterr(ingerr);
48     initialize_sms_error_table();
49     initialize_pt_error_table();
50     Initdb();
51
52 ##  ingres sms
53 ##  set lockmode session where level = table
54 ##  begin transaction
55
56     do_passwd();
57     do_groups();
58
59 ##  end transaction
60 ##  exit
61
62     exit(MR_SUCCESS);
63 }
64
65
66 /*
67  * ingerr: (supposedly) called when Ingres indicates an error.
68  * I have not yet been able to get this to work to intercept a
69  * database open error.
70  */
71 #define INGRES_DEADLOCK 4700
72
73 static int ingerr(num)
74     int *num;
75 {
76     char buf[256];
77     int ingres_errno;
78
79     switch (*num) {
80     case INGRES_DEADLOCK:
81         ingres_errno = MR_DEADLOCK;
82         break;
83     default:
84         ingres_errno = MR_INGRES_ERR;
85     }
86     com_err(whoami, MR_INGRES_ERR, " code %d\n", *num);
87     exit(ingres_errno);
88 }
89
90
91
92 do_passwd()
93 ##{
94 ##  char login[9];
95 ##  int uid, id, status;
96
97     fprintf(stderr, "Doing users\n");
98     users = create_hash(10000);
99 ##  range of u is users
100 ##  retrieve (login = u.#login, uid = u.#uid, id = u.users_id)
101 ##      where u.#uid > 0 and (u.#status = 1 or u.#status = 2) {
102             strtrim(login);
103             hash_store(users, id, uid);
104             if (FindByID (0, uid))
105                 status = PRIDEXIST;
106             else
107                 status = CreateEntry(0, login, &uid, 1/*idflag*/, 0/*gflag*/,
108                                      SYSADMINID/*oid*/, SYSADMINID/*cid*/);
109             if (status)
110                 fprintf(stderr, "Error adding user %s uid %d: %s\n",
111                         login, uid, error_message(status));
112 ##  }
113 ##}
114
115
116
117 do_groups()
118 ##{
119     struct hash *groups;
120     long u, g, status, gpos, upos;
121     struct prentry gentry, uentry;
122 ##  char name[33], namebuf[128];
123 ##  int gid, id, lid, hide;
124     int aid;
125     
126     fprintf(stderr, "Doing groups\n");
127
128     /* make space for group list */
129     groups = create_hash(15000);
130
131     /* retrieve simple groups */
132 ##  range of l is list
133 ##  range of m is imembers
134     /* get lock records */
135 ##  retrieve (name = l.modtime) where l.list_id = 0
136 ##  retrieve (name = users.modtime) where users.users_id = 0
137
138 ##  retrieve (name = l.#name, gid = l.#gid, lid = l.list_id, hide = l.hidden)
139 ##      where l.group != 0 and l.active != 0 and l.#gid > 0 {
140             strtrim(name);
141             sprintf(namebuf, "system:%s", name);
142             hash_store(groups, lid, -gid);
143             aid = -gid;
144             if (gpos = FindByID (0, aid))
145                 status = PRIDEXIST;
146             else
147                 status = CreateEntry(0,namebuf,&aid,1/*idflag*/,PRGRP/*gflag*/,
148                                      SYSADMINID/*oid*/, SYSADMINID/*cid*/);
149             if (status)
150                 fprintf(stderr, "Error adding group %s id %d: %s\n",
151                         namebuf, aid, error_message(status));
152
153             /* If this list is hidden, set the pts entry to be s---- */
154             if (hide && (status==0 || status==PRIDEXIST)) {
155                 if (!gpos)
156                     gpos = FindByID(0, aid);
157                 status = pr_ReadEntry(0, 0, gpos, &gentry);
158                 if (!status) {
159                     gentry.flags = PRGRP|PRACCESS|PRP_STATUS_ANY;
160                     status = pr_WriteEntry(0, 0, gpos, &gentry);
161                 }
162                 if (status)
163                     fprintf(stderr, "Error setting flags on group %s: %s\n",
164                             namebuf, error_message(status));
165             }
166 ##  }
167
168
169     fprintf(stderr, "Doing members\n");
170
171 ##  retrieve (lid = m.list_id, id = m.member_id)
172 ##      where m.member_type = "USER" {
173       if ((u = (long) hash_lookup(users, id)) &&
174           (g = (long) hash_lookup(groups, lid))) {
175           if (g==ANYUSERID || g==AUTHUSERID || u==ANONYMOUSID) {
176               status = PRPERM;
177           } else if ((gpos = FindByID(0, g)) && (upos = FindByID(0, u))) {
178               status = pr_ReadEntry(0,0,upos,&uentry);
179               if (!status) status = pr_ReadEntry(0,0,gpos,&gentry);
180               if (!status) status = AddToEntry (0, &gentry, gpos, u);
181               if (!status) status = AddToEntry (0, &uentry, upos, g);
182           } else {
183               status = PRNOENT;
184           }
185           if (status)
186               fprintf(stderr, "Error adding uid %d to group %d: %s\n",
187                       u, -g, error_message(status));
188       }
189 ##  }
190
191 ##}
192
This page took 0.054176 seconds and 5 git commands to generate.