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