[0001]
[0002]
[0003]
[0004]
[0005]
[0006]
[0007]
[0008]
[0009]
[0010]
[0011]
[0012]
[0013]
[0014]
[0015]
[0016]
[0017]
[0018]
[0019]
[0020]
[0021]
[0022]
[0023]
[0024]
[0025]
[0026]
[0027]
[0028]
[0029]
[0030]
[0031]
[0032]
[0033]
[0034]
[0035]
[0036]
[0037]
[0038]
[0039]
[0040]
[0041]
[0042]
[0043]
[0044]
[0045]
[0046]
[0047]
[0048]
[0049]
[0050]
[0051]
[0052]
[0053]
[0054]
[0055]
[0056]
[0057]
[0058]
[0059]
[0060]
[0061]
[0062]
[0063]
[0064]
[0065]
[0066]
[0067]
[0068]
[0069]
[0070]
[0071]
[0072]
[0073]
[0074]
[0075]
[0076]
[0077]
[0078]
[0079]
[0080]
[0081]
[0082]
[0083]
[0084]
[0085]
[0086]
[0087]
[0088]
[0089]
[0090]
[0091]
[0092]
[0093]
[0094]
[0095]
[0096]
[0097]
[0098]
[0099]
[0100]
[0101]
[0102]
[0103]
[0104]
[0105]
[0106]
[0107]
[0108]
[0109]
[0110]
[0111]
[0112]
[0113]
[0114]
[0115]
[0116]
[0117]
[0118]
[0119]
[0120]
[0121]
[0122]
[0123]
[0124]
[0125]
[0126]
[0127]
[0128]
[0129]
[0130]
[0131]
[0132]
[0133]
[0134]
[0135]
[0136]
[0137]
[0138]
[0139]
[0140]
[0141]
[0142]
[0143]
[0144]
[0145]
[0146]
[0147]
[0148]
[0149]
[0150]
[0151]
[0152]
[0153]
[0154]
[0155]
[0156]
[0157]
[0158]
[0159]
[0160]
[0161]
[0162]
[0163]
[0164]
[0165]
[0166]
[0167]
[0168]
[0169]
[0170]
[0171]
[0172]
[0173]
[0174]
[0175]
[0176]
[0177]
[0178]
[0179]
[0180]
[0181]
[0182]
[0183]
[0184]
[0185]
[0186]
[0187]
[0188]
[0189]
[0190]
[0191]
[0192]
[0193]
[0194]
[0195]
[0196]
[0197]
[0198]
[0199]
[0200]
[0201]
[0202]
[0203]
[0204]
[0205]
[0206]
[0207]
[0208]
[0209]
[0210]
[0211]
[0212]
[0213]
[0214]
[0215]
[0216]
[0217]
[0218]
[0219]
[0220]
[0221]
[0222]
[0223]
[0224]
[0225]
[0226]
[0227]
[0228]
[0229]
[0230]
[0231]
[0232]
[0233]
[0234]
[0235]
[0236]
[0237]
[0238]
[0239]
[0240]
[0241]
[0242]
[0243]
[0244]
[0245]
[0246]
[0247]
[0248]
[0249]
[0250]
[0251]
[0252]
[0253]
[0254]
[0255]
[0256]
[0257]
[0258]
[0259]
[0260]
[0261]
[0262]
[0263]
[0264]
[0265]
[0266]
[0267]
[0268]
[0269]
[0270]
[0271]
[0272]
[0273]
[0274]
[0275]
[0276]
[0277]
[0278]
[0279]
[0280]
[0281]
[0282]
[0283]
[0284]
[0285]
[0286]
[0287]
[0288]
[0289]
[0290]
[0291]
[0292]
[0293]
[0294]
[0295]
[0296]
[0297]
[0298]
[0299]
[0300]
[0301]
[0302]
[0303]
[0304]
[0305]
[0306]
[0307]
[0308]
[0309]
[0310]
[0311]
[0312]
[0313]
[0314]
[0315]
[0316]
[0317]
[0318]
[0319]
[0320]
[0321]
[0322]
[0323]
[0324]
[0325]
[0326]
[0327]
[0328]
[0329]
[0330]
[0331]
[0332]
[0333]
[0334]
[0335]
[0336]
[0337]
[0338]
[0339]
[0340]
[0341]
[0342]
[0343]
[0344]
[0345]
[0346]
[0347]
[0348]
[0349]
[0350]
[0351]
[0352]
[0353]
[0354]
[0355]
[0356]
[0357]
[0358]
[0359]
[0360]
[0361]
[0362]
[0363]
[0364]
[0365]
[0366]
[0367]
[0368]
[0369]
[0370]
[0371]
[0372]
[0373]
[0374]
[0375]
[0376]
[0377]
[0378]
[0379]
[0380]
[0381]
[0382]
[0383]
[0384]
[0385]
[0386]
[0387]
[0388]
[0389]
[0390]
[0391]
[0392]
[0393]
[0394]
[0395]
[0396]
[0397]
[0398]
[0399]
[0400]
[0401]
[0402]
[0403]
[0404]
[0405]
[0406]
[0407]
[0408]
[0409]
[0410]
[0411]
[0412]
[0413]
[0414]
[0415]
[0416]
[0417]
[0418]
[0419]
[0420]
[0421]
[0422]
[0423]
[0424]
[0425]
[0426]
[0427]
[0428]
[0429]
[0430]
[0431]
[0432]
[0433]
[0434]
[0435]
[0436]
[0437]
[0438]
[0439]
[0440]
[0441]
[0442]
[0443]
[0444]
[0445]
[0446]
[0447]
[0448]
[0449]
[0450]
[0451]
[0452]
[0453]
[0454]
[0455]
[0456]
[0457]
[0458]
[0459]
[0460]
[0461]
[0462]
[0463]
[0464]
[0465]
[0466]
[0467]
[0468]
[0469]
[0470]
[0471]
[0472]
[0473]
[0474]
[0475]
[0476]
[0477]
[0478]
[0479]
[0480]
[0481]
[0482]
[0483]
[0484]
[0485]
[0486]
[0487]
[0488]
[0489]
[0490]
[0491]
[0492]
[0493]
[0494]
[0495]
[0496]
[0497]
[0498]
[0499]
[0500]
[0501]
[0502]
[0503]
[0504]
[0505]
[0506]
[0507]
[0508]
[0509]
[0510]
[0511]
[0512]
[0513]
[0514]
[0515]
[0516]
[0517]
[0518]
[0519]
[0520]
[0521]
[0522]
[0523]
[0524]
[0525]
[0526]
[0527]
[0528]
[0529]
[0530]
[0531]
[0532]
[0533]
[0534]
[0535]
[0536]
[0537]
[0538]
[0539]
[0540]
[0541]
[0542]
[0543]
[0544]
[0545]
[0546]
[0547]
[0548]
[0549]
[0550]
[0551]
[0552]
[0553]
[0554]
[0555]
[0556]
[0557]
[0558]
[0559]
[0560]
[0561]
[0562]
[0563]
[0564]
[0565]
[0566]
[0567]
[0568]
[0569]
[0570]
[0571]
[0572]
[0573]
[0574]
[0575]
[0576]
[0577]
[0578]
[0579]
[0580]
[0581]
[0582]
[0583]
[0584]
[0585]
[0586]
[0587]
[0588]
[0589]
[0590]
[0591]
[0592]
[0593]
[0594]
[0595]
[0596]
[0597]
[0598]
[0599]
[0600]
[0601]
[0602]
[0603]
[0604]
[0605]
[0606]
[0607]
[0608]
[0609]
[0610]
[0611]
[0612]
[0613]
[0614]
[0615]
[0616]
[0617]
[0618]
[0619]
[0620]
[0621]
[0622]
[0623]
[0624]
[0625]
[0626]
[0627]
[0628]
[0629]
[0630]
[0631]
[0632]
[0633]
[0634]
[0635]
[0636]
[0637]
[0638]
[0639]
[0640]
[0641]
[0642]
[0643]
[0644]
[0645]
[0646]
[0647]
[0648]
[0649]
[0650]
[0651]
[0652]
[0653]
[0654]
[0655]
[0656]
[0657]
[0658]
[0659]
[0660]
[0661]
[0662]
[0663]
[0664]
[0665]
[0666]
[0667]
[0668]
[0669]
[0670]
[0671]
[0672]
[0673]
[0674]
[0675]
[0676]
[0677]
[0678]
[0679]
[0680]
[0681]
[0682]
[0683]
[0684]
[0685]
[0686]
[0687]
[0688]
[0689]
[0690]
[0691]
[0692]
[0693]
[0694]
[0695]
[0696]
[0697]
[0698]
[0699]
[0700]
[0701]
[0702]
[0703]
[0704]
[0705]
[0706]
[0707]
[0708]
[0709]
[0710]
[0711]
[0712]
[0713]
[0714]
[0715]
[0716]
[0717]
[0718]
[0719]
[0720]
[0721]
[0722]
[0723]
[0724]
[0725]
[0726]
[0727]
[0728]
[0729]
[0730]
[0731]
[0732]
[0733]
[0734]
[0735]
[0736]
[0737]
[0738]
[0739]
[0740]
[0741]
[0742]
[0743]
[0744]
[0745]
[0746]
[0747]
[0748]
[0749]
[0750]
[0751]
[0752]
[0753]
[0754]
[0755]
[0756]
[0757]
[0758]
[0759]
[0760]
[0761]
[0762]
[0763]
[0764]
[0765]
[0766]
[0767]
[0768]
[0769]
[0770]
[0771]
[0772]
[0773]
[0774]
[0775]
[0776]
[0777]
[0778]
[0779]
[0780]
[0781]
[0782]
[0783]
[0784]
[0785]
[0786]
[0787]
[0788]
[0789]
[0790]
[0791]
[0792]
[0793]
[0794]
[0795]
[0796]
[0797]
[0798]
[0799]
[0800]
[0801]
[0802]
[0803]
[0804]
[0805]
[0806]
[0807]
[0808]
[0809]
[0810]
[0811]
[0812]
[0813]
[0814]
[0815]
[0816]
[0817]
[0818]
[0819]
[0820]
[0821]
[0822]
[0823]
[0824]
[0825]
[0826]
[0827]
[0828]
[0829]
[0830]
[0831]
[0832]
[0833]
[0834]
[0835]
[0836]
[0837]
[0838]
[0839]
[0840]
[0841]
[0842]
[0843]
[0844]
[0845]
[0846]
[0847]
[0848]
[0849]
[0850]
[0851]
[0852]
[0853]
[0854]
[0855]
[0856]
[0857]
[0858]
[0859]
[0860]
[0861]
[0862]
[0863]
[0864]
[0865]
[0866]
[0867]
[0868]
[0869]
[0870]
[0871]
[0872]
[0873]
[0874]
[0875]
[0876]
[0877]
[0878]
[0879]
[0880]
[0881]
[0882]
[0883]
[0884]
[0885]
[0886]
[0887]
[0888]
[0889]
[0890]
[0891]
[0892]
[0893]
[0894]
[0895]
[0896]
[0897]
[0898]
[0899]
[0900]
[0901]
[0902]
[0903]
[0904]
[0905]
[0906]
[0907]
[0908]
[0909]
[0910]
[0911]
[0912]
[0913]
[0914]
[0915]
[0916]
[0917]
[0918]
[0919]
[0920]
[0921]
[0922]
[0923]
[0924]
[0925]
[0926]
[0927]
[0928]
[0929]
[0930]
[0931]
[0932]
[0933]
[0934]
[0935]
[0936]
[0937]
[0938]
[0939]
[0940]
[0941]
[0942]
[0943]
[0944]
[0945]
[0946]
[0947]
[0948]
[0949]
[0950]
[0951]
[0952]
[0953]
[0954]
[0955]
[0956]
[0957]
[0958]
[0959]
[0960]
[0961]
[0962]
[0963]
[0964]
[0965]
[0966]
[0967]
[0968]
[0969]
[0970]
[0971]
[0972]
[0973]
[0974]
[0975]
[0976]
[0977]
[0978]
[0979]
[0980]
[0981]
/*****************************************************************************/
/*
                                Persona.c

Tuck in the cape.
Don the hat and glasses.
There ... we're hardly recognizable!

For VMS versions 6.2 and later the $PERSONA system services provide a reliable
mechanism for a process to assume all the defining characteristics of another
user.  This offers a number of potential uses, in particular scripting under
non-server user names.  As the persona services affect processes job-wide (i.e.
a (sub)process tree) this facility may only be used where scripting processes
can be detached and not spawned.

For VMS versions prior to V6.2 the $PERSONA services do not exist and so this
functionality is not available.

For server NETWORK MODE operation this module is used as a convenient set of
stubs and for checking whether an account is allowed to be used for scripting
or not from the SYSUAF.  The persona of the process is never actually changed
however.


PERSONA_MACRO
-------------
For VAX VMS versions that do not support the $PERSONA services (i.e. 6.0 and
6.1) there is an unsupported alternative, the PERSONA.MAR module.  Essentially
WASD only uses the $PERSONA services to change the process username before
creating a detached or script process.  The PERSONA.MAR routine also performs
this function by explicitly manipulating the process structures in kernel mode. 
This could have been done using a C module but being unsure of any changes to
the kernel structures involved over VMS 6.0 and 6.1 it will be better to
generate the object code on the system it is going to be executing on.  A basic
integrity check of the supplied username is made (greater than zero and less
than or equal to twelve upper-case alpha-numeric/underscore characters).  No
check is performed that the supplied username exists, the process creation will
just fail if it does not.  The /PERSONA=<identifier> account restriction
mechanism is NOT available when using this kludge.  THIS IS KERNEL MODE CODE! 
I am not a VMS internals specialist!!  Use at your own risk!!!  YOU HAVE BEEN
WARNED!!!!


ENABLING PERSONA
----------------
Be aware that there are all sorts of issues surrounding scripting, and an order
of magnitude more so if under multiple accounts, and two orders of magnitude
more again if any of those accounts have anything more than the average Joe's
privileges.  Believe it!  It has been demonstrated using WASD :^(

If you must script under non-server accounts (and there are still good reasons
why this can be desireable) then take great care!

There are some features of WASD persona that help in reducing the possibility
of scripting with an unintended account.

1)  Unless the server is started with /PERSONA scripting under a non-server
account is not possible.  Persona is disabled by default.

2)  Unless the startup includes /PERSONA=RELAXED then an account with any
privilege other than NETMBX and TMPMBX is prohibited.

3)  Exactly which accounts are allowed to script can optionally be controlled
using a VMS rights identifier.  Starting the server with /PERSONA=ident-name
restricts persona scripting to those accounts granted that identifier.  The
name "WASD_SCRIPTING" is probably a reasonable choice.

4)  If the startup included /PERSONA=AUTHORIZED then only requests that have
been subject to HTTP authorization and authentication are allowed to script
under non-server accounts.

5)  If the qualifier /PERSONA=RELAXED=AUTHORIZED then privileged account are
allowed to be used for scripting but only if the request has been subject to
HTTP authorization and authentication.

Meaningful combinations of these startup parameters are possible:

  /PERSONA
  /PERSONA=ident-name
  /PERSONA=RELAXED
  /PERSONA=(ident-name,RELAXED)
  /PERSONA=AUTHORIZED
  /PERSONA=(ident-name,AUTHORIZED,RELAXED)
  /PERSONA=(ident-name,RELAXED=AUTHORIZED)

Remember to review site configuration carefully!


VERSION HISTORY
---------------
10-JUL-2013  MGD  PersonaAssume() wrap sys$persona_create() with SYSPRV
                    after modifications to DclMailboxAcl() to allow usernames
                    without associated identifiers (i.e. shared UICs)
22-JUL-2003  MGD  PersonaAllowedUai() to support network mode
16-MAR-2003  MGD  bugfix; Alpha VMS V7.1 or earlier sys$persona_assume() needs
                  to be used in the same way as for VAX (thanks Giles Burrows)
08-OCT-2002  MGD  consider UIC system group membership as privileged
16-SEP-2002  MGD  implement /PERSONA=[AUTHORIZED|RELAXED|RELAXED=AUTHORIZED]
                  to prevent inadvertant scripting using privileged accounts,
                  reorganise and refine some of the code
29-OCT-2001  MGD  PERSONA_MACRO and PERSONA.MAR for a rude-and-crude
                  (and certainly unsupported) pre-VMS 6.2 VAX persona
04-AUG-2001  MGD  support module WATCHing
19-NOV-2000  MGD  bugfix; VAX requires IMP$M_ASSUME_SECURITY 
01-OCT-2000  MGD  initial
*/
/*****************************************************************************/

#ifdef WASD_VMS_V7
#undef _VMS__V6__SOURCE
#define _VMS__V6__SOURCE
#undef __VMS_VER
#define __VMS_VER 70000000
#undef __CRTL_VER
#define __CRTL_VER 70000000
#endif

#define WASD_MODULE "PERSONA"

#ifdef PERSONA_MACRO
#  undef PERSONA_STUB
#  define PERSONA_STUB
#endif

#ifdef PERSONA_STUB 

#undef _VMS__V6__SOURCE
#define _VMS__V6__SOURCE
#undef __VMS_VER
#define __VMS_VER 70000000
#undef __CRTL_VER
#define __CRTL_VER 70000000

#else /* PERSONA_STUB */

#undef _VMS__V6__SOURCE
#define _VMS__V6__SOURCE
#undef __VMS_VER
#define __VMS_VER 60200000
#undef  __CRTL_VER
#define __CRTL_VER 60200000

#endif /* PERSONA_STUB */

/* standard C header files */
#include <errno.h>
#include <stdio.h>
#include <string.h>

/* VMS related header files */
#include <descrip.h>
#include <jpidef.h>
#include <prvdef.h>
#include <ssdef.h>
#include <stsdef.h>
#include <uaidef.h>

/* application-related header files */
#include "wasd.h"

#ifdef PERSONA_STUB 

/*
For versions of VMS where the sys$persona...() services are not available
provide some that can be linked to but just return a detectable error.  On
these systems the WASD persona-based features will of course be unavailable
unless using the macro kernel-mode routine to change the username.
*/
#define sys$persona_create PersonaStub
#define sys$persona_delete PersonaStub
#define sys$persona_assume PersonaStub

#endif /* PERSONA_STUB */

/******************/
/* global storage */
/******************/

#define PERSONA_DEFAULT_CACHE_SIZE 32

BOOL  PersonaMacro;

LIST_HEAD  PersonaCacheList;
int  PersonaCacheCount,
     PersonaCacheEntries;

unsigned long  CmKrnlMask [2] = { PRV$M_CMKRNL, 0 };

char  PersonaIdentName [64];

unsigned long  PersonaRightsIdent;

#ifdef PERSONA_MACRO
int  PersonaUserName12Length;
/* used solely by STARTUP.COM to detect if CMKRNL is required */
char  PersonaServerUserName12 [12],
      PersonaUserName12 [12];
char  PersonaDotMar [] = "PERSONA.MAR";
#endif

/********************/
/* external storage */
/********************/

extern BOOL  CliPersonaAuthorized,
             CliPersonaEnabled,
             CliPersonaRelaxed,
             CliPersonaRelaxedAuthorized,
             HttpdNetworkMode;

extern int  EfnWait;

extern int  ToLowerCase[],
            ToUpperCase[];

extern unsigned long  AveJoePrvMask[],
                      SysPrvMask[];

extern char  ErrorSanityCheck[];

extern CONFIG_STRUCT  Config;
extern SYS_INFO  SysInfo;
extern WATCH_STRUCT  Watch;

/****************************************************************************/

#ifndef PERSONA_MACRO 

   /*********************/
   /* $PERSONA SERVICES */
   /*********************/

/****************************************************************************/
/*
For $PERSONA service.
Initialize the PERSONA module. Check if an identifier is required for access to
the non-server-account scripting persona services then establish that it
exists.
*/ 

int PersonaInit ()

{
   static $DESCRIPTOR (PersonaIdentNameDsc, PersonaIdentName);

   int  status;

   /*********/
   /* begin */
   /*********/

   if (WATCH_MODULE(WATCH_MOD_DCL))
      WatchThis (WATCHALL, WATCH_MOD_DCL, "PersonaInit()\n");

#ifdef PERSONA_STUB

   return (SS$_UNSUPPORTED);

#else /* PERSONA_STUB */

   if (CliPersonaRelaxed) FaoToStdout ("%HTTPD-I-PERSONA, relaxed\n");

   if (!PersonaIdentName[0]) return (SS$_NORMAL);

   PersonaIdentNameDsc.dsc$w_length = strlen(PersonaIdentName);

   status = sys$asctoid (&PersonaIdentNameDsc, &PersonaRightsIdent, 0);

   if (VMSok (status))
      FaoToStdout ("%HTTPD-I-PERSONA, services identifier \'!AZ\'\n",
                   PersonaIdentName);
   else
   {
      FaoToStdout ("%HTTPD-E-PERSONA, services identifier \'!AZ\'\n-!&M\n",
                   PersonaIdentName, status);
      CliPersonaEnabled = CliPersonaRelaxed = false;
   }

   return (status);

#endif /* PERSONA_STUB */
}

/*****************************************************************************/
/*
For $PERSONA service.
Assume the 'persona' of the specified user name.  If 'UserName' is NULL then
return to the "natural" persona of the server process.
*/

#define PERSONA_NETWORK_MODE 2
#define ISS$C_ID_NATURAL 1
#define IMP$M_ASSUME_SECURITY 1

int PersonaAssume (char *UserName)

{
   static int  PersonaHandle;
   static long  PersonaCreateFlags;
   static long  PersonaAssumeFlags = IMP$M_ASSUME_SECURITY;
   static $DESCRIPTOR (UserNameDsc, "");

   int  status;

   /*********/
   /* begin */
   /*********/

   if (WATCH_MODULE(WATCH_MOD_DCL))
      WatchThis (WATCHALL, WATCH_MOD_DCL,
                 "PersonaAssume() !&Z !8XL !&B",
                 UserName, PersonaHandle, HttpdNetworkMode);

   if (!UserName)
   {
      /* if we haven't actually been able to assume a persona */
      if (!PersonaHandle) return (SS$_NORMAL);

      /* resume being the original server persona */
      PersonaHandle = ISS$C_ID_NATURAL;
   }
   else
   if (!(PersonaHandle = PersonaCache (UserName, 0)))
   {
      UserNameDsc.dsc$a_pointer = UserName;
      UserNameDsc.dsc$w_length = strlen(UserName);

      if (HttpdNetworkMode)
      {
         /* fudge the persona create */
         PersonaHandle = PERSONA_NETWORK_MODE;
         status = SS$_NORMAL;
      }
      else
      {
         sys$setprv (1, &SysPrvMask, 0, 0);
         status = sys$persona_create (&PersonaHandle, &UserNameDsc,
                                      PersonaCreateFlags, 0, 0);
         sys$setprv (0, &SysPrvMask, 0, 0);
         if (WATCH_MODULE(WATCH_MOD_DCL))
            WatchThis (WATCHALL, WATCH_MOD_DCL,
                       "sys$persona_create() !&S", status);
      }

      if (VMSnok (status))
      {
         PersonaHandle = 0;
         return (status);
      }

      /* update the persona cache */
      PersonaCache (UserName, PersonaHandle);
   }

   if (HttpdNetworkMode)
   {
      /* fudge the persona assume */
      status = SS$_NORMAL;
   }
   else
   {
      if (SysInfo.VersionInteger >= 720)
         status = sys$persona_assume (&PersonaHandle, 0, 0, 0);
      else
         status = sys$persona_assume (&PersonaHandle, PersonaAssumeFlags);

      if (WATCH_MODULE(WATCH_MOD_DCL))
         WatchThis (WATCHALL, WATCH_MOD_DCL,
                    "sys$persona_assume() !&S", status);
   }

   if (VMSnok (status))
      PersonaHandle = 0;
   else
   if (PersonaHandle == ISS$C_ID_NATURAL)
      PersonaHandle = 0;

   return (status);
} 

/*****************************************************************************/
/*
For $PERSONA service.
Keep a linked-list of cache entries.  If 'UserName' is NULL then the list is
reset (this happen on authorization rule reload and DCL script purge).  If
'UserName' is  non-NULL and 'PersonaHandle' zero the list is searched for a
matching username and any associated persona returned.  If 'UserName' is
non-NULL and 'PersonaHandle' non-zero 'PersonaHandle' add/update a cache entry. 
If the list has reached maximum size reuse the last entry, otherwise create a
new entry.  Move/add the entry to the head of the list.  It's therefore a
first-in/first-out queue.  Cache contents remain current until demands on space
(due to new entries) cycles through the maximum available entries.  To
explicitly flush the contents reload the authorization rules. 
*/ 

int PersonaCache
(
char *UserName,
int PersonaHandle
)
{
   char  *cptr, *sptr, *zptr;
   LIST_ENTRY  *leptr;
   PERSONA_ENTRY  *pcptr;

   /*********/
   /* begin */
   /*********/

   if (WATCH_MODULE(WATCH_MOD_DCL))
      WatchThis (WATCHALL, WATCH_MOD_DCL,
                 "PersonaCache() !&Z !UL !&B",
                 UserName, PersonaHandle, HttpdNetworkMode);

   if (!(UserName && PersonaCacheEntries))
   {
      /***************/
      /* reset cache */
      /***************/

      PersonaCacheEntries = Config.cfMisc.PersonaCacheEntries;
      if (!PersonaCacheEntries)
         PersonaCacheEntries = PERSONA_DEFAULT_CACHE_SIZE;
      PersonaCacheCount = 0;

      /* empty the list */
      leptr = PersonaCacheList.HeadPtr;
      PersonaCacheList.HeadPtr = PersonaCacheList.TailPtr = NULL;
      PersonaCacheList.EntryCount = 0;
      while (leptr)
      {
         pcptr = (PERSONA_ENTRY*)leptr;
         leptr = leptr->NextPtr;
         VmFree (pcptr, FI_LI);
      }

      if (!UserName) return (0);
   }

   if (!PersonaHandle)
   {
      /****************/
      /* search cache */
      /****************/

      /* process the cache entry list from most to least recent */
      for (leptr = PersonaCacheList.HeadPtr;
           leptr;
           leptr = leptr->NextPtr)
      {
         pcptr = (PERSONA_ENTRY*)leptr;

         /* if this one has been reset there won't be any more down the list */
         if (!pcptr->UserName[0]) break;

         /* string comparison */
         cptr = UserName;
         sptr = pcptr->UserName;
         while (*cptr && *sptr && TOUP(*cptr) == TOUP(*sptr))
         {
            cptr++;
            sptr++;
         }
         if (*cptr || *sptr) continue;

         /*************/
         /* cache hit */
         /*************/

         if ((void*)PersonaCacheList.HeadPtr != (void*)pcptr)
         {
            /* move it to the head of the list */
            ListRemove (&PersonaCacheList, pcptr);
            ListAddHead (&PersonaCacheList, pcptr, LIST_ENTRY_TYPE_PERSONA);
         }

         pcptr->HitCount++;
         sys$gettim (&pcptr->LastTime64);

         return (pcptr->PersonaHandle);
      }

      /* not found */
      return (0);
   }

   /****************/
   /* update cache */
   /****************/

   if (PersonaCacheCount < PersonaCacheEntries)
   {
      /* allocate memory for a new entry */
      pcptr = (PERSONA_ENTRY*) VmGet (sizeof (PERSONA_ENTRY));
      PersonaCacheCount++;
   }
   else
   {
      /* reuse the tail entry (least recent) */
      pcptr = PersonaCacheList.TailPtr;
      pcptr->ReuseCount++;
      ListRemove (&PersonaCacheList, pcptr);
      if (!HttpdNetworkMode)
      {
         /* delete the previous persona (uses dynamic storage) */
         if (pcptr->PersonaHandle) sys$persona_delete (&pcptr->PersonaHandle);
      }
   }

   /* add entry to the head of the user cache list (most recent) */
   ListAddHead (&PersonaCacheList, pcptr, LIST_ENTRY_TYPE_PERSONA);

   zptr = (sptr = pcptr->UserName) + sizeof(pcptr->UserName)-1;
   for (cptr = UserName; *cptr && sptr < zptr; *sptr++ = *cptr++);
   *sptr = '\0';
   pcptr->PersonaHandle = PersonaHandle;
   pcptr->HitCount = 1;
   sys$gettim (&pcptr->LastTime64);

   return (PersonaHandle);
}

/*****************************************************************************/

#endif /* ifndef PERSONA_MACRO */

/****************************************************************************/
/*
For $PERSONA service.
Get the current persona's authorized privileges, UIC and process rights list. 
Make appropriate checks against it's UIC group membership and authorized
privileges.  Check for any required identifiers.  If OK to be used for
scripting return success, if not found return failure status. 
Can return other VMS error status.
*/ 

int PersonaAllowed
(
REQUEST_STRUCT *rqptr,
char *UserName
)
{
   static unsigned short  RetLength;
   static unsigned long  JpiUic;
   static unsigned long  JpiAuthPriv [2];

   static struct
   {
      unsigned short  buf_len;
      unsigned short  item;
      unsigned char   *buf_addr;
      unsigned short  *short_ret_len;
   }
   JpiItems [] =
   {
      { 0, JPI$_PROCESS_RIGHTS, 0, &RetLength },
      { sizeof(JpiAuthPriv), JPI$_AUTHPRIV, &JpiAuthPriv, 0 },
      { sizeof(JpiUic), JPI$_UIC, &JpiUic, 0 },
      {0,0,0,0}
   };

   int  idx, status;
   IO_SB  IOsb;
   struct
   {
      unsigned long  identifier;
      unsigned long  attributes;
   } JpiProcessRights [PERSONA_RIGHTS_MAX+1];

   /*********/
   /* begin */
   /*********/

   if (WATCH_MODULE(WATCH_MOD_DCL))
      WatchThis (WATCHALL, WATCH_MOD_DCL,
                 "PersonaAllowed() !&Z !&B",
                 UserName, HttpdNetworkMode);

   if (CliPersonaRelaxed) return (SS$_NORMAL);

   if (CliPersonaAuthorized && !rqptr->RemoteUser[0])
   {
      /* the request must have been subject to authorization to use persona */
      if (WATCHING (rqptr, WATCH_REQUEST))
         WatchThis (WATCHITM(rqptr), WATCH_REQUEST,
                    "FAIL authorization mandatory");
      return (SS$_INVUSER); 
   }

   if (HttpdNetworkMode || PersonaMacro)
   {
      /* using the SYSUAF to establish this */
      status = PersonaAllowedUai (rqptr, UserName);
      return (status);
   }

   /* use the final, additional element as a zeroed sentinal */
   JpiProcessRights[PERSONA_RIGHTS_MAX].identifier = 0;
   JpiItems[0].buf_len = sizeof(JpiProcessRights) - sizeof(JpiProcessRights[0]);
   JpiItems[0].buf_addr = &JpiProcessRights;

   status = sys$getjpiw (EfnWait, 0, 0, &JpiItems, &IOsb, 0, 0);
   if (VMSok (status)) status = IOsb.Status;
   if (VMSnok (status))
   {
      ErrorNoticed (rqptr, status, NULL, FI_LI);
      return (status);
   }

   if (WATCH_MODULE(WATCH_MOD_DCL))
      WatchThis (WATCHALL, WATCH_MOD_DCL,
"sys$getjpi() !&S uic:!8XL priv:<63-32>!8XL<31-00>!8XL id:!UL/!UL",
                 status, JpiUic, JpiAuthPriv[1], JpiAuthPriv[0],
                 RetLength, RetLength/8);

   if ((JpiUic & 0xffff0000) >> 16 <= SysInfo.MaxSysGroup ||
       JpiAuthPriv[0] & ~AveJoePrvMask[0] ||
       JpiAuthPriv[1] & ~AveJoePrvMask[1])
   {
      /* system group or something other than NETMBX and TMPMBX authorized */
      if (!CliPersonaRelaxed)
      {
         /* not a relaxed privileged environment */
         if (WATCHING (rqptr, WATCH_REQUEST))
            WatchThis (WATCHITM(rqptr), WATCH_REQUEST,
"FAIL uic:!8XL privileges <63-32>!8XL<31-00>!8XL",
                       JpiUic, JpiAuthPriv[1], JpiAuthPriv[0]);
         return (SS$_INVUSER); 
      }
      /* it is a relaxed privileged environment */
      if (CliPersonaRelaxedAuthorized && !rqptr->RemoteUser[0])
      {
         /* but must and has not been authorized */
         if (WATCHING (rqptr, WATCH_REQUEST))
            WatchThis (WATCHITM(rqptr), WATCH_REQUEST,
"FAIL uic:!8XL privileges <63-32>!8XL<31-00>!8XL authorization mandatory",
                       JpiUic, JpiAuthPriv[1], JpiAuthPriv[0]);
         return (SS$_INVUSER); 
      }
      /* bogus success message indicates a privileged account */
      status = SS$_CREATED;
   }
   else
      status = SS$_NORMAL;

   if (!PersonaRightsIdent) return (status);

   /* does the account possess the required identifier? */
   for (idx = 0; JpiProcessRights[idx].identifier; idx++)
      if (JpiProcessRights[idx].identifier == PersonaRightsIdent)
         return (status);

   if (WATCHING (rqptr, WATCH_REQUEST))
      WatchThis (WATCHITM(rqptr), WATCH_REQUEST,
                 "FAIL identifier !AZ", PersonaIdentName);
   return (SS$_INVUSER);
}

/****************************************************************************/
/*
Get the username's UAI authorized privileges and UIC and assess that.
Return success if allowed to be used for scripting, a failure status if not.
Used for both PERSONA_MACRO and network mode.
*/ 

int PersonaAllowedUai
(
REQUEST_STRUCT *rqptr,
char *UserName
)
{
   static unsigned long  Context = -1;
   static unsigned long  UaiUic;
   static unsigned long  UaiPriv [2];
   static char  GetUserName [AUTH_MAX_USERNAME_LENGTH+1];
   static $DESCRIPTOR (UserNameDsc, GetUserName);

   static struct
   {
      unsigned short  buf_len;
      unsigned short  item;
      unsigned char   *buf_addr;
      unsigned short  *short_ret_len;
   }
   UaiItems [] =
   {
      { sizeof(UaiPriv), UAI$_PRIV, &UaiPriv, 0 },
      { sizeof(UaiUic), UAI$_UIC, &UaiUic, 0 },
      {0,0,0,0}
   };

   int  status;
   char  *cptr, *sptr, *zptr;
   unsigned long  FindHeldCtx,
                  FindHeldIdent;
   unsigned long  FindHeldUic [2];

   /*********/
   /* begin */
   /*********/

   if (WATCH_MODULE(WATCH_MOD_DCL))
      WatchThis (WATCHALL, WATCH_MOD_DCL,
                 "PersonaAllowedUai() !&Z", UserName);

   if (CliPersonaRelaxed) return (SS$_NORMAL);

   if (CliPersonaAuthorized && !rqptr->RemoteUser[0])
   {
      /* the request must have been subject to authorization to use persona */
      if (WATCHING (rqptr, WATCH_REQUEST))
         WatchThis (WATCHITM(rqptr), WATCH_REQUEST,
                    "FAIL authorization mandatory");
      return (SS$_INVUSER); 
   }

   /* to uppercase! */
   zptr = (sptr = GetUserName) + sizeof(GetUserName)-1;
   for (cptr = UserName; *cptr && sptr < zptr; *sptr++ = TOUP(*cptr++));
   *sptr = '\0';
   UserNameDsc.dsc$w_length = sptr - GetUserName;

   /* turn on SYSPRV to allow access to SYSUAF records */
   sys$setprv (1, &SysPrvMask, 0, 0);
   status = sys$getuai (0, &Context, &UserNameDsc, &UaiItems, 0, 0, 0);
   sys$setprv (0, &SysPrvMask, 0, 0);

   if (WATCH_MODULE(WATCH_MOD_DCL))
      WatchThis (WATCHALL, WATCH_MOD_DCL,
                 "sys$getuai() !&S uic:!8XL priv:<63-32>!8XL<31-00>!8XL",
                 status, UaiUic, UaiPriv[1], UaiPriv[0]);

   if (VMSnok (status))
   {
      ErrorNoticed (rqptr, status, "sys$getuai()", FI_LI);
      return (status);
   }

   if ((UaiUic & 0xffff0000) >> 16 <= SysInfo.MaxSysGroup ||
       UaiPriv[0] & ~AveJoePrvMask[0] ||
       UaiPriv[1] & ~AveJoePrvMask[1])
   {
      /* system group or something other than NETMBX and TMPMBX authorized */
      if (!CliPersonaRelaxed)
      {
         /* not a relaxed privileged environment */
         if (WATCHING (rqptr, WATCH_REQUEST))
            WatchThis (WATCHITM(rqptr), WATCH_REQUEST,
"FAIL uic:!8XL privileges <63-32>!8XL<31-00>!8XL",
                       UaiUic, UaiPriv[1], UaiPriv[0]);
         return (SS$_INVUSER); 
      }
      /* it is a relaxed privileged environment */
      if (CliPersonaRelaxedAuthorized && !rqptr->RemoteUser[0])
      {
         /* but must and has not been authorized */
         if (WATCHING (rqptr, WATCH_REQUEST))
            WatchThis (WATCHITM(rqptr), WATCH_REQUEST,
"FAIL uic:!8XL privileges <63-32>!8XL<31-00>!8XL authorization mandatory",
               UaiUic, UaiPriv[1], UaiPriv[0]);
         return (SS$_INVUSER); 
      }
      /* bogus success message indicates a privileged account */
      status = SS$_CREATED;
   }
   else
      status = SS$_NORMAL;

   if (!PersonaRightsIdent) return (status);

   /* does the account possess the required identifier? */
   FindHeldCtx = FindHeldUic[1] = 0;
   FindHeldUic[0] = UaiUic;
   for (;;)
   {
      status = sys$find_held (&FindHeldUic, &FindHeldIdent, 0, &FindHeldCtx);
      if (WATCH_MODULE(WATCH_MOD_DCL))
         WatchThis (WATCHALL, WATCH_MOD_DCL,
                    "sys$find_held() !&S !&X !&X",
                    status, FindHeldIdent, PersonaRightsIdent);
      if (VMSnok(status)) break;
      if (FindHeldIdent == PersonaRightsIdent)
      {
         sys$finish_rdb (&FindHeldCtx);
         return (SS$_NORMAL);
      }
   }

   if (WATCHING (rqptr, WATCH_REQUEST))
      WatchThis (WATCHITM(rqptr), WATCH_REQUEST,
                 "FAIL identifier !AZ", PersonaIdentName);
   return (SS$_INVUSER);
}

/*****************************************************************************/

#ifdef PERSONA_MACRO 

   /*****************/
   /* PERSONA_MACRO */
   /*****************/

/****************************************************************************/
/*
For PERSONA_MACRO kludge.
Get the current account username for resuming that identity (using $GETJPIW as
this routine can be used before the server proper is actually initialized).
*/ 

int PersonaInit ()

{
   static $DESCRIPTOR (PersonaIdentNameDsc, PersonaIdentName);

   static int  Pid = 0;
   static struct
   {
      unsigned short  buf_len;
      unsigned short  item;
      unsigned char  *buf_addr;
      void  *ret_len;
   }
   JpiItems [] =
   {
     { sizeof(PersonaServerUserName12), JPI$_USERNAME,
       &PersonaServerUserName12, 0 },
     { 0,0,0,0 }
   };

   int  status;
   IO_SB  IOsb;

   /*********/
   /* begin */
   /*********/

   if (WATCH_MODULE(WATCH_MOD_DCL))
      WatchThis (WATCHALL, WATCH_MOD_DCL,
                 "PersonaInit() PERSONA_MACRO\n");

   /* ensure that CMKRNL privilege is available */
   status = sys$setprv (1, &CmKrnlMask, 0, 0);
   sys$setprv (0, &CmKrnlMask, 0, 0);
   if (status == SS$_NOTALLPRIV) status = SS$_NOCMKRNL;
   if (VMSnok (status))
   {
      FaoToStdout ("%HTTPD-W-PERSONA, services MACRO\n-!&M\n", status);
      CliPersonaEnabled = CliPersonaRelaxed = false;
      return (status);
   }

   /* used for resuming the original (server account) username */
   status = sys$getjpiw (EfnWait, &Pid, 0, &JpiItems, &IOsb, 0, 0);
   if (VMSok (status)) status = IOsb.Status;
   if (VMSnok (status)) return (status);

   if (CliPersonaRelaxed) FaoToStdout ("%HTTPD-I-PERSONA, relaxed\n");

   /* set a flag indicating that this kludge is in use */
   PersonaMacro = true;

   return (SS$_NORMAL);
}

/*****************************************************************************/
/*
For PERSONA_MACRO kludge.
Change the JIB username to that supplied.  If 'UserName' is NULL then return to
the server username of the process.  Returns a VMS status value.
*/

int PersonaAssume (char *UserName)

{
   static $DESCRIPTOR (UserNameDsc, "");

   int  cnt, status;
   char  *cptr, *sptr;

   /*********/
   /* begin */
   /*********/

   if (WATCH_MODULE(WATCH_MOD_DCL))
      WatchThis (WATCHALL, WATCH_MOD_DCL,
                 "PersonaAssume() PERSONA_MACRO !&Z", UserName);

   status = sys$setprv (1, &CmKrnlMask, 0, 0);
   if (VMSnok (status)) return (status);

   if (!UserName)
   {
      status = MACRO_SET_USERNAME (PersonaServerUserName12);
      memset (PersonaUserName12, 0, sizeof(PersonaUserName12));
      PersonaUserName12Length = 0;
   }
   else
   {
      /* perform a basic check of the username string being passed */
      status = SS$_NORMAL;
      cptr = UserName;
      sptr = PersonaUserName12;
      for (cnt = 12; cnt; cnt--)
      {
         if (*cptr)
         {
            if (!isalnum(*cptr) && *cptr != '_' && *cptr != '$')
            {
               status = SS$_BADPARAM;
               break;
            }
            *sptr++ = TOUP(*cptr++);
         }
         else
         {
            if (!PersonaUserName12Length)
               PersonaUserName12Length = sptr - PersonaUserName12;
            *sptr++ = ' ';
         }
      }
      if (PersonaUserName12[0] == ' ' || *cptr) status = SS$_BADPARAM;
      if (!PersonaUserName12Length) PersonaUserName12Length = 12;

      if (VMSok (status)) status = MACRO_SET_USERNAME (PersonaUserName12);
   }

   sys$setprv (0, &CmKrnlMask, 0, 0);

   return (status);
} 

/*****************************************************************************/
/*
For PERSONA_MACRO kludge.
Just a stub for calling from AUTHCONFIG.C and DCL.C!
*/ 

int PersonaCache
(
char *UserName,
int PersonaHandle
)
{
   /*********/
   /* begin */
   /*********/

   if (WATCH_MODULE(WATCH_MOD_DCL))
      WatchThis (WATCHALL, WATCH_MOD_DCL,
                 "PersonaCache() PERSONA_MACRO !&Z !UL",
                 UserName, PersonaHandle);

   return (0);
}

/*****************************************************************************/

#endif /* PERSONA_MACRO  */

   /****************/
   /* PERSONA_STUB */
   /****************/

/*****************************************************************************/
/*
See note in module macro section.
*/

int PersonaStub (...)

{
   if (WATCH_MODULE(WATCH_MOD_DCL))
      WatchThis (WATCHALL, WATCH_MOD_DCL, "PersonaStub()");

   ErrorNoticed (NULL, 0, "feature not supported on this platform", FI_LI);

   return (SS$_ABORT);
}

/*****************************************************************************/