comparison libmpdemux/dvdauth.c @ 3975:04b2227ab75a

Return of the 'Old-style-DVD-support', with dynamic loading (using libdl) so no more conflict with libdvdread: you can compile in both of libdvdread and libcss support! You can even select libcss.so to load from command line or configuration file, with '-csslib /usr/local/lib/libcss.so' or something similar. Default for this option is /usr/local/lib/libcss.so. Note: libcss version (ver>0.1) with newer API is not supported in this version! This is the first version so stay tuned :)
author lgb
date Fri, 04 Jan 2002 13:08:14 +0000
parents 91f801a94a59
children d197584460dc
comparison
equal deleted inserted replaced
3974:90e7917f945f 3975:04b2227ab75a
1 /* (C)2001 by LGB (Gabor Lenart), based on example programs in libcss 1 /* (C)2001,2002 by LGB (Gabor Lenart), based on example programs in libcss
2 lgb@lgb.hu */ 2 lgb@lgb.hu
3
4 This source is part of MPlayer project. This source is copyrighted by
5 the author according to rules declared in GNU/GPL license.
6
7 2001 Inital version (LGB)
8 2001 fibmap_mplayer to avoid uid=0 mplayer need (LGB)
9 2001 Support for libcss with the new API (by ???)
10 2002/Jan/04 Use dlopen to access libcss.so to avoid conflict with
11 libdvdread [now with only libcss with old API (LGB)
12
13 TODO:
14 support for libcss libraries with new API */
3 15
4 /* don't do anything with this source if css support was not requested */ 16 /* don't do anything with this source if css support was not requested */
5 #include "config.h" 17 #include "config.h"
6 #ifdef HAVE_LIBCSS 18 #ifdef HAVE_LIBCSS
19
20 #warning FIXME: Dynamic loading of libcss.so with newer (ver>0.1) libcss API is not supported in this version!
7 21
8 #include <stdio.h> 22 #include <stdio.h>
9 #include <stdlib.h> 23 #include <stdlib.h>
10 //#include <string.h> // FIXME: conflicts with fs.h 24 //#include <string.h> // FIXME: conflicts with fs.h
11 #include <errno.h> 25 #include <errno.h>
13 #include <fcntl.h> 27 #include <fcntl.h>
14 #include <sys/types.h> 28 #include <sys/types.h>
15 #include <sys/ioctl.h> 29 #include <sys/ioctl.h>
16 #include <sys/stat.h> 30 #include <sys/stat.h>
17 #include <sys/wait.h> 31 #include <sys/wait.h>
18 #include <css.h> 32 // #include <css.h>
19 #if CSS_MAJOR_VERSION > 0 || (CSS_MAJOR_VERSION == 0 && CSS_MINOR_VERSION > 1) 33
20 # include <dvd.h> 34 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
21 # undef OLD_CSS_API 35 # include <sys/dvdio.h>
36 #elif defined(__linux__)
37 # include <linux/cdrom.h>
38 #elif defined(__sun)
39 # include <sun/dvdio.h>
22 #else 40 #else
23 # if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
24 # include <sys/dvdio.h>
25 # elif defined(__linux__)
26 # include <linux/cdrom.h>
27 # elif defined(__sun)
28 # include <sun/dvdio.h>
29 # else
30 # error "Need the DVD ioctls" 41 # error "Need the DVD ioctls"
31 # endif
32 # define OLD_CSS_API 1
33 #endif 42 #endif
34 43
44 #include <dlfcn.h>
35 #include "dvdauth.h" 45 #include "dvdauth.h"
36 46
37 47
38 #if OLD_CSS_API 48 // #if OLD_CSS_API
39 /* 49 /*
40 * provide some backward compatibiliy macros to compile this 50 * provide some backward compatibiliy macros to compile this
41 * code using the old libcss-0.1 51 * code using the old libcss-0.1
42 */ 52 */
43 #define DVDHandle int 53 #define DVDHandle int
44 #define DVDOpenFailed (-1) 54 #define DVDOpenFailed (-1)
45 55
46 #define DVDAuth(hdl, s) ioctl(hdl, DVD_AUTH, s) 56 #define DVDAuth(hdl, s) ioctl(hdl, DVD_AUTH, s)
47 #define DVDOpenDevice(path) open(path, O_RDONLY) 57 #define DVDOpenDevice(path) open(path, O_RDONLY)
48 #define DVDCloseDevice(hdl) close(hdl) 58 #define DVDCloseDevice(hdl) close(hdl)
49 #define CSSDVDisEncrypted(hdl) CSSisEncrypted(hdl) 59 // #define CSSDVDisEncrypted(hdl) CSSisEncrypted(hdl)
50 #define CSSDVDAuthDisc CSSAuthDisc 60 // #define CSSDVDAuthDisc CSSAuthDisc
51 /* Arghhh! Please think before you commit! You forget to check the return
52 value of path_to_lba (-1 for error) in this way ... - LGB */
53 //#define CSSDVDAuthTitlePath(hdl,key_title,path) \
54 // CSSAuthTitle(hdl,key_title,path_to_lba(path))
55
56 #else /*OLD_CSS_API*/
57
58 #define DVDHandle struct dvd_device *
59 #define DVDOpenFailed NULL
60
61 #endif /*OLD_CSS_API*/
62 61
63 62
64 char *dvd_auth_device=NULL; 63 char *dvd_auth_device=NULL;
65 char *dvd_device=NULL; 64 char *dvd_device=NULL;
66 char *dvd_raw_device=NULL; 65 char *dvd_raw_device=NULL;
66 char *css_so=NULL;
67 unsigned char key_disc[2048]; 67 unsigned char key_disc[2048];
68 unsigned char key_title[5]; 68 unsigned char key_title[5];
69 unsigned char *dvdimportkey=NULL; 69 unsigned char *dvdimportkey=NULL;
70 int descrambling=0; 70 int descrambling=0;
71 71
72 72 static void *dlid;
73 #if OLD_CSS_API 73 static int (*dl_CSSisEncrypted)(int);
74 /* 74 static int (*dl_CSSAuthDisc)(int,char *);
75 * With the old libcss-0.1 api, we have to find out the LBA for 75 static int (*dl_CSSAuthTitle)(int, char *,int);
76 * a title for title authentication. 76 static int (*dl_CSSGetASF)(int);
77 */ 77 static int (*dl_CSSDecryptTitleKey)(char *, char *);
78 static void (*dl_CSSDescramble)(u_char *, u_char *);
79
80 dvd_css_descramble ( u_char *sec , u_char *key )
81 {
82 (*dl_CSSDescramble)(sec,key);
83 }
84
85
78 #ifdef __linux__ 86 #ifdef __linux__
79 #include <linux/fs.h> 87 #include <linux/fs.h>
80 88
81 #ifndef FIBMAP 89 #ifndef FIBMAP
82 #define FIBMAP 1 90 #define FIBMAP 1
83 #endif 91 #endif
92
84 93
85 static int path_to_lba (char *path) 94 static int path_to_lba (char *path)
86 { 95 {
87 int lba = 0; 96 int lba = 0;
88 char cmd[100]; 97 char cmd[100];
92 fp=popen(cmd,"r"); 101 fp=popen(cmd,"r");
93 if (fp) { 102 if (fp) {
94 int ret; 103 int ret;
95 memset(cmd,0,sizeof(cmd)); 104 memset(cmd,0,sizeof(cmd));
96 fgets(cmd,99,fp); 105 fgets(cmd,99,fp);
106 // printf("DVD: cmd: %s\n",cmd);
97 if ((ret=pclose(fp))) 107 if ((ret=pclose(fp)))
98 fprintf(stderr,"fibmap_mplayer: %s\n",*cmd?cmd:"no error info"); 108 fprintf(stderr,"DVD: fibmap_mplayer: %s\n",*cmd?cmd:"no error info");
99 if(WIFEXITED(ret) && !WEXITSTATUS(ret)) 109 if (cmd[0]<'0'||cmd[0]>'9') fp=NULL; else {
100 lba=atoi(cmd); 110 if(WIFEXITED(ret) && !WEXITSTATUS(ret)) {
101 else 111 lba=atoi(cmd);
102 fp=NULL; 112 printf("DVD: fibmap_mplayer is being used\n");
113 } else
114 fp=NULL;
115 }
103 } 116 }
104 if (!fp) { 117 if (!fp) {
105 int fd; 118 int fd;
106 printf("fibmap_mplayer could not run, trying with ioctl() ...\n"); 119 printf("DVD: fibmap_mplayer could not run, trying with ioctl() ...\n");
107 if ((fd = open(path, O_RDONLY)) == -1) { 120 if ((fd = open(path, O_RDONLY)) == -1) {
108 fprintf(stderr, "Cannot open file %s: %s", 121 fprintf(stderr, "DVD: Cannot open file %s: %s",
109 path ? path : "(NULL)", strerror(errno)); 122 path ? path : "(NULL)", strerror(errno));
110 return -1; 123 return -1;
111 } 124 }
112 if (ioctl(fd, FIBMAP, &lba) != 0) { 125 if (ioctl(fd, FIBMAP, &lba) != 0) {
113 perror ("ioctl FIBMAP"); 126 perror("DVD: ioctl FIBMAP");
114 fprintf(stderr,"Hint: run mplayer as root (or better to install fibmap_mplayer as suid root)!\n"); 127 fprintf(stderr," Hint: run mplayer as root (or better to install fibmap_mplayer as suid root)!\n");
115 close(fd); 128 close(fd);
116 return -1; 129 return -1;
117 } 130 }
118 close(fd); 131 close(fd);
119 } 132 }
120 printf("LBA: %d\n",lba); 133 printf("DVD: LBA: %d\n",lba);
121 return lba; 134 return lba;
122 } 135 }
123 136
124 137 #else /*linux*/
125 int CSSDVDAuthTitlePath(DVDHandle hdl,unsigned char *key_title,char *path) 138
139 static int path_to_lba (char *path)
140 {
141 #warning translating pathname to iso9660 LBA is not supported on this platform
142 fprintf(stderr, "DVD: Translating pathname to iso9660 LBA is not supported on this platform\n");
143 return -1;
144 }
145
146 #endif /*linux*/
147
148
149 static int CSSDVDAuthTitlePath(DVDHandle hdl,unsigned char *key_title,char *path)
126 { 150 {
127 int lba=path_to_lba(path); 151 int lba=path_to_lba(path);
128 if (lba==-1) return -1; 152 if (lba==-1) return -1;
129 return CSSAuthTitle(hdl,key_title,lba); 153 return (*dl_CSSAuthTitle)(hdl,key_title,lba);
130 } 154 }
131 155
132
133 #else /*linux*/
134 static int path_to_lba (char *path)
135 {
136 #warning translating pathname to iso9660 LBA is not supported on this platform
137 fprintf(stderr, "Translating pathname to iso9660 LBA is not supported on this platform\n");
138 return -1;
139 }
140 #endif /*linux*/
141 #endif /*OLD_CSS_API*/
142 156
143 157
144 static void reset_agids ( DVDHandle dvd ) 158 static void reset_agids ( DVDHandle dvd )
145 { 159 {
146 dvd_authinfo ai; 160 dvd_authinfo ai;
171 t++; 185 t++;
172 } 186 }
173 hexkey++; 187 hexkey++;
174 } 188 }
175 if (*hexkey) return 1; 189 if (*hexkey) return 1;
176 printf("DVD key (requested): %02X%02X%02X%02X%02X\n",key_title[0],key_title[1],key_title[2],key_title[3],key_title[4]); 190 printf("DVD: DVD key (requested): %02X%02X%02X%02X%02X\n",key_title[0],key_title[1],key_title[2],key_title[3],key_title[4]);
177 descrambling=1; 191 descrambling=1;
178 return 0; 192 return 0;
179 } 193 }
180 194
181 195
182 196
183 int dvd_auth ( char *dev , char *filename ) 197 int dvd_auth ( char *dev , char *filename )
184 { 198 {
185 DVDHandle dvd; /* DVD device handle */ 199 DVDHandle dvd; /* DVD device handle */
200
201 if (!css_so) css_so=strdup("/usr/local/lib/libcss.so");
202 printf("DVD: opening libcss.so as %s ...\n",css_so);
203 dlid=dlopen(css_so,RTLD_NOW);
204 if (!dlid) {
205 printf("DVD: dlopen: %s\n",dlerror());
206 return 1;
207 } printf("DVD: dlopen OK!\n");
208
209 #define CSS_DLSYM(v,s) if (!(v=dlsym(dlid,s))) {\
210 fprintf(stderr,"DVD: %s\n Hint: use libcss version 0.1!\n",dlerror());\
211 return 1; }
212
213 CSS_DLSYM(dl_CSSisEncrypted,"CSSisEncrypted");
214 CSS_DLSYM(dl_CSSAuthDisc,"CSSAuthDisc");
215 CSS_DLSYM(dl_CSSAuthTitle,"CSSAuthTitle");
216 CSS_DLSYM(dl_CSSGetASF,"CSSGetASF");
217 CSS_DLSYM(dl_CSSDecryptTitleKey,"CSSDecryptTitleKey");
218 CSS_DLSYM(dl_CSSDescramble,"CSSDescramble");
219
220 #undef CSS_DLSYM
186 221
187 if ((dvd=DVDOpenDevice(dev)) == DVDOpenFailed) { 222 if ((dvd=DVDOpenDevice(dev)) == DVDOpenFailed) {
188 fprintf(stderr,"DVD: cannot open DVD device \"%s\": %s.\n", 223 fprintf(stderr,"DVD: cannot open DVD device \"%s\": %s.\n",
189 dev, strerror(errno)); 224 dev, strerror(errno));
190 return 1; 225 return 1;
191 } 226 }
192 227
193 if (!CSSDVDisEncrypted(dvd)) { 228 if (!(*dl_CSSisEncrypted)(dvd)) {
194 printf("DVD is unencrypted! Skipping authentication!\n(note: you should not use -dvd switch for unencrypted discs!)\n"); 229 printf("DVD: DVD is unencrypted! Skipping authentication!\n(note: you should not use -dvd switch for unencrypted discs!)\n");
195 DVDCloseDevice(dvd); 230 DVDCloseDevice(dvd);
196 return 0; 231 return 0;
197 } else printf("DVD is encrypted, issuing authentication ...\n"); 232 } else printf("DVD: DVD is encrypted, issuing authentication ...\n");
198
199 /* reset AGIDs */ 233 /* reset AGIDs */
200 reset_agids(dvd); 234 reset_agids(dvd);
201
202 /* authenticate disc */ 235 /* authenticate disc */
203 if (CSSDVDAuthDisc(dvd,key_disc)) { 236 if ((*dl_CSSAuthDisc)(dvd,key_disc)) {
204 fprintf(stderr,"DVD: CSSDVDAuthDisc() failed.\n"); 237 fprintf(stderr,"DVD: CSSDVDAuthDisc() failed.\n");
205 DVDCloseDevice(dvd); 238 DVDCloseDevice(dvd);
206 return 1; 239 return 1;
207 } 240 }
208
209 if (CSSDVDAuthTitlePath(dvd,key_title,filename)) { 241 if (CSSDVDAuthTitlePath(dvd,key_title,filename)) {
210 fprintf(stderr,"DVD: CSSDVDAuthTitle() failed.\n"); 242 fprintf(stderr,"DVD: CSSDVDAuthTitle() failed.\n");
211 DVDCloseDevice(dvd); 243 DVDCloseDevice(dvd);
212 return 1; 244 return 1;
213 } 245 }
214 246
215 /* decrypting title */ 247 /* decrypting title */
216 if (CSSDecryptTitleKey (key_title, key_disc) < 0) { 248 if ((*dl_CSSDecryptTitleKey)(key_title, key_disc) < 0) {
217 fprintf(stderr,"DVD: CSSDecryptTitleKey() failed.\n"); 249 fprintf(stderr,"DVD: CSSDecryptTitleKey() failed.\n");
218 DVDCloseDevice(dvd); 250 DVDCloseDevice(dvd);
219 return 1; 251 return 1;
220 } 252 }
221 253
222 DVDCloseDevice(dvd); 254 DVDCloseDevice(dvd);
223 printf("DVD title key is: %02X%02X%02X%02X%02X\n",key_title[0],key_title[1],key_title[2],key_title[3],key_title[4]); 255 printf("DVD: DVD title key is: %02X%02X%02X%02X%02X\n",key_title[0],key_title[1],key_title[2],key_title[3],key_title[4]);
224 descrambling=1; 256 descrambling=1;
225 return 0; 257 return 0;
226 } 258 }
227 259
228 260