rpcrt4: Store the assoc_group_id in the connections.
[wine] / dlls / setupapi / setupx_main.c
1 /*
2  *      SETUPX library
3  *
4  *      Copyright 1998,2000  Andreas Mohr
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  *
20  * FIXME: Rather non-functional functions for now.
21  *
22  * See:
23  * http://www.geocities.com/SiliconValley/Network/5317/drivers.html
24  * http://willemer.de/informatik/windows/inf_info.htm (German)
25  * http://www.microsoft.com/ddk/ddkdocs/win98ddk/devinst_12uw.htm
26  * DDK: setupx.h
27  * http://mmatrix.tripod.com/customsystemfolder/infsysntaxfull.html
28  * http://www.rdrop.com/~cary/html/inf_faq.html
29  * http://support.microsoft.com/support/kb/articles/q194/6/40.asp
30  *
31  * Stuff tested with:
32  * - rs405deu.exe (German Acroread 4.05 setup)
33  * - ie5setup.exe
34  * - Netmeeting
35  *
36  * FIXME:
37  * - string handling is... weird ;) (buflen etc.)
38  * - memory leaks ?
39  * - separate that mess (but probably only when it's done completely)
40  *
41  * SETUPX consists of several parts with the following acronyms/prefixes:
42  * Di   device installer (devinst.c ?)
43  * Gen  generic installer (geninst.c ?)
44  * Ip   .INF parsing (infparse.c)
45  * LDD  logical device descriptor (ldd.c ?)
46  * LDID logical device ID
47  * SU   setup (setup.c ?)
48  * Tp   text processing (textproc.c ?)
49  * Vcp  virtual copy module (vcp.c ?)
50  * ...
51  *
52  * The SETUPX DLL is NOT thread-safe. That's why many installers urge you to
53  * "close all open applications".
54  * All in all the design of it seems to be a bit weak.
55  * Not sure whether my implementation of it is better, though ;-)
56  */
57
58 #include <stdlib.h>
59 #include <stdarg.h>
60 #include <stdio.h>
61 #include <string.h>
62 #include "windef.h"
63 #include "winbase.h"
64 #include "winreg.h"
65 #include "winerror.h"
66 #include "wine/winuser16.h"
67 #include "wownt32.h"
68 #include "winuser.h"
69 #include "winnls.h"
70 #include "setupapi.h"
71 #include "setupx16.h"
72 #include "setupapi_private.h"
73 #include "winerror.h"
74 #include "wine/debug.h"
75
76 WINE_DEFAULT_DEBUG_CHANNEL(setupapi);
77
78 #define HINSTANCE_32(h16) ((HINSTANCE)(ULONG_PTR)(h16))
79
80 /***********************************************************************
81  *              SURegOpenKey (SETUPX.47)
82  */
83 DWORD WINAPI SURegOpenKey( HKEY hkey, LPCSTR lpszSubKey, PHKEY retkey )
84 {
85     FIXME("(%p,%s,%p), semi-stub.\n",hkey,debugstr_a(lpszSubKey),retkey);
86     return RegOpenKeyA( hkey, lpszSubKey, retkey );
87 }
88
89 /***********************************************************************
90  *              SURegQueryValueEx (SETUPX.50)
91  */
92 DWORD WINAPI SURegQueryValueEx( HKEY hkey, LPSTR lpszValueName,
93                                 LPDWORD lpdwReserved, LPDWORD lpdwType,
94                                 LPBYTE lpbData, LPDWORD lpcbData )
95 {
96     FIXME("(%p,%s,%p,%p,%p,%d), semi-stub.\n",hkey,debugstr_a(lpszValueName),
97           lpdwReserved,lpdwType,lpbData,lpcbData?*lpcbData:0);
98     return RegQueryValueExA( hkey, lpszValueName, lpdwReserved, lpdwType,
99                                lpbData, lpcbData );
100 }
101
102
103 /***********************************************************************
104  *              InstallHinfSection (SETUPX.527)
105  *
106  * hwnd = parent window
107  * hinst = instance of SETUPX.DLL
108  * lpszCmdLine = e.g. "DefaultInstall 132 C:\MYINSTALL\MYDEV.INF"
109  * Here "DefaultInstall" is the .inf file section to be installed (optional).
110  * The 132 value is made of the HOW_xxx flags and sometimes 128 (-> setupx16.h).
111  *
112  * nCmdShow = nCmdShow of CreateProcess
113  */
114 RETERR16 WINAPI InstallHinfSection16( HWND16 hwnd, HINSTANCE16 hinst, LPCSTR lpszCmdLine, INT16 nCmdShow)
115 {
116     InstallHinfSectionA( HWND_32(hwnd), HINSTANCE_32(hinst), lpszCmdLine, nCmdShow );
117     return OK;
118 }
119
120 typedef struct
121 {
122     LPCSTR RegValName;
123     LPCSTR StdString; /* fallback string; sub dir of windows directory */
124 } LDID_DATA;
125
126 static const LDID_DATA LDID_Data[34] =
127 {
128     { /* 0 (LDID_NULL) -- not defined */
129         NULL,
130         NULL
131     },
132     { /* 1 (LDID_SRCPATH) = source of installation. hmm, what to do here ? */
133         "SourcePath", /* hmm, does SETUPX have to care about updating it ?? */
134         NULL
135     },
136     { /* 2 (LDID_SETUPTEMP) = setup temp dir */
137         "SetupTempDir",
138         NULL
139     },
140     { /* 3 (LDID_UNINSTALL) = uninstall backup dir */
141         "UninstallDir",
142         NULL
143     },
144     { /* 4 (LDID_BACKUP) = backup dir */
145         "BackupDir",
146         NULL
147     },
148     { /* 5 (LDID_SETUPSCRATCH) = setup scratch dir */
149         "SetupScratchDir",
150         NULL
151     },
152     { /* 6 -- not defined */
153         NULL,
154         NULL
155     },
156     { /* 7 -- not defined */
157         NULL,
158         NULL
159     },
160     { /* 8 -- not defined */
161         NULL,
162         NULL
163     },
164     { /* 9 -- not defined */
165         NULL,
166         NULL
167     },
168     { /* 10 (LDID_WIN) = windows dir */
169         "WinDir",
170         ""
171     },
172     { /* 11 (LDID_SYS) = system dir */
173         "SysDir",
174         NULL /* call GetSystemDirectory() instead */
175     },
176     { /* 12 (LDID_IOS) = IOSubSys dir */
177         NULL, /* FIXME: registry string ? */
178         "SYSTEM\\IOSUBSYS"
179     },
180     { /* 13 (LDID_CMD) = COMMAND dir */
181         NULL, /* FIXME: registry string ? */
182         "COMMAND"
183     },
184     { /* 14 (LDID_CPL) = control panel dir */
185         NULL,
186         ""
187     },
188     { /* 15 (LDID_PRINT) = windows printer dir */
189         NULL,
190         "SYSTEM" /* correct ?? */
191     },
192     { /* 16 (LDID_MAIL) = destination mail dir */
193         NULL,
194         ""
195     },
196     { /* 17 (LDID_INF) = INF dir */
197         "SetupScratchDir", /* correct ? */
198         "INF"
199     },
200     { /* 18 (LDID_HELP) = HELP dir */
201         NULL, /* ??? */
202         "HELP"
203     },
204     { /* 19 (LDID_WINADMIN) = Admin dir */
205         "WinAdminDir",
206         ""
207     },
208     { /* 20 (LDID_FONTS) = Fonts dir */
209         NULL, /* ??? */
210         "FONTS"
211     },
212     { /* 21 (LDID_VIEWERS) = Viewers */
213         NULL, /* ??? */
214         "SYSTEM\\VIEWERS"
215     },
216     { /* 22 (LDID_VMM32) = VMM32 dir */
217         NULL, /* ??? */
218         "SYSTEM\\VMM32"
219     },
220     { /* 23 (LDID_COLOR) = ICM dir */
221         "ICMPath",
222         "SYSTEM\\COLOR"
223     },
224     { /* 24 (LDID_APPS) = root of boot drive ? */
225         "AppsDir",
226         "C:\\"
227     },
228     { /* 25 (LDID_SHARED) = shared dir */
229         "SharedDir",
230         ""
231     },
232     { /* 26 (LDID_WINBOOT) = Windows boot dir */
233         "WinBootDir",
234         ""
235     },
236     { /* 27 (LDID_MACHINE) = machine specific files */
237         "MachineDir",
238         NULL
239     },
240     { /* 28 (LDID_HOST_WINBOOT) = Host Windows boot dir */
241         "HostWinBootDir",
242         NULL
243     },
244     { /* 29 -- not defined */
245         NULL,
246         NULL
247     },
248     { /* 30 (LDID_BOOT) = Root of boot drive */
249         "BootDir",
250         NULL
251     },
252     { /* 31 (LDID_BOOT_HOST) = Root of boot drive host */
253         "BootHost",
254         NULL
255     },
256     { /* 32 (LDID_OLD_WINBOOT) = subdir of root */
257         "OldWinBootDir",
258         NULL
259     },
260     { /* 33 (LDID_OLD_WIN) = old win dir */
261         "OldWinDir",
262         NULL
263     }
264     /* the rest (34-38) isn't too interesting, so I'll forget about it */
265 };
266
267 /*
268  * LDD  == Logical Device Descriptor
269  * LDID == Logical Device ID
270  *
271  * The whole LDD/LDID business might go into a separate file named
272  * ldd.c.
273  * At the moment I don't know what the hell these functions are really doing.
274  * That's why I added reporting stubs.
275  * The only thing I do know is that I need them for the LDD/LDID infrastructure.
276  * That's why I implemented them in a way that's suitable for my purpose.
277  */
278 static LDD_LIST *pFirstLDD = NULL;
279
280 static BOOL std_LDDs_done = FALSE;
281
282 static void SETUPX_CreateStandardLDDs(void)
283 {
284     HKEY hKey = 0;
285     WORD n;
286     DWORD type, len;
287     LOGDISKDESC_S ldd;
288     char buffer[MAX_PATH];
289
290     /* has to be here, otherwise loop */
291     std_LDDs_done = TRUE;
292
293     RegOpenKeyA(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Setup", &hKey);
294
295     for (n=0; n < sizeof(LDID_Data)/sizeof(LDID_DATA); n++)
296     {
297         buffer[0] = '\0';
298
299         len = MAX_PATH;
300         if ( (hKey) && (LDID_Data[n].RegValName)
301         &&   (RegQueryValueExA(hKey, LDID_Data[n].RegValName,
302                 NULL, &type, (LPBYTE)buffer, &len) == ERROR_SUCCESS)
303         &&   (type == REG_SZ) )
304         {
305             TRACE("found value '%s' for LDID %d\n", buffer, n);
306         }
307         else
308         switch(n)
309         {
310             case LDID_SRCPATH:
311                 FIXME("LDID_SRCPATH: what exactly do we have to do here ?\n");
312                 strcpy(buffer, "X:\\FIXME");
313                 break;
314             case LDID_SYS:
315                 GetSystemDirectoryA(buffer, MAX_PATH);
316                 break;
317             case LDID_APPS:
318             case LDID_MACHINE:
319             case LDID_HOST_WINBOOT:
320             case LDID_BOOT:
321             case LDID_BOOT_HOST:
322                 strcpy(buffer, "C:\\");
323                 break;
324             default:
325                 if (LDID_Data[n].StdString)
326                 {
327                     DWORD len = GetWindowsDirectoryA(buffer, MAX_PATH);
328                     LPSTR p;
329                     p = buffer + len;
330                     *p++ = '\\';
331                     strcpy(p, LDID_Data[n].StdString);
332                 }
333                 break;
334         }
335         if (buffer[0])
336         {
337             INIT_LDD(ldd, n);
338             ldd.pszPath = buffer;
339             TRACE("LDID %d -> '%s'\n", ldd.ldid, ldd.pszPath);
340             CtlSetLdd16(&ldd);
341         }
342     }
343     if (hKey) RegCloseKey(hKey);
344 }
345
346 /***********************************************************************
347  * CtlDelLdd            (SETUPX.37)
348  *
349  * RETURN
350  *   ERR_VCP_LDDINVALID if ldid < LDID_ASSIGN_START.
351  */
352 static RETERR16 SETUPX_DelLdd(LOGDISKID16 ldid)
353 {
354     LDD_LIST *pCurr, *pPrev = NULL;
355
356     TRACE("(%d)\n", ldid);
357
358     if (!std_LDDs_done)
359         SETUPX_CreateStandardLDDs();
360
361     if (ldid < LDID_ASSIGN_START)
362         return ERR_VCP_LDDINVALID;
363
364     pCurr = pFirstLDD;
365     /* search until we find the appropriate LDD or hit the end */
366     while ((pCurr != NULL) && (ldid > pCurr->pldd->ldid))
367     {
368          pPrev = pCurr;
369          pCurr = pCurr->next;
370     }
371     if ( (pCurr == NULL) /* hit end of list */
372       || (ldid != pCurr->pldd->ldid) )
373         return ERR_VCP_LDDFIND; /* correct ? */
374
375     /* ok, found our victim: eliminate it */
376
377     if (pPrev)
378         pPrev->next = pCurr->next;
379
380     if (pCurr == pFirstLDD)
381         pFirstLDD = NULL;
382     HeapFree(GetProcessHeap(), 0, pCurr);
383
384     return OK;
385 }
386
387 /***********************************************************************
388  *              CtlDelLdd (SETUPX.37)
389  */
390 RETERR16 WINAPI CtlDelLdd16(LOGDISKID16 ldid)
391 {
392     FIXME("(%d); - please report this!\n", ldid);
393     return SETUPX_DelLdd(ldid);
394 }
395
396 /***********************************************************************
397  * CtlFindLdd           (SETUPX.35)
398  *
399  * doesn't check pldd ptr validity: crash (W98SE)
400  *
401  * RETURN
402  *   ERR_VCP_LDDINVALID if pldd->cbSize != structsize
403  *   1 in all other cases ??
404  *
405  */
406 RETERR16 WINAPI CtlFindLdd16(LPLOGDISKDESC pldd)
407 {
408     LDD_LIST *pCurr, *pPrev = NULL;
409
410     TRACE("(%p)\n", pldd);
411
412     if (!std_LDDs_done)
413         SETUPX_CreateStandardLDDs();
414
415     if (pldd->cbSize != sizeof(LOGDISKDESC_S))
416         return ERR_VCP_LDDINVALID;
417
418     pCurr = pFirstLDD;
419     /* search until we find the appropriate LDD or hit the end */
420     while ((pCurr != NULL) && (pldd->ldid > pCurr->pldd->ldid))
421     {
422         pPrev = pCurr;
423         pCurr = pCurr->next;
424     }
425     if ( (pCurr == NULL) /* hit end of list */
426       || (pldd->ldid != pCurr->pldd->ldid) )
427         return ERR_VCP_LDDFIND; /* correct ? */
428
429     memcpy(pldd, pCurr->pldd, pldd->cbSize);
430     /* hmm, we probably ought to strcpy() the string ptrs here */
431
432     return 1; /* what is this ?? */
433 }
434
435 /***********************************************************************
436  * CtlSetLdd                    (SETUPX.33)
437  *
438  * Set an LDD entry.
439  *
440  * RETURN
441  *   ERR_VCP_LDDINVALID if pldd.cbSize != sizeof(LOGDISKDESC_S)
442  *
443  */
444 RETERR16 WINAPI CtlSetLdd16(LPLOGDISKDESC pldd)
445 {
446     LDD_LIST *pCurr, *pPrev = NULL;
447     LPLOGDISKDESC pCurrLDD;
448     HANDLE heap;
449     BOOL is_new = FALSE;
450
451     TRACE("(%p)\n", pldd);
452
453     if (!std_LDDs_done)
454         SETUPX_CreateStandardLDDs();
455
456     if (pldd->cbSize != sizeof(LOGDISKDESC_S))
457         return ERR_VCP_LDDINVALID;
458
459     heap = GetProcessHeap();
460     pCurr = pFirstLDD;
461     /* search until we find the appropriate LDD or hit the end */
462     while ((pCurr != NULL) && (pldd->ldid > pCurr->pldd->ldid))
463     {
464          pPrev = pCurr;
465          pCurr = pCurr->next;
466     }
467     if (!pCurr || pldd->ldid != pCurr->pldd->ldid)
468     {
469         is_new = TRUE;
470         pCurr = HeapAlloc(heap, 0, sizeof(LDD_LIST));
471         pCurr->pldd = HeapAlloc(heap, 0, sizeof(LOGDISKDESC_S));
472         pCurr->next = NULL;
473         pCurrLDD = pCurr->pldd;
474     }
475     else
476     {
477         pCurrLDD = pCurr->pldd;
478         HeapFree(heap, 0, pCurrLDD->pszPath);
479         HeapFree(heap, 0, pCurrLDD->pszVolLabel);
480         HeapFree(heap, 0, pCurrLDD->pszDiskName);
481     }
482
483     memcpy(pCurrLDD, pldd, sizeof(LOGDISKDESC_S));
484
485     if (pldd->pszPath)
486     {
487         pCurrLDD->pszPath = HeapAlloc( heap, 0, strlen(pldd->pszPath)+1 );
488         strcpy( pCurrLDD->pszPath, pldd->pszPath );
489     }
490     if (pldd->pszVolLabel)
491     {
492         pCurrLDD->pszVolLabel = HeapAlloc( heap, 0, strlen(pldd->pszVolLabel)+1 );
493         strcpy( pCurrLDD->pszVolLabel, pldd->pszVolLabel );
494     }
495     if (pldd->pszDiskName)
496     {
497         pCurrLDD->pszDiskName = HeapAlloc( heap, 0, strlen(pldd->pszDiskName)+1 );
498         strcpy( pCurrLDD->pszDiskName, pldd->pszDiskName );
499     }
500
501     if (is_new) /* link into list */
502     {
503         if (pPrev)
504         {
505             pCurr->next = pPrev->next;
506             pPrev->next = pCurr;
507         }
508         if (!pFirstLDD)
509             pFirstLDD = pCurr;
510     }
511
512     return OK;
513 }
514
515
516 /***********************************************************************
517  * CtlAddLdd            (SETUPX.36)
518  *
519  * doesn't check pldd ptr validity: crash (W98SE)
520  *
521  */
522 static LOGDISKID16 ldid_to_add = LDID_ASSIGN_START;
523 RETERR16 WINAPI CtlAddLdd16(LPLOGDISKDESC pldd)
524 {
525     pldd->ldid = ldid_to_add++;
526     return CtlSetLdd16(pldd);
527 }
528
529 /***********************************************************************
530  * CtlGetLdd            (SETUPX.34)
531  *
532  * doesn't check pldd ptr validity: crash (W98SE)
533  * What the !@#$%&*( is the difference between CtlFindLdd() and CtlGetLdd() ??
534  *
535  * RETURN
536  *   ERR_VCP_LDDINVALID if pldd->cbSize != structsize
537  *
538  */
539 static RETERR16 SETUPX_GetLdd(LPLOGDISKDESC pldd)
540 {
541     LDD_LIST *pCurr, *pPrev = NULL;
542
543     if (!std_LDDs_done)
544         SETUPX_CreateStandardLDDs();
545
546     if (pldd->cbSize != sizeof(LOGDISKDESC_S))
547         return ERR_VCP_LDDINVALID;
548
549     pCurr = pFirstLDD;
550     /* search until we find the appropriate LDD or hit the end */
551     while ((pCurr != NULL) && (pldd->ldid > pCurr->pldd->ldid))
552     {
553          pPrev = pCurr;
554          pCurr = pCurr->next;
555     }
556     if (pCurr == NULL) /* hit end of list */
557         return ERR_VCP_LDDFIND; /* correct ? */
558
559     memcpy(pldd, pCurr->pldd, pldd->cbSize);
560     /* hmm, we probably ought to strcpy() the string ptrs here */
561
562     return OK;
563 }
564
565 /**********************************************************************/
566
567 RETERR16 WINAPI CtlGetLdd16(LPLOGDISKDESC pldd)
568 {
569     FIXME("(%p); - please report this!\n", pldd);
570     return SETUPX_GetLdd(pldd);
571 }
572
573 /***********************************************************************
574  *              CtlGetLddPath           (SETUPX.38)
575  *
576  * Gets the path of an LDD.
577  * No crash if szPath == NULL.
578  * szPath has to be at least MAX_PATH_LEN bytes long.
579  * RETURN
580  *   ERR_VCP_LDDUNINIT if LDD for LDID not found.
581  */
582 RETERR16 WINAPI CtlGetLddPath16(LOGDISKID16 ldid, LPSTR szPath)
583 {
584     TRACE("(%d, %p);\n", ldid, szPath);
585
586     if (szPath)
587     {
588         LOGDISKDESC_S ldd;
589         INIT_LDD(ldd, ldid);
590         if (CtlFindLdd16(&ldd) == ERR_VCP_LDDFIND)
591             return ERR_VCP_LDDUNINIT;
592         SETUPX_GetLdd(&ldd);
593         strcpy(szPath, ldd.pszPath);
594         TRACE("ret '%s' for LDID %d\n", szPath, ldid);
595     }
596     return OK;
597 }
598
599 /***********************************************************************
600  *              CtlSetLddPath           (SETUPX.508)
601  *
602  * Sets the path of an LDD.
603  * Creates LDD for LDID if not existing yet.
604  */
605 RETERR16 WINAPI CtlSetLddPath16(LOGDISKID16 ldid, LPSTR szPath)
606 {
607     LOGDISKDESC_S ldd;
608     TRACE("(%d, '%s');\n", ldid, szPath);
609
610     SetupSetDirectoryIdA( 0, ldid, szPath );
611     INIT_LDD(ldd, ldid);
612     ldd.pszPath = szPath;
613     return CtlSetLdd16(&ldd);
614 }