-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathurreg
executable file
·462 lines (345 loc) · 11 KB
/
urreg
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
#!/bin/sh
if [ "$1" = '--version' ] || [ "$1" = '-v' ]
then
echo "URReg v1.0"
exit 0
elif [ "$1" = '--help' ] || [ "$1" = '-h' ]
then
printf 'usage: %s [OPTIONS]\n\n' "$0"
echo "Options that must come first:"
echo " -h, --help Displays this help message and exits."
echo " -v, --version Displays the version number and exits."
echo
echo "Other options:"
echo " -q, --quiet Turns off messages sent to stdout."
echo " --verbose Displays extra information for debugging."
exit 0
fi
v=
while [ -n "$1" ]
do
case "$1" in
--verbose)
set -x
v=-v
q=
;;
-q|--quiet)
set +x
q=-q
v=
;;
*)
printf "Unrecognized argument: %s\n" "$1"
esac
shift
done
COOKIE_JAR=COOKIES
LOG_FILE=LOG
CONFIRM_CRN=false
# I'm not sure why but for both curl and Ruby's Mechanize, but not for
# browsers, the SSL certificate for the registration pages is found invalid
# and can't be validated properly. It works with the check disabled but is
# less secure.
curl_args="-s $v -k -c $COOKIE_JAR -b $COOKIE_JAR"
> "$LOG_FILE"
> "$COOKIE_JAR"
log()
{
msg="$(date +%s) [$(date)]"
[ $# = 0 ] || msg="$msg $*"
printf "%s\n" "$msg" >> "$LOG_FILE"
}
log 'URReg v0.0'
log "Using $COOKIE_JAR as the cookie jar"
log "\$curl_args = '$curl_args'"
log 'BEGIN'
id=
password=
crns=
ts="TS=$(date +%s)"
log "\$ts = '$ts'"
is_insecure()
{
# Check if a file is accessible to other users
if [ -n "$(find $1 -perm /066)" ]
then
log "is_insecure(): $1 => TRUE"
return 0
fi
log "is_insecure(): $1 => FALSE"
return 1
}
# TODO - Test this
make_secure()
{
log "make_secure(): securing $1"
chmod -066 "$1"
}
# get saved login info from ~/.netrc
parse_netrc()
{
log 'parse_netrc(): BEGIN'
regexid='/login/!d;s/.*login \("\(\([^"\]\|\\.\)*\)"\|\(\([^ \]\|\\.\)*\)\).*/\2\4/;s/\\\([^\\]\)/\1/g;s/\\$//g;s/\\\\/\\/g'
regexpass='/password/!d;s/.*password \("\(\([^"\]\|\\.\)*\)"\|\(\([^ \]\|\\.\)*\)\).*/\2\4/;s/\\\([^\\]\)/\1/g;s/\\$//g;s/\\\\/\\/g'
netrc="$HOME/.netrc"
if [ ! -f "$netrc" ]
then
log "parse_netrc(): netrc ($netrc) does not exist."
log 'parse_netrc(): END'
return 1
fi
if is_insecure $netrc
then
echo 'Warning: .netrc has insecure permissions.' >&2
log 'parse_netrc(): netrc is insecure'
log 'parse_netrc(): asking to fix netrc security'
read -p 'Do you want to fix it? [Y/n] ' fix
log "parse_netrc(): asked to fix netrc; fix = $fix"
case $fix in
[yY][eE][sS]|[yY])
log 'parse_netrc(): securing netrc'
make_secure $netrc
;;
*)
log 'parse_netrc(): not securing netrc'
;;
esac
fi
cred="$(sed -n '/machine my\.rochester\.edu/,/machine /p' $netrc)"
id="$(echo "$cred" | sed "$regexid")"
password="$(echo "$cred" | sed "$regexpass")"
log "parse_netrc(): \$cred = '$cred'; \$id = '$id'"
log "parse_netrc(): END"
}
# has_valid_session()
# {
# $logged_in || return 0
#
# # TODO - implement this
# if true
# then
# logged_in=true
# return 0
# fi
#
# logged_in=false
# password=
#
# return 1
# }
send_request()
{
log 'send_request(): BEGIN'
html="$(curl $curl_args "$@")"
err=$?
if [ $err != 0 ]
then
echo 'Error: request failed.' >&2
log "send_request(): curl failed; error code $err"
log 'send_request(): END'
return 1
fi
TS="$(printf %s "$html" | grep -o -e 'TS=[0-9]\{13\}' | tail -1)"
if [ -n "$TS" ]
then
log "send_request(): setting \$ts = '$TS'"
ts="$TS"
fi
printf "%s\n" "$html"
log 'send_request(): END'
}
log_in()
{
log 'log_in(): BEGIN'
parse_netrc
while [ -z "$id" ]
do
log "log_in(): \$id = '$id'; reading id"
read -p 'Net ID: ' id
log "log_in(): read id; \$id = '$id'"
done
while [ -z "$password" ]
do
log 'log_in(): reading password'
stty -echo
read -r -p 'Password: ' password; echo
stty echo
log 'log_in(): read password'
done
[ -n "$q" ] || echo 'Logging in...'
log 'log_in(): sending login request'
# I don't think all these requests are necessary, but I'm not sure which
# ones are, so I'm just leaving them all in. TODO - remove unnecessary
# requests.
{
send_request 'https://webreg.its.rochester.edu/prod/tapp' --data "Navigate=redirect.jsp&OnError=LoginError.jsp&START_APP=true&JSP_TYPE=web&$ts&TRX_ID=ValidateStudent&CHECK_REG_RESTR=true&LDAP_AUTH=true&LOAD_DEF_LOG_TERM=true&LOAD_COUNTRY_CODES=true&LOAD_STATE_CODES=true&LOAD_COLLEGE_CODES=true&LOAD_HOLD_CODES=true&LOAD_RESTRICTION_CODES=true&LOAD_RELATIONSHIP_CODES=true&STUDENT_ID=$id&STUDENT_PIN=$password" >/dev/null
} && {
send_request 'https://webreg.its.rochester.edu/prod/tapp' --data "Navigate=redirect.jsp&OnError=LoginError.jsp&START_APP=true&JSP_TYPE=web&$ts&TRX_ID=ValidateStudent&CHECK_REG_RESTR=true&LDAP_AUTH=true&LOAD_DEF_LOG_TERM=true&LOAD_COUNTRY_CODES=true&LOAD_STATE_CODES=true&LOAD_COLLEGE_CODES=true&LOAD_HOLD_CODES=true&LOAD_RESTRICTION_CODES=true&LOAD_RELATIONSHIP_CODES=true&STUDENT_ID=$id&STUDENT_PIN=$password" >/dev/null
} && {
send_request 'https://webreg.its.rochester.edu/prod/tapp?Navigate=RchHoldsIndex.jsp&OnError=error.jsp' >/dev/null
} && {
send_request "https://webreg.its.rochester.edu/prod/tapp?Navigate=classindex.jsp&WAITPAGE=Loading.htm&LOAD_TERMS=true&TRX_ID=GetCollegeRegTerms&LOAD_DEF_REG_TERM=true&LOAD_SCHEDULE=true&OnError=error.jsp&$ts" >/dev/null
} && {
send_request "https://webreg.its.rochester.edu/prod/tapp?Navigate=classindex.jsp&WAITPAGE=Loading.htm&LOAD_TERMS=true&TRX_ID=GetCollegeRegTerms&LOAD_DEF_REG_TERM=true&LOAD_SCHEDULE=true&OnError=error.jsp&$ts" >/dev/null
} && {
send_request "https://webreg.its.rochester.edu/prod/web/scheduletopbar.jsp?$ts" >/dev/null
} && {
send_request "https://webreg.its.rochester.edu/prod/web/regmain.jsp?$ts" >/dev/null
} && {
send_request "https://webreg.its.rochester.edu/prod/web/classschedule.jsp?$ts" >/dev/null
} || {
echo 'Error: unable to log in.' >&2
log 'log_in(): Error: unable to log in'
log 'log_in(): END'
return 1
}
log 'log_in(): should be successfully logged in'
log 'log_in(): END'
}
is_valid_crn()
{
printf %s "$1" | grep -q '[0-9]\{5\}' || {
log "is_valid_crn(): $1 => FALSE"
return 1
}
log "is_valid_crn(): $1 => TRUE"
}
read_crn()
{
log 'read_crn(): BEGIN'
while true
do
while true
do
log "read_crn(): \$crn = '$crn'; reading crn"
read -p 'CRN: ' crn
log "read_crn(): read crn; \$crn = '$crn'"
if [ -z $crn ]
then
log 'read_crn(): $crn is empty; returning'
log 'read_crn(): END'
return 1
fi
if is_valid_crn $crn
then
log "read_crn(): using \$crn = '$crn'"
break
fi
echo 'Error: invalid CRN.' >&2
log 'read_crn(): invalid crn entered; looping'
done
if ! $CONFIRM_CRN
then
log 'read_crn(): skipping crn confirmation'
return 0
fi
log "read_crn(): \$crn_confirm = '$crn_confirm'; reading crn"\
"confirmation"
read -p 'CRN confirmation: ' crn_confirm
log "read_crn(): read crn confirmation; \$crn_confirm = '$crn_confirm'"
if [ "$crn" = "$crn_confirm" ]
then
log "read_crn(): CRNs $crn and $crn_confirm match"
log 'read_crn(): END'
return 0
fi
echo 'Error: CRNs do not match.' >&2
log "read_crn(): CRNs $crn and $crn_confirm do not match; looping"
done
log 'read_crn(): Error: reached unreachable statement.'
}
read_crns()
{
log 'read_crns(): BEGIN'
while read_crn
do
crns="$crns $crn"
log "read_crns(): crn read; \$crn = '$crn'; \$crns = '$crns'"
done
if [ -z "$crns" ]
then
echo 'Warning: you have not entered any classes to register for.' >&2
log 'read_crns(): \$crns is empty; not registering for any classes'
log 'read_crns(): END'
return 1
fi
log 'read_crns(): END'
return 0
}
add_course()
{
log 'add_course(): BEGIN'
crn="$1"
log "add_course(): using \$crn = '$crn'"
# 'A' is for auditing; 'N' is for normal grading
if [ "$2" = 'A' ]
then
gt=A
log "add_course(): grade type given; \$gt = '$gt'"
else
gt=N
log "add_course(): grade type not given; \$gt = '$gt'"
fi
echo "Adding course $crn"
{
send_request "https://webreg.its.rochester.edu/prod/tapp?Navigate=regdisplay1.jsp&OnError=error.jsp&$ts&ADD_CALL_NUM=15156&LOAD_SCHEDULE=false&SHOP_CART=true&GRADE_TYPE=N" >/dev/null
} && {
send_request "https://webreg.its.rochester.edu/prod/tapp?Navigate=CartResults.jsp&OnError=error.jsp&$ts&SUBMIT_CART=true&WAITPAGE=PleaseWait.htm" >/dev/null
} && (
html="$(send_request "https://webreg.its.rochester.edu/prod/tapp?Navigate=CartResults.jsp&OnError=error.jsp&$ts&SUBMIT_CART=true&WAITPAGE=PleaseWait.htm")"
if printf "%s\n" "$html" | grep -qF "Permission required"; then
log 'add_course(): Error: permission code required'
echo 'Error: permission code required.' >&2
exit 1
elif printf "%s\n" "$html" | grep -qF "Unable"; then
log 'add_course(): Error: unable to register'
echo 'Error: unable to add course.' >&2
echo 'Trying again in 5 seconds...' >&2
sleep 5
log 'add_course(): Trying again'
add_course "$@" || exit 1
elif printf "%s\n" "$html" | grep -qF "Cannot"; then
log 'add_course(): Error: cannot register at this time'
echo 'Error: cannot register at this time.' >&2
echo 'Trying again in 5 seconds...' >&2
sleep 5
log 'add_course(): Trying again'
add_course "$@" || exit 1
fi
) || {
echo 'Error: unable to add course.' >&2
log 'add_course(): Error: unable to add course'
log 'add_course(): END'
return 1
}
[ -n "$q" ] || echo "Course $crn has been successfully added."
log 'add_course(): course should be successfully added'
log 'add_course(): END'
}
[ -n "$q" ] || {
echo 'Enter the CRNs you wish to register for.'
echo 'End your list by entering an empty CRN.'
}
if ! read_crns
then
log 'no CRNs entered'
log 'END'
exit 0
fi
if ! log_in
then
log 'log_in failed'
log 'END'
exit 1
fi
for crn in $crns
do
if ! add_course $crn
then
log "adding course $crn failed"
log 'END'
exit 1
fi
done
log 'END'