1 /*----------------------------------------------------------------------------
2 --
3 --  Module:           xtmAfsAclBs
4 --
5 --  Project:          XDiary
6 --  System:           xtm - X Desktop Calendar
7 --    Subsystem:      <>
8 --    Function block: <>
9 --
10 --  Description:
11 --    Base functions for ACLs in AFS.
12 --
13 --  Filename:         xtmAfsAclBs.c
14 --
15 --  Authors:          Roger Larsson, Ulrika Bornetun
16 --  Creation date:    1991-10-15
17 --
18 --
19 --  (C) Copyright Ulrika Bornetun, Roger Larsson (1995)
20 --      All rights reserved
21 --
22 --  Permission to use, copy, modify, and distribute this software and its
23 --  documentation for any purpose and without fee is hereby granted,
24 --  provided that the above copyright notice appear in all copies. Ulrika
25 --  Bornetun and Roger Larsson make no representations about the usability
26 --  of this software for any purpose. It is provided "as is" without express
27 --  or implied warranty.
28 ----------------------------------------------------------------------------*/
29 
30 /* SCCS module identifier. */
31 static char SCCSID[] = "@(#) Module: xtmAfsAclBs.c, Version: 1.1, Date: 95/02/18 15:58:53";
32 
33 
34 /*----------------------------------------------------------------------------
35 --  Include files
36 ----------------------------------------------------------------------------*/
37 
38 #include <limits.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <sys/types.h>
43 
44 #include <afs/cellconfig.h>
45 #include <afs/ptint.h>
46 #include <afs/venus.h>
47 
48 #include "System.h"
49 #include "LstLinked.h"
50 
51 #include "xtmAfsAclBs.h"
52 
53 
54 /*----------------------------------------------------------------------------
55 --  Macro definitions
56 ----------------------------------------------------------------------------*/
57 
58 
59 /*----------------------------------------------------------------------------
60 --  Type declarations
61 ----------------------------------------------------------------------------*/
62 
63 
64 /*----------------------------------------------------------------------------
65 --  Global definitions
66 ----------------------------------------------------------------------------*/
67 
68 /* Name of module. */
69 static char  *module_name = "xtmAfsAclBase";
70 
71 
72 /*----------------------------------------------------------------------------
73 --  Function prototypes
74 ----------------------------------------------------------------------------*/
75 
76 
77 
78 /*----------------------------------------------------------------------------
79 --  Functions
80 ----------------------------------------------------------------------------*/
81 
82 XTM_AF_STATUS
xtmAfInitialize()83   xtmAfInitialize()
84 {
85 
86   /* Variables. */
87   int  status;
88 
89 
90   /* Code. */
91 
92   /* Initialize connection to server. */
93   status = pr_Initialize( 1, AFSCONF_CLIENTNAME, 0 );
94 
95   if( status != 0 )
96     return( XTM_AF_ERROR );
97 
98 
99   return( XTM_AF_OK );
100 
101 } /* xtmAfInitialize */
102 
103 
104 /*----------------------------------------------------------------------*/
105 
106 XTM_AF_STATUS
xtmAfAccess(char * filename,UINT32 rights)107   xtmAfAccess( char    *filename,
108                UINT32  rights )
109 {
110 
111   /* Variables. */
112   int               status;
113   struct ViceIoctl  io_record;
114 
115 
116   /* Code. */
117 
118   /* Can the caller access the file as specified. */
119   io_record.in       = (char *) &rights;
120   io_record.in_size  = sizeof( UINT32 );
121   io_record.out      = NULL;
122   io_record.out_size = 0;
123 
124   status = pioctl( filename, VIOCACCESS, &io_record, 0 );
125   if( status != 0 )
126     return( XTM_AF_ERROR );
127 
128 
129   return( XTM_AF_OK );
130 
131 } /* xtmAfAccess */
132 
133 
134 /*----------------------------------------------------------------------*/
135 
136 void
xtmAfAclFlagToString(UINT32 flags,char * acl_string)137   xtmAfAclFlagToString( UINT32  flags,
138                         char    *acl_string )
139 {
140 
141   /* Variables. */
142   *acl_string = '\0';
143 
144 
145   /* Code. */
146 
147   if( flagIsSet( flags, XTM_AF_FLAG_READ ) )
148     strcat( acl_string, "r" );
149 
150   if( flagIsSet( flags, XTM_AF_FLAG_LOOKUP ) )
151     strcat( acl_string, "l" );
152 
153   if( flagIsSet( flags, XTM_AF_FLAG_INSERT ) )
154     strcat( acl_string, "i" );
155 
156   if( flagIsSet( flags, XTM_AF_FLAG_DELETE ) )
157     strcat( acl_string, "d" );
158 
159   if( flagIsSet( flags, XTM_AF_FLAG_WRITE ) )
160     strcat( acl_string, "w" );
161 
162   if( flagIsSet( flags, XTM_AF_FLAG_LOCK ) )
163     strcat( acl_string, "k" );
164 
165   if( flagIsSet( flags, XTM_AF_FLAG_ADMINISTER ) )
166     strcat( acl_string, "a" );
167 
168 
169   return;
170 
171 } /* xtmAfAclFlagToString */
172 
173 
174 /*----------------------------------------------------------------------*/
175 
176 UINT32
xtmAfAclStringToFlag(char * acl_string)177   xtmAfAclStringToFlag( char  *acl_string )
178 {
179 
180   /* Variables. */
181   UINT32  flags = 0;
182 
183 
184   /* Code. */
185 
186   if( strchr( acl_string, 'r' ) != NULL )
187     flagSet( flags, XTM_AF_FLAG_READ );
188 
189   if( strchr( acl_string, 'l' ) != NULL )
190     flagSet( flags, XTM_AF_FLAG_LOOKUP );
191 
192   if( strchr( acl_string, 'i' ) != NULL )
193     flagSet( flags, XTM_AF_FLAG_INSERT );
194 
195   if( strchr( acl_string, 'd' ) != NULL )
196     flagSet( flags, XTM_AF_FLAG_DELETE );
197 
198   if( strchr( acl_string, 'w' ) != NULL )
199     flagSet( flags, XTM_AF_FLAG_WRITE );
200 
201   if( strchr( acl_string, 'k' ) != NULL )
202     flagSet( flags, XTM_AF_FLAG_LOCK );
203 
204   if( strchr( acl_string, 'a' ) != NULL )
205     flagSet( flags, XTM_AF_FLAG_ADMINISTER );
206 
207 
208   return( flags );
209 
210 } /* xtmAfAclStringToFlag */
211 
212 
213 /*----------------------------------------------------------------------*/
214 
215 int
xtmAfFetchId(char * long_id)216   xtmAfFetchId( char  *long_id )
217 {
218 
219   /* Variables. */
220   int       id;
221   int       ids[ 10 ];
222   int       status;
223   idlist    list_ids;
224   namelist  list_name;
225 
226 
227   /* Code. */
228 
229   list_ids.idlist_len = 0;
230   list_ids.idlist_val = 0;
231 
232   list_name.namelist_val = (prname *) SysMalloc( 1 * PR_MAXNAMELEN );
233   list_name.namelist_len = 1;
234 
235   strcpy( list_name.namelist_val[ 0 ], long_id );
236 
237   /* Fetch ID for this user. */
238   status = pr_NameToId( &list_name, &list_ids );
239 
240   SysFree( list_name.namelist_val );
241 
242   id = list_ids.idlist_val[ 0 ];
243   if( list_ids.idlist_val != NULL )
244     SysFree( list_ids.idlist_val );
245 
246   if( status != 0 )
247     return( XTM_AF_NOID );
248 
249   return( id );
250 
251 } /* xtmAfFetchId */
252 
253 
254 /*----------------------------------------------------------------------*/
255 
256 XTM_AF_STATUS
xtmAfIsAfsFile(char * filename)257   xtmAfIsAfsFile( char  *filename )
258 {
259 
260   /* Variables. */
261   int               rights;
262   int               status;
263   struct ViceIoctl  io_record;
264 
265 
266   /* Code. */
267 
268   /* Make a simple read check. */
269   rights = XTM_AF_FLAG_READ;
270 
271   io_record.in       = (char *) &rights;
272   io_record.in_size  = sizeof( UINT32 );
273   io_record.out      = NULL;
274   io_record.out_size = 0;
275 
276   status = pioctl( filename, VIOCACCESS, &io_record, 0 );
277   if( status != 0 && status != 0 )
278     return( XTM_AF_ERROR );
279 
280 
281   return( XTM_AF_OK );
282 
283 } /* xtmAfIsAfsFile */
284 
285 
286 /*----------------------------------------------------------------------*/
287 
288 XTM_AF_STATUS
xtmAfIsAfsMountPoint(char * dir_path,char * file_name)289   xtmAfIsAfsMountPoint( char  *dir_path,
290                         char  *file_name )
291 {
292 
293   /* Variables. */
294   int               status;
295   char              buffer[ PATH_MAX + 1 ];
296   struct ViceIoctl  io_record;
297 
298 
299   /* Code. */
300 
301   /* Make a simple mount point check. */
302   io_record.in       = file_name;
303   io_record.in_size  = strlen( file_name ) + 1;
304   io_record.out      = buffer;
305   io_record.out_size = PATH_MAX;
306 
307   status = pioctl( dir_path, VIOC_AFS_STAT_MT_PT, &io_record, 1 );
308   if( status != 0 )
309     return( XTM_AF_ERROR );
310 
311 
312   return( XTM_AF_OK );
313 
314 } /* xtmAfIsAfsMountPoint */
315 
316 
317 /*----------------------------------------------------------------------*/
318 
319 XTM_AF_STATUS
xtmAfGetAclForFile(char * filename,LST_DESC_TYPE * acl_list)320   xtmAfGetAclForFile( char           *filename,
321                       LST_DESC_TYPE  *acl_list )
322 {
323 
324   /* Variables. */
325   int               char_read;
326   int               index;
327   int               no_negative_acl;
328   int               no_positive_acl;
329   int               rights;
330   int               status;
331   char              acl[ 5000 ];
332   char              id[ 1000 ];
333   char              *char_ref;
334   LST_STATUS        lst_status;
335   XTM_AF_ACL_INFO   acl_info;
336   struct ViceIoctl  io_record;
337 
338 
339   /* Code. */
340 
341   *acl_list = NULL;
342 
343   /* Go and get the acl. */
344   io_record.in       = NULL;
345   io_record.in_size  = 0;
346   io_record.out      = acl;
347   io_record.out_size = sizeof( acl );
348 
349   status = pioctl( filename, VIOCGETAL, &io_record, 0 );
350   if( status != 0 )
351     return( XTM_AF_ERROR );
352 
353 
354   *acl_list = LstLinkNew( sizeof( XTM_AF_ACL_INFO ), NULL );
355 
356   /* Nomber of entries. */
357   char_read = strlen( acl );
358   sscanf( acl, "%d %d%n", &no_positive_acl, &no_negative_acl, &char_read );
359   char_ref = &acl[ char_read ];
360 
361 
362   /* The positive rights. */
363   for( index = 0; index < no_positive_acl; index++ ) {
364     char_read = strlen( char_ref );
365     sscanf( char_ref, "%s %d%n", id, &rights, &char_read );
366 
367     /* Check the length of the id. */
368     if( strlen( id ) < XTM_AF_MAX_ID_LENGTH ) {
369 
370       /* Save the information. */
371       acl_info.positive = True;
372       acl_info.rights   = rights;
373 
374       strcpy( acl_info.id, id );
375 
376       lst_status = LstLinkInsertLast( *acl_list, &acl_info );
377 
378     } /* if */
379 
380     char_ref = char_ref + char_read;
381   } /* loop */
382 
383 
384   /* The negative rights. */
385   for( index = 0; index < no_negative_acl; index++ ) {
386     char_read = strlen( char_ref );
387     sscanf( char_ref, "%s %d%n", id, &rights, &char_read );
388 
389     /* Check the length of the id. */
390     if( strlen( id ) < XTM_AF_MAX_ID_LENGTH ) {
391 
392       /* Save the information. */
393       acl_info.positive = False;
394       acl_info.rights   = rights;
395 
396       strcpy( acl_info.id, id );
397 
398       lst_status = LstLinkInsertLast( *acl_list, &acl_info );
399 
400     } /* if */
401 
402     char_ref = char_ref + char_read;
403   } /* loop */
404 
405   return( XTM_AF_OK );
406 
407 } /* xtmAfGetAclForFile */
408 
409 
410 /*----------------------------------------------------------------------*/
411 
412 XTM_AF_STATUS
xtmAfSetAclForFile(char * filename,LST_DESC_TYPE acl_list)413   xtmAfSetAclForFile( char           *filename,
414                       LST_DESC_TYPE  acl_list )
415 {
416 
417   /* Variables. */
418   int               acl_minus_count = 0;
419   int               acl_plus_count = 0;
420   int               status;
421   char              acl[ 5000 ];
422   char              acl_minus[ 5000 ];
423   char              acl_plus[ 5000 ];
424   char              buffer[ XTM_AF_MAX_ID_LENGTH + 20 ];
425   LST_STATUS        lst_status;
426   XTM_AF_ACL_INFO   acl_info;
427   struct ViceIoctl  io_record;
428 
429 
430   /* Code. */
431 
432   /* Create the correct AFS ACL string. */
433   acl_minus[ 0 ] = '\0';
434   acl_plus[  0 ] = '\0';
435 
436   lst_status = LstLinkCurrentFirst( acl_list );
437 
438   while( lst_status == LST_OK ) {
439     lst_status = LstLinkGetCurrent( acl_list, &acl_info );
440 
441     /* Positive acl? */
442     if( acl_info.positive ) {
443       sprintf( buffer, "%s\t%d\n", acl_info.id, acl_info.rights );
444       strcat( acl_plus, buffer );
445 
446       acl_plus_count++;
447     }
448 
449     /* Negative acl? */
450     if( ! acl_info.positive ) {
451       sprintf( buffer, "%s\t%d\n", acl_info.id, acl_info.rights );
452       strcat( acl_minus, buffer );
453 
454       acl_minus_count++;
455     }
456 
457     /* Next entry. */
458     lst_status = LstLinkCurrentNext( acl_list );
459   } /* while */
460 
461 
462   /* The complete ACL. */
463   sprintf( acl, "%d\n%d\n%s%s",
464            acl_plus_count, acl_minus_count, acl_plus, acl_minus );
465 
466   /* Set the acl. */
467   io_record.in       = acl;
468   io_record.in_size  = strlen( acl ) + 1;
469   io_record.out      = NULL;
470   io_record.out_size = 0;
471 
472   status = pioctl( filename, VIOCSETAL, &io_record, 0 );
473   if( status != 0 )
474     return( XTM_AF_ERROR );
475 
476 
477   return( XTM_AF_OK );
478 
479 } /* xtmAfSetAclForFile */
480