File: os2acl.c

package info (click to toggle)
unzip 5.40-1
  • links: PTS
  • area: non-free
  • in suites: potato
  • size: 4,120 kB
  • ctags: 5,900
  • sloc: ansic: 40,977; cpp: 3,778; makefile: 1,384; asm: 1,228; sh: 133
file content (378 lines) | stat: -rw-r--r-- 9,162 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/* os2acl.c - access to OS/2 (LAN Server) ACLs
 *
 * Author:  Kai Uwe Rommel <rommel@ars.de>
 * Created: Mon Aug 08 1994
 *
 * This code is in the public domain.
 */

/*
 * supported 32-bit compilers:
 * - emx+gcc
 * - IBM C Set++ 2.1 or newer
 * - Watcom C/C++ 10.0 or newer
 *
 * supported 16-bit compilers:
 * - MS C 6.00A
 * - Watcom C/C++ 10.0 or newer
 *
 * supported OS/2 LAN environments:
 * - IBM LAN Server/Requester 3.0, 4.0 and 5.0 (Warp Server)
 * - IBM Peer 1.0 (Warp Connect)
 */

#ifdef KUR
   static char *rcsid =
   "$Id: os2acl.c,v 1.3 1996/04/03 19:18:27 rommel Exp rommel $";
   static char *rcsrev = "$Revision: 1.3 $";
#endif

/*
 * $Log: os2acl.c,v $
 * Revision 1.3  1996/04/03 19:18:27  rommel
 * minor fixes
 *
 * Revision 1.2  1996/03/30 22:03:52  rommel
 * avoid frequent dynamic allocation for every call
 * streamlined code
 *
 * Revision 1.1  1996/03/30 09:35:00  rommel
 * Initial revision
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <malloc.h>

#define INCL_NOPM
#define INCL_DOS
#define INCL_DOSERRORS
#include <os2.h>

#include "os2/os2acl.h"

#define UNLEN 20

#if defined(__WATCOMC__) && defined(__386__) && !defined(__32BIT__)
#define __32BIT__
#endif

#ifdef __32BIT__
typedef ULONG U_INT;
#ifdef __EMX__
#define PSTR16 _far16ptr
#define PTR16(x) _emx_32to16(x)
#else /* other 32-bit */
#define PSTR16 PCHAR16
#define PTR16(x) ((PCHAR16)(x))
#endif
#else /* 16-bit */
typedef USHORT U_INT;
#define PSTR16 PSZ
#define PTR16(x) (x)
#endif

typedef struct access_list
{
  char acl_ugname[UNLEN+1];
  char acl_pad;
  USHORT acl_access;
}
ACCLIST;

typedef struct access_info
{
  PSTR16 acc_resource_name;
  USHORT acc_attr;
  USHORT acc_count;
}
ACCINFO;

static ACCINFO *ai;
static char *path, *data;

#ifdef __32BIT__

#ifdef __EMX__

static USHORT (APIENTRY *_NetAccessGetInfo)(PSZ pszServer, PSZ pszResource,
  USHORT sLevel, PVOID pbBuffer, USHORT cbBuffer, PUSHORT pcbTotalAvail);
static USHORT (APIENTRY *_NetAccessSetInfo)(PSZ pszServer, PSZ pszResource,
  USHORT sLevel, PVOID pbBuffer, USHORT cbBuffer, USHORT sParmNum);
static USHORT (APIENTRY *_NetAccessAdd)(PSZ pszServer,
  USHORT sLevel, PVOID pbBuffer, USHORT cbBuffer);

USHORT NetAccessGetInfo(PSZ pszServer, PSZ pszResource, USHORT sLevel,
                        PVOID pbBuffer, USHORT cbBuffer, PUSHORT pcbTotalAvail)
{
  return (USHORT)
          (_THUNK_PROLOG (4+4+2+4+2+4);
           _THUNK_FLAT (pszServer);
           _THUNK_FLAT (pszResource);
           _THUNK_SHORT (sLevel);
           _THUNK_FLAT (pbBuffer);
           _THUNK_SHORT (cbBuffer);
           _THUNK_FLAT (pcbTotalAvail);
           _THUNK_CALLI (_emx_32to16(_NetAccessGetInfo)));
}

USHORT NetAccessSetInfo(PSZ pszServer, PSZ pszResource, USHORT sLevel,
                        PVOID pbBuffer, USHORT cbBuffer, USHORT sParmNum)
{
  return (USHORT)
          (_THUNK_PROLOG (4+4+2+4+2+2);
           _THUNK_FLAT (pszServer);
           _THUNK_FLAT (pszResource);
           _THUNK_SHORT (sLevel);
           _THUNK_FLAT (pbBuffer);
           _THUNK_SHORT (cbBuffer);
           _THUNK_SHORT (sParmNum);
           _THUNK_CALLI (_emx_32to16(_NetAccessSetInfo)));
}

USHORT NetAccessAdd(PSZ pszServer, USHORT sLevel,
                    PVOID pbBuffer, USHORT cbBuffer)
{
  return (USHORT)
          (_THUNK_PROLOG (4+2+4+2);
           _THUNK_FLAT (pszServer);
           _THUNK_SHORT (sLevel);
           _THUNK_FLAT (pbBuffer);
           _THUNK_SHORT (cbBuffer);
           _THUNK_CALLI (_emx_32to16(_NetAccessAdd)));
}

#else /* other 32-bit */

APIRET16 (* APIENTRY16 NetAccessGetInfo)(PCHAR16 pszServer, PCHAR16 pszResource,
  USHORT sLevel, PVOID16 pbBuffer, USHORT cbBuffer, PVOID16 pcbTotalAvail);
APIRET16 (* APIENTRY16 NetAccessSetInfo)(PCHAR16 pszServer, PCHAR16 pszResource,
  USHORT sLevel, PVOID16 pbBuffer, USHORT cbBuffer, USHORT sParmNum);
APIRET16 (* APIENTRY16 NetAccessAdd)(PCHAR16 pszServer,
  USHORT sLevel, PVOID16 pbBuffer, USHORT cbBuffer);

#define _NetAccessGetInfo NetAccessGetInfo
#define _NetAccessSetInfo NetAccessSetInfo
#define _NetAccessAdd NetAccessAdd

#if !defined(__IBMC__) || !defined(__TILED__)
#define _tmalloc malloc
#define _tfree free
#endif

#endif
#else /* 16-bit */

USHORT (APIENTRY *NetAccessGetInfo)(PSZ pszServer, PSZ pszResource,
  USHORT sLevel, PVOID pbBuffer, USHORT cbBuffer, PUSHORT pcbTotalAvail);
USHORT (APIENTRY *NetAccessSetInfo)(PSZ pszServer, PSZ pszResource,
  USHORT sLevel, PVOID pbBuffer, USHORT cbBuffer, USHORT sParmNum);
USHORT (APIENTRY *NetAccessAdd)(PSZ pszServer,
  USHORT sLevel, PVOID pbBuffer, USHORT cbBuffer);

#define _NetAccessGetInfo NetAccessGetInfo
#define _NetAccessSetInfo NetAccessSetInfo
#define _NetAccessAdd NetAccessAdd

#define _tmalloc malloc
#define _tfree free

#define DosQueryProcAddr(handle, ord, name, funcptr) \
        DosGetProcAddr(handle, name, funcptr)
#define DosQueryCurrentDir DosQCurDir
#define DosQueryCurrentDisk DosQCurDisk

#endif


static BOOL acl_init(void)
{
  static BOOL initialized, netapi_avail;
  HMODULE netapi;
  char buf[256];

  if (initialized)
    return netapi_avail;

  initialized = TRUE;

  if (DosLoadModule(buf, sizeof(buf), "NETAPI", &netapi))
    return FALSE;

  if (DosQueryProcAddr(netapi, 0, "NETACCESSGETINFO", (PFN *) &_NetAccessGetInfo) ||
      DosQueryProcAddr(netapi, 0, "NETACCESSSETINFO", (PFN *) &_NetAccessSetInfo) ||
      DosQueryProcAddr(netapi, 0, "NETACCESSADD", (PFN *) &_NetAccessAdd))
    return FALSE;

#if defined(__WATCOMC__) && defined(__386__)
  NetAccessGetInfo = (PVOID) (ULONG) (PVOID16) NetAccessGetInfo;
  NetAccessSetInfo = (PVOID) (ULONG) (PVOID16) NetAccessSetInfo;
  NetAccessAdd     = (PVOID) (ULONG) (PVOID16) NetAccessAdd;
#endif

  if ((path = _tmalloc(CCHMAXPATH)) == NULL)
    return FALSE;
  if ((data = _tmalloc(ACL_BUFFERSIZE)) == NULL)
    return FALSE;
  if ((ai = _tmalloc(sizeof(ACCINFO))) == NULL)
    return -1;

  netapi_avail = TRUE;

  return netapi_avail;
}

static void acl_mkpath(char *buffer, const char *source)
{
  char *ptr;
  static char cwd[CCHMAXPATH];
  static U_INT cwdlen;
  U_INT cdrive;
  ULONG drivemap;

  if (isalpha(source[0]) && source[1] == ':')
    buffer[0] = 0; /* fully qualified names */
  else
  {
    if (cwd[0] == 0)
    {
      DosQueryCurrentDisk(&cdrive, &drivemap);
      cwd[0] = (char)(cdrive + '@');
      cwd[1] = ':';
      cwd[2] = '\\';
      cwdlen = sizeof(cwd) - 3;
      DosQueryCurrentDir(0, cwd + 3, &cwdlen);
      cwdlen = strlen(cwd);
    }

    if (source[0] == '/' || source[0] == '\\')
    {
      if (source[1] == '/' || source[1] == '\\')
        buffer[0] = 0; /* UNC names */
      else
      {
        strncpy(buffer, cwd, 2);
        buffer[2] = 0;
      }
    }
    else
    {
      strcpy(buffer, cwd);
      if (cwd[cwdlen - 1] != '\\' && cwd[cwdlen - 1] != '/')
        strcat(buffer, "/");
    }
  }

  strcat(buffer, source);

  for (ptr = buffer; *ptr; ptr++)
    if (*ptr == '/')
      *ptr = '\\';

  if (ptr[-1] == '\\')
    ptr[-1] = 0;

  strupr(buffer);
}

static int acl_bin2text(char *data, char *text)
{
  ACCINFO *ai;
  ACCLIST *al;
  U_INT cnt, offs;

  ai = (ACCINFO *) data;
  al = (ACCLIST *) (data + sizeof(ACCINFO));

  offs = sprintf(text, "ACL1:%X,%d\n",
                 ai -> acc_attr, ai -> acc_count);

  for (cnt = 0; cnt < ai -> acc_count; cnt++)
    offs += sprintf(text + offs, "%s,%X\n",
                    al[cnt].acl_ugname, al[cnt].acl_access);

  return strlen(text);
}

int acl_get(char *server, const char *resource, char *buffer)
{
  USHORT datalen;
  PSZ srv = NULL;
  int rc;

  if (!acl_init())
    return -1;

  if (server)
    srv = server;

  acl_mkpath(path, resource);
  datalen = 0;

  rc = NetAccessGetInfo(srv, path, 1, data, ACL_BUFFERSIZE, &datalen);

  if (rc == 0)
    acl_bin2text(data, buffer);

  return rc;
}

static int acl_text2bin(char *data, char *text, char *path)
{
  ACCINFO *ai;
  ACCLIST *al;
  char *ptr, *ptr2;
  U_INT cnt;

  ai = (ACCINFO *) data;
  ai -> acc_resource_name = PTR16(path);

  if (sscanf(text, "ACL1:%hX,%hd",
             &ai -> acc_attr, &ai -> acc_count) != 2)
    return ERROR_INVALID_PARAMETER;

  al = (ACCLIST *) (data + sizeof(ACCINFO));
  ptr = strchr(text, '\n') + 1;

  for (cnt = 0; cnt < ai -> acc_count; cnt++)
  {
    ptr2 = strchr(ptr, ',');
    strncpy(al[cnt].acl_ugname, ptr, ptr2 - ptr);
    al[cnt].acl_ugname[ptr2 - ptr] = 0;
    sscanf(ptr2 + 1, "%hx", &al[cnt].acl_access);
    ptr = strchr(ptr, '\n') + 1;
  }

  return sizeof(ACCINFO) + ai -> acc_count * sizeof(ACCLIST);
}

int acl_set(char *server, const char *resource, char *buffer)
{
  USHORT datalen;
  PSZ srv = NULL;

  if (!acl_init())
    return -1;

  if (server)
    srv = server;

  acl_mkpath(path, resource);

  ai -> acc_resource_name = PTR16(path);
  ai -> acc_attr = 0;
  ai -> acc_count = 0;

  NetAccessAdd(srv, 1, ai, sizeof(ACCINFO));
  /* Ignore any errors, most probably because ACL already exists. */
  /* In any such case, try updating the existing ACL. */

  datalen = acl_text2bin(data, buffer, path);

  return NetAccessSetInfo(srv, path, 1, data, datalen, 0);
}

/* end of os2acl.c */