in plugin/:
[moon.git] / src / security.c
blobaa3651a2d3fd3a1942de9a1b9c99820c62d7b8f5
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3 * security.c:
5 * Contact:
6 * Moonlight List (moonlight-list@lists.ximian.com)
8 * Copyright 2009 Novell, Inc. (http://www.novell.com)
10 * See the LICENSE file included with the distribution for details.
13 #include "config.h"
14 #include "security.h"
16 #if MONO_ENABLE_CORECLR_SECURITY
18 static struct stat platform_stat;
20 #if MOON_A11Y_INTERNAL_HACK_ENABLED
21 static struct stat platform_a11y_stat;
23 void
24 a11y_stat_init (char *platform_dir)
26 //please keep this lookup pattern in sync with the one in A11yHelper.cs (Initiailize() method)
27 const char* moonlight_at_novell = g_strrstr (platform_dir, "moonlight@novell.com");
28 if (moonlight_at_novell != NULL) {
29 const char* after = g_strdup ("moonlight-a11y@novell.com/components");
30 const char* before = g_strndup (platform_dir,
31 strlen (platform_dir) - strlen (moonlight_at_novell));
32 const char* platform_a11y_dir = g_strconcat (before, after, NULL);
34 memset (&platform_a11y_stat, 0, sizeof (platform_a11y_stat));
35 stat (platform_a11y_dir, &platform_a11y_stat);
36 g_free (platform_a11y_dir);
37 g_free (before);
38 g_free (after);
39 moonlight_at_novell = NULL;
43 #endif
45 const static char* platform_code_assemblies [] = {
46 "mscorlib.dll",
47 "System.dll",
48 "System.Core.dll",
49 "System.Net.dll",
50 "System.Runtime.Serialization.dll",
51 "System.ServiceModel.dll",
52 "System.ServiceModel.Web.dll",
53 "System.Windows.dll",
54 "System.Windows.Browser.dll",
55 "System.Xml.dll",
56 "Microsoft.VisualBasic.dll",
57 #if DEBUG
58 "jtr.dll",
59 #endif
62 static gboolean
63 determine_platform_image (const char *image_name)
65 struct stat info;
66 gchar *dir, *name;
67 unsigned int i;
68 struct stat the_platform_stat = platform_stat;
69 gboolean a11y = FALSE;
71 if (!image_name)
72 return FALSE;
74 /* all platform code resides in the same directory */
75 dir = g_path_get_dirname (image_name);
76 if (!dir || stat (dir, &info) != 0) {
77 g_free (dir);
78 return FALSE;
81 name = g_path_get_basename (image_name);
82 if (!name) {
83 g_free (dir);
84 return FALSE;
87 #if MOON_A11Y_INTERNAL_HACK_ENABLED
88 if (g_ascii_strcasecmp (name, "MoonAtkBridge.dll") == 0) {
89 the_platform_stat = platform_a11y_stat;
90 a11y = TRUE;
92 #endif
94 /* we avoid comparing strings, e.g. /opt/mono/lib/moon versus /opt/mono//lib/moon */
95 if ((the_platform_stat.st_mode != info.st_mode) ||
96 (the_platform_stat.st_ino != info.st_ino) ||
97 (the_platform_stat.st_dev != info.st_dev)) {
98 g_free (dir);
99 g_free (name);
100 return FALSE;
102 g_free (dir);
104 if (a11y == TRUE){
105 g_free (name);
106 return TRUE;
109 /* we know the names of every platform assembly, because we ship them */
110 for (i = 0; i < G_N_ELEMENTS (platform_code_assemblies); i++) {
111 if (g_ascii_strcasecmp (name, platform_code_assemblies [i]) == 0) {
112 g_free (name);
113 return TRUE;
116 g_free (name);
117 return FALSE;
120 #define DISABLE_SECURITY "MOON_DISABLE_SECURITY_PREVIEW_" PREVIEW_VERSION
122 void
123 security_enable_coreclr (const char *platform_dir)
125 if (g_getenv (DISABLE_SECURITY) != NULL) {
126 g_warning ("CORECLR was DISABLED using %s override", DISABLE_SECURITY);
127 g_warning ("this disables both code verification and metadata verification on code\n"
128 "downloaded from untrusted sources, and therefore opens up your machine\n"
129 "to a wide variety of attack vectors. Don't do this unless you know what\n"
130 "you're doing!");
131 } else if (g_path_is_absolute (platform_dir)) {
132 memset (&platform_stat, 0, sizeof (platform_stat));
134 if (stat (platform_dir, &platform_stat) == 0) {
136 #if MOON_A11Y_INTERNAL_HACK_ENABLED
137 a11y_stat_init (platform_dir);
138 #endif
140 mono_security_enable_core_clr ();
141 mono_security_set_core_clr_platform_callback (determine_platform_image);
143 } else {
144 g_warning ("CORECLR was DISABLED due to invalid, non-absolute, platform directory");
147 mono_assembly_setrootdir (platform_dir);
150 #else
152 void
153 security_enable_coreclr (const char *platform_dir)
157 #endif