summaryrefslogtreecommitdiffstats
path: root/private/nw/nwscript/maplist.c
blob: 5dbe8a13223a380e01904a2080bb1620568141c7 (plain) (blame)
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

/*************************************************************************
*
*  QATTACH.C
*
*  Do any neccessary attach user queries
*
*  Copyright (c) 1995 Microsoft Corporation
*
*  $Log:   N:\NT\PRIVATE\NW4\NWSCRIPT\VCS\MAPLIST.C  $
*  
*     Rev 1.1   10 Apr 1996 14:22:42   terryt
*  Hotfix for 21181hq
*  
*     Rev 1.1   12 Mar 1996 19:53:58   terryt
*  Relative NDS names and merge
*  
*     Rev 1.0   22 Jan 1996 16:49:24   terryt
*  Initial revision.
*  
*************************************************************************/
#include "common.h"

//
//  4X login script behavior for map commands to servers not in the
//  logged in NDS tree that have not been ATTACH'ed is different from
//  the 3X behavior.
//
//  The 4X behavior is to always ask for a user name and password for
//  these servers, doing the attach at that point.  The user gets
//  two trys.
//
//  Since NT doesn't have an list of attached servers, and will try
//  to connect to a volume with the default user name and password,
//  a wrapper must be put around the MAP commands.  This code
//  must determine that a bindery connection will be made and that
//  this server has not previously been MAP'ed or ATTACH'ed.
//  The user will be always prompted for user name and password.
//  The server will then be logged into with those credentials.
//
//  One problem with the below is that it's not easy to tell that
//  a connection "will be" made with the bindery, this is done in
//  the redirector.  So to simplify things the assumption is that
//  only 3X servers use bindery connections.  This means that 
//  4X servers using bindery emulation on a different NDS tree will
//  not always be asked for the user name and password.
//
//  Already processed servers are kept in a list and marked as 4X or 3X
//  for possible future use.
//
//  The behavior for a 3X login depends on the LOGIN.EXE version.
//  The old behavior is that you must always ATTACH before mapping.
//  However, if you login to a 3X server with a 4X version LOGIN.EXE
//  it will try to authenticate using your user name (and password)
//  on the first attempt and ask for a password if that fails.  The
//  second attempt will ask for your user name.  Since this 4X behavior
//  is more forgiving (more scripts "work") that is the one being
//  emulated.
//

typedef struct _SERVERLIST
{
   char * ServerName;
   unsigned int ServerType;
   struct _SERVERLIST *pNextServer;
} SERVERLIST, *PSERVERLIST;

PSERVERLIST pMainList = NULL;

BOOL IsServerInAttachList( char *, unsigned int );
void AddServerToAttachList( char *, unsigned int );
int DoAttachProcessing( char * );

/*
 * Scan the list for the server
 */
BOOL
IsServerInAttachList( char * Server, unsigned int ServerType )
{
   PSERVERLIST pServerList = pMainList;

   while ( pServerList != NULL )
   {
       if ( !_strcmpi( Server, pServerList->ServerName ) &&
            ( ServerType & pServerList->ServerType ) )
           return TRUE;
        pServerList = pServerList->pNextServer;
   }
   
   return FALSE;
}

/*
 * Add the server to the list of attached servers
 *
 * This is used during MAP's and ATTACH's
 */
void
AddServerToAttachList( char * Server, unsigned int ServerType )
{
    PSERVERLIST pServerList;

    pServerList = (PSERVERLIST) malloc( sizeof( SERVERLIST ) );

    if ( pServerList == NULL )
    {
        DisplayMessage( IDR_NOT_ENOUGH_MEMORY );
        return;
    }

    pServerList->ServerName = _strdup( Server );
    pServerList->ServerType = ServerType;
    pServerList->pNextServer = pMainList;
    pMainList = pServerList;
}

/*
 *  Do any Attach processing
 *  Return error code. 0 is success.
 *  880F is the special "attached failed" error
 */

int
DoAttachProcessing( char * PossibleServer )
{
    unsigned int  iRet = 0;
    unsigned int  conn;
    char userName[MAX_NAME_LEN] = "";
    char password[MAX_PASSWORD_LEN] = "";
    BOOL AlreadyConnected = FALSE;

    //
    // Must have a server to process
    //
    if ( !*PossibleServer )
       return iRet;

    // See if this server has been processed before
    // No since in doing a 4X server twice, and you only ask
    // for the user name and password once.

    if ( IsServerInAttachList( PossibleServer,
             LIST_4X_SERVER | LIST_3X_SERVER ) )
        return iRet;

    // See if there is already a connection to the server

    if ( NTIsConnected( PossibleServer ) )
       AlreadyConnected = TRUE;
    else
       AlreadyConnected = FALSE;

    // Try and attach to the server

    iRet = NTAttachToFileServer( PossibleServer, &conn );

    // If attach failed, return

    if ( iRet ) 
       return iRet;

    // If this is a 4X server then add it to the list of attached
    // servers.  We don't want to do this again.  4X servers must 
    // use the NDS attachment anyway (or at least I don't see a 
    // way of telling that it's going to be a bindery emulation
    // connection ahead of time).

    if ( fNDS && Is40Server( conn ) )
    {
        AddServerToAttachList( PossibleServer, LIST_4X_SERVER );
        DetachFromFileServer ( conn );
        return iRet;
    }

    // Close that first connection

    DetachFromFileServer ( conn );

    // If we are already connected, don't mess with things
    // The credentials can't be changed anyway

    if ( AlreadyConnected )
    {
        AddServerToAttachList( PossibleServer, LIST_3X_SERVER );
        return iRet;
    }

    // Ask for user name on an NDS login
    //
    // Use the current login name for a 3X login on the first attempt

    if ( fNDS )
    {
        DisplayMessage(IDR_ENTER_LOGIN_NAME, PossibleServer);
        if (!ReadName(userName))
            return 0x880F;
    }
    else
    {
        strncpy( userName, LOGIN_NAME, sizeof( userName ) );
    }

    // Try to log the user in, asking for a password 

    iRet = Login( userName,
                  PossibleServer,
                  password,
                  TRUE );

    // Clear out the password
    // We don't need it again

    memset( password, 0, sizeof( password ) );

    // If failed, give the user one more chance

    if ( iRet )
    {
        // Ask for user name

        DisplayMessage(IDR_ENTER_LOGIN_NAME, PossibleServer);
        if (!ReadName(userName))
            return 0x880F;

        // Try to log the user in 

        iRet = Login( userName,
                      PossibleServer,
                      password,
                      TRUE );

        // Clear out the password

        memset( password, 0, sizeof( password ) );

    }
    
    // Add servername to list of attached servers, marked as 3X

    if ( !iRet )
    {
        AddServerToAttachList( PossibleServer, LIST_3X_SERVER );
    }
    else
    {  
        iRet = 0x880F;   // Special I am not attached error
    }

    return iRet;

}