Back to index

lightdm  1.3.2
x-authority.c
Go to the documentation of this file.
00001 #include <string.h>
00002 
00003 #include "x-authority.h"
00004 #include "x-common.h"
00005 
00006 struct XAuthorityPrivate
00007 {
00008     GList *records;
00009 };
00010 
00011 struct XAuthorityRecordPrivate
00012 {
00013     guint16 family;
00014     guint16 address_length;
00015     guint8 *address;
00016     gchar *number;
00017     gchar *authorization_name;
00018     guint16 authorization_data_length;
00019     guint8 *authorization_data;
00020 };
00021 
00022 G_DEFINE_TYPE (XAuthority, x_authority, G_TYPE_OBJECT);
00023 G_DEFINE_TYPE (XAuthorityRecord, x_authority_record, G_TYPE_OBJECT);
00024 
00025 XAuthority *
00026 x_authority_new (void)
00027 {
00028     return g_object_new (x_authority_get_type (), NULL);
00029 }
00030 
00031 gboolean
00032 x_authority_load (XAuthority *authority, const gchar *filename, GError **error)
00033 {
00034     guint8 *xauth_data;
00035     gsize xauth_length;
00036     gsize offset = 0;
00037 
00038     if (!g_file_get_contents (filename, (gchar **) &xauth_data, &xauth_length, error))
00039         return FALSE;
00040 
00041     while (offset < xauth_length)
00042     {
00043         XAuthorityRecord *record;
00044         guint16 length;
00045 
00046         record = g_object_new (x_authority_record_get_type (), NULL);
00047         record->priv->family = read_card16 (xauth_data, xauth_length, X_BYTE_ORDER_MSB, &offset);
00048         record->priv->address_length = read_card16 (xauth_data, xauth_length, X_BYTE_ORDER_MSB, &offset);
00049         record->priv->address = read_string8 (xauth_data, xauth_length, record->priv->address_length, &offset);
00050         length = read_card16 (xauth_data, xauth_length, X_BYTE_ORDER_MSB, &offset);
00051         record->priv->number = (gchar *) read_string8 (xauth_data, xauth_length, length, &offset);
00052         length = read_card16 (xauth_data, xauth_length, X_BYTE_ORDER_MSB, &offset);
00053         record->priv->authorization_name = (gchar *) read_string8 (xauth_data, xauth_length, length, &offset);
00054         record->priv->authorization_data_length = read_card16 (xauth_data, xauth_length, X_BYTE_ORDER_MSB, &offset);
00055         record->priv->authorization_data = read_string8 (xauth_data, xauth_length, record->priv->authorization_data_length, &offset);
00056 
00057         authority->priv->records = g_list_append (authority->priv->records, record);
00058     }
00059 
00060     return TRUE;
00061 }
00062 
00063 XAuthorityRecord *
00064 x_authority_match_local (XAuthority *authority, const gchar *authorization_name)
00065 {
00066     GList *link;
00067 
00068     for (link = authority->priv->records; link; link = link->next)
00069     {
00070         XAuthorityRecord *record = link->data;
00071       
00072         if (strcmp (record->priv->authorization_name, authorization_name) != 0)
00073             continue;
00074       
00075         if (record->priv->family == XAUTH_FAMILY_WILD || record->priv->family == XAUTH_FAMILY_LOCAL)
00076             return record;
00077     }
00078 
00079     return NULL;
00080 }
00081 
00082 XAuthorityRecord *
00083 x_authority_match_localhost (XAuthority *authority, const gchar *authorization_name)
00084 {
00085     GList *link;
00086 
00087     for (link = authority->priv->records; link; link = link->next)
00088     {
00089         XAuthorityRecord *record = link->data;
00090       
00091         if (strcmp (record->priv->authorization_name, authorization_name) != 0)
00092             continue;
00093       
00094         if (record->priv->family == XAUTH_FAMILY_WILD || record->priv->family == XAUTH_FAMILY_LOCALHOST)
00095             return record;
00096     }
00097 
00098     return NULL;
00099 }
00100 
00101 XAuthorityRecord *
00102 x_authority_match_inet (XAuthority *authority, GInetAddress *address, const gchar *authorization_name)
00103 {
00104     GList *link;
00105     guint16 family;
00106     gssize address_data_length;
00107     const guint8 *address_data;
00108 
00109     switch (g_inet_address_get_family (address))
00110     {
00111     case G_SOCKET_FAMILY_IPV4:
00112         family = XAUTH_FAMILY_INTERNET;
00113         break;
00114     case G_SOCKET_FAMILY_IPV6:
00115         family = XAUTH_FAMILY_INTERNET6;
00116         break;
00117     default:
00118         return NULL;
00119     }
00120 
00121     address_data_length = g_inet_address_get_native_size (address);
00122     address_data = g_inet_address_to_bytes (address);
00123     for (link = authority->priv->records; link; link = link->next)
00124     {
00125         XAuthorityRecord *record = link->data;
00126         int i;
00127         gboolean matches = TRUE;
00128 
00129         if (strcmp (record->priv->authorization_name, authorization_name) != 0)
00130             continue;
00131 
00132         if (record->priv->family == XAUTH_FAMILY_WILD)
00133             return record;
00134 
00135         if (record->priv->family != family)
00136             continue;
00137 
00138         if (record->priv->address_length != address_data_length)
00139             continue;
00140 
00141         for (i = 0; i < address_data_length; i++)
00142         {
00143             if (address_data[i] != record->priv->address[i])
00144             {
00145                 matches = FALSE;
00146                 break;
00147             }
00148         }
00149         if (matches)
00150             return record;
00151     }
00152 
00153     return NULL;
00154 }
00155 
00156 static void
00157 x_authority_init (XAuthority *authority)
00158 {
00159     authority->priv = G_TYPE_INSTANCE_GET_PRIVATE (authority, x_authority_get_type (), XAuthorityPrivate);
00160 }
00161 
00162 static void
00163 x_authority_finalize (GObject *object)
00164 {
00165     XAuthority *authority = (XAuthority *) object;
00166     g_list_free_full (authority->priv->records, g_object_unref);
00167 }
00168 
00169 static void
00170 x_authority_class_init (XAuthorityClass *klass)
00171 {
00172     GObjectClass *object_class = G_OBJECT_CLASS (klass);
00173     object_class->finalize = x_authority_finalize;
00174     g_type_class_add_private (klass, sizeof (XAuthorityPrivate));
00175 }
00176 
00177 guint16
00178 x_authority_record_get_authorization_data_length (XAuthorityRecord *record)
00179 {
00180     return record->priv->authorization_data_length;
00181 }
00182 
00183 const guint8 *
00184 x_authority_record_get_authorization_data (XAuthorityRecord *record)
00185 {
00186     return record->priv->authorization_data;
00187 }
00188 
00189 gboolean
00190 x_authority_record_check_cookie (XAuthorityRecord *record, const guint8 *cookie_data, guint16 cookie_data_length)
00191 {
00192     guint16 i;
00193 
00194     if (strcmp (record->priv->authorization_name, "MIT-MAGIC-COOKIE-1") != 0)
00195         return FALSE;
00196   
00197     if (cookie_data_length != record->priv->authorization_data_length)
00198         return FALSE;
00199 
00200     for (i = 0; i < cookie_data_length; i++)
00201         if (cookie_data[i] != record->priv->authorization_data[i])
00202             return FALSE;
00203 
00204     return TRUE;
00205 }
00206 
00207 static void
00208 x_authority_record_init (XAuthorityRecord *record)
00209 {
00210     record->priv = G_TYPE_INSTANCE_GET_PRIVATE (record, x_authority_record_get_type (), XAuthorityRecordPrivate);
00211 }
00212 
00213 static void
00214 x_authority_record_finalize (GObject *object)
00215 {
00216     XAuthorityRecord *record = (XAuthorityRecord *) object;
00217     g_free (record->priv->address);
00218     g_free (record->priv->number);
00219     g_free (record->priv->authorization_name);
00220     g_free (record->priv->authorization_data);
00221 }
00222 
00223 static void
00224 x_authority_record_class_init (XAuthorityRecordClass *klass)
00225 {
00226     GObjectClass *object_class = G_OBJECT_CLASS (klass);
00227     object_class->finalize = x_authority_record_finalize;
00228     g_type_class_add_private (klass, sizeof (XAuthorityRecordPrivate));
00229 }