File: postinstall

package info (click to toggle)
afbackup 3.3.8.1beta2-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 4,128 kB
  • ctags: 3,370
  • sloc: ansic: 46,932; sh: 4,654; tcl: 4,199; makefile: 536; csh: 416; perl: 133; sed: 93
file content (460 lines) | stat: -rw-r--r-- 12,501 bytes parent folder | download
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
#!/bin/sh
#
# $Source: /home/alb/afbackup/afbackup-3.3.8beta7/RCS/postinstall,v $
# $Id: postinstall,v 1.1 2004/07/08 20:34:48 alb Exp alb $
#

#
# This script can be used as postinstall, preremove and checkinstall
# script in Solaris-2 packages
#

PASSWDFILE=/etc/passwd
INETDCONF=/etc/inetd.conf
SERVICES=/etc/services
xinetdfile="/etc/xinetd.conf"

TMPFILE=/tmp/inst.afbackup.$$

/bin/rm -f $TMPFILE
if [ -f $TMPFILE ] ; then
  echo "ERROR: tempfile $TMPFILE could not be removed." >&2
  exit 2
fi

PROGNAME=`basename $0`

ps -uxawwww >/dev/null 2>&1
if [ $? -eq 0 ] ; then
  ps -uxawwww 2>&1 | egrep -i 'bad.*syntax.*bogus' >/dev/null
  if [ $? -eq 0 ] ; then
    PSALL="ps uxawwww"
  else
    PSALL="ps -uxawwww"
  fi
else
  ps uxawwww >/dev/null 2>&1
  if [ $? -eq 0 ] ; then
    PSALL="ps uxawwww"
  else
    PSALL="ps -ef"
  fi
fi

use_xinetd=no
if [ -f /etc/xinetd.conf ] ; then
  $PSALL | grep -v grep | grep xinetd >/dev/null 2>&1
  if [ $? -eq 0 ] ; then
    use_xinetd=yes
  fi
fi

resolv(){
  FN="$1"

  if [ ! -f "$FN" ] ; then
    echo "$FN"
    return 0
  fi

  while [ `ls -l "$FN"|cut -c1` = l ] ; do
    SYML=`/bin/ls -l "$FN" | sed 's/^.*->[ 	]*//g'`

    if [ _`echo "$SYML" | cut -c1` != _/ ] ; then
      FN=`dirname "$FN"`/"$SYML"
    else
      FN="$SYML"
    fi
  done

  echo $FN
}

for f in PASSWDFILE INETDCONF SERVICES xinetdfile ; do
  eval $f'=`resolv $'"$f"'`'
done

#
# get xinetd entry stanza
#
get_xinetd_entry(){
  sed -n '/^\([ 	]\)\?service[	 ][	 ]*'"$1"'\([	 ].*\)\?/,/^[	 ]*[}][	 ]*/ p' "$2"
}
#
# get xinetd param value
#
get_xinetd_param(){
  sed -n '/^\([ 	]\)\?service[	 ][	 ]*'"$1"'\([	 ].*\)\?/,/^[	 ]*[}][	 ]*/ p' "$3"|grep '^[ 	]*'"$2"'[ 	]*=[ 	]*'|sed 's/^[^=]*=[	 ]*//g'
}
#
# remove named xinetd entry from data stream
#
strip_xinetd_entry(){
  sed '/^\([ 	]\)\?service[	 ][	 ]*'"$1"'\([	 ].*\)\?/,/^[	 ]*[}][	 ]*/ d' "$2"
}
#
# combine empty lines to one empty line
#
comb_empty_lines(){
  awk 'BEGIN{s = 0;}{if(NF == 0){if(s == 0)print; s = 1}else{s = 0; print}}'
}

#
# check and add services
#
check_add_service(){
  CHECKONLY=no
  if [ _"$1" = _-n ] ; then
    CHECKONLY=yes
    shift
  fi

  SNAME="$1"
  PROT="`echo $2|tr A-Z a-z`"
  PORTNUM=`echo $PROT|cut -d/ -f1`
  TRANSP=`echo $PROT|cut -d/ -f2`

  egrep "^[ 	]*($SNAME[ 	][ 	]*$PROT|[^ 	][^ 	]*[ 	][ 	]*$PROT.*[ 	]$SNAME)([ 	].*)?$" $SERVICES >/dev/null
  if [ $? -eq 0 ] ; then
    for P in `grep "^[ 	]*($SNAME[ 	][ 	]*[1-9][0-9]*/[tT][Cc][Pp]|[^ 	][^ 	]*[ 	][ 	]*[1-9][0-9]*/[tT][Cc][Pp].*[ 	]$SNAME)([ 	].*)?$" $SERVICES|awk '{print $2}'` ; do
      PN=`echo $P|cut -d/ -f1`
      TP=`echo $P|cut -d/ -f2`
      if [ "$TP" = "$TRANSP" -a "$PN" != "$PORTNUM" ] ; then
	echo "ERROR: Services entry exists with wrong port specification." >&2
	exit 3
      fi
    done

    return 0
  fi

  if [ $CHECKONLY = yes ] ; then
    return 0
  fi

  awk '{print $2}' $SERVICES | egrep -i '^'"$PROT"'$' >/dev/null
  if [ $? -eq 0 ] ; then
    SERVLINE=`egrep '^[ 	]*[^ 	][^ 	]*[ 	][ 	]*'"$PORTNUM/"'[tT][Cc][Pp]([ 	].*)?$' $SERVICES`
    SERVLINENO=`egrep -n '^[ 	]*[^ 	][^ 	]*[ 	][ 	]*'"$PORTNUM/"'[tT][Cc][Pp]([ 	].*)?$' $SERVICES | sed 's/:.*$//g'`
    COMMENT=`echo "$SERVLINE"|grep '#'|sed 's/^[^#]*#/	#/g'`
    THEBEEF=`echo "$SERVLINE"|sed 's/[ 	]*#.*$//g'`
    TMPFILE=/etc/services_add_tmp.$$
    /bin/rm -f $TMPFILE
    if [ -f $TMPFILE ] ; then
      echo "ERROR: Cannot remove file $TMPFILE" >&2
      exit 14
    fi
    cp $SERVICES $TMPFILE
    if [ $? -ne 0 ] ; then
      echo "ERROR: Cannot make copy of file $SERVICES" >&2
      exit 13
    fi
    awk '{if(NR == '"$SERVLINENO"') printf "%s '"$SNAME"'%s\n","'"$THEBEEF"'","'"$COMMENT"'"; else print $0}' $SERVICES > $TMPFILE
    NDIFFS1=`diff $SERVICES $TMPFILE|grep '^< '|wc -l`
    NDIFFS2=`diff $SERVICES $TMPFILE|grep '^> '|wc -l`
    if [ $NDIFFS1 -ne 1 -o $NDIFFS2 -ne 1 ] ; then
      echo "ERROR: Cannot modify $SERVICES correctly" >&2
      /bin/rm -f $TMPFILE
      exit 15
    fi

    /bin/mv $TMPFILE $SERVICES
    return 0
  fi

  echo "$SNAME	$PROT" >> $SERVICES
}

#
# check and remove services
#
check_rm_service(){
  SNAME="$1"
  PROT="`echo $2|tr A-Z a-z`"
  PORTNUM=`echo $PROT|cut -d/ -f1`

  egrep "^[ 	]*($SNAME[ 	][ 	]*$PROT|[^ 	][^ 	]*[ 	][ 	]*$PROT.*[ 	]$SNAME)([ 	].*)?$" $SERVICES >/dev/null
  if [ $? -ne 0 ] ; then
    return 0
  fi

  egrep -v "^[ 	]*($SNAME[ 	][ 	]*$PROT|[^ 	][^ 	]*[ 	][ 	]*$PROT.*[ 	]$SNAME)([ 	].*)?$" $SERVICES > $SERVICES.new.$$
  N1=`diff $SERVICES $SERVICES.new.$$ | grep '^[<>]' | wc -l`
  N2=`diff $SERVICES $SERVICES.new.$$ | grep -v '^[<>]' | wc -l`
  if [ $N1 -ne 1 -o $N2 -ne 1 ] ; then
    echo "ERROR: Removing entry from $SERVICES failed." >&2
    exit 11
  fi

  /bin/mv $SERVICES.new.$$ $SERVICES
}

#
# check and add user
#
check_add_user(){
  CHECKONLY=no
  if [ _"$1" = _-n ] ; then
    CHECKONLY=yes
    shift
  fi

  USERNAME="$1"
  USERID="$2"
  GROUPID="$3"

  grep "^$USERNAME:[^:]*:$USERID:" $PASSWDFILE >/dev/null
  if [ $? -eq 0 ] ; then
    return 0
  fi

  grep "^$USERNAME:" $PASSWDFILE >/dev/null
  if [ $? -eq 0 ] ; then
    UID=`grep "^$USERNAME:" $PASSWDFILE|cut -d: -f3`
    echo "ERROR: User entry exists with wrong UID $UID." >&2
    exit 6
  fi

  grep "^[^:]*:[^:]*:$USERID:" $PASSWDFILE >/dev/null
  if [ $? -eq 0 ] ; then
    U=`grep "^[^:]*:[^:]*:$USERID:" $PASSWDFILE|cut -d: -f1`
    echo "ERROR: User entry with UID $USERID exists: $U." >&2
    exit 7
  fi

  if [ $CHECKONLY = yes ] ; then
    return 0
  fi

  NEW_ENTRY="$USERNAME:*:$USERID:$GROUPID:Backup Server:/usr/local/afbackup/server:"

  if [ `grep '^+' $PASSWDFILE|wc -l` -gt 0 ] ; then
    awk 'BEGIN{p=1}{if($1 ~ /^[+]/){if(p) print ne; p=0} print}' "ne=$NEW_ENTRY" $PASSWDFILE > $PASSWDFILE.new.$$
    /bin/rm -f $TMPFILE
    N1=`diff $PASSWDFILE $PASSWDFILE.new.$$ | grep '^[<>]' | wc -l`
    N2=`diff $PASSWDFILE $PASSWDFILE.new.$$ | grep -v '^[<>]' | wc -l`
    if [ $N1 -ne 1 -o $N2 -ne 1 ] ; then
	echo "ERROR: Removing entry from $PASSWDFILE failed." >&2
	exit 12
    fi
    /bin/mv $PASSWDFILE.new.$$ $PASSWDFILE
    return $?
  fi

  echo "$NEW_ENTRY" >> $PASSWDFILE
}

#
# check and remove user
#
check_rm_user(){
  USERNAME="$1"
  USERID="$2"

  grep "^$USERNAME:[^:]*:$USERID:" $PASSWDFILE >/dev/null
  if [ $? -ne 0 ] ; then
    return 0
  fi

  grep -v "^$USERNAME:[^:]*:$USERID:" $PASSWDFILE > $PASSWDFILE.new.$$
  N1=`diff $PASSWDFILE $PASSWDFILE.new.$$ | grep '^[<>]' | wc -l`
  N2=`diff $PASSWDFILE $PASSWDFILE.new.$$ | grep -v '^[<>]' | wc -l`
  if [ $N1 -ne 1 -o $N2 -ne 1 ] ; then
    echo "ERROR: Removing entry from $PASSWDFILE failed." >&2
    exit 11
  fi

  /bin/mv $PASSWDFILE.new.$$ $PASSWDFILE
}

#
# check and add (x)inetd entry
#
check_add_inetd(){
  CHECKONLY=no
  if [ _"$1" = _-n ] ; then
    CHECKONLY=yes
    shift
  fi

  SERVNAME="$1"
  SOCKTYPE="$2"
  PROT="$3"
  FLAGS="$4"
  USERNAME="$5"
  PROGRAM="$6"

  if [ $use_xinetd = no ] ; then
    grep "^$SERVNAME[ 	][ 	]*$SOCKTYPE[ 	][ 	]*$PROT[ 	]" $INETDCONF >/dev/null
    if [ $? -eq 0 ] ; then
      ENTRY=`grep "^$SERVNAME[ 	][ 	]*$SOCKTYPE[ 	][ 	]*$PROT[ 	]" $INETDCONF | sed 's/[ 	][ 	]*/ /g'`
      if [ "$ENTRY" != "$SERVNAME $SOCKTYPE $PROT $FLAGS $USERNAME $PROGRAM" ] ; then
	echo "ERROR: Entry for service $SERVNAME exists, but is wrong." >&2
	exit 9
      else
	return 0
      fi
    fi
  else
    if [ _"$FLAGS" = _wait ] ; then
	WAITYESNO=yes
    else
	WAITYESNO=no
    fi
    SERVER_ARGS=`echo "$PROGRAM"|awk 'BEGIN{sep="";}{for(i=2;i<=NF;i++){printf "%s%s",sep,$i;sep=" "}}'`
    SERVERPROG=`echo "$PROGRAM" | awk '{print $1}'`

    if [ `get_xinetd_entry $SERVNAME $xinetdfile | wc -l` -gt 0 ] ; then
      server="`get_xinetd_param $SERVNAME server $xinetdfile`"
      server_args="`get_xinetd_param $SERVNAME server_args $xinetdfile`"
      server_args_=`echo "$server_args"|sed 's/[	 ][ 	]*/ /g'`
      username="`get_xinetd_param $SERVNAME user $xinetdfile`"
      prot="`get_xinetd_param $SERVNAME protocol $xinetdfile`"
      wait="`get_xinetd_param $SERVNAME wait $xinetdfile`"
      sockt="`get_xinetd_param $SERVNAME socket_type $xinetdfile`"
      flags="`get_xinetd_param $SERVNAME flags $xinetdfile`"
      test _"$server" = _"$SERVERPROG" \
	&& test _"$server_args_" = _"$SERVER_ARGS" \
	&& test _"$username" = _"$USERNAME" \
	&& test _"$sockt" = _"$SOCKTYPE" \
	&& test _"$wait" = _$WAITYESNO \
	&& test  _"$prot" = _"$PROT" \
	&& test `echo "$flags" | egrep '^(.* )?REUSE( .*$)?' |wc -l` -gt 0 \
	&& test `echo "$flags" | egrep '^(.* )?NAMEINARGS( .*$)?' | wc -l` -gt 0
      if [ $? -ne 0 ] ; then
	echo "ERROR: Entry for service $SERVNAME exists in $xinetdfile, but is wrong." >&2
	exit 9
      else
	return 0
      fi
    fi
  fi

  if [ $CHECKONLY = yes ] ; then
    return 0
  fi

  if [ $use_xinetd = no ] ; then
    echo "$SERVNAME	$SOCKTYPE	$PROT	$FLAGS	$USERNAME	$PROGRAM" >> $INETDCONF
  else
    cut -c10- >> $xinetdfile << EOF
       > 
       > service $SERVNAME
       > {
       >         flags           = REUSE NAMEINARGS
       >         socket_type     = $SOCKTYPE
       >         protocol        = $PROT
       >         wait            = $WAITYESNO
       >         user            = $USERNAME
       >         server          = $SERVERPROG
       >         server_args     = $SERVER_ARGS
       > }
EOF

  fi
}

#
# check and remove inetd entry
#
check_rm_inetd(){
  SERVNAME="$1"
  SOCKTYPE="$2"
  PROT="$3"

  if [ $use_xinetd = no ] ; then
    grep "^$SERVNAME[ 	][ 	]*$SOCKTYPE[ 	][ 	]*$PROT[ 	]" $INETDCONF >/dev/null
    if [ $? -ne 0 ] ; then
      return 0
    fi

    grep -v "^$SERVNAME[ 	][ 	]*$SOCKTYPE[ 	][ 	]*$PROT[ 	]" $INETDCONF > $INETDCONF.new.$$
    N1=`diff $INETDCONF $INETDCONF.new.$$ | grep '^[<>]' | wc -l`
    N2=`diff $INETDCONF $INETDCONF.new.$$ | grep -v '^[<>]' | wc -l`
    if [ $N1 -ne 1 -o $N2 -ne 1 ] ; then
      echo "ERROR: Removing entry from $INETDCONF failed." >&2
      exit 11
    fi

    /bin/mv $INETDCONF.new.$$ $INETDCONF
  else
    if [ `get_xinetd_entry $SERVNAME $xinetdfile | wc -l` -lt 1 ] ; then
	return 0
    fi

    strip_xinetd_entry "$SERVNAME" $xinetdfile | comb_empty_lines > $xinetdfile.new
## this is too heavy to check
#    diff $xinetdfile $xinetdfile.new | egrep '^<[ 	]*[^ 	]' >/dev/null
#    if [ $? -eq 0 ] ; then
#      echo "ERROR: Removing entry from $INETDCONF failed." >&2
#      exit 11
#    fi

    /bin/mv $xinetdfile.new $xinetdfile
  fi
}


if echo "$PROGNAME" | egrep '^(postinstall|checkinstall|prereq)' >/dev/null ; then
  CHECKFLAG=""
  if echo "$PROGNAME" | egrep '^(checkinstall|prereq)' >/dev/null ; then
    CHECKFLAG="-n"
  fi

  check_add_inetd $CHECKFLAG afbackup stream tcp nowait root "/usr/local/afbackup/server/bin/afserver /usr/local/afbackup/server/bin/afserver /usr/local/afbackup/server/etc/backup.conf" \
   && check_add_inetd $CHECKFLAG afmbackup stream tcp wait root "/usr/local/afbackup/server/bin/afmserver /usr/local/afbackup/server/bin/afmserver /usr/local/afbackup/server/etc/backup.conf"
  if [ $? -ne 0 ] ; then
    echo "ERROR: Could not add entry to $INETDCONF" >&2
    exit 10
  fi

  check_add_user $CHECKFLAG backup 2988 14
  if [ $? -ne 0 ] ; then
    echo "ERROR: Could not add entry to $PASSWDFILE" >&2
    exit 8
  fi

  check_add_service $CHECKFLAG afbackup 2988/tcp \
   && check_add_service $CHECKFLAG afmbackup 2989/tcp
  if [ $? -ne 0 ] ; then
    echo "ERROR: Could not create entry to $SERVICES" >&2
    exit 5
  fi
fi
if [ $PROGNAME = preremove ] ; then
  check_rm_inetd afbackup stream tcp \
   && check_rm_inetd afmbackup stream tcp
  if [ $? -ne 0 ] ; then
    echo "WARNING: Could not remove entry from $INETDCONF" >&2
  fi

  check_rm_user backup 2988
  if [ $? -ne 0 ] ; then
    echo "WARNING: Could not remove entry from $PASSWDFILE" >&2
  fi

  check_rm_service afbackup 2988/tcp \
   && check_rm_service afmbackup 2989/tcp
  if [ $? -ne 0 ] ; then
    echo "WARNING: Could not remove entry from $SERVICES" >&2
  fi
fi

echo "$PROGNAME" | egrep '^(checkinstall|prereq)' >/dev/null
if [ $? -ne 0 ] ; then
  if [ $use_xinetd = no ] ; then
    PID=`$PSALL|grep -v grep|egrep '[ 	/]inetd([ 	].*)?$'|awk '{print $2}'`
    if [ _"$PID" != _ ] ; then
      kill -HUP $PID
    else
      echo "WARNING: No (x)inetd running. Remote start will not work." >&2
    fi
  else
    PID=`$PSALL|grep -v grep|egrep '[ 	/]xinetd([ 	].*)?$'|awk '{print $2}'`
    if [ _"$PID" != _ ] ; then
      kill -USR2 $PID
    else
      echo "WARNING: No (x)inetd running. Remote start will not work." >&2
    fi
  fi
fi