File: nis2sql

package info (click to toggle)
scotty 2.1.9-1
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 9,984 kB
  • ctags: 4,313
  • sloc: ansic: 35,946; sh: 12,591; tcl: 8,122; yacc: 2,442; makefile: 898; lex: 370
file content (267 lines) | stat: -rwxr-xr-x 7,090 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
#! /usr/local/bin/scotty -nf
## -*- tcl -*-
##
## This file contains a sample script to fill the bones database with
## already existing information, usually taken from nis aka yp maps.
##
## Copyright (c) 1995
##
## J. Schoenwaelder
## TU Braunschweig, Germany
## Institute for Operating Systems and Computer Networks
##
## Permission to use, copy, modify, and distribute this
## software and its documentation for any purpose and without
## fee is hereby granted, provided that the above copyright
## notice appear in all copies.  The University of Braunschweig
## makes no representations about the suitability of this
## software for any purpose.  It is provided "as is" without
## express or implied warranty.
##

##
## The following procs are used to read the information either
## from NIS (aka yp) files or from the /etc/ directory. The information
## is stored in some global arrays for later access.
##

proc forevery {var file body} {
    upvar $var line

    # check for yellow pages
    set noyp [catch {exec ypcat $file}]
    if {$noyp} {
	set f [open "/etc/$file" r]
	puts -nonewline stderr "Reading /etc/$file "
    } else {
	set f [open "|ypcat $file" r]
	puts -nonewline stderr "Reading YP $file "
    }
    while {[gets $f line] >= 0} {
	set line [string trim $line]
	if {$line == "" || [string index $line 0] == "#"} continue
	puts -nonewline stderr "."
	uplevel 1 $body
    }
    close $f
    puts stderr " done"
}

proc readLogins {} {
    global login
    forevery line passwd {
	set line [split $line :]
	set uid	[lindex $line 2]
	set login(name,$uid)	[lindex $line 0]
	set login(passwd,$uid)	[lindex $line 1]
	set login(uid,$uid)	[lindex $line 2]
	set login(gid,$uid)	[lindex $line 3]
	set login(gcos,$uid)	[lindex $line 4]
	set login(home,$uid)	[lindex $line 5]
	set login(shell,$uid)	[lindex $line 6]
    }
}

proc readGroups {} {
    global group
    forevery line group {
        set line [split $line :]
        set gid [lindex $line 2]
        set group(name,$gid)    [lindex $line 0]
        set group(passwd,$gid)  [lindex $line 1]
        set group(gid,$gid)     [lindex $line 2]
	if {[info exists group(user,$gid)]} {
	    set list [concat $group(user,$gid) [split [lindex $line 3] ,] ] 
	    set group(user,$gid)    $list
	} else {
	    set group(user,$gid)    [split [lindex $line 3] ,]
	}
    }
}

proc readNetworks {} {
    global network
    forevery line networks {
        set ip [lindex $line 0]
        set network(ip,$ip)	[lindex $line 0]
	set network(name,$ip)	[lindex $line 1]
        set network(alias,$ip)	[lrange $line 2 end]
    }
}

proc readHosts {} {
    global host
    forevery line hosts {
        set ip [lindex $line 0]
        set host(ip,$ip)	[lindex $line 0]
	set host(name,$ip)	[lindex $line 1]
        set host(alias,$ip)	[lrange $line 2 end]
    }
}

proc readServices {} {
    global service
    forevery line services {
        set port [lindex $line 1]
	set service(name,$port)		[lindex $line 0]
        set service(port,$port)		[lindex $line 1]
        set service(alias,$port)	[lrange $line 2 end]
    }
}

##
## Here are some procedures that create bones objects. There is
## one proc foreach class that will be set up with this script.
##

proc writeNetworks {} {
    global network
    puts -nonewline stderr "Network "
    set id 42
    foreach a [lsort [array names network]] {
	if {[string match "ip,*" $a]} {
	    puts -nonewline stderr "."
	    set a [lindex [split $a ,] 1]
	    set name $network(name,$a)
	    set address $network(ip,$a)
	    set aliases $network(alias,$a)
	    incr id
	    puts "insert into Network (id,name,address) \
		    values ($id,'$name','$address') \\g"
	}
    }
    puts stderr " done"
}

proc writeHosts {} {
    global host
    puts -nonewline stderr "Device Interface"
    set date [exec date]
    set devId 42
    set ifId 42
    foreach a [lsort [array names host]] {
        if {[string match "ip,*" $a]} {
            puts -nonewline stderr "."
            set a [lindex [split $a ,] 1]
	    set name $host(name,$a)
	    set address $host(ip,$a)
	    set os "unknown"
	    incr devId
	    incr ifId
	    puts "insert into Device (id,name,type) \
                    values ($devId, '$name','computer') \\g"
	    puts "insert into Interface (id,name,address,device) \
                    values ($ifId,'$name','$address',$devId) \\g"
        }
    }
    puts stderr " done"
}

proc writeGroup {} {
    global group
    puts -nonewline stderr "Group "
    foreach a [lsort [array names group]] {
	if {[string match "gid,*" $a]} {
	    puts -nonewline stderr "."
	    set a [lindex [split $a ,] 1]
	    set name $group(name,$a)
	    set id $group(gid,$a)
	    puts "insert into Group (id,name) values ($id,'$name') \\g"
	}
    }
    puts stderr " done"
}

proc writeLogin {} {
    global login group
    puts -nonewline stderr "Login Person "
    set perId 42
    foreach a [lsort [array names login]] {
	if {[string match "uid,*" $a]} {
	    puts -nonewline stderr "."
	    set a [lindex [split $a ,] 1]
	    set name $login(name,$a)
	    set passwd $login(passwd,$a)
	    set uid $login(uid,$a)
	    set home $login(home,$a)
	    set shell $login(shell,$a)
	    set gid $login(gid,$a)
	    puts "insert into Login \
		    (id,name,passwd,home,shell,group) \
                    values ($uid,'$name','$passwd','$home','$shell',$gid) \\g"
	    if {$login(gcos,$a) != ""} {
		set pname $login(gcos,$a)
		incr perId
		puts "insert into Person (id,name) \
			values ($perId,'$pname') \\g"
		puts "update Login set owner = $perId where id = $uid \\g"
	    }
	    set login(id,$name) $uid
	}
    }
    puts stderr " done"
}

proc writeGroupMember {} {
    global group login
    puts -nonewline stderr "Updating Group member "
    foreach a [lsort [array names group]] {
        if {[string match "gid,*" $a]} {
            set a [lindex [split $a ,] 1]
	    set gid $group(gid,$a)
	    set member ""
	    foreach l $group(user,$gid) {
		if {[info exists login(id,$l)]} {
		    lappend member $login(id,$l)
		}
	    }
	    if {$member == ""} continue
	    foreach uid $member {
		puts -nonewline stderr "."
		puts "insert into GroupMember (uid,gid) \
			values ($uid,$gid) \\g"
	    }
	}
    }
    puts stderr " done"
}

proc writeService {} {
    global service
    puts -nonewline stderr "Service "
    set id 0
    foreach a [lsort [array names service]] {
	if {[string match "port,*" $a]} {
	    puts -nonewline stderr "."
	    set a [lindex [split $a ,] 1]
	    set name $service(name,$a)
	    set port [lindex [split $service(port,$a) /] 0]
	    set protocol [lindex [split $service(port,$a) /] 1]
	    set aliases $service(alias,$a)
	    incr id
	    puts "insert into Service (id,name,protocol,port) \
		    values ($id,'$name','$protocol',$port) \\g"
	}
    }
    puts stderr " done"
}

##
## Here we start the whole thing. First, we read in all information
## available on this machine. Afterward, we clear/create all classes.
##

readLogins
readGroups
readNetworks
readHosts
readServices

writeNetworks
writeHosts
writeGroup
writeLogin
writeGroupMember
writeService

exit