sbIPDFairPlay.cpp
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set sw=2 :miv */
3 /*
4 //=BEGIN SONGBIRD GPL
5 //
6 // This file is part of the Songbird web player.
7 //
8 // Copyright(c) 2005-2011 POTI, Inc.
9 // http://www.songbirdnest.com
10 //
11 // This file may be licensed under the terms of of the
12 // GNU General Public License Version 2 (the GPL).
13 //
14 // Software distributed under the License is distributed
15 // on an AS IS basis, WITHOUT WARRANTY OF ANY KIND, either
16 // express or implied. See the GPL for the specific language
17 // governing rights and limitations.
18 //
19 // You should have received a copy of the GPL along with this
20 // program. If not, go to http://www.gnu.org/licenses/gpl.html
21 // or write to the Free Software Foundation, Inc.,
22 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 //
24 //=END SONGBIRD GPL
25 */
26 
27 //------------------------------------------------------------------------------
28 //
29 // iPod device FairPlay services.
30 //
31 // These services may only be used within both the connect and request locks.
32 //
33 //------------------------------------------------------------------------------
34 
40 //------------------------------------------------------------------------------
41 //
42 // iPod device FairPlay imported services.
43 //
44 //------------------------------------------------------------------------------
45 
46 // Local imports.
47 #include "QTAtomReader.h"
48 #include "sbIPDDevice.h"
49 #include "sbIIPDDeviceEvent.h"
50 #include "sbIPDLog.h"
51 
52 // Songbird imports.
53 #include <sbDeviceUtils.h>
54 #include <sbStringUtils.h>
55 
56 // Mozilla imports.
57 #include <nsComponentManagerUtils.h>
58 #include <nsILocalFile.h>
59 
60 
61 //------------------------------------------------------------------------------
62 //
63 // iPod device FairPlay services.
64 //
65 //------------------------------------------------------------------------------
66 
75 nsresult
76 sbIPDDevice::FPConnect()
77 {
78  nsresult rv;
79 
80  // Operate under the request lock.
81  nsAutoMonitor autoRequestLock(mDBLock);
82 
83  // Get the FairPlay key information.
84  rv = FPGetKeyInfo();
85  NS_ENSURE_SUCCESS(rv, rv);
86 
87  return NS_OK;
88 }
89 
90 
99 void
100 sbIPDDevice::FPDisconnect()
101 {
102  // Operate under the request lock.
103  nsAutoMonitor autoRequestLock(mDBLock);
104 
105  // Get the list of authorized FairPlay user IDs.
106  mFPUserIDList.Clear();
107 }
108 
109 
118 nsresult
119 sbIPDDevice::FPCheckTrackAuth(Itdb_Track* aTrack)
120 {
121  // Validate parameters.
122  NS_ASSERTION(aTrack, "aTrack is null");
123 
124  // Function variables.
125  nsresult rv;
126 
127  // Do nothing if not a FairPlay track.
128  if (!aTrack->unk148)
129  return NS_OK;
130 
131  // Get the FairPlay user ID.
132  PRUint32 userID = aTrack->drm_userid;
133 
134  // Check if the device is authorized for the user ID.
135  PRBool authorized = PR_FALSE;
136  for (PRUint32 i = 0; i < mFPUserIDList.Length(); i++) {
137  if (mFPUserIDList[i] == userID) {
138  authorized = PR_TRUE;
139  break;
140  }
141  }
142 
143  // If not authorized, send an event.
144  if (!authorized) {
145  // Get the authorization info.
146  PRUint32 userID;
147  nsAutoString accountName;
148  nsAutoString userName;
149  rv = FPGetTrackAuthInfo(aTrack, &userID, accountName, userName);
150  NS_ENSURE_SUCCESS(rv, rv);
151 
152  // Get the Songbird main library media item for the track. Ignore errors.
153  nsCOMPtr<sbIMediaItem> mediaItem;
155  (mDeviceLibrary,
156  sbAutoString(aTrack->dbid),
157  getter_AddRefs(mediaItem));
158  NS_ENSURE_SUCCESS(rv, rv);
159 
160  // Log event.
161  FIELD_LOG(("Track not authorized 0x%08x %s %s\n",
162  userID,
163  NS_ConvertUTF16toUTF8(accountName).get(),
164  NS_ConvertUTF16toUTF8(userName).get()));
165 
166  // Create and dispatch an event.
167  CreateAndDispatchFairPlayEvent
169  userID,
170  accountName,
171  userName,
172  mediaItem);
173  }
174 
175  return NS_OK;
176 }
177 
178 
188 nsresult
189 sbIPDDevice::FPSetupTrackInfo(nsIFile* aTrackFile,
190  Itdb_Track* aTrack)
191 {
192  // Validate parameters.
193  NS_ASSERTION(aTrackFile, "aTrackFile is null");
194  NS_ASSERTION(aTrack, "aTrack is null");
195 
196  // Function variables.
197  nsresult rv;
198 
199  // Get the track file name extension.
200  nsAutoString fileName;
201  nsAutoString fileExt;
202  rv = aTrackFile->GetLeafName(fileName);
203  NS_ENSURE_SUCCESS(rv, rv);
204  fileExt.Assign(StringTail(fileName, 3));
205 
206  // Check for protected extensions. Do nothing if not protected.
207  if (!fileExt.Equals(NS_LITERAL_STRING("m4p")))
208  return NS_OK;
209 
210  // Set up an atom reader and set up for auto-disposal.
211  QTAtomReader* atomReader = new QTAtomReader();
212  NS_ENSURE_TRUE(atomReader, NS_ERROR_OUT_OF_MEMORY);
213  sbAutoQTAtomReader autoAtomReader(atomReader);
214  rv = atomReader->Open(aTrackFile);
215  NS_ENSURE_SUCCESS(rv, rv);
216 
217  // Get the FairPlay user ID.
218  PRUint32 userID;
219  rv = atomReader->GetFairPlayUserID(&userID);
220  NS_ENSURE_SUCCESS(rv, rv);
221 
222  // Set the track FairPlay fields.
223  aTrack->drm_userid = userID;
224  aTrack->unk148 = 0x01010100;
225  aTrack->filetype = g_strdup("Protected AAC audio file");
226 
227  return NS_OK;
228 }
229 
230 
231 //------------------------------------------------------------------------------
232 //
233 // Internal iPod device FairPlay services.
234 //
235 //------------------------------------------------------------------------------
236 
247 nsresult
248 sbIPDDevice::FPGetKeyInfo()
249 {
250  nsresult rv;
251 
252  // Get the iPod key info file.
253  nsCOMPtr<nsILocalFile>
254  iEKInfoFile = do_CreateInstance("@mozilla.org/file/local;1", &rv);
255  NS_ENSURE_SUCCESS(rv, rv);
256  rv = iEKInfoFile->InitWithPath(mMountPath);
257  NS_ENSURE_SUCCESS(rv, rv);
258  rv = iEKInfoFile->Append(NS_LITERAL_STRING("iPod_Control"));
259  NS_ENSURE_SUCCESS(rv, rv);
260  rv = iEKInfoFile->Append(NS_LITERAL_STRING("iTunes"));
261  NS_ENSURE_SUCCESS(rv, rv);
262  rv = iEKInfoFile->Append(NS_LITERAL_STRING("iEKInfo"));
263  NS_ENSURE_SUCCESS(rv, rv);
264 
265  // Check for existence of the iPod key info file. Do nothing more if it
266  // doesn't exist.
267  PRBool exists;
268  rv = iEKInfoFile->Exists(&exists);
269  NS_ENSURE_SUCCESS(rv, rv);
270  if (!exists)
271  return NS_OK;
272 
273  // Set up an atom reader and set up for auto-disposal.
274  QTAtomReader* atomReader = new QTAtomReader();
275  NS_ENSURE_TRUE(atomReader, NS_ERROR_OUT_OF_MEMORY);
276  sbAutoQTAtomReader autoAtomReader(atomReader);
277  rv = atomReader->Open(iEKInfoFile);
278  NS_ENSURE_SUCCESS(rv, rv);
279 
280  // Get the list of authorized FairPlay user IDs.
281  rv = atomReader->GetIEKInfoUserIDs(mFPUserIDList);
282  NS_ENSURE_SUCCESS(rv, rv);
283 
284  return NS_OK;
285 }
286 
287 
298 nsresult
299 sbIPDDevice::FPGetTrackAuthInfo(Itdb_Track* aTrack,
300  PRUint32* aUserID,
301  nsAString& aAccountName,
302  nsAString& aUserName)
303 {
304  // Validate arguments.
305  NS_ASSERTION(aTrack, "aTrack is null");
306  NS_ASSERTION(aUserID, "aUserID is null");
307 
308  // Function variables.
309  nsresult rv;
310 
311  // Get the track file path and set up for auto-disposal.
312  gchar* trackFilePath = itdb_filename_on_ipod(aTrack);
313  NS_ENSURE_TRUE(trackFilePath, NS_ERROR_OUT_OF_MEMORY);
314  sbAutoGMemPtr autoTrackFilePath(trackFilePath);
315 
316  // Get the track file.
317  nsCOMPtr<nsILocalFile>
318  trackFile = do_CreateInstance("@mozilla.org/file/local;1", &rv);
319  NS_ENSURE_SUCCESS(rv, rv);
320  rv = trackFile->InitWithPath(NS_ConvertUTF8toUTF16(trackFilePath));
321  NS_ENSURE_SUCCESS(rv, rv);
322 
323  // Open an atom reader and set up for auto-disposal.
324  QTAtomReader* atomReader = new QTAtomReader();
325  NS_ENSURE_TRUE(atomReader, NS_ERROR_OUT_OF_MEMORY);
326  sbAutoQTAtomReader autoAtomReader(atomReader);
327  rv = atomReader->Open(trackFile);
328  NS_ENSURE_SUCCESS(rv, rv);
329 
330  // Read the track authorization info. Ignore errors getting the account and
331  // user names.
332  rv = atomReader->GetFairPlayUserID(aUserID);
333  NS_ENSURE_SUCCESS(rv, rv);
334  atomReader->GetFairPlayAccountName(aAccountName);
335  atomReader->GetFairPlayUserName(aUserName);
336 
337  return NS_OK;
338 }
339 
static nsresult GetOriginMediaItemByDevicePersistentId(sbILibrary *aLibrary, const nsAString &aDevicePersistentId, sbIMediaItem **aItem)
return NS_OK
const unsigned long EVENT_IPOD_FAIRPLAY_NOT_AUTHORIZED
nsresult GetFairPlayAccountName(nsAString &aAccountName)
nsresult GetFairPlayUserName(nsAString &aUserName)
nsresult Open(nsIFile *aFile)
Songbird iPod Device Definitions.
QuickTime Atom Reader Definitions.
#define FIELD_LOG(args)
Definition: sbIPDLog.h:126
nsresult GetIEKInfoUserIDs(nsTArray< PRUint32 > &aUserIDList)
Songbird iPod Device Logging Definitions.
_getSelectedPageStyle s i
nsresult GetFairPlayUserID(PRUint32 *aUserID)